Small fixes in README.
3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2008
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 >
53 typedef typename _ItemIntMap::Key Item;
57 typedef std::pair<Item, Prio> Pair;
59 typedef _ItemIntMap ItemIntMap;
61 /// \brief Type to represent the items states.
63 /// Each Item element have a state associated to it. It may be "in heap",
64 /// "pre heap" or "post heap". The latter two are indifferent from the
65 /// heap's point of view, but may be useful to the user.
67 /// The ItemIntMap \e should be initialized in such way that it maps
68 /// PRE_HEAP (-1) to any element to be put in the heap...
76 /// \brief The constructor.
79 /// \param _index should be given to the constructor, since it is used
80 /// internally to handle the cross references. The value of the map
81 /// should be PRE_HEAP (-1) for each element.
82 explicit BucketHeap(ItemIntMap &_index) : index(_index), minimal(0) {}
84 /// The number of items stored in the heap.
86 /// \brief Returns the number of items stored in the heap.
87 int size() const { return data.size(); }
89 /// \brief Checks if the heap stores no items.
91 /// Returns \c true if and only if the heap stores no items.
92 bool empty() const { return data.empty(); }
94 /// \brief Make empty this heap.
96 /// Make empty this heap. It does not change the cross reference
97 /// map. If you want to reuse a heap what is not surely empty you
98 /// should first clear the heap and after that you should set the
99 /// cross reference map for each item to \c PRE_HEAP.
101 data.clear(); first.clear(); minimal = 0;
106 void relocate_last(int idx) {
107 if (idx + 1 < int(data.size())) {
108 data[idx] = data.back();
109 if (data[idx].prev != -1) {
110 data[data[idx].prev].next = idx;
112 first[data[idx].value] = idx;
114 if (data[idx].next != -1) {
115 data[data[idx].next].prev = idx;
117 index[data[idx].item] = idx;
122 void unlace(int idx) {
123 if (data[idx].prev != -1) {
124 data[data[idx].prev].next = data[idx].next;
126 first[data[idx].value] = data[idx].next;
128 if (data[idx].next != -1) {
129 data[data[idx].next].prev = data[idx].prev;
134 if (int(first.size()) <= data[idx].value) {
135 first.resize(data[idx].value + 1, -1);
137 data[idx].next = first[data[idx].value];
138 if (data[idx].next != -1) {
139 data[data[idx].next].prev = idx;
141 first[data[idx].value] = idx;
146 /// \brief Insert a pair of item and priority into the heap.
148 /// Adds \c p.first to the heap with priority \c p.second.
149 /// \param p The pair to insert.
150 void push(const Pair& p) {
151 push(p.first, p.second);
154 /// \brief Insert an item into the heap with the given priority.
156 /// Adds \c i to the heap with priority \c p.
157 /// \param i The item to insert.
158 /// \param p The priority of the item.
159 void push(const Item &i, const Prio &p) {
160 int idx = data.size();
162 data.push_back(BucketItem(i, p));
169 /// \brief Returns the item with minimum priority.
171 /// This method returns the item with minimum priority.
172 /// \pre The heap must be nonempty.
174 while (first[minimal] == -1) {
177 return data[first[minimal]].item;
180 /// \brief Returns the minimum priority.
182 /// It returns the minimum priority.
183 /// \pre The heap must be nonempty.
185 while (first[minimal] == -1) {
191 /// \brief Deletes the item with minimum priority.
193 /// This method deletes the item with minimum priority from the heap.
194 /// \pre The heap must be non-empty.
196 while (first[minimal] == -1) {
199 int idx = first[minimal];
200 index[data[idx].item] = -2;
205 /// \brief Deletes \c i from the heap.
207 /// This method deletes item \c i from the heap, if \c i was
208 /// already stored in the heap.
209 /// \param i The item to erase.
210 void erase(const Item &i) {
212 index[data[idx].item] = -2;
218 /// \brief Returns the priority of \c i.
220 /// This function returns the priority of item \c i.
221 /// \pre \c i must be in the heap.
222 /// \param i The item.
223 Prio operator[](const Item &i) const {
225 return data[idx].value;
228 /// \brief \c i gets to the heap with priority \c p independently
229 /// if \c i was already there.
231 /// This method calls \ref push(\c i, \c p) if \c i is not stored
232 /// in the heap and sets the priority of \c i to \c p otherwise.
233 /// \param i The item.
234 /// \param p The priority.
235 void set(const Item &i, const Prio &p) {
239 } else if (p > data[idx].value) {
246 /// \brief Decreases the priority of \c i to \c p.
248 /// This method decreases the priority of item \c i to \c p.
249 /// \pre \c i must be stored in the heap with priority at least \c
250 /// p relative to \c Compare.
251 /// \param i The item.
252 /// \param p The priority.
253 void decrease(const Item &i, const Prio &p) {
263 /// \brief Increases the priority of \c i to \c p.
265 /// This method sets the priority of item \c i to \c p.
266 /// \pre \c i must be stored in the heap with priority at most \c
267 /// p relative to \c Compare.
268 /// \param i The item.
269 /// \param p The priority.
270 void increase(const Item &i, const Prio &p) {
277 /// \brief Returns if \c item is in, has already been in, or has
278 /// never been in the heap.
280 /// This method returns PRE_HEAP if \c item has never been in the
281 /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
282 /// otherwise. In the latter case it is possible that \c item will
283 /// get back to the heap again.
284 /// \param i The item.
285 State state(const Item &i) const {
287 if (idx >= 0) idx = 0;
291 /// \brief Sets the state of the \c item in the heap.
293 /// Sets the state of the \c item in the heap. It can be used to
294 /// manually clear the heap when it is important to achive the
295 /// better time complexity.
296 /// \param i The item.
297 /// \param st The state. It should not be \c IN_HEAP.
298 void state(const Item& i, State st) {
302 if (state(i) == IN_HEAP) {
315 BucketItem(const Item& _item, int _value)
316 : item(_item), value(_value) {}
325 std::vector<int> first;
326 std::vector<BucketItem> data;
329 }; // class BucketHeap
332 template <typename _ItemIntMap>
333 class BucketHeap<_ItemIntMap, false> {
336 typedef typename _ItemIntMap::Key Item;
338 typedef std::pair<Item, Prio> Pair;
339 typedef _ItemIntMap ItemIntMap;
349 explicit BucketHeap(ItemIntMap &_index) : index(_index), maximal(-1) {}
351 int size() const { return data.size(); }
352 bool empty() const { return data.empty(); }
355 data.clear(); first.clear(); maximal = -1;
360 void relocate_last(int idx) {
361 if (idx + 1 != int(data.size())) {
362 data[idx] = data.back();
363 if (data[idx].prev != -1) {
364 data[data[idx].prev].next = idx;
366 first[data[idx].value] = idx;
368 if (data[idx].next != -1) {
369 data[data[idx].next].prev = idx;
371 index[data[idx].item] = idx;
376 void unlace(int idx) {
377 if (data[idx].prev != -1) {
378 data[data[idx].prev].next = data[idx].next;
380 first[data[idx].value] = data[idx].next;
382 if (data[idx].next != -1) {
383 data[data[idx].next].prev = data[idx].prev;
388 if (int(first.size()) <= data[idx].value) {
389 first.resize(data[idx].value + 1, -1);
391 data[idx].next = first[data[idx].value];
392 if (data[idx].next != -1) {
393 data[data[idx].next].prev = idx;
395 first[data[idx].value] = idx;
401 void push(const Pair& p) {
402 push(p.first, p.second);
405 void push(const Item &i, const Prio &p) {
406 int idx = data.size();
408 data.push_back(BucketItem(i, p));
410 if (data[idx].value > maximal) {
411 maximal = data[idx].value;
416 while (first[maximal] == -1) {
419 return data[first[maximal]].item;
423 while (first[maximal] == -1) {
430 while (first[maximal] == -1) {
433 int idx = first[maximal];
434 index[data[idx].item] = -2;
439 void erase(const Item &i) {
441 index[data[idx].item] = -2;
446 Prio operator[](const Item &i) const {
448 return data[idx].value;
451 void set(const Item &i, const Prio &p) {
455 } else if (p > data[idx].value) {
462 void decrease(const Item &i, const Prio &p) {
472 void increase(const Item &i, const Prio &p) {
479 State state(const Item &i) const {
481 if (idx >= 0) idx = 0;
485 void state(const Item& i, State st) {
489 if (state(i) == IN_HEAP) {
502 BucketItem(const Item& _item, int _value)
503 : item(_item), value(_value) {}
512 std::vector<int> first;
513 std::vector<BucketItem> data;
516 }; // class BucketHeap
520 /// \brief A Simplified Bucket Heap implementation.
522 /// This class implements a simplified \e bucket \e heap data
523 /// structure. It does not provide some functionality but it faster
524 /// and simplier data structure than the BucketHeap. The main
525 /// difference is that the BucketHeap stores for every key a double
526 /// linked list while this class stores just simple lists. In the
527 /// other way it does not supports erasing each elements just the
528 /// minimal and it does not supports key increasing, decreasing.
530 /// \param _ItemIntMap A read and writable Item int map, used internally
531 /// to handle the cross references.
532 /// \param minimize If the given parameter is true then the heap gives back
533 /// the lowest priority.
536 template <typename _ItemIntMap, bool minimize = true >
537 class SimpleBucketHeap {
540 typedef typename _ItemIntMap::Key Item;
542 typedef std::pair<Item, Prio> Pair;
543 typedef _ItemIntMap ItemIntMap;
545 /// \brief Type to represent the items states.
547 /// Each Item element have a state associated to it. It may be "in heap",
548 /// "pre heap" or "post heap". The latter two are indifferent from the
549 /// heap's point of view, but may be useful to the user.
551 /// The ItemIntMap \e should be initialized in such way that it maps
552 /// PRE_HEAP (-1) to any element to be put in the heap...
561 /// \brief The constructor.
564 /// \param _index should be given to the constructor, since it is used
565 /// internally to handle the cross references. The value of the map
566 /// should be PRE_HEAP (-1) for each element.
567 explicit SimpleBucketHeap(ItemIntMap &_index)
568 : index(_index), free(-1), num(0), minimal(0) {}
570 /// \brief Returns the number of items stored in the heap.
572 /// The number of items stored in the heap.
573 int size() const { return num; }
575 /// \brief Checks if the heap stores no items.
577 /// Returns \c true if and only if the heap stores no items.
578 bool empty() const { return num == 0; }
580 /// \brief Make empty this heap.
582 /// Make empty this heap. It does not change the cross reference
583 /// map. If you want to reuse a heap what is not surely empty you
584 /// should first clear the heap and after that you should set the
585 /// cross reference map for each item to \c PRE_HEAP.
587 data.clear(); first.clear(); free = -1; num = 0; minimal = 0;
590 /// \brief Insert a pair of item and priority into the heap.
592 /// Adds \c p.first to the heap with priority \c p.second.
593 /// \param p The pair to insert.
594 void push(const Pair& p) {
595 push(p.first, p.second);
598 /// \brief Insert an item into the heap with the given priority.
600 /// Adds \c i to the heap with priority \c p.
601 /// \param i The item to insert.
602 /// \param p The priority of the item.
603 void push(const Item &i, const Prio &p) {
607 data.push_back(BucketItem(i));
610 free = data[idx].next;
614 if (p >= int(first.size())) first.resize(p + 1, -1);
615 data[idx].next = first[p];
623 /// \brief Returns the item with minimum priority.
625 /// This method returns the item with minimum priority.
626 /// \pre The heap must be nonempty.
628 while (first[minimal] == -1) {
631 return data[first[minimal]].item;
634 /// \brief Returns the minimum priority.
636 /// It returns the minimum priority.
637 /// \pre The heap must be nonempty.
639 while (first[minimal] == -1) {
645 /// \brief Deletes the item with minimum priority.
647 /// This method deletes the item with minimum priority from the heap.
648 /// \pre The heap must be non-empty.
650 while (first[minimal] == -1) {
653 int idx = first[minimal];
654 index[data[idx].item] = -2;
655 first[minimal] = data[idx].next;
656 data[idx].next = free;
661 /// \brief Returns the priority of \c i.
663 /// This function returns the priority of item \c i.
664 /// \warning This operator is not a constant time function
665 /// because it scans the whole data structure to find the proper
667 /// \pre \c i must be in the heap.
668 /// \param i The item.
669 Prio operator[](const Item &i) const {
670 for (int k = 0; k < first.size(); ++k) {
673 if (data[idx].item == i) {
676 idx = data[idx].next;
682 /// \brief Returns if \c item is in, has already been in, or has
683 /// never been in the heap.
685 /// This method returns PRE_HEAP if \c item has never been in the
686 /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
687 /// otherwise. In the latter case it is possible that \c item will
688 /// get back to the heap again.
689 /// \param i The item.
690 State state(const Item &i) const {
692 if (idx >= 0) idx = 0;
699 BucketItem(const Item& _item)
707 std::vector<int> first;
708 std::vector<BucketItem> data;
712 }; // class SimpleBucketHeap
714 template <typename _ItemIntMap>
715 class SimpleBucketHeap<_ItemIntMap, false> {
718 typedef typename _ItemIntMap::Key Item;
720 typedef std::pair<Item, Prio> Pair;
721 typedef _ItemIntMap ItemIntMap;
731 explicit SimpleBucketHeap(ItemIntMap &_index)
732 : index(_index), free(-1), num(0), maximal(0) {}
734 int size() const { return num; }
736 bool empty() const { return num == 0; }
739 data.clear(); first.clear(); free = -1; num = 0; maximal = 0;
742 void push(const Pair& p) {
743 push(p.first, p.second);
746 void push(const Item &i, const Prio &p) {
750 data.push_back(BucketItem(i));
753 free = data[idx].next;
757 if (p >= int(first.size())) first.resize(p + 1, -1);
758 data[idx].next = first[p];
767 while (first[maximal] == -1) {
770 return data[first[maximal]].item;
774 while (first[maximal] == -1) {
781 while (first[maximal] == -1) {
784 int idx = first[maximal];
785 index[data[idx].item] = -2;
786 first[maximal] = data[idx].next;
787 data[idx].next = free;
792 Prio operator[](const Item &i) const {
793 for (int k = 0; k < first.size(); ++k) {
796 if (data[idx].item == i) {
799 idx = data[idx].next;
805 State state(const Item &i) const {
807 if (idx >= 0) idx = 0;
814 BucketItem(const Item& _item) : item(_item) {}
822 std::vector<int> first;
823 std::vector<BucketItem> data;