Fix crash when an arrow is clicked with the delete tool.
2 * lemon/linear_heap.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2006 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.
32 /// \brief A Linear Heap implementation.
34 /// This class implements the \e linear \e heap data structure. A \e heap
35 /// is a data structure for storing items with specified values called \e
36 /// priorities in such a way that finding the item with minimum priority is
37 /// efficient. The linear heap is very simple implementation, it can store
38 /// only integer priorities and it stores for each priority in the [0..C]
39 /// range a list of items. So it should be used only when the priorities
40 /// are small. It is not intended to use as dijkstra heap.
42 /// \param _Item Type of the items to be stored.
43 /// \param _ItemIntMap A read and writable Item int map, used internally
44 /// to handle the cross references.
45 /// \param minimize If the given parameter is true then the heap gives back
46 /// the lowest priority.
47 template <typename _Item, typename _ItemIntMap, bool minimize = true >
53 typedef std::pair<Item, Prio> Pair;
54 typedef _ItemIntMap ItemIntMap;
56 /// \brief Type to represent the items states.
58 /// Each Item element have a state associated to it. It may be "in heap",
59 /// "pre heap" or "post heap". The latter two are indifferent from the
60 /// heap's point of view, but may be useful to the user.
62 /// The ItemIntMap \e should be initialized in such way that it maps
63 /// PRE_HEAP (-1) to any element to be put in the heap...
71 /// \brief The constructor.
74 /// \param _index should be given to the constructor, since it is used
75 /// internally to handle the cross references. The value of the map
76 /// should be PRE_HEAP (-1) for each element.
77 explicit LinearHeap(ItemIntMap &_index) : index(_index), minimal(0) {}
79 /// The number of items stored in the heap.
81 /// \brief Returns the number of items stored in the heap.
82 int size() const { return data.size(); }
84 /// \brief Checks if the heap stores no items.
86 /// Returns \c true if and only if the heap stores no items.
87 bool empty() const { return data.empty(); }
89 /// \brief Make empty this heap.
91 /// Make empty this heap.
93 for (int i = 0; i < (int)data.size(); ++i) {
94 index[data[i].item] = -2;
96 data.clear(); first.clear(); minimal = 0;
101 void relocate_last(int idx) {
102 if (idx + 1 < (int)data.size()) {
103 data[idx] = data.back();
104 if (data[idx].prev != -1) {
105 data[data[idx].prev].next = idx;
107 first[data[idx].value] = idx;
109 if (data[idx].next != -1) {
110 data[data[idx].next].prev = idx;
112 index[data[idx].item] = idx;
117 void unlace(int idx) {
118 if (data[idx].prev != -1) {
119 data[data[idx].prev].next = data[idx].next;
121 first[data[idx].value] = data[idx].next;
123 if (data[idx].next != -1) {
124 data[data[idx].next].prev = data[idx].prev;
129 if ((int)first.size() <= data[idx].value) {
130 first.resize(data[idx].value + 1, -1);
132 data[idx].next = first[data[idx].value];
133 if (data[idx].next != -1) {
134 data[data[idx].next].prev = idx;
136 first[data[idx].value] = idx;
141 /// \brief Insert a pair of item and priority into the heap.
143 /// Adds \c p.first to the heap with priority \c p.second.
144 /// \param p The pair to insert.
145 void push(const Pair& p) {
146 push(p.first, p.second);
149 /// \brief Insert an item into the heap with the given priority.
151 /// Adds \c i to the heap with priority \c p.
152 /// \param i The item to insert.
153 /// \param p The priority of the item.
154 void push(const Item &i, const Prio &p) {
155 int idx = data.size();
157 data.push_back(LinearItem(i, p));
164 /// \brief Returns the item with minimum priority.
166 /// This method returns the item with minimum priority.
167 /// \pre The heap must be nonempty.
169 while (first[minimal] == -1) {
172 return data[first[minimal]].item;
175 /// \brief Returns the minimum priority.
177 /// It returns the minimum priority.
178 /// \pre The heap must be nonempty.
180 while (first[minimal] == -1) {
186 /// \brief Deletes the item with minimum priority.
188 /// This method deletes the item with minimum priority from the heap.
189 /// \pre The heap must be non-empty.
191 while (first[minimal] == -1) {
194 int idx = first[minimal];
195 index[data[idx].item] = -2;
200 /// \brief Deletes \c i from the heap.
202 /// This method deletes item \c i from the heap, if \c i was
203 /// already stored in the heap.
204 /// \param i The item to erase.
205 void erase(const Item &i) {
207 index[data[idx].item] = -2;
213 /// \brief Returns the priority of \c i.
215 /// This function returns the priority of item \c i.
216 /// \pre \c i must be in the heap.
217 /// \param i The item.
218 Prio operator[](const Item &i) const {
220 return data[idx].value;
223 /// \brief \c i gets to the heap with priority \c p independently
224 /// if \c i was already there.
226 /// This method calls \ref push(\c i, \c p) if \c i is not stored
227 /// in the heap and sets the priority of \c i to \c p otherwise.
228 /// \param i The item.
229 /// \param p The priority.
230 void set(const Item &i, const Prio &p) {
234 } else if (p > data[idx].value) {
241 /// \brief Decreases the priority of \c i to \c p.
243 /// This method decreases the priority of item \c i to \c p.
244 /// \pre \c i must be stored in the heap with priority at least \c
245 /// p relative to \c Compare.
246 /// \param i The item.
247 /// \param p The priority.
248 void decrease(const Item &i, const Prio &p) {
258 /// \brief Increases the priority of \c i to \c p.
260 /// This method sets the priority of item \c i to \c p.
261 /// \pre \c i must be stored in the heap with priority at most \c
262 /// p relative to \c Compare.
263 /// \param i The item.
264 /// \param p The priority.
265 void increase(const Item &i, const Prio &p) {
272 /// \brief Returns if \c item is in, has already been in, or has
273 /// never been in the heap.
275 /// This method returns PRE_HEAP if \c item has never been in the
276 /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
277 /// otherwise. In the latter case it is possible that \c item will
278 /// get back to the heap again.
279 /// \param i The item.
280 state_enum state(const Item &i) const {
282 if (idx >= 0) idx = 0;
283 return state_enum(idx);
286 /// \brief Sets the state of the \c item in the heap.
288 /// Sets the state of the \c item in the heap. It can be used to
289 /// manually clear the heap when it is important to achive the
290 /// better time complexity.
291 /// \param i The item.
292 /// \param st The state. It should not be \c IN_HEAP.
293 void state(const Item& i, state_enum st) {
297 if (state(i) == IN_HEAP) {
310 LinearItem(const Item& _item, int _value)
311 : item(_item), value(_value) {}
320 std::vector<int> first;
321 std::vector<LinearItem> data;
324 }; // class LinearHeap
327 template <typename _Item, typename _ItemIntMap>
328 class LinearHeap<_Item, _ItemIntMap, false> {
333 typedef std::pair<Item, Prio> Pair;
334 typedef _ItemIntMap ItemIntMap;
344 explicit LinearHeap(ItemIntMap &_index) : index(_index), maximal(-1) {}
346 int size() const { return data.size(); }
347 bool empty() const { return data.empty(); }
350 for (int i = 0; i < (int)data.size(); ++i) {
351 index[data[i].item] = -2;
353 data.clear(); first.clear(); maximal = -1;
358 void relocate_last(int idx) {
359 if (idx + 1 != (int)data.size()) {
360 data[idx] = data.back();
361 if (data[idx].prev != -1) {
362 data[data[idx].prev].next = idx;
364 first[data[idx].value] = idx;
366 if (data[idx].next != -1) {
367 data[data[idx].next].prev = idx;
369 index[data[idx].item] = idx;
374 void unlace(int idx) {
375 if (data[idx].prev != -1) {
376 data[data[idx].prev].next = data[idx].next;
378 first[data[idx].value] = data[idx].next;
380 if (data[idx].next != -1) {
381 data[data[idx].next].prev = data[idx].prev;
386 if ((int)first.size() <= data[idx].value) {
387 first.resize(data[idx].value + 1, -1);
389 data[idx].next = first[data[idx].value];
390 if (data[idx].next != -1) {
391 data[data[idx].next].prev = idx;
393 first[data[idx].value] = idx;
399 void push(const Pair& p) {
400 push(p.first, p.second);
403 void push(const Item &i, const Prio &p) {
404 int idx = data.size();
406 data.push_back(LinearItem(i, p));
408 if (data[idx].value > maximal) {
409 maximal = data[idx].value;
414 while (first[maximal] == -1) {
417 return data[first[maximal]].item;
421 while (first[maximal] == -1) {
428 while (first[maximal] == -1) {
431 int idx = first[maximal];
432 index[data[idx].item] = -2;
437 void erase(const Item &i) {
439 index[data[idx].item] = -2;
444 Prio operator[](const Item &i) const {
446 return data[idx].value;
449 void set(const Item &i, const Prio &p) {
453 } else if (p > data[idx].value) {
460 void decrease(const Item &i, const Prio &p) {
470 void increase(const Item &i, const Prio &p) {
477 state_enum state(const Item &i) const {
479 if (idx >= 0) idx = 0;
480 return state_enum(idx);
483 void state(const Item& i, state_enum st) {
487 if (state(i) == IN_HEAP) {
500 LinearItem(const Item& _item, int _value)
501 : item(_item), value(_value) {}
510 std::vector<int> first;
511 std::vector<LinearItem> data;
514 }; // class LinearHeap