Added the function isFinite(), and replaced the calls to finite() with it.
This was necessary because finite() is not a standard function. Neither can
we use its standard counterpart isfinite(), because it was introduced only
in C99, and therefore it is not supplied by all C++ implementations.
3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2007
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
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.
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
19 #ifndef LEMON_BUCKET_HEAP_H
20 #define LEMON_BUCKET_HEAP_H
24 ///\brief Bucket Heap implementation.
34 /// \brief A Bucket Heap implementation.
36 /// This class implements the \e bucket \e heap data structure. A \e heap
37 /// is a data structure for storing items with specified values called \e
38 /// priorities in such a way that finding the item with minimum priority is
39 /// efficient. The bucket heap is very simple implementation, it can store
40 /// only integer priorities and it stores for each priority in the
41 /// \f$ [0..C) \f$ range a list of items. So it should be used only when
42 /// the priorities are small. It is not intended to use as dijkstra heap.
44 /// \param _ItemIntMap A read and writable Item int map, used internally
45 /// to handle the cross references.
46 /// \param minimize If the given parameter is true then the heap gives back
47 /// the lowest priority.
48 template <typename _ItemIntMap, bool minimize = true >
52 typedef typename _ItemIntMap::Key Item;
54 typedef std::pair<Item, Prio> Pair;
55 typedef _ItemIntMap ItemIntMap;
57 /// \brief Type to represent the items states.
59 /// Each Item element have a state associated to it. It may be "in heap",
60 /// "pre heap" or "post heap". The latter two are indifferent from the
61 /// heap's point of view, but may be useful to the user.
63 /// The ItemIntMap \e should be initialized in such way that it maps
64 /// PRE_HEAP (-1) to any element to be put in the heap...
72 /// \brief The constructor.
75 /// \param _index should be given to the constructor, since it is used
76 /// internally to handle the cross references. The value of the map
77 /// should be PRE_HEAP (-1) for each element.
78 explicit BucketHeap(ItemIntMap &_index) : index(_index), minimal(0) {}
80 /// The number of items stored in the heap.
82 /// \brief Returns the number of items stored in the heap.
83 int size() const { return data.size(); }
85 /// \brief Checks if the heap stores no items.
87 /// Returns \c true if and only if the heap stores no items.
88 bool empty() const { return data.empty(); }
90 /// \brief Make empty this heap.
92 /// Make empty this heap. It does not change the cross reference
93 /// map. If you want to reuse a heap what is not surely empty you
94 /// should first clear the heap and after that you should set the
95 /// cross reference map for each item to \c PRE_HEAP.
97 data.clear(); first.clear(); minimal = 0;
102 void relocate_last(int idx) {
103 if (idx + 1 < int(data.size())) {
104 data[idx] = data.back();
105 if (data[idx].prev != -1) {
106 data[data[idx].prev].next = idx;
108 first[data[idx].value] = idx;
110 if (data[idx].next != -1) {
111 data[data[idx].next].prev = idx;
113 index[data[idx].item] = idx;
118 void unlace(int idx) {
119 if (data[idx].prev != -1) {
120 data[data[idx].prev].next = data[idx].next;
122 first[data[idx].value] = data[idx].next;
124 if (data[idx].next != -1) {
125 data[data[idx].next].prev = data[idx].prev;
130 if (int(first.size()) <= data[idx].value) {
131 first.resize(data[idx].value + 1, -1);
133 data[idx].next = first[data[idx].value];
134 if (data[idx].next != -1) {
135 data[data[idx].next].prev = idx;
137 first[data[idx].value] = idx;
142 /// \brief Insert a pair of item and priority into the heap.
144 /// Adds \c p.first to the heap with priority \c p.second.
145 /// \param p The pair to insert.
146 void push(const Pair& p) {
147 push(p.first, p.second);
150 /// \brief Insert an item into the heap with the given priority.
152 /// Adds \c i to the heap with priority \c p.
153 /// \param i The item to insert.
154 /// \param p The priority of the item.
155 void push(const Item &i, const Prio &p) {
156 int idx = data.size();
158 data.push_back(BucketItem(i, p));
165 /// \brief Returns the item with minimum priority.
167 /// This method returns the item with minimum priority.
168 /// \pre The heap must be nonempty.
170 while (first[minimal] == -1) {
173 return data[first[minimal]].item;
176 /// \brief Returns the minimum priority.
178 /// It returns the minimum priority.
179 /// \pre The heap must be nonempty.
181 while (first[minimal] == -1) {
187 /// \brief Deletes the item with minimum priority.
189 /// This method deletes the item with minimum priority from the heap.
190 /// \pre The heap must be non-empty.
192 while (first[minimal] == -1) {
195 int idx = first[minimal];
196 index[data[idx].item] = -2;
201 /// \brief Deletes \c i from the heap.
203 /// This method deletes item \c i from the heap, if \c i was
204 /// already stored in the heap.
205 /// \param i The item to erase.
206 void erase(const Item &i) {
208 index[data[idx].item] = -2;
214 /// \brief Returns the priority of \c i.
216 /// This function returns the priority of item \c i.
217 /// \pre \c i must be in the heap.
218 /// \param i The item.
219 Prio operator[](const Item &i) const {
221 return data[idx].value;
224 /// \brief \c i gets to the heap with priority \c p independently
225 /// if \c i was already there.
227 /// This method calls \ref push(\c i, \c p) if \c i is not stored
228 /// in the heap and sets the priority of \c i to \c p otherwise.
229 /// \param i The item.
230 /// \param p The priority.
231 void set(const Item &i, const Prio &p) {
235 } else if (p > data[idx].value) {
242 /// \brief Decreases the priority of \c i to \c p.
244 /// This method decreases the priority of item \c i to \c p.
245 /// \pre \c i must be stored in the heap with priority at least \c
246 /// p relative to \c Compare.
247 /// \param i The item.
248 /// \param p The priority.
249 void decrease(const Item &i, const Prio &p) {
259 /// \brief Increases the priority of \c i to \c p.
261 /// This method sets the priority of item \c i to \c p.
262 /// \pre \c i must be stored in the heap with priority at most \c
263 /// p relative to \c Compare.
264 /// \param i The item.
265 /// \param p The priority.
266 void increase(const Item &i, const Prio &p) {
273 /// \brief Returns if \c item is in, has already been in, or has
274 /// never been in the heap.
276 /// This method returns PRE_HEAP if \c item has never been in the
277 /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
278 /// otherwise. In the latter case it is possible that \c item will
279 /// get back to the heap again.
280 /// \param i The item.
281 state_enum state(const Item &i) const {
283 if (idx >= 0) idx = 0;
284 return state_enum(idx);
287 /// \brief Sets the state of the \c item in the heap.
289 /// Sets the state of the \c item in the heap. It can be used to
290 /// manually clear the heap when it is important to achive the
291 /// better time complexity.
292 /// \param i The item.
293 /// \param st The state. It should not be \c IN_HEAP.
294 void state(const Item& i, state_enum st) {
298 if (state(i) == IN_HEAP) {
311 BucketItem(const Item& _item, int _value)
312 : item(_item), value(_value) {}
321 std::vector<int> first;
322 std::vector<BucketItem> data;
325 }; // class BucketHeap
328 template <typename _ItemIntMap>
329 class BucketHeap<_ItemIntMap, false> {
332 typedef typename _ItemIntMap::Key Item;
334 typedef std::pair<Item, Prio> Pair;
335 typedef _ItemIntMap ItemIntMap;
345 explicit BucketHeap(ItemIntMap &_index) : index(_index), maximal(-1) {}
347 int size() const { return data.size(); }
348 bool empty() const { return data.empty(); }
351 data.clear(); first.clear(); maximal = -1;
356 void relocate_last(int idx) {
357 if (idx + 1 != int(data.size())) {
358 data[idx] = data.back();
359 if (data[idx].prev != -1) {
360 data[data[idx].prev].next = idx;
362 first[data[idx].value] = idx;
364 if (data[idx].next != -1) {
365 data[data[idx].next].prev = idx;
367 index[data[idx].item] = idx;
372 void unlace(int idx) {
373 if (data[idx].prev != -1) {
374 data[data[idx].prev].next = data[idx].next;
376 first[data[idx].value] = data[idx].next;
378 if (data[idx].next != -1) {
379 data[data[idx].next].prev = data[idx].prev;
384 if (int(first.size()) <= data[idx].value) {
385 first.resize(data[idx].value + 1, -1);
387 data[idx].next = first[data[idx].value];
388 if (data[idx].next != -1) {
389 data[data[idx].next].prev = idx;
391 first[data[idx].value] = idx;
397 void push(const Pair& p) {
398 push(p.first, p.second);
401 void push(const Item &i, const Prio &p) {
402 int idx = data.size();
404 data.push_back(BucketItem(i, p));
406 if (data[idx].value > maximal) {
407 maximal = data[idx].value;
412 while (first[maximal] == -1) {
415 return data[first[maximal]].item;
419 while (first[maximal] == -1) {
426 while (first[maximal] == -1) {
429 int idx = first[maximal];
430 index[data[idx].item] = -2;
435 void erase(const Item &i) {
437 index[data[idx].item] = -2;
442 Prio operator[](const Item &i) const {
444 return data[idx].value;
447 void set(const Item &i, const Prio &p) {
451 } else if (p > data[idx].value) {
458 void decrease(const Item &i, const Prio &p) {
468 void increase(const Item &i, const Prio &p) {
475 state_enum state(const Item &i) const {
477 if (idx >= 0) idx = 0;
478 return state_enum(idx);
481 void state(const Item& i, state_enum st) {
485 if (state(i) == IN_HEAP) {
498 BucketItem(const Item& _item, int _value)
499 : item(_item), value(_value) {}
508 std::vector<int> first;
509 std::vector<BucketItem> data;
512 }; // class BucketHeap
516 /// \brief A Simplified Bucket Heap implementation.
518 /// This class implements a simplified \e bucket \e heap data
519 /// structure. It does not provide some functionality but it faster
520 /// and simplier data structure than the BucketHeap. The main
521 /// difference is that the BucketHeap stores for every key a double
522 /// linked list while this class stores just simple lists. In the
523 /// other way it does not supports erasing each elements just the
524 /// minimal and it does not supports key increasing, decreasing.
526 /// \param _ItemIntMap A read and writable Item int map, used internally
527 /// to handle the cross references.
528 /// \param minimize If the given parameter is true then the heap gives back
529 /// the lowest priority.
532 template <typename _ItemIntMap, bool minimize = true >
533 class SimpleBucketHeap {
536 typedef typename _ItemIntMap::Key Item;
538 typedef std::pair<Item, Prio> Pair;
539 typedef _ItemIntMap ItemIntMap;
541 /// \brief Type to represent the items states.
543 /// Each Item element have a state associated to it. It may be "in heap",
544 /// "pre heap" or "post heap". The latter two are indifferent from the
545 /// heap's point of view, but may be useful to the user.
547 /// The ItemIntMap \e should be initialized in such way that it maps
548 /// PRE_HEAP (-1) to any element to be put in the heap...
557 /// \brief The constructor.
560 /// \param _index should be given to the constructor, since it is used
561 /// internally to handle the cross references. The value of the map
562 /// should be PRE_HEAP (-1) for each element.
563 explicit SimpleBucketHeap(ItemIntMap &_index)
564 : index(_index), free(-1), num(0), minimal(0) {}
566 /// \brief Returns the number of items stored in the heap.
568 /// The number of items stored in the heap.
569 int size() const { return num; }
571 /// \brief Checks if the heap stores no items.
573 /// Returns \c true if and only if the heap stores no items.
574 bool empty() const { return num == 0; }
576 /// \brief Make empty this heap.
578 /// Make empty this heap. It does not change the cross reference
579 /// map. If you want to reuse a heap what is not surely empty you
580 /// should first clear the heap and after that you should set the
581 /// cross reference map for each item to \c PRE_HEAP.
583 data.clear(); first.clear(); free = -1; num = 0; minimal = 0;
586 /// \brief Insert a pair of item and priority into the heap.
588 /// Adds \c p.first to the heap with priority \c p.second.
589 /// \param p The pair to insert.
590 void push(const Pair& p) {
591 push(p.first, p.second);
594 /// \brief Insert an item into the heap with the given priority.
596 /// Adds \c i to the heap with priority \c p.
597 /// \param i The item to insert.
598 /// \param p The priority of the item.
599 void push(const Item &i, const Prio &p) {
603 data.push_back(BucketItem(i));
606 free = data[idx].next;
610 if (p >= int(first.size())) first.resize(p + 1, -1);
611 data[idx].next = first[p];
619 /// \brief Returns the item with minimum priority.
621 /// This method returns the item with minimum priority.
622 /// \pre The heap must be nonempty.
624 while (first[minimal] == -1) {
627 return data[first[minimal]].item;
630 /// \brief Returns the minimum priority.
632 /// It returns the minimum priority.
633 /// \pre The heap must be nonempty.
635 while (first[minimal] == -1) {
641 /// \brief Deletes the item with minimum priority.
643 /// This method deletes the item with minimum priority from the heap.
644 /// \pre The heap must be non-empty.
646 while (first[minimal] == -1) {
649 int idx = first[minimal];
650 index[data[idx].item] = -2;
651 first[minimal] = data[idx].next;
652 data[idx].next = free;
657 /// \brief Returns the priority of \c i.
659 /// This function returns the priority of item \c i.
660 /// \warning This operator is not a constant time function
661 /// because it scans the whole data structure to find the proper
663 /// \pre \c i must be in the heap.
664 /// \param i The item.
665 Prio operator[](const Item &i) const {
666 for (int k = 0; k < first.size(); ++k) {
669 if (data[idx].item == i) {
672 idx = data[idx].next;
678 /// \brief Returns if \c item is in, has already been in, or has
679 /// never been in the heap.
681 /// This method returns PRE_HEAP if \c item has never been in the
682 /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
683 /// otherwise. In the latter case it is possible that \c item will
684 /// get back to the heap again.
685 /// \param i The item.
686 state_enum state(const Item &i) const {
688 if (idx >= 0) idx = 0;
689 return state_enum(idx);
695 BucketItem(const Item& _item)
703 std::vector<int> first;
704 std::vector<BucketItem> data;
708 }; // class SimpleBucketHeap
710 template <typename _ItemIntMap>
711 class SimpleBucketHeap<_ItemIntMap, false> {
714 typedef typename _ItemIntMap::Key Item;
716 typedef std::pair<Item, Prio> Pair;
717 typedef _ItemIntMap ItemIntMap;
727 explicit SimpleBucketHeap(ItemIntMap &_index)
728 : index(_index), free(-1), num(0), maximal(0) {}
730 int size() const { return num; }
732 bool empty() const { return num == 0; }
735 data.clear(); first.clear(); free = -1; num = 0; maximal = 0;
738 void push(const Pair& p) {
739 push(p.first, p.second);
742 void push(const Item &i, const Prio &p) {
746 data.push_back(BucketItem(i));
749 free = data[idx].next;
753 if (p >= int(first.size())) first.resize(p + 1, -1);
754 data[idx].next = first[p];
763 while (first[maximal] == -1) {
766 return data[first[maximal]].item;
770 while (first[maximal] == -1) {
777 while (first[maximal] == -1) {
780 int idx = first[maximal];
781 index[data[idx].item] = -2;
782 first[maximal] = data[idx].next;
783 data[idx].next = free;
788 Prio operator[](const Item &i) const {
789 for (int k = 0; k < first.size(); ++k) {
792 if (data[idx].item == i) {
795 idx = data[idx].next;
801 state_enum state(const Item &i) const {
803 if (idx >= 0) idx = 0;
804 return state_enum(idx);
810 BucketItem(const Item& _item) : item(_item) {}
818 std::vector<int> first;
819 std::vector<BucketItem> data;