summaryrefslogtreecommitdiff
path: root/boost/geometry/index/detail/priority_dequeue.hpp
blob: bbe563abad9cf766f5a34e9ff1eeb502010ec630 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Boost.Geometry

// Copyright (c) 2021, Oracle and/or its affiliates.
// Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle

// Licensed under the Boost Software License version 1.0.
// http://www.boost.org/users/license.html

#ifndef BOOST_GEOMETRY_INDEX_DETAIL_PRIORITY_DEQUEUE_HPP
#define BOOST_GEOMETRY_INDEX_DETAIL_PRIORITY_DEQUEUE_HPP

#include <vector>

#include <boost/geometry/index/detail/maxmin_heap.hpp>

namespace boost { namespace geometry { namespace index { namespace detail
{

template
<
    typename T,
    typename Container = std::vector<T>,
    typename Compare = std::less<typename Container::value_type>
>
class priority_dequeue
{
public:
    using container_type = Container;
    using value_compare = Compare;
    using value_type = typename Container::value_type;
    using size_type = typename Container::size_type;
    using reference = typename Container::reference;
    using const_reference = typename Container::const_reference;

    priority_dequeue()
        : c(), comp()
    {}

    explicit priority_dequeue(Compare const& compare)
        : c(), comp(compare)
    {}

    priority_dequeue(Compare const& compare, Container const& cont)
        : c(cont), comp(compare)
    {
        make_maxmin_heap(c.begin(), c.end(), comp);
    }

    priority_dequeue(Compare const& compare, Container&& cont)
        : c(std::move(cont)), comp(compare)
    {
        make_maxmin_heap(c.begin(), c.end(), comp);
    }

    template <typename It>
    priority_dequeue(It first, It last)
        : c(first, last), comp()
    {
        make_maxmin_heap(c.begin(), c.end(), comp);
    }

    template <typename It>
    priority_dequeue(It first, It last, Compare const& compare)
        : c(first, last), comp(compare)
    {
        make_maxmin_heap(c.begin(), c.end(), comp);
    }

    template <typename It>
    priority_dequeue(It first, It last, Compare const& compare, Container const& cont)
        : c(cont), comp(compare)
    {
        c.insert(first, last);
        make_maxmin_heap(c.begin(), c.end(), comp);
    }

    template <typename It>
    priority_dequeue(It first, It last, Compare const& compare, Container&& cont)
        : c(std::move(cont)), comp(compare)
    {
        c.insert(first, last);
        make_maxmin_heap(c.begin(), c.end(), comp);
    }

    const_reference top() const
    {
        return *c.begin();
    }

    const_reference bottom() const
    {
        return bottom_maxmin_heap(c.begin(), c.end(), comp);
    }

    void push(const value_type& value)
    {
        c.push_back(value);
        push_maxmin_heap(c.begin(), c.end(), comp);
    }

    void push(value_type&& value)
    {
        c.push_back(std::move(value));
        push_maxmin_heap(c.begin(), c.end(), comp);
    }

    template <typename ...Args>
    void emplace(Args&& ...args)
    {
        c.emplace_back(std::forward<Args>(args)...);
        push_maxmin_heap(c.begin(), c.end(), comp);
    }

    void pop_top()
    {
        pop_top_maxmin_heap(c.begin(), c.end(), comp);
        c.pop_back();
    }

    void pop_bottom()
    {
        pop_bottom_maxmin_heap(c.begin(), c.end(), comp);
        c.pop_back();
    }

    bool empty() const
    {
        return c.empty();
    }

    size_t size() const
    {
        return c.size();
    }

    void swap(priority_dequeue& other)
    {
        using std::swap;
        std::swap(c, other.c);
        std::swap(comp, other.comp);
    }

protected:
    Container c;
    Compare comp;
};

}}}} // namespace boost::geometry::index::detail

#endif // BOOST_GEOMETRY_INDEX_DETAIL_PRIORITY_DEQUEUE_HPP