1 | /* -*- C++ -*- |
---|
2 | * src/lemon/bin_heap.h - Part of LEMON, a generic C++ optimization library |
---|
3 | * |
---|
4 | * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
5 | * (Egervary Combinatorial Optimization Research Group, EGRES). |
---|
6 | * |
---|
7 | * Permission to use, modify and distribute this software is granted |
---|
8 | * provided that this copyright notice appears in all copies. For |
---|
9 | * precise terms see the accompanying LICENSE file. |
---|
10 | * |
---|
11 | * This software is provided "AS IS" with no warranty of any kind, |
---|
12 | * express or implied, and with no claim as to its suitability for any |
---|
13 | * purpose. |
---|
14 | * |
---|
15 | */ |
---|
16 | |
---|
17 | #ifndef LEMON_BIN_HEAP_H |
---|
18 | #define LEMON_BIN_HEAP_H |
---|
19 | |
---|
20 | ///\ingroup auxdat |
---|
21 | ///\file |
---|
22 | ///\brief Binary Heap implementation. |
---|
23 | ///\todo It should be documented. |
---|
24 | |
---|
25 | #include <vector> |
---|
26 | #include <utility> |
---|
27 | #include <functional> |
---|
28 | |
---|
29 | namespace lemon { |
---|
30 | |
---|
31 | /// \addtogroup auxdat |
---|
32 | /// @{ |
---|
33 | |
---|
34 | /// A Binary Heap implementation. |
---|
35 | template <typename Item, typename Prio, typename ItemIntMap, |
---|
36 | typename Compare = std::less<Prio> > |
---|
37 | class BinHeap { |
---|
38 | |
---|
39 | public: |
---|
40 | typedef Item ItemType; |
---|
41 | // FIXME: stl-ben nem ezt hivjak value_type -nak, hanem a kovetkezot... |
---|
42 | typedef Prio PrioType; |
---|
43 | typedef std::pair<ItemType,PrioType> PairType; |
---|
44 | typedef ItemIntMap ItemIntMapType; |
---|
45 | typedef Compare PrioCompare; |
---|
46 | |
---|
47 | /** |
---|
48 | * Each Item element have a state associated to it. It may be "in heap", |
---|
49 | * "pre heap" or "post heap". The later two are indifferent from the |
---|
50 | * heap's point of view, but may be useful to the user. |
---|
51 | * |
---|
52 | * The ItemIntMap _should_ be initialized in such way, that it maps |
---|
53 | * PRE_HEAP (-1) to any element to be put in the heap... |
---|
54 | */ |
---|
55 | ///\todo it is used nowhere |
---|
56 | /// |
---|
57 | enum state_enum { |
---|
58 | IN_HEAP = 0, |
---|
59 | PRE_HEAP = -1, |
---|
60 | POST_HEAP = -2 |
---|
61 | }; |
---|
62 | |
---|
63 | private: |
---|
64 | std::vector<PairType> data; |
---|
65 | Compare comp; |
---|
66 | // FIXME: jo ez igy??? |
---|
67 | ItemIntMap &iim; |
---|
68 | |
---|
69 | public: |
---|
70 | BinHeap(ItemIntMap &_iim) : iim(_iim) {} |
---|
71 | BinHeap(ItemIntMap &_iim, const Compare &_comp) : comp(_comp), iim(_iim) {} |
---|
72 | |
---|
73 | |
---|
74 | int size() const { return data.size(); } |
---|
75 | bool empty() const { return data.empty(); } |
---|
76 | |
---|
77 | private: |
---|
78 | static int parent(int i) { return (i-1)/2; } |
---|
79 | static int second_child(int i) { return 2*i+2; } |
---|
80 | bool less(const PairType &p1, const PairType &p2) const { |
---|
81 | return comp(p1.second, p2.second); |
---|
82 | } |
---|
83 | |
---|
84 | int bubble_up(int hole, PairType p); |
---|
85 | int bubble_down(int hole, PairType p, int length); |
---|
86 | |
---|
87 | void move(const PairType &p, int i) { |
---|
88 | data[i] = p; |
---|
89 | iim.set(p.first, i); |
---|
90 | } |
---|
91 | |
---|
92 | void rmidx(int h) { |
---|
93 | int n = data.size()-1; |
---|
94 | if( h>=0 && h<=n ) { |
---|
95 | iim.set(data[h].first, POST_HEAP); |
---|
96 | if ( h<n ) { |
---|
97 | bubble_down(h, data[n], n); |
---|
98 | } |
---|
99 | data.pop_back(); |
---|
100 | } |
---|
101 | } |
---|
102 | |
---|
103 | public: |
---|
104 | void push(const PairType &p) { |
---|
105 | int n = data.size(); |
---|
106 | data.resize(n+1); |
---|
107 | bubble_up(n, p); |
---|
108 | } |
---|
109 | void push(const Item &i, const Prio &p) { push(PairType(i,p)); } |
---|
110 | |
---|
111 | Item top() const { |
---|
112 | return data[0].first; |
---|
113 | } |
---|
114 | /// Returns the prio of the top element of the heap. |
---|
115 | Prio prio() const { |
---|
116 | return data[0].second; |
---|
117 | } |
---|
118 | |
---|
119 | void pop() { |
---|
120 | rmidx(0); |
---|
121 | } |
---|
122 | |
---|
123 | void erase(const Item &i) { |
---|
124 | rmidx(iim[i]); |
---|
125 | } |
---|
126 | |
---|
127 | Prio operator[](const Item &i) const { |
---|
128 | int idx = iim[i]; |
---|
129 | return data[idx].second; |
---|
130 | } |
---|
131 | |
---|
132 | void set(const Item &i, const Prio &p) { |
---|
133 | int idx = iim[i]; |
---|
134 | if( idx < 0 ) { |
---|
135 | push(i,p); |
---|
136 | } |
---|
137 | else if( comp(p, data[idx].second) ) { |
---|
138 | bubble_up(idx, PairType(i,p)); |
---|
139 | } |
---|
140 | else { |
---|
141 | bubble_down(idx, PairType(i,p), data.size()); |
---|
142 | } |
---|
143 | } |
---|
144 | |
---|
145 | void decrease(const Item &i, const Prio &p) { |
---|
146 | int idx = iim[i]; |
---|
147 | bubble_up(idx, PairType(i,p)); |
---|
148 | } |
---|
149 | void increase(const Item &i, const Prio &p) { |
---|
150 | int idx = iim[i]; |
---|
151 | bubble_down(idx, PairType(i,p), data.size()); |
---|
152 | } |
---|
153 | |
---|
154 | state_enum state(const Item &i) const { |
---|
155 | int s = iim[i]; |
---|
156 | if( s>=0 ) |
---|
157 | s=0; |
---|
158 | return state_enum(s); |
---|
159 | } |
---|
160 | |
---|
161 | }; // class BinHeap |
---|
162 | |
---|
163 | |
---|
164 | template <typename K, typename V, typename M, typename C> |
---|
165 | int BinHeap<K,V,M,C>::bubble_up(int hole, PairType p) { |
---|
166 | int par = parent(hole); |
---|
167 | while( hole>0 && less(p,data[par]) ) { |
---|
168 | move(data[par],hole); |
---|
169 | hole = par; |
---|
170 | par = parent(hole); |
---|
171 | } |
---|
172 | move(p, hole); |
---|
173 | return hole; |
---|
174 | } |
---|
175 | |
---|
176 | template <typename K, typename V, typename M, typename C> |
---|
177 | int BinHeap<K,V,M,C>::bubble_down(int hole, PairType p, int length) { |
---|
178 | int child = second_child(hole); |
---|
179 | while(child < length) { |
---|
180 | if( less(data[child-1], data[child]) ) { |
---|
181 | --child; |
---|
182 | } |
---|
183 | if( !less(data[child], p) ) |
---|
184 | goto ok; |
---|
185 | move(data[child], hole); |
---|
186 | hole = child; |
---|
187 | child = second_child(hole); |
---|
188 | } |
---|
189 | child--; |
---|
190 | if( child<length && less(data[child], p) ) { |
---|
191 | move(data[child], hole); |
---|
192 | hole=child; |
---|
193 | } |
---|
194 | ok: |
---|
195 | move(p, hole); |
---|
196 | return hole; |
---|
197 | } |
---|
198 | |
---|
199 | ///@} |
---|
200 | |
---|
201 | } // namespace lemon |
---|
202 | |
---|
203 | #endif // LEMON_BIN_HEAP_H |
---|