Debian Codegraph @

lemon/bucket_heap.h

raw symbols SCIP
1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
2 *
3 * This file is a part of LEMON, a generic C++ optimization library.
4 *
5 * Copyright (C) 2003-2010
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 *
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
12 *
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
15 * purpose.
16 *
17 */
18
19 #ifndef LEMON_BUCKET_HEAP_H
20 #define LEMON_BUCKET_HEAP_Hcxx . . $ `lemon/bucket_heap.h:20:9`!
21
22 ///\ingroup heaps
23 ///\file
24 ///\brief Bucket heap implementation.
25
26 #include <vector>
27 #include <utility>
28 #include <functional>
29
30 namespace lemoncxx . . $ lemon/ {
31
32 namespace _bucket_heap_bitscxx . . $ lemon/_bucket_heap_bits/ {
33
34 template <bool MIN>
35 struct DirectionTraitscxx . . $ lemon/_bucket_heap_bits/DirectionTraits# {
36 static bool lesscxx . . $ lemon/_bucket_heap_bits/DirectionTraits#less(cf9ef193275fa6d8).(int left, int right) {
37 return left < right;
38 }
39 static void increasecxx . . $ lemon/_bucket_heap_bits/DirectionTraits#increase(2afed6f74e8a6d90).(int& value) {
40 ++value;
41 }
42 };
43
44 template <>
45 struct DirectionTraitscxx . . $ lemon/_bucket_heap_bits/DirectionTraits#<false> {
46 static bool lesscxx . . $ lemon/_bucket_heap_bits/DirectionTraits#less(cf9ef193275fa6d8).(int left, int right) {
47 return left > right;
48 }
49 static void increasecxx . . $ lemon/_bucket_heap_bits/DirectionTraits#increase(2afed6f74e8a6d90).(int& value) {
50 --value;
51 }
52 };
53
54 }
55
56 /// \ingroup heaps
57 ///
58 /// \brief Bucket heap data structure.
59 ///
60 /// This class implements the \e bucket \e heap data structure.
61 /// It practically conforms to the \ref concepts::Heap "heap concept",
62 /// but it has some limitations.
63 ///
64 /// The bucket heap is a very simple structure. It can store only
65 /// \c int priorities and it maintains a list of items for each priority
66 /// in the range <tt>[0..C)</tt>. So it should only be used when the
67 /// priorities are small. It is not intended to use as a Dijkstra heap.
68 ///
69 /// \tparam IM A read-writable item map with \c int values, used
70 /// internally to handle the cross references.
71 /// \tparam MIN Indicate if the heap is a \e min-heap or a \e max-heap.
72 /// The default is \e min-heap. If this parameter is set to \c false,
73 /// then the comparison is reversed, so the top(), prio() and pop()
74 /// functions deal with the item having maximum priority instead of the
75 /// minimum.
76 ///
77 /// \sa SimpleBucketHeap
78 template <typename IM, bool MIN = true>
79 class BucketHeapcxx . . $ lemon/BucketHeap# {
80
81 public:
82
83 /// Type of the item-int map.
84 typedef IM ItemIntMapcxx . . $ lemon/BucketHeap#ItemIntMap#;
85 /// Type of the priorities.
86 typedef int Priocxx . . $ lemon/BucketHeap#Prio#;
87 /// Type of the items stored in the heap.
88 typedef typename ItemIntMapcxx . . $ lemon/BucketHeap#ItemIntMap#::Key Itemcxx . . $ lemon/BucketHeap#Item#;
89 /// Type of the item-priority pairs.
90 typedef stdcxx . . $ std/::paircxx . . $ std/pair#<Itemcxx . . $ lemon/BucketHeap#Item#,Priocxx . . $ lemon/BucketHeap#Prio#> Paircxx . . $ lemon/BucketHeap#Pair#;
91
92 private:
93
94 typedef _bucket_heap_bitscxx . . $ lemon/_bucket_heap_bits/::DirectionTraitscxx . . $ lemon/_bucket_heap_bits/DirectionTraits#<MIN> Directioncxx . . $ lemon/BucketHeap#Direction#;
95
96 public:
97
98 /// \brief Type to represent the states of the items.
99 ///
100 /// Each item has a state associated to it. It can be "in heap",
101 /// "pre-heap" or "post-heap". The latter two are indifferent from the
102 /// heap's point of view, but may be useful to the user.
103 ///
104 /// The item-int map must be initialized in such way that it assigns
105 /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
106 enum Statecxx . . $ lemon/BucketHeap#State# {
107 IN_HEAPcxx . . $ lemon/BucketHeap#IN_HEAP. = 0, ///< = 0.
108 PRE_HEAPcxx . . $ lemon/BucketHeap#PRE_HEAP. = -1, ///< = -1.
109 POST_HEAPcxx . . $ lemon/BucketHeap#POST_HEAP. = -2 ///< = -2.
110 };
111
112 public:
113
114 /// \brief Constructor.
115 ///
116 /// Constructor.
117 /// \param map A map that assigns \c int values to the items.
118 /// It is used internally to handle the cross references.
119 /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
120 explicit BucketHeapcxx . . $ lemon/BucketHeap#`BucketHeap<IM, MIN>`(8e935d9c2e576e3a).(ItemIntMapcxx . . $ lemon/BucketHeap#ItemIntMap# &map) : _iimcxx . . $ lemon/BucketHeap#_iim.(map), _minimumcxx . . $ lemon/BucketHeap#_minimum.(0) {}
121
122 /// \brief The number of items stored in the heap.
123 ///
124 /// This function returns the number of items stored in the heap.
125 int sizecxx . . $ lemon/BucketHeap#size(455f465bc33b4cdf).() const { return _datacxx . . $ lemon/BucketHeap#_data..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).(); }
126
127 /// \brief Check if the heap is empty.
128 ///
129 /// This function returns \c true if the heap is empty.
130 bool emptycxx . . $ lemon/BucketHeap#empty(50ce9a9e25b4a850).() const { return _datacxx . . $ lemon/BucketHeap#_data..emptycxx . . $ std/vector#empty(3482b152b9333168).(); }
131
132 /// \brief Make the heap empty.
133 ///
134 /// This functon makes the heap empty.
135 /// It does not change the cross reference map. If you want to reuse
136 /// a heap that is not surely empty, you should first clear it and
137 /// then you should set the cross reference map to \c PRE_HEAP
138 /// for each item.
139 void clearcxx . . $ lemon/BucketHeap#clear(49f6e7a06ebc5aa8).() {
140 _datacxx . . $ lemon/BucketHeap#_data..clearcxx . . $ std/vector#clear(ced63f7c635d850d).(); _firstcxx . . $ lemon/BucketHeap#_first..clearcxx . . $ std/vector#clear(ced63f7c635d850d).(); _minimumcxx . . $ lemon/BucketHeap#_minimum. = 0;
141 }
142
143 private:
144
145 void relocateLastcxx . . $ lemon/BucketHeap#relocateLast(d4f767463ce0a6b3).(int idx) {
146 if (idx + 1 < int(_datacxx . . $ lemon/BucketHeap#_data..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).())) {
147 _datacxx . . $ lemon/BucketHeap#_data.[idx] = _datacxx . . $ lemon/BucketHeap#_data..backcxx . . $ std/vector#back(6baabfa6fc7b3a8e).();
148 if (_datacxx . . $ lemon/BucketHeap#_data.[idx].prev != -1) {
149 _datacxx . . $ lemon/BucketHeap#_data.[_datacxx . . $ lemon/BucketHeap#_data.[idx].prev].next = idx;
150 } else {
151 _firstcxx . . $ lemon/BucketHeap#_first.[_datacxx . . $ lemon/BucketHeap#_data.[idx].value] = idx;
152 }
153 if (_datacxx . . $ lemon/BucketHeap#_data.[idx].next != -1) {
154 _datacxx . . $ lemon/BucketHeap#_data.[_datacxx . . $ lemon/BucketHeap#_data.[idx].next].prev = idx;
155 }
156 _iimcxx . . $ lemon/BucketHeap#_iim.[_datacxx . . $ lemon/BucketHeap#_data.[idx].item] = idx;
157 }
158 _datacxx . . $ lemon/BucketHeap#_data..pop_backcxx . . $ std/vector#pop_back(ced63f7c635d850d).();
159 }
160
161 void unlacecxx . . $ lemon/BucketHeap#unlace(d4f767463ce0a6b3).(int idx) {
162 if (_datacxx . . $ lemon/BucketHeap#_data.[idx].prev != -1) {
163 _datacxx . . $ lemon/BucketHeap#_data.[_datacxx . . $ lemon/BucketHeap#_data.[idx].prev].next = _datacxx . . $ lemon/BucketHeap#_data.[idx].next;
164 } else {
165 _firstcxx . . $ lemon/BucketHeap#_first.[_datacxx . . $ lemon/BucketHeap#_data.[idx].value] = _datacxx . . $ lemon/BucketHeap#_data.[idx].next;
166 }
167 if (_datacxx . . $ lemon/BucketHeap#_data.[idx].next != -1) {
168 _datacxx . . $ lemon/BucketHeap#_data.[_datacxx . . $ lemon/BucketHeap#_data.[idx].next].prev = _datacxx . . $ lemon/BucketHeap#_data.[idx].prev;
169 }
170 }
171
172 void lacecxx . . $ lemon/BucketHeap#lace(d4f767463ce0a6b3).(int idx) {
173 if (int(_firstcxx . . $ lemon/BucketHeap#_first..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).()) <= _datacxx . . $ lemon/BucketHeap#_data.[idx].value) {
174 _firstcxx . . $ lemon/BucketHeap#_first..resizecxx . . $ std/vector#resize(3804226c8c0acbc3).(_datacxx . . $ lemon/BucketHeap#_data.[idx].value + 1, -1);
175 }
176 _datacxx . . $ lemon/BucketHeap#_data.[idx].next = _firstcxx . . $ lemon/BucketHeap#_first.[_datacxx . . $ lemon/BucketHeap#_data.[idx].value];
177 if (_datacxx . . $ lemon/BucketHeap#_data.[idx].next != -1) {
178 _datacxx . . $ lemon/BucketHeap#_data.[_datacxx . . $ lemon/BucketHeap#_data.[idx].next].prev = idx;
179 }
180 _firstcxx . . $ lemon/BucketHeap#_first.[_datacxx . . $ lemon/BucketHeap#_data.[idx].value] = idx;
181 _datacxx . . $ lemon/BucketHeap#_data.[idx].prev = -1;
182 }
183
184 public:
185
186 /// \brief Insert a pair of item and priority into the heap.
187 ///
188 /// This function inserts \c p.first to the heap with priority
189 /// \c p.second.
190 /// \param p The pair to insert.
191 /// \pre \c p.first must not be stored in the heap.
192 void pushcxx . . $ lemon/BucketHeap#push(89f52d8154fea9b4).(const Paircxx . . $ lemon/BucketHeap#Pair#& p) {
193 pushcxx . . $ lemon/BucketHeap#push(cf2f737abe8b3505).(p.firstcxx . . $ std/pair#first., p.secondcxx . . $ std/pair#second.);
194 }
195
196 /// \brief Insert an item into the heap with the given priority.
197 ///
198 /// This function inserts the given item into the heap with the
199 /// given priority.
200 /// \param i The item to insert.
201 /// \param p The priority of the item.
202 /// \pre \e i must not be stored in the heap.
203 void pushcxx . . $ lemon/BucketHeap#push(cf2f737abe8b3505).(const Itemcxx . . $ lemon/BucketHeap#Item# &i, const Priocxx . . $ lemon/BucketHeap#Prio# &p) {
204 int idx = _datacxx . . $ lemon/BucketHeap#_data..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).();
205 _iimcxx . . $ lemon/BucketHeap#_iim.[i] = idx;
206 _datacxx . . $ lemon/BucketHeap#_data..push_backcxx . . $ std/vector#push_back(fe7a8a4d1eddb70).(BucketItemcxx . . $ lemon/BucketHeap#BucketItem#(i, p));
207 lacecxx . . $ lemon/BucketHeap#lace(d4f767463ce0a6b3).(idx);
208 if (Directioncxx . . $ lemon/BucketHeap#Direction#::less(p, _minimumcxx . . $ lemon/BucketHeap#_minimum.)) {
209 _minimumcxx . . $ lemon/BucketHeap#_minimum. = p;
210 }
211 }
212
213 /// \brief Return the item having minimum priority.
214 ///
215 /// This function returns the item having minimum priority.
216 /// \pre The heap must be non-empty.
217 Itemcxx . . $ lemon/BucketHeap#Item# topcxx . . $ lemon/BucketHeap#top(e6707c32af87d969).() const {
218 while (_firstcxx . . $ lemon/BucketHeap#_first.[cxx . . $ std/vector#`operator[]`(c8255e0fa326ac32)._minimumcxx . . $ lemon/BucketHeap#_minimum.] == -1) {
219 Directioncxx . . $ lemon/BucketHeap#Direction#::increase(_minimumcxx . . $ lemon/BucketHeap#_minimum.);
220 }
221 return _datacxx . . $ lemon/BucketHeap#_data.[_firstcxx . . $ lemon/BucketHeap#_first.[cxx . . $ std/vector#`operator[]`(c8255e0fa326ac32)._minimumcxx . . $ lemon/BucketHeap#_minimum.]].item;
222 }
223
224 /// \brief The minimum priority.
225 ///
226 /// This function returns the minimum priority.
227 /// \pre The heap must be non-empty.
228 Priocxx . . $ lemon/BucketHeap#Prio# priocxx . . $ lemon/BucketHeap#prio(455f465bc33b4cdf).() const {
229 while (_firstcxx . . $ lemon/BucketHeap#_first.[cxx . . $ std/vector#`operator[]`(c8255e0fa326ac32)._minimumcxx . . $ lemon/BucketHeap#_minimum.] == -1) {
230 Directioncxx . . $ lemon/BucketHeap#Direction#::increase(_minimumcxx . . $ lemon/BucketHeap#_minimum.);
231 }
232 return _minimumcxx . . $ lemon/BucketHeap#_minimum.;
233 }
234
235 /// \brief Remove the item having minimum priority.
236 ///
237 /// This function removes the item having minimum priority.
238 /// \pre The heap must be non-empty.
239 void popcxx . . $ lemon/BucketHeap#pop(49f6e7a06ebc5aa8).() {
240 while (_firstcxx . . $ lemon/BucketHeap#_first.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce)._minimumcxx . . $ lemon/BucketHeap#_minimum.] == -1) {
241 Directioncxx . . $ lemon/BucketHeap#Direction#::increase(_minimumcxx . . $ lemon/BucketHeap#_minimum.);
242 }
243 int idx = _firstcxx . . $ lemon/BucketHeap#_first.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce)._minimumcxx . . $ lemon/BucketHeap#_minimum.];
244 _iimcxx . . $ lemon/BucketHeap#_iim.[_datacxx . . $ lemon/BucketHeap#_data.[idx].item] = -2;
245 unlacecxx . . $ lemon/BucketHeap#unlace(d4f767463ce0a6b3).(idx);
246 relocateLastcxx . . $ lemon/BucketHeap#relocateLast(d4f767463ce0a6b3).(idx);
247 }
248
249 /// \brief Remove the given item from the heap.
250 ///
251 /// This function removes the given item from the heap if it is
252 /// already stored.
253 /// \param i The item to delete.
254 /// \pre \e i must be in the heap.
255 void erasecxx . . $ lemon/BucketHeap#erase(8c13afcd6b0c9deb).(const Itemcxx . . $ lemon/BucketHeap#Item# &i) {
256 int idx = _iimcxx . . $ lemon/BucketHeap#_iim.[i];
257 _iimcxx . . $ lemon/BucketHeap#_iim.[_datacxx . . $ lemon/BucketHeap#_data.[idx].item] = -2;
258 unlacecxx . . $ lemon/BucketHeap#unlace(d4f767463ce0a6b3).(idx);
259 relocateLastcxx . . $ lemon/BucketHeap#relocateLast(d4f767463ce0a6b3).(idx);
260 }
261
262 /// \brief The priority of the given item.
263 ///
264 /// This function returns the priority of the given item.
265 /// \param i The item.
266 /// \pre \e i must be in the heap.
267 Priocxx . . $ lemon/BucketHeap#Prio# operatorcxx . . $ lemon/BucketHeap#`operator[]`(8585a36945f51745).[](const Itemcxx . . $ lemon/BucketHeap#Item# &i) const {
268 int idx = _iimcxx . . $ lemon/BucketHeap#_iim.[i];
269 return _datacxx . . $ lemon/BucketHeap#_data.[idx].value;
270 }
271
272 /// \brief Set the priority of an item or insert it, if it is
273 /// not stored in the heap.
274 ///
275 /// This method sets the priority of the given item if it is
276 /// already stored in the heap. Otherwise it inserts the given
277 /// item into the heap with the given priority.
278 /// \param i The item.
279 /// \param p The priority.
280 void setcxx . . $ lemon/BucketHeap#set(cf2f737abe8b3505).(const Itemcxx . . $ lemon/BucketHeap#Item# &i, const Priocxx . . $ lemon/BucketHeap#Prio# &p) {
281 int idx = _iimcxx . . $ lemon/BucketHeap#_iim.[i];
282 if (idx < 0) {
283 pushcxx . . $ lemon/BucketHeap#push(89f52d8154fea9b4).(i, p);
284 } else if (Directioncxx . . $ lemon/BucketHeap#Direction#::less(p, _datacxx . . $ lemon/BucketHeap#_data.[idx].value)) {
285 decreasecxx . . $ lemon/BucketHeap#decrease(cf2f737abe8b3505).(i, p);
286 } else {
287 increasecxx . . $ lemon/BucketHeap#increase(cf2f737abe8b3505).(i, p);
288 }
289 }
290
291 /// \brief Decrease the priority of an item to the given value.
292 ///
293 /// This function decreases the priority of an item to the given value.
294 /// \param i The item.
295 /// \param p The priority.
296 /// \pre \e i must be stored in the heap with priority at least \e p.
297 void decreasecxx . . $ lemon/BucketHeap#decrease(cf2f737abe8b3505).(const Itemcxx . . $ lemon/BucketHeap#Item# &i, const Priocxx . . $ lemon/BucketHeap#Prio# &p) {
298 int idx = _iimcxx . . $ lemon/BucketHeap#_iim.[i];
299 unlacecxx . . $ lemon/BucketHeap#unlace(d4f767463ce0a6b3).(idx);
300 _datacxx . . $ lemon/BucketHeap#_data.[idx].value = p;
301 if (Directioncxx . . $ lemon/BucketHeap#Direction#::less(p, _minimumcxx . . $ lemon/BucketHeap#_minimum.)) {
302 _minimumcxx . . $ lemon/BucketHeap#_minimum. = p;
303 }
304 lacecxx . . $ lemon/BucketHeap#lace(d4f767463ce0a6b3).(idx);
305 }
306
307 /// \brief Increase the priority of an item to the given value.
308 ///
309 /// This function increases the priority of an item to the given value.
310 /// \param i The item.
311 /// \param p The priority.
312 /// \pre \e i must be stored in the heap with priority at most \e p.
313 void increasecxx . . $ lemon/BucketHeap#increase(cf2f737abe8b3505).(const Itemcxx . . $ lemon/BucketHeap#Item# &i, const Priocxx . . $ lemon/BucketHeap#Prio# &p) {
314 int idx = _iimcxx . . $ lemon/BucketHeap#_iim.[i];
315 unlacecxx . . $ lemon/BucketHeap#unlace(d4f767463ce0a6b3).(idx);
316 _datacxx . . $ lemon/BucketHeap#_data.[idx].value = p;
317 lacecxx . . $ lemon/BucketHeap#lace(d4f767463ce0a6b3).(idx);
318 }
319
320 /// \brief Return the state of an item.
321 ///
322 /// This method returns \c PRE_HEAP if the given item has never
323 /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
324 /// and \c POST_HEAP otherwise.
325 /// In the latter case it is possible that the item will get back
326 /// to the heap again.
327 /// \param i The item.
328 Statecxx . . $ lemon/BucketHeap#State# statecxx . . $ lemon/BucketHeap#state(6d8180bf1ec0922b).(const Itemcxx . . $ lemon/BucketHeap#Item# &i) const {
329 int idx = _iimcxx . . $ lemon/BucketHeap#_iim.[i];
330 if (idx >= 0) idx = 0;
331 return Statecxx . . $ lemon/BucketHeap#State#(idx);
332 }
333
334 /// \brief Set the state of an item in the heap.
335 ///
336 /// This function sets the state of the given item in the heap.
337 /// It can be used to manually clear the heap when it is important
338 /// to achive better time complexity.
339 /// \param i The item.
340 /// \param st The state. It should not be \c IN_HEAP.
341 void statecxx . . $ lemon/BucketHeap#state(cf010b91f1cf44da).(const Itemcxx . . $ lemon/BucketHeap#Item#& i, Statecxx . . $ lemon/BucketHeap#State# st) {
342 switch (st) {
343 case POST_HEAPcxx . . $ lemon/BucketHeap#POST_HEAP.:
344 case PRE_HEAPcxx . . $ lemon/BucketHeap#PRE_HEAP.:
345 if (statecxx . . $ lemon/BucketHeap#state(6d8180bf1ec0922b).(i) == IN_HEAPcxx . . $ lemon/BucketHeap#IN_HEAP.) {
346 erasecxx . . $ lemon/BucketHeap#erase(8c13afcd6b0c9deb).(i);
347 }
348 _iimcxx . . $ lemon/BucketHeap#_iim.[i] = st;
349 break;
350 case IN_HEAPcxx . . $ lemon/BucketHeap#IN_HEAP.:
351 break;
352 }
353 }
354
355 private:
356
357 struct BucketItemcxx . . $ lemon/BucketHeap#BucketItem# {
358 BucketItemcxx . . $ lemon/BucketHeap#BucketItem#BucketItem(530ceb62dc262d88).(const Itemcxx . . $ lemon/BucketHeap#Item#& _item, int _value)
359 : itemcxx . . $ lemon/BucketHeap#BucketItem#item.(_item), valuecxx . . $ lemon/BucketHeap#BucketItem#value.(_value) {}
360
361 Itemcxx . . $ lemon/BucketHeap#Item# itemcxx . . $ lemon/BucketHeap#BucketItem#item.;
362 int valuecxx . . $ lemon/BucketHeap#BucketItem#value.;
363
364 int prevcxx . . $ lemon/BucketHeap#BucketItem#prev., nextcxx . . $ lemon/BucketHeap#BucketItem#next.;
365 };
366
367 ItemIntMapcxx . . $ lemon/BucketHeap#ItemIntMap#& _iimcxx . . $ lemon/BucketHeap#_iim.;
368 stdcxx . . $ std/::vectorcxx . . $ std/vector#<int> _firstcxx . . $ lemon/BucketHeap#_first.;
369 stdcxx . . $ std/::vectorcxx . . $ std/vector#<BucketItemcxx . . $ lemon/BucketHeap#BucketItem#> _datacxx . . $ lemon/BucketHeap#_data.;
370 mutable int _minimumcxx . . $ lemon/BucketHeap#_minimum.;
371
372 }; // class BucketHeap
373
374 /// \ingroup heaps
375 ///
376 /// \brief Simplified bucket heap data structure.
377 ///
378 /// This class implements a simplified \e bucket \e heap data
379 /// structure. It does not provide some functionality, but it is
380 /// faster and simpler than BucketHeap. The main difference is
381 /// that BucketHeap stores a doubly-linked list for each key while
382 /// this class stores only simply-linked lists. It supports erasing
383 /// only for the item having minimum priority and it does not support
384 /// key increasing and decreasing.
385 ///
386 /// Note that this implementation does not conform to the
387 /// \ref concepts::Heap "heap concept" due to the lack of some
388 /// functionality.
389 ///
390 /// \tparam IM A read-writable item map with \c int values, used
391 /// internally to handle the cross references.
392 /// \tparam MIN Indicate if the heap is a \e min-heap or a \e max-heap.
393 /// The default is \e min-heap. If this parameter is set to \c false,
394 /// then the comparison is reversed, so the top(), prio() and pop()
395 /// functions deal with the item having maximum priority instead of the
396 /// minimum.
397 ///
398 /// \sa BucketHeap
399 template <typename IM, bool MIN = true >
400 class SimpleBucketHeapcxx . . $ lemon/SimpleBucketHeap# {
401
402 public:
403
404 /// Type of the item-int map.
405 typedef IM ItemIntMapcxx . . $ lemon/SimpleBucketHeap#ItemIntMap#;
406 /// Type of the priorities.
407 typedef int Priocxx . . $ lemon/SimpleBucketHeap#Prio#;
408 /// Type of the items stored in the heap.
409 typedef typename ItemIntMapcxx . . $ lemon/SimpleBucketHeap#ItemIntMap#::Key Itemcxx . . $ lemon/SimpleBucketHeap#Item#;
410 /// Type of the item-priority pairs.
411 typedef stdcxx . . $ std/::paircxx . . $ std/pair#<Itemcxx . . $ lemon/SimpleBucketHeap#Item#,Priocxx . . $ lemon/SimpleBucketHeap#Prio#> Paircxx . . $ lemon/SimpleBucketHeap#Pair#;
412
413 private:
414
415 typedef _bucket_heap_bitscxx . . $ lemon/_bucket_heap_bits/::DirectionTraitscxx . . $ lemon/_bucket_heap_bits/DirectionTraits#<MIN> Directioncxx . . $ lemon/SimpleBucketHeap#Direction#;
416
417 public:
418
419 /// \brief Type to represent the states of the items.
420 ///
421 /// Each item has a state associated to it. It can be "in heap",
422 /// "pre-heap" or "post-heap". The latter two are indifferent from the
423 /// heap's point of view, but may be useful to the user.
424 ///
425 /// The item-int map must be initialized in such way that it assigns
426 /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
427 enum Statecxx . . $ lemon/SimpleBucketHeap#State# {
428 IN_HEAPcxx . . $ lemon/SimpleBucketHeap#IN_HEAP. = 0, ///< = 0.
429 PRE_HEAPcxx . . $ lemon/SimpleBucketHeap#PRE_HEAP. = -1, ///< = -1.
430 POST_HEAPcxx . . $ lemon/SimpleBucketHeap#POST_HEAP. = -2 ///< = -2.
431 };
432
433 public:
434
435 /// \brief Constructor.
436 ///
437 /// Constructor.
438 /// \param map A map that assigns \c int values to the items.
439 /// It is used internally to handle the cross references.
440 /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
441 explicit SimpleBucketHeapcxx . . $ lemon/SimpleBucketHeap#`SimpleBucketHeap<IM, MIN>`(8e935d9c2e576e3a).(ItemIntMapcxx . . $ lemon/SimpleBucketHeap#ItemIntMap# &map)
442 : _iimcxx . . $ lemon/SimpleBucketHeap#_iim.(map), _freecxx . . $ lemon/SimpleBucketHeap#_free.(-1), _numcxx . . $ lemon/SimpleBucketHeap#_num.(0), _minimumcxx . . $ lemon/SimpleBucketHeap#_minimum.(0) {}
443
444 /// \brief The number of items stored in the heap.
445 ///
446 /// This function returns the number of items stored in the heap.
447 int sizecxx . . $ lemon/SimpleBucketHeap#size(455f465bc33b4cdf).() const { return _numcxx . . $ lemon/SimpleBucketHeap#_num.; }
448
449 /// \brief Check if the heap is empty.
450 ///
451 /// This function returns \c true if the heap is empty.
452 bool emptycxx . . $ lemon/SimpleBucketHeap#empty(50ce9a9e25b4a850).() const { return _numcxx . . $ lemon/SimpleBucketHeap#_num. == 0; }
453
454 /// \brief Make the heap empty.
455 ///
456 /// This functon makes the heap empty.
457 /// It does not change the cross reference map. If you want to reuse
458 /// a heap that is not surely empty, you should first clear it and
459 /// then you should set the cross reference map to \c PRE_HEAP
460 /// for each item.
461 void clearcxx . . $ lemon/SimpleBucketHeap#clear(49f6e7a06ebc5aa8).() {
462 _datacxx . . $ lemon/SimpleBucketHeap#_data..clearcxx . . $ std/vector#clear(ced63f7c635d850d).(); _firstcxx . . $ lemon/SimpleBucketHeap#_first..clearcxx . . $ std/vector#clear(ced63f7c635d850d).(); _freecxx . . $ lemon/SimpleBucketHeap#_free. = -1; _numcxx . . $ lemon/SimpleBucketHeap#_num. = 0; _minimumcxx . . $ lemon/SimpleBucketHeap#_minimum. = 0;
463 }
464
465 /// \brief Insert a pair of item and priority into the heap.
466 ///
467 /// This function inserts \c p.first to the heap with priority
468 /// \c p.second.
469 /// \param p The pair to insert.
470 /// \pre \c p.first must not be stored in the heap.
471 void pushcxx . . $ lemon/SimpleBucketHeap#push(89f52d8154fea9b4).(const Paircxx . . $ lemon/SimpleBucketHeap#Pair#& p) {
472 pushcxx . . $ lemon/SimpleBucketHeap#push(cf2f737abe8b3505).(p.firstcxx . . $ std/pair#first., p.secondcxx . . $ std/pair#second.);
473 }
474
475 /// \brief Insert an item into the heap with the given priority.
476 ///
477 /// This function inserts the given item into the heap with the
478 /// given priority.
479 /// \param i The item to insert.
480 /// \param p The priority of the item.
481 /// \pre \e i must not be stored in the heap.
482 void pushcxx . . $ lemon/SimpleBucketHeap#push(cf2f737abe8b3505).(const Itemcxx . . $ lemon/SimpleBucketHeap#Item# &i, const Priocxx . . $ lemon/SimpleBucketHeap#Prio# &p) {
483 int idx;
484 if (_freecxx . . $ lemon/SimpleBucketHeap#_free. == -1) {
485 idx = _datacxx . . $ lemon/SimpleBucketHeap#_data..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).();
486 _datacxx . . $ lemon/SimpleBucketHeap#_data..push_backcxx . . $ std/vector#push_back(fe7a8a4d1eddb70).(BucketItemcxx . . $ lemon/SimpleBucketHeap#BucketItem#(i));
487 } else {
488 idx = _freecxx . . $ lemon/SimpleBucketHeap#_free.;
489 _freecxx . . $ lemon/SimpleBucketHeap#_free. = _datacxx . . $ lemon/SimpleBucketHeap#_data.[idx].next;
490 _datacxx . . $ lemon/SimpleBucketHeap#_data.[idx].item = i;
491 }
492 _iimcxx . . $ lemon/SimpleBucketHeap#_iim.[i] = idx;
493 if (p >= int(_firstcxx . . $ lemon/SimpleBucketHeap#_first..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).())) _firstcxx . . $ lemon/SimpleBucketHeap#_first..resizecxx . . $ std/vector#resize(3804226c8c0acbc3).(p + 1, -1);
494 _datacxx . . $ lemon/SimpleBucketHeap#_data.[idx].next = _firstcxx . . $ lemon/SimpleBucketHeap#_first.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).p];
495 _firstcxx . . $ lemon/SimpleBucketHeap#_first.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).p] = idx;
496 if (Directioncxx . . $ lemon/SimpleBucketHeap#Direction#::less(p, _minimumcxx . . $ lemon/SimpleBucketHeap#_minimum.)) {
497 _minimumcxx . . $ lemon/SimpleBucketHeap#_minimum. = p;
498 }
499 ++_numcxx . . $ lemon/SimpleBucketHeap#_num.;
500 }
501
502 /// \brief Return the item having minimum priority.
503 ///
504 /// This function returns the item having minimum priority.
505 /// \pre The heap must be non-empty.
506 Itemcxx . . $ lemon/SimpleBucketHeap#Item# topcxx . . $ lemon/SimpleBucketHeap#top(e6707c32af87d969).() const {
507 while (_firstcxx . . $ lemon/SimpleBucketHeap#_first.[cxx . . $ std/vector#`operator[]`(c8255e0fa326ac32)._minimumcxx . . $ lemon/SimpleBucketHeap#_minimum.] == -1) {
508 Directioncxx . . $ lemon/SimpleBucketHeap#Direction#::increase(_minimumcxx . . $ lemon/SimpleBucketHeap#_minimum.);
509 }
510 return _datacxx . . $ lemon/SimpleBucketHeap#_data.[_firstcxx . . $ lemon/SimpleBucketHeap#_first.[cxx . . $ std/vector#`operator[]`(c8255e0fa326ac32)._minimumcxx . . $ lemon/SimpleBucketHeap#_minimum.]].item;
511 }
512
513 /// \brief The minimum priority.
514 ///
515 /// This function returns the minimum priority.
516 /// \pre The heap must be non-empty.
517 Priocxx . . $ lemon/SimpleBucketHeap#Prio# priocxx . . $ lemon/SimpleBucketHeap#prio(455f465bc33b4cdf).() const {
518 while (_firstcxx . . $ lemon/SimpleBucketHeap#_first.[cxx . . $ std/vector#`operator[]`(c8255e0fa326ac32)._minimumcxx . . $ lemon/SimpleBucketHeap#_minimum.] == -1) {
519 Directioncxx . . $ lemon/SimpleBucketHeap#Direction#::increase(_minimumcxx . . $ lemon/SimpleBucketHeap#_minimum.);
520 }
521 return _minimumcxx . . $ lemon/SimpleBucketHeap#_minimum.;
522 }
523
524 /// \brief Remove the item having minimum priority.
525 ///
526 /// This function removes the item having minimum priority.
527 /// \pre The heap must be non-empty.
528 void popcxx . . $ lemon/SimpleBucketHeap#pop(49f6e7a06ebc5aa8).() {
529 while (_firstcxx . . $ lemon/SimpleBucketHeap#_first.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce)._minimumcxx . . $ lemon/SimpleBucketHeap#_minimum.] == -1) {
530 Directioncxx . . $ lemon/SimpleBucketHeap#Direction#::increase(_minimumcxx . . $ lemon/SimpleBucketHeap#_minimum.);
531 }
532 int idx = _firstcxx . . $ lemon/SimpleBucketHeap#_first.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce)._minimumcxx . . $ lemon/SimpleBucketHeap#_minimum.];
533 _iimcxx . . $ lemon/SimpleBucketHeap#_iim.[_datacxx . . $ lemon/SimpleBucketHeap#_data.[idx].item] = -2;
534 _firstcxx . . $ lemon/SimpleBucketHeap#_first.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce)._minimumcxx . . $ lemon/SimpleBucketHeap#_minimum.] = _datacxx . . $ lemon/SimpleBucketHeap#_data.[idx].next;
535 _datacxx . . $ lemon/SimpleBucketHeap#_data.[idx].next = _freecxx . . $ lemon/SimpleBucketHeap#_free.;
536 _freecxx . . $ lemon/SimpleBucketHeap#_free. = idx;
537 --_numcxx . . $ lemon/SimpleBucketHeap#_num.;
538 }
539
540 /// \brief The priority of the given item.
541 ///
542 /// This function returns the priority of the given item.
543 /// \param i The item.
544 /// \pre \e i must be in the heap.
545 /// \warning This operator is not a constant time function because
546 /// it scans the whole data structure to find the proper value.
547 Priocxx . . $ lemon/SimpleBucketHeap#Prio# operatorcxx . . $ lemon/SimpleBucketHeap#`operator[]`(8585a36945f51745).[](const Itemcxx . . $ lemon/SimpleBucketHeap#Item# &i) const {
548 for (int k = 0; k < int(_firstcxx . . $ lemon/SimpleBucketHeap#_first..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).()); ++k) {
549 int idx = _firstcxx . . $ lemon/SimpleBucketHeap#_first.[cxx . . $ std/vector#`operator[]`(c8255e0fa326ac32).k];
550 while (idx != -1) {
551 if (_datacxx . . $ lemon/SimpleBucketHeap#_data.[idx].item == i) {
552 return k;
553 }
554 idx = _datacxx . . $ lemon/SimpleBucketHeap#_data.[idx].next;
555 }
556 }
557 return -1;
558 }
559
560 /// \brief Return the state of an item.
561 ///
562 /// This method returns \c PRE_HEAP if the given item has never
563 /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
564 /// and \c POST_HEAP otherwise.
565 /// In the latter case it is possible that the item will get back
566 /// to the heap again.
567 /// \param i The item.
568 Statecxx . . $ lemon/SimpleBucketHeap#State# statecxx . . $ lemon/SimpleBucketHeap#state(f57199a1da46f686).(const Itemcxx . . $ lemon/SimpleBucketHeap#Item# &i) const {
569 int idx = _iimcxx . . $ lemon/SimpleBucketHeap#_iim.[i];
570 if (idx >= 0) idx = 0;
571 return Statecxx . . $ lemon/SimpleBucketHeap#State#(idx);
572 }
573
574 private:
575
576 struct BucketItemcxx . . $ lemon/SimpleBucketHeap#BucketItem# {
577 BucketItemcxx . . $ lemon/SimpleBucketHeap#BucketItem#BucketItem(8c13afcd6b0c9deb).(const Itemcxx . . $ lemon/SimpleBucketHeap#Item#& _item)
578 : itemcxx . . $ lemon/SimpleBucketHeap#BucketItem#item.(_item) {}
579
580 Itemcxx . . $ lemon/SimpleBucketHeap#Item# itemcxx . . $ lemon/SimpleBucketHeap#BucketItem#item.;
581 int nextcxx . . $ lemon/SimpleBucketHeap#BucketItem#next.;
582 };
583
584 ItemIntMapcxx . . $ lemon/SimpleBucketHeap#ItemIntMap#& _iimcxx . . $ lemon/SimpleBucketHeap#_iim.;
585 stdcxx . . $ std/::vectorcxx . . $ std/vector#<int> _firstcxx . . $ lemon/SimpleBucketHeap#_first.;
586 stdcxx . . $ std/::vectorcxx . . $ std/vector#<BucketItemcxx . . $ lemon/SimpleBucketHeap#BucketItem#> _datacxx . . $ lemon/SimpleBucketHeap#_data.;
587 int _freecxx . . $ lemon/SimpleBucketHeap#_free., _numcxx . . $ lemon/SimpleBucketHeap#_num.;
588 mutable int _minimumcxx . . $ lemon/SimpleBucketHeap#_minimum.;
589
590 }; // class SimpleBucketHeap
591
592 }
593
594 #endif