Set props.
3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2006
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 _Item Type of the items to be stored.
45 /// \param _ItemIntMap A read and writable Item int map, used internally
46 /// to handle the cross references.
47 /// \param minimize If the given parameter is true then the heap gives back
48 /// the lowest priority.
49 template <typename _Item, typename _ItemIntMap, bool minimize = true >
55 typedef std::pair<Item, Prio> Pair;
56 typedef _ItemIntMap ItemIntMap;
58 /// \brief Type to represent the items states.
60 /// Each Item element have a state associated to it. It may be "in heap",
61 /// "pre heap" or "post heap". The latter two are indifferent from the
62 /// heap's point of view, but may be useful to the user.
64 /// The ItemIntMap \e should be initialized in such way that it maps
65 /// PRE_HEAP (-1) to any element to be put in the heap...
73 /// \brief The constructor.
76 /// \param _index should be given to the constructor, since it is used
77 /// internally to handle the cross references. The value of the map
78 /// should be PRE_HEAP (-1) for each element.
79 explicit BucketHeap(ItemIntMap &_index) : index(_index), minimal(0) {}
81 /// The number of items stored in the heap.
83 /// \brief Returns the number of items stored in the heap.
84 int size() const { return data.size(); }
86 /// \brief Checks if the heap stores no items.
88 /// Returns \c true if and only if the heap stores no items.
89 bool empty() const { return data.empty(); }
91 /// \brief Make empty this heap.
93 /// Make empty this heap. It does not change the cross reference
94 /// map. If you want to reuse a heap what is not surely empty you
95 /// should first clear the heap and after that you should set the
96 /// cross reference map for each item to \c PRE_HEAP.
98 data.clear(); first.clear(); minimal = 0;
103 void relocate_last(int idx) {
104 if (idx + 1 < (int)data.size()) {
105 data[idx] = data.back();
106 if (data[idx].prev != -1) {
107 data[data[idx].prev].next = idx;
109 first[data[idx].value] = idx;
111 if (data[idx].next != -1) {
112 data[data[idx].next].prev = idx;
114 index[data[idx].item] = idx;
119 void unlace(int idx) {
120 if (data[idx].prev != -1) {
121 data[data[idx].prev].next = data[idx].next;
123 first[data[idx].value] = data[idx].next;
125 if (data[idx].next != -1) {
126 data[data[idx].next].prev = data[idx].prev;
131 if ((int)first.size() <= data[idx].value) {
132 first.resize(data[idx].value + 1, -1);
134 data[idx].next = first[data[idx].value];
135 if (data[idx].next != -1) {
136 data[data[idx].next].prev = idx;
138 first[data[idx].value] = idx;
143 /// \brief Insert a pair of item and priority into the heap.
145 /// Adds \c p.first to the heap with priority \c p.second.
146 /// \param p The pair to insert.
147 void push(const Pair& p) {
148 push(p.first, p.second);
151 /// \brief Insert an item into the heap with the given priority.
153 /// Adds \c i to the heap with priority \c p.
154 /// \param i The item to insert.
155 /// \param p The priority of the item.
156 void push(const Item &i, const Prio &p) {
157 int idx = data.size();
159 data.push_back(BucketItem(i, p));
166 /// \brief Returns the item with minimum priority.
168 /// This method returns the item with minimum priority.
169 /// \pre The heap must be nonempty.
171 while (first[minimal] == -1) {
174 return data[first[minimal]].item;
177 /// \brief Returns the minimum priority.
179 /// It returns the minimum priority.
180 /// \pre The heap must be nonempty.
182 while (first[minimal] == -1) {
188 /// \brief Deletes the item with minimum priority.
190 /// This method deletes the item with minimum priority from the heap.
191 /// \pre The heap must be non-empty.
193 while (first[minimal] == -1) {
196 int idx = first[minimal];
197 index[data[idx].item] = -2;
202 /// \brief Deletes \c i from the heap.
204 /// This method deletes item \c i from the heap, if \c i was
205 /// already stored in the heap.
206 /// \param i The item to erase.
207 void erase(const Item &i) {
209 index[data[idx].item] = -2;
215 /// \brief Returns the priority of \c i.
217 /// This function returns the priority of item \c i.
218 /// \pre \c i must be in the heap.
219 /// \param i The item.
220 Prio operator[](const Item &i) const {
222 return data[idx].value;
225 /// \brief \c i gets to the heap with priority \c p independently
226 /// if \c i was already there.
228 /// This method calls \ref push(\c i, \c p) if \c i is not stored
229 /// in the heap and sets the priority of \c i to \c p otherwise.
230 /// \param i The item.
231 /// \param p The priority.
232 void set(const Item &i, const Prio &p) {
236 } else if (p > data[idx].value) {
243 /// \brief Decreases the priority of \c i to \c p.
245 /// This method decreases the priority of item \c i to \c p.
246 /// \pre \c i must be stored in the heap with priority at least \c
247 /// p relative to \c Compare.
248 /// \param i The item.
249 /// \param p The priority.
250 void decrease(const Item &i, const Prio &p) {
260 /// \brief Increases the priority of \c i to \c p.
262 /// This method sets the priority of item \c i to \c p.
263 /// \pre \c i must be stored in the heap with priority at most \c
264 /// p relative to \c Compare.
265 /// \param i The item.
266 /// \param p The priority.
267 void increase(const Item &i, const Prio &p) {
274 /// \brief Returns if \c item is in, has already been in, or has
275 /// never been in the heap.
277 /// This method returns PRE_HEAP if \c item has never been in the
278 /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
279 /// otherwise. In the latter case it is possible that \c item will
280 /// get back to the heap again.
281 /// \param i The item.
282 state_enum state(const Item &i) const {
284 if (idx >= 0) idx = 0;
285 return state_enum(idx);
288 /// \brief Sets the state of the \c item in the heap.
290 /// Sets the state of the \c item in the heap. It can be used to
291 /// manually clear the heap when it is important to achive the
292 /// better time complexity.
293 /// \param i The item.
294 /// \param st The state. It should not be \c IN_HEAP.
295 void state(const Item& i, state_enum st) {
299 if (state(i) == IN_HEAP) {
312 BucketItem(const Item& _item, int _value)
313 : item(_item), value(_value) {}
322 std::vector<int> first;
323 std::vector<BucketItem> data;
326 }; // class BucketHeap
329 template <typename _Item, typename _ItemIntMap>
330 class BucketHeap<_Item, _ItemIntMap, false> {
335 typedef std::pair<Item, Prio> Pair;
336 typedef _ItemIntMap ItemIntMap;
346 explicit BucketHeap(ItemIntMap &_index) : index(_index), maximal(-1) {}
348 int size() const { return data.size(); }
349 bool empty() const { return data.empty(); }
352 data.clear(); first.clear(); maximal = -1;
357 void relocate_last(int idx) {
358 if (idx + 1 != (int)data.size()) {
359 data[idx] = data.back();
360 if (data[idx].prev != -1) {
361 data[data[idx].prev].next = idx;
363 first[data[idx].value] = idx;
365 if (data[idx].next != -1) {
366 data[data[idx].next].prev = idx;
368 index[data[idx].item] = idx;
373 void unlace(int idx) {
374 if (data[idx].prev != -1) {
375 data[data[idx].prev].next = data[idx].next;
377 first[data[idx].value] = data[idx].next;
379 if (data[idx].next != -1) {
380 data[data[idx].next].prev = data[idx].prev;
385 if ((int)first.size() <= data[idx].value) {
386 first.resize(data[idx].value + 1, -1);
388 data[idx].next = first[data[idx].value];
389 if (data[idx].next != -1) {
390 data[data[idx].next].prev = idx;
392 first[data[idx].value] = idx;
398 void push(const Pair& p) {
399 push(p.first, p.second);
402 void push(const Item &i, const Prio &p) {
403 int idx = data.size();
405 data.push_back(BucketItem(i, p));
407 if (data[idx].value > maximal) {
408 maximal = data[idx].value;
413 while (first[maximal] == -1) {
416 return data[first[maximal]].item;
420 while (first[maximal] == -1) {
427 while (first[maximal] == -1) {
430 int idx = first[maximal];
431 index[data[idx].item] = -2;
436 void erase(const Item &i) {
438 index[data[idx].item] = -2;
443 Prio operator[](const Item &i) const {
445 return data[idx].value;
448 void set(const Item &i, const Prio &p) {
452 } else if (p > data[idx].value) {
459 void decrease(const Item &i, const Prio &p) {
469 void increase(const Item &i, const Prio &p) {
476 state_enum state(const Item &i) const {
478 if (idx >= 0) idx = 0;
479 return state_enum(idx);
482 void state(const Item& i, state_enum st) {
486 if (state(i) == IN_HEAP) {
499 BucketItem(const Item& _item, int _value)
500 : item(_item), value(_value) {}
509 std::vector<int> first;
510 std::vector<BucketItem> data;
513 }; // class BucketHeap
517 /// \brief A Simplified Bucket Heap implementation.
519 /// This class implements a simplified \e bucket \e heap data
520 /// structure. It does not provide some functionality but it faster
521 /// and simplier data structure than the BucketHeap. The main
522 /// difference is that the BucketHeap stores for every key a double
523 /// linked list while this class stores just simple lists. In the
524 /// other way it does not supports erasing each elements just the
525 /// minimal and it does not supports key increasing, decreasing.
527 /// \param _Item Type of the items to be stored.
528 /// \param _ItemIntMap A read and writable Item int map, used internally
529 /// to handle the cross references.
530 /// \param minimize If the given parameter is true then the heap gives back
531 /// the lowest priority.
534 template <typename _Item, typename _ItemIntMap, bool minimize = true >
535 class SimpleBucketHeap {
540 typedef std::pair<Item, Prio> Pair;
541 typedef _ItemIntMap ItemIntMap;
543 /// \brief Type to represent the items states.
545 /// Each Item element have a state associated to it. It may be "in heap",
546 /// "pre heap" or "post heap". The latter two are indifferent from the
547 /// heap's point of view, but may be useful to the user.
549 /// The ItemIntMap \e should be initialized in such way that it maps
550 /// PRE_HEAP (-1) to any element to be put in the heap...
559 /// \brief The constructor.
562 /// \param _index should be given to the constructor, since it is used
563 /// internally to handle the cross references. The value of the map
564 /// should be PRE_HEAP (-1) for each element.
565 explicit SimpleBucketHeap(ItemIntMap &_index)
566 : index(_index), free(-1), num(0), minimal(0) {}
568 /// \brief Returns the number of items stored in the heap.
570 /// The number of items stored in the heap.
571 int size() const { return num; }
573 /// \brief Checks if the heap stores no items.
575 /// Returns \c true if and only if the heap stores no items.
576 bool empty() const { return num == 0; }
578 /// \brief Make empty this heap.
580 /// Make empty this heap. It does not change the cross reference
581 /// map. If you want to reuse a heap what is not surely empty you
582 /// should first clear the heap and after that you should set the
583 /// cross reference map for each item to \c PRE_HEAP.
585 data.clear(); first.clear(); free = -1; num = 0; minimal = 0;
588 /// \brief Insert a pair of item and priority into the heap.
590 /// Adds \c p.first to the heap with priority \c p.second.
591 /// \param p The pair to insert.
592 void push(const Pair& p) {
593 push(p.first, p.second);
596 /// \brief Insert an item into the heap with the given priority.
598 /// Adds \c i to the heap with priority \c p.
599 /// \param i The item to insert.
600 /// \param p The priority of the item.
601 void push(const Item &i, const Prio &p) {
605 data.push_back(BucketItem(i, p));
608 free = data[idx].next;
609 data[idx].item = i; data[idx].value = p;
612 if (p >= (int)first.size()) first.resize(p + 1, -1);
613 data[idx].next = first[p];
621 /// \brief Returns the item with minimum priority.
623 /// This method returns the item with minimum priority.
624 /// \pre The heap must be nonempty.
626 while (first[minimal] == -1) {
629 return data[first[minimal]].item;
632 /// \brief Returns the minimum priority.
634 /// It returns the minimum priority.
635 /// \pre The heap must be nonempty.
637 while (first[minimal] == -1) {
643 /// \brief Deletes the item with minimum priority.
645 /// This method deletes the item with minimum priority from the heap.
646 /// \pre The heap must be non-empty.
648 while (first[minimal] == -1) {
651 int idx = first[minimal];
652 index[data[idx].item] = -2;
653 first[minimal] = data[idx].next;
654 data[idx].next = free;
659 /// \brief Returns the priority of \c i.
661 /// This function returns the priority of item \c i.
662 /// \pre \c i must be in the heap.
663 /// \param i The item.
664 Prio operator[](const Item &i) const {
666 return data[idx].value;
669 /// \brief Returns if \c item is in, has already been in, or has
670 /// never been in the heap.
672 /// This method returns PRE_HEAP if \c item has never been in the
673 /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
674 /// otherwise. In the latter case it is possible that \c item will
675 /// get back to the heap again.
676 /// \param i The item.
677 state_enum state(const Item &i) const {
679 if (idx >= 0) idx = 0;
680 return state_enum(idx);
686 BucketItem(const Item& _item, int _value)
687 : item(_item), value(_value) {}
696 std::vector<int> first;
697 std::vector<BucketItem> data;
701 }; // class SimpleBucketHeap
703 template <typename _Item, typename _ItemIntMap>
704 class SimpleBucketHeap<_Item, _ItemIntMap, false> {
709 typedef std::pair<Item, Prio> Pair;
710 typedef _ItemIntMap ItemIntMap;
720 explicit SimpleBucketHeap(ItemIntMap &_index)
721 : index(_index), free(-1), num(0), maximal(0) {}
723 int size() const { return num; }
725 bool empty() const { return num == 0; }
728 data.clear(); first.clear(); free = -1; num = 0; maximal = 0;
731 void push(const Pair& p) {
732 push(p.first, p.second);
735 void push(const Item &i, const Prio &p) {
739 data.push_back(BucketItem(i, p));
742 free = data[idx].next;
743 data[idx].item = i; data[idx].value = p;
746 if (p >= (int)first.size()) first.resize(p + 1, -1);
747 data[idx].next = first[p];
756 while (first[maximal] == -1) {
759 return data[first[maximal]].item;
763 while (first[maximal] == -1) {
770 while (first[maximal] == -1) {
773 int idx = first[maximal];
774 index[data[idx].item] = -2;
775 first[maximal] = data[idx].next;
776 data[idx].next = free;
781 Prio operator[](const Item &i) const {
783 return data[idx].value;
786 state_enum state(const Item &i) const {
788 if (idx >= 0) idx = 0;
789 return state_enum(idx);
795 BucketItem(const Item& _item, int _value)
796 : item(_item), value(_value) {}
805 std::vector<int> first;
806 std::vector<BucketItem> data;