1.1 --- a/lemon/bin_heap.h Thu Jun 11 23:13:24 2009 +0200
1.2 +++ b/lemon/bin_heap.h Wed Jul 08 17:21:30 2009 +0200
1.3 @@ -21,7 +21,7 @@
1.4
1.5 ///\ingroup auxdat
1.6 ///\file
1.7 -///\brief Binary Heap implementation.
1.8 +///\brief Binary heap implementation.
1.9
1.10 #include <vector>
1.11 #include <utility>
1.12 @@ -29,45 +29,41 @@
1.13
1.14 namespace lemon {
1.15
1.16 - ///\ingroup auxdat
1.17 + /// \ingroup auxdat
1.18 ///
1.19 - ///\brief A Binary Heap implementation.
1.20 + /// \brief Binary heap data structure.
1.21 ///
1.22 - ///This class implements the \e binary \e heap data structure.
1.23 + /// This class implements the \e binary \e heap data structure.
1.24 + /// It fully conforms to the \ref concepts::Heap "heap concept".
1.25 ///
1.26 - ///A \e heap is a data structure for storing items with specified values
1.27 - ///called \e priorities in such a way that finding the item with minimum
1.28 - ///priority is efficient. \c CMP specifies the ordering of the priorities.
1.29 - ///In a heap one can change the priority of an item, add or erase an
1.30 - ///item, etc.
1.31 - ///
1.32 - ///\tparam PR Type of the priority of the items.
1.33 - ///\tparam IM A read and writable item map with int values, used internally
1.34 - ///to handle the cross references.
1.35 - ///\tparam CMP A functor class for the ordering of the priorities.
1.36 - ///The default is \c std::less<PR>.
1.37 - ///
1.38 - ///\sa FibHeap
1.39 - ///\sa Dijkstra
1.40 + /// \tparam PR Type of the priorities of the items.
1.41 + /// \tparam IM A read-writable item map with \c int values, used
1.42 + /// internally to handle the cross references.
1.43 + /// \tparam CMP A functor class for comparing the priorities.
1.44 + /// The default is \c std::less<PR>.
1.45 +#ifdef DOXYGEN
1.46 + template <typename PR, typename IM, typename CMP>
1.47 +#else
1.48 template <typename PR, typename IM, typename CMP = std::less<PR> >
1.49 +#endif
1.50 class BinHeap {
1.51 + public:
1.52
1.53 - public:
1.54 - ///\e
1.55 + /// Type of the item-int map.
1.56 typedef IM ItemIntMap;
1.57 - ///\e
1.58 + /// Type of the priorities.
1.59 typedef PR Prio;
1.60 - ///\e
1.61 + /// Type of the items stored in the heap.
1.62 typedef typename ItemIntMap::Key Item;
1.63 - ///\e
1.64 + /// Type of the item-priority pairs.
1.65 typedef std::pair<Item,Prio> Pair;
1.66 - ///\e
1.67 + /// Functor type for comparing the priorities.
1.68 typedef CMP Compare;
1.69
1.70 - /// \brief Type to represent the items states.
1.71 + /// \brief Type to represent the states of the items.
1.72 ///
1.73 - /// Each Item element have a state associated to it. It may be "in heap",
1.74 - /// "pre heap" or "post heap". The latter two are indifferent from the
1.75 + /// Each item has a state associated to it. It can be "in heap",
1.76 + /// "pre-heap" or "post-heap". The latter two are indifferent from the
1.77 /// heap's point of view, but may be useful to the user.
1.78 ///
1.79 /// The item-int map must be initialized in such way that it assigns
1.80 @@ -84,42 +80,43 @@
1.81 ItemIntMap &_iim;
1.82
1.83 public:
1.84 - /// \brief The constructor.
1.85 +
1.86 + /// \brief Constructor.
1.87 ///
1.88 - /// The constructor.
1.89 - /// \param map should be given to the constructor, since it is used
1.90 - /// internally to handle the cross references. The value of the map
1.91 - /// must be \c PRE_HEAP (<tt>-1</tt>) for every item.
1.92 + /// Constructor.
1.93 + /// \param map A map that assigns \c int values to the items.
1.94 + /// It is used internally to handle the cross references.
1.95 + /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
1.96 explicit BinHeap(ItemIntMap &map) : _iim(map) {}
1.97
1.98 - /// \brief The constructor.
1.99 + /// \brief Constructor.
1.100 ///
1.101 - /// The constructor.
1.102 - /// \param map should be given to the constructor, since it is used
1.103 - /// internally to handle the cross references. The value of the map
1.104 - /// should be PRE_HEAP (-1) for each element.
1.105 - ///
1.106 - /// \param comp The comparator function object.
1.107 + /// Constructor.
1.108 + /// \param map A map that assigns \c int values to the items.
1.109 + /// It is used internally to handle the cross references.
1.110 + /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
1.111 + /// \param comp The function object used for comparing the priorities.
1.112 BinHeap(ItemIntMap &map, const Compare &comp)
1.113 : _iim(map), _comp(comp) {}
1.114
1.115
1.116 - /// The number of items stored in the heap.
1.117 + /// \brief The number of items stored in the heap.
1.118 ///
1.119 - /// \brief Returns the number of items stored in the heap.
1.120 + /// This function returns the number of items stored in the heap.
1.121 int size() const { return _data.size(); }
1.122
1.123 - /// \brief Checks if the heap stores no items.
1.124 + /// \brief Check if the heap is empty.
1.125 ///
1.126 - /// Returns \c true if and only if the heap stores no items.
1.127 + /// This function returns \c true if the heap is empty.
1.128 bool empty() const { return _data.empty(); }
1.129
1.130 - /// \brief Make empty this heap.
1.131 + /// \brief Make the heap empty.
1.132 ///
1.133 - /// Make empty this heap. It does not change the cross reference map.
1.134 - /// If you want to reuse what is not surely empty you should first clear
1.135 - /// the heap and after that you should set the cross reference map for
1.136 - /// each item to \c PRE_HEAP.
1.137 + /// This functon makes the heap empty.
1.138 + /// It does not change the cross reference map. If you want to reuse
1.139 + /// a heap that is not surely empty, you should first clear it and
1.140 + /// then you should set the cross reference map to \c PRE_HEAP
1.141 + /// for each item.
1.142 void clear() {
1.143 _data.clear();
1.144 }
1.145 @@ -171,44 +168,47 @@
1.146 }
1.147
1.148 public:
1.149 +
1.150 /// \brief Insert a pair of item and priority into the heap.
1.151 ///
1.152 - /// Adds \c p.first to the heap with priority \c p.second.
1.153 + /// This function inserts \c p.first to the heap with priority
1.154 + /// \c p.second.
1.155 /// \param p The pair to insert.
1.156 + /// \pre \c p.first must not be stored in the heap.
1.157 void push(const Pair &p) {
1.158 int n = _data.size();
1.159 _data.resize(n+1);
1.160 bubble_up(n, p);
1.161 }
1.162
1.163 - /// \brief Insert an item into the heap with the given heap.
1.164 + /// \brief Insert an item into the heap with the given priority.
1.165 ///
1.166 - /// Adds \c i to the heap with priority \c p.
1.167 + /// This function inserts the given item into the heap with the
1.168 + /// given priority.
1.169 /// \param i The item to insert.
1.170 /// \param p The priority of the item.
1.171 + /// \pre \e i must not be stored in the heap.
1.172 void push(const Item &i, const Prio &p) { push(Pair(i,p)); }
1.173
1.174 - /// \brief Returns the item with minimum priority relative to \c Compare.
1.175 + /// \brief Return the item having minimum priority.
1.176 ///
1.177 - /// This method returns the item with minimum priority relative to \c
1.178 - /// Compare.
1.179 - /// \pre The heap must be nonempty.
1.180 + /// This function returns the item having minimum priority.
1.181 + /// \pre The heap must be non-empty.
1.182 Item top() const {
1.183 return _data[0].first;
1.184 }
1.185
1.186 - /// \brief Returns the minimum priority relative to \c Compare.
1.187 + /// \brief The minimum priority.
1.188 ///
1.189 - /// It returns the minimum priority relative to \c Compare.
1.190 - /// \pre The heap must be nonempty.
1.191 + /// This function returns the minimum priority.
1.192 + /// \pre The heap must be non-empty.
1.193 Prio prio() const {
1.194 return _data[0].second;
1.195 }
1.196
1.197 - /// \brief Deletes the item with minimum priority relative to \c Compare.
1.198 + /// \brief Remove the item having minimum priority.
1.199 ///
1.200 - /// This method deletes the item with minimum priority relative to \c
1.201 - /// Compare from the heap.
1.202 + /// This function removes the item having minimum priority.
1.203 /// \pre The heap must be non-empty.
1.204 void pop() {
1.205 int n = _data.size()-1;
1.206 @@ -219,11 +219,12 @@
1.207 _data.pop_back();
1.208 }
1.209
1.210 - /// \brief Deletes \c i from the heap.
1.211 + /// \brief Remove the given item from the heap.
1.212 ///
1.213 - /// This method deletes item \c i from the heap.
1.214 - /// \param i The item to erase.
1.215 - /// \pre The item should be in the heap.
1.216 + /// This function removes the given item from the heap if it is
1.217 + /// already stored.
1.218 + /// \param i The item to delete.
1.219 + /// \pre \e i must be in the heap.
1.220 void erase(const Item &i) {
1.221 int h = _iim[i];
1.222 int n = _data.size()-1;
1.223 @@ -236,22 +237,22 @@
1.224 _data.pop_back();
1.225 }
1.226
1.227 -
1.228 - /// \brief Returns the priority of \c i.
1.229 + /// \brief The priority of the given item.
1.230 ///
1.231 - /// This function returns the priority of item \c i.
1.232 + /// This function returns the priority of the given item.
1.233 /// \param i The item.
1.234 - /// \pre \c i must be in the heap.
1.235 + /// \pre \e i must be in the heap.
1.236 Prio operator[](const Item &i) const {
1.237 int idx = _iim[i];
1.238 return _data[idx].second;
1.239 }
1.240
1.241 - /// \brief \c i gets to the heap with priority \c p independently
1.242 - /// if \c i was already there.
1.243 + /// \brief Set the priority of an item or insert it, if it is
1.244 + /// not stored in the heap.
1.245 ///
1.246 - /// This method calls \ref push(\c i, \c p) if \c i is not stored
1.247 - /// in the heap and sets the priority of \c i to \c p otherwise.
1.248 + /// This method sets the priority of the given item if it is
1.249 + /// already stored in the heap. Otherwise it inserts the given
1.250 + /// item into the heap with the given priority.
1.251 /// \param i The item.
1.252 /// \param p The priority.
1.253 void set(const Item &i, const Prio &p) {
1.254 @@ -267,37 +268,35 @@
1.255 }
1.256 }
1.257
1.258 - /// \brief Decreases the priority of \c i to \c p.
1.259 + /// \brief Decrease the priority of an item to the given value.
1.260 ///
1.261 - /// This method decreases the priority of item \c i to \c p.
1.262 + /// This function decreases the priority of an item to the given value.
1.263 /// \param i The item.
1.264 /// \param p The priority.
1.265 - /// \pre \c i must be stored in the heap with priority at least \c
1.266 - /// p relative to \c Compare.
1.267 + /// \pre \e i must be stored in the heap with priority at least \e p.
1.268 void decrease(const Item &i, const Prio &p) {
1.269 int idx = _iim[i];
1.270 bubble_up(idx, Pair(i,p));
1.271 }
1.272
1.273 - /// \brief Increases the priority of \c i to \c p.
1.274 + /// \brief Increase the priority of an item to the given value.
1.275 ///
1.276 - /// This method sets the priority of item \c i to \c p.
1.277 + /// This function increases the priority of an item to the given value.
1.278 /// \param i The item.
1.279 /// \param p The priority.
1.280 - /// \pre \c i must be stored in the heap with priority at most \c
1.281 - /// p relative to \c Compare.
1.282 + /// \pre \e i must be stored in the heap with priority at most \e p.
1.283 void increase(const Item &i, const Prio &p) {
1.284 int idx = _iim[i];
1.285 bubble_down(idx, Pair(i,p), _data.size());
1.286 }
1.287
1.288 - /// \brief Returns if \c item is in, has already been in, or has
1.289 - /// never been in the heap.
1.290 + /// \brief Return the state of an item.
1.291 ///
1.292 - /// This method returns PRE_HEAP if \c item has never been in the
1.293 - /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
1.294 - /// otherwise. In the latter case it is possible that \c item will
1.295 - /// get back to the heap again.
1.296 + /// This method returns \c PRE_HEAP if the given item has never
1.297 + /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
1.298 + /// and \c POST_HEAP otherwise.
1.299 + /// In the latter case it is possible that the item will get back
1.300 + /// to the heap again.
1.301 /// \param i The item.
1.302 State state(const Item &i) const {
1.303 int s = _iim[i];
1.304 @@ -306,11 +305,11 @@
1.305 return State(s);
1.306 }
1.307
1.308 - /// \brief Sets the state of the \c item in the heap.
1.309 + /// \brief Set the state of an item in the heap.
1.310 ///
1.311 - /// Sets the state of the \c item in the heap. It can be used to
1.312 - /// manually clear the heap when it is important to achive the
1.313 - /// better time complexity.
1.314 + /// This function sets the state of the given item in the heap.
1.315 + /// It can be used to manually clear the heap when it is important
1.316 + /// to achive better time complexity.
1.317 /// \param i The item.
1.318 /// \param st The state. It should not be \c IN_HEAP.
1.319 void state(const Item& i, State st) {
1.320 @@ -327,12 +326,13 @@
1.321 }
1.322 }
1.323
1.324 - /// \brief Replaces an item in the heap.
1.325 + /// \brief Replace an item in the heap.
1.326 ///
1.327 - /// The \c i item is replaced with \c j item. The \c i item should
1.328 - /// be in the heap, while the \c j should be out of the heap. The
1.329 - /// \c i item will out of the heap and \c j will be in the heap
1.330 - /// with the same prioriority as prevoiusly the \c i item.
1.331 + /// This function replaces item \c i with item \c j.
1.332 + /// Item \c i must be in the heap, while \c j must be out of the heap.
1.333 + /// After calling this method, item \c i will be out of the
1.334 + /// heap and \c j will be in the heap with the same prioriority
1.335 + /// as item \c i had before.
1.336 void replace(const Item& i, const Item& j) {
1.337 int idx = _iim[i];
1.338 _iim.set(i, _iim[j]);