1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2009
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
21 ///\brief The concept of heaps.
23 #ifndef LEMON_CONCEPTS_HEAP_H
24 #define LEMON_CONCEPTS_HEAP_H
26 #include <lemon/core.h>
27 #include <lemon/concept_check.h>
33 /// \addtogroup concept
36 /// \brief The heap concept.
38 /// Concept class describing the main interface of heaps. A \e heap
39 /// is a data structure for storing items with specified values called
40 /// \e priorities in such a way that finding the item with minimum
41 /// priority is efficient. In a heap one can change the priority of an
42 /// item, add or erase an item, etc.
44 /// \tparam PR Type of the priority of the items.
45 /// \tparam IM A read and writable item map with int values, used
46 /// internally to handle the cross references.
47 /// \tparam Comp A functor class for the ordering of the priorities.
48 /// The default is \c std::less<PR>.
50 template <typename PR, typename IM, typename Comp = std::less<PR> >
52 template <typename PR, typename IM>
57 /// Type of the item-int map.
58 typedef IM ItemIntMap;
59 /// Type of the priorities.
61 /// Type of the items stored in the heap.
62 typedef typename ItemIntMap::Key Item;
64 /// \brief Type to represent the states of the items.
66 /// Each item has a state associated to it. It can be "in heap",
67 /// "pre heap" or "post heap". The later two are indifferent
68 /// from the point of view of the heap, but may be useful for
71 /// The item-int map must be initialized in such way that it assigns
72 /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
74 IN_HEAP = 0, ///< = 0. The "in heap" state constant.
75 PRE_HEAP = -1, ///< = -1. The "pre heap" state constant.
76 POST_HEAP = -2 ///< = -2. The "post heap" state constant.
79 /// \brief The constructor.
82 /// \param map A map that assigns \c int values to keys of type
83 /// \c Item. It is used internally by the heap implementations to
84 /// handle the cross references. The assigned value must be
85 /// \c PRE_HEAP (<tt>-1</tt>) for every item.
86 explicit Heap(ItemIntMap &map) {}
88 /// \brief The number of items stored in the heap.
90 /// Returns the number of items stored in the heap.
91 int size() const { return 0; }
93 /// \brief Checks if the heap is empty.
95 /// Returns \c true if the heap is empty.
96 bool empty() const { return false; }
98 /// \brief Makes the heap empty.
100 /// Makes the heap empty.
103 /// \brief Inserts an item into the heap with the given priority.
105 /// Inserts the given item into the heap with the given priority.
106 /// \param i The item to insert.
107 /// \param p The priority of the item.
108 void push(const Item &i, const Prio &p) {}
110 /// \brief Returns the item having minimum priority.
112 /// Returns the item having minimum priority.
113 /// \pre The heap must be non-empty.
116 /// \brief The minimum priority.
118 /// Returns the minimum priority.
119 /// \pre The heap must be non-empty.
122 /// \brief Removes the item having minimum priority.
124 /// Removes the item having minimum priority.
125 /// \pre The heap must be non-empty.
128 /// \brief Removes an item from the heap.
130 /// Removes the given item from the heap if it is already stored.
131 /// \param i The item to delete.
132 void erase(const Item &i) {}
134 /// \brief The priority of an item.
136 /// Returns the priority of the given item.
137 /// \param i The item.
138 /// \pre \c i must be in the heap.
139 Prio operator[](const Item &i) const {}
141 /// \brief Sets the priority of an item or inserts it, if it is
142 /// not stored in the heap.
144 /// This method sets the priority of the given item if it is
145 /// already stored in the heap.
146 /// Otherwise it inserts the given item with the given priority.
148 /// \param i The item.
149 /// \param p The priority.
150 void set(const Item &i, const Prio &p) {}
152 /// \brief Decreases the priority of an item to the given value.
154 /// Decreases the priority of an item to the given value.
155 /// \param i The item.
156 /// \param p The priority.
157 /// \pre \c i must be stored in the heap with priority at least \c p.
158 void decrease(const Item &i, const Prio &p) {}
160 /// \brief Increases the priority of an item to the given value.
162 /// Increases the priority of an item to the given value.
163 /// \param i The item.
164 /// \param p The priority.
165 /// \pre \c i must be stored in the heap with priority at most \c p.
166 void increase(const Item &i, const Prio &p) {}
168 /// \brief Returns if an item is in, has already been in, or has
169 /// never been in the heap.
171 /// This method returns \c PRE_HEAP if the given item has never
172 /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
173 /// and \c POST_HEAP otherwise.
174 /// In the latter case it is possible that the item will get back
175 /// to the heap again.
176 /// \param i The item.
177 State state(const Item &i) const {}
179 /// \brief Sets the state of an item in the heap.
181 /// Sets the state of the given item in the heap. It can be used
182 /// to manually clear the heap when it is important to achive the
183 /// better time complexity.
184 /// \param i The item.
185 /// \param st The state. It should not be \c IN_HEAP.
186 void state(const Item& i, State st) {}
189 template <typename _Heap>
193 typedef typename _Heap::Item OwnItem;
194 typedef typename _Heap::Prio OwnPrio;
195 typedef typename _Heap::State OwnState;
201 ::lemon::ignore_unused_variable_warning(item);
202 ::lemon::ignore_unused_variable_warning(prio);
209 ::lemon::ignore_unused_variable_warning(own_item);
210 ::lemon::ignore_unused_variable_warning(own_prio);
211 ::lemon::ignore_unused_variable_warning(own_state);
215 ::lemon::ignore_unused_variable_warning(heap1);
216 ::lemon::ignore_unused_variable_warning(heap2);
219 ::lemon::ignore_unused_variable_warning(s);
220 bool e = heap.empty();
221 ::lemon::ignore_unused_variable_warning(e);
226 own_prio = heap.prio();
227 own_item = heap.top();
228 own_prio = heap[own_item];
230 heap.push(item, prio);
231 heap.push(own_item, own_prio);
234 heap.set(item, prio);
235 heap.decrease(item, prio);
236 heap.increase(item, prio);
237 heap.set(own_item, own_prio);
238 heap.decrease(own_item, own_prio);
239 heap.increase(own_item, own_prio);
242 heap.erase(own_item);
245 own_state = heap.state(own_item);
246 heap.state(own_item, own_state);
248 own_state = _Heap::PRE_HEAP;
249 own_state = _Heap::IN_HEAP;
250 own_state = _Heap::POST_HEAP;