Mapselector widget reached its first release, but there are still work to do on it, I know...
2 * lemon/linear_heap.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Research Group on Combinatorial Optimization, EGRES).
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.
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
17 #ifndef LEMON_LINEAR_HEAP_H
18 #define LEMON_LINEAR_HEAP_H
22 ///\brief Binary Heap implementation.
30 /// \addtogroup auxdat
33 /// \brief A Linear Heap implementation.
35 /// This class implements the \e linear \e heap data structure. A \e heap
36 /// is a data structure for storing items with specified values called \e
37 /// priorities in such a way that finding the item with minimum priority is
38 /// efficient. The linear heap is very simple implementation, it can store
39 /// only integer priorities and it stores for each priority in the [0..C]
40 /// range a list of items. So it should be used only when the priorities
41 /// are small. It is not intended to use as dijkstra heap.
43 /// \param _Item Type of the items to be stored.
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 _Item, typename _ItemIntMap, bool minimize = true >
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 LinearHeap(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.
94 for (int i = 0; i < (int)data.size(); ++i) {
95 index[data[i].item] = -2;
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(LinearItem(i, p));
165 /// \brief Returns the item with minimum priority relative to \c Compare.
167 /// This method returns the item with minimum priority relative to \c
169 /// \pre The heap must be nonempty.
171 while (first[minimal] == -1) {
174 return data[first[minimal]].item;
177 /// \brief Returns the minimum priority relative to \c Compare.
179 /// It returns the minimum priority relative to \c Compare.
180 /// \pre The heap must be nonempty.
182 while (first[minimal] == -1) {
188 /// \brief Deletes the item with minimum priority relative to \c Compare.
190 /// This method deletes the item with minimum priority relative to \c
191 /// Compare from the heap.
192 /// \pre The heap must be non-empty.
194 while (first[minimal] == -1) {
197 int idx = first[minimal];
198 index[data[idx].item] = -2;
203 /// \brief Deletes \c i from the heap.
205 /// This method deletes item \c i from the heap, if \c i was
206 /// already stored in the heap.
207 /// \param i The item to erase.
208 void erase(const Item &i) {
210 index[data[idx].item] = -2;
216 /// \brief Returns the priority of \c i.
218 /// This function returns the priority of item \c i.
219 /// \pre \c i must be in the heap.
220 /// \param i The item.
221 Prio operator[](const Item &i) const {
223 return data[idx].value;
226 /// \brief \c i gets to the heap with priority \c p independently
227 /// if \c i was already there.
229 /// This method calls \ref push(\c i, \c p) if \c i is not stored
230 /// in the heap and sets the priority of \c i to \c p otherwise.
231 /// \param i The item.
232 /// \param p The priority.
233 void set(const Item &i, const Prio &p) {
237 } else if (p > data[idx].value) {
244 /// \brief Decreases the priority of \c i to \c p.
246 /// This method decreases the priority of item \c i to \c p.
247 /// \pre \c i must be stored in the heap with priority at least \c
248 /// p relative to \c Compare.
249 /// \param i The item.
250 /// \param p The priority.
251 void decrease(const Item &i, const Prio &p) {
261 /// \brief Increases the priority of \c i to \c p.
263 /// This method sets the priority of item \c i to \c p.
264 /// \pre \c i must be stored in the heap with priority at most \c
265 /// p relative to \c Compare.
266 /// \param i The item.
267 /// \param p The priority.
268 void increase(const Item &i, const Prio &p) {
275 /// \brief Returns if \c item is in, has already been in, or has
276 /// never been in the heap.
278 /// This method returns PRE_HEAP if \c item has never been in the
279 /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
280 /// otherwise. In the latter case it is possible that \c item will
281 /// get back to the heap again.
282 /// \param i The item.
283 state_enum state(const Item &i) const {
285 if (idx >= 0) idx = 0;
286 return state_enum(idx);
292 LinearItem(const Item& _item, int _value)
293 : item(_item), value(_value) {}
302 std::vector<int> first;
303 std::vector<LinearItem> data;
306 }; // class LinearHeap
309 template <typename _Item, typename _ItemIntMap>
310 class LinearHeap<_Item, _ItemIntMap, false> {
315 typedef std::pair<Item, Prio> Pair;
316 typedef _ItemIntMap ItemIntMap;
326 explicit LinearHeap(ItemIntMap &_index) : index(_index), maximal(-1) {}
328 int size() const { return data.size(); }
329 bool empty() const { return data.empty(); }
332 for (int i = 0; i < (int)data.size(); ++i) {
333 index[data[i].item] = -2;
335 data.clear(); first.clear(); maximal = -1;
340 void relocate_last(int idx) {
341 if (idx + 1 != (int)data.size()) {
342 data[idx] = data.back();
343 if (data[idx].prev != -1) {
344 data[data[idx].prev].next = idx;
346 first[data[idx].value] = idx;
348 if (data[idx].next != -1) {
349 data[data[idx].next].prev = idx;
351 index[data[idx].item] = idx;
356 void unlace(int idx) {
357 if (data[idx].prev != -1) {
358 data[data[idx].prev].next = data[idx].next;
360 first[data[idx].value] = data[idx].next;
362 if (data[idx].next != -1) {
363 data[data[idx].next].prev = data[idx].prev;
368 if ((int)first.size() <= data[idx].value) {
369 first.resize(data[idx].value + 1, -1);
371 data[idx].next = first[data[idx].value];
372 if (data[idx].next != -1) {
373 data[data[idx].next].prev = idx;
375 first[data[idx].value] = idx;
381 void push(const Pair& p) {
382 push(p.first, p.second);
385 void push(const Item &i, const Prio &p) {
386 int idx = data.size();
388 data.push_back(LinearItem(i, p));
390 if (data[idx].value > maximal) {
391 maximal = data[idx].value;
396 while (first[maximal] == -1) {
399 return data[first[maximal]].item;
403 while (first[maximal] == -1) {
410 while (first[maximal] == -1) {
413 int idx = first[maximal];
414 index[data[idx].item] = -2;
419 void erase(const Item &i) {
421 index[data[idx].item] = -2;
426 Prio operator[](const Item &i) const {
428 return data[idx].value;
431 void set(const Item &i, const Prio &p) {
435 } else if (p > data[idx].value) {
442 void decrease(const Item &i, const Prio &p) {
452 void increase(const Item &i, const Prio &p) {
459 state_enum state(const Item &i) const {
461 if (idx >= 0) idx = 0;
462 return state_enum(idx);
468 LinearItem(const Item& _item, int _value)
469 : item(_item), value(_value) {}
478 std::vector<int> first;
479 std::vector<LinearItem> data;
482 }; // class LinearHeap