1.1 --- a/lemon/bucket_heap.h Thu Jun 11 23:13:24 2009 +0200
1.2 +++ b/lemon/bucket_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 Bucket Heap implementation.
1.8 +///\brief Bucket heap implementation.
1.9
1.10 #include <vector>
1.11 #include <utility>
1.12 @@ -55,33 +55,39 @@
1.13
1.14 /// \ingroup auxdat
1.15 ///
1.16 - /// \brief A Bucket Heap implementation.
1.17 + /// \brief Bucket heap data structure.
1.18 ///
1.19 - /// This class implements the \e bucket \e heap data structure. A \e heap
1.20 - /// is a data structure for storing items with specified values called \e
1.21 - /// priorities in such a way that finding the item with minimum priority is
1.22 - /// efficient. The bucket heap is very simple implementation, it can store
1.23 - /// only integer priorities and it stores for each priority in the
1.24 - /// \f$ [0..C) \f$ range a list of items. So it should be used only when
1.25 - /// the priorities are small. It is not intended to use as dijkstra heap.
1.26 + /// This class implements the \e bucket \e heap data structure.
1.27 + /// It practically conforms to the \ref concepts::Heap "heap concept",
1.28 + /// but it has some limitations.
1.29 ///
1.30 - /// \param IM A read and write Item int map, used internally
1.31 - /// to handle the cross references.
1.32 - /// \param MIN If the given parameter is false then instead of the
1.33 - /// minimum value the maximum can be retrivied with the top() and
1.34 - /// prio() member functions.
1.35 + /// The bucket heap is a very simple structure. It can store only
1.36 + /// \c int priorities and it maintains a list of items for each priority
1.37 + /// in the range <tt>[0..C)</tt>. So it should only be used when the
1.38 + /// priorities are small. It is not intended to use as a Dijkstra heap.
1.39 + ///
1.40 + /// \tparam IM A read-writable item map with \c int values, used
1.41 + /// internally to handle the cross references.
1.42 + /// \tparam MIN Indicate if the heap is a \e min-heap or a \e max-heap.
1.43 + /// The default is \e min-heap. If this parameter is set to \c false,
1.44 + /// then the comparison is reversed, so the top(), prio() and pop()
1.45 + /// functions deal with the item having maximum priority instead of the
1.46 + /// minimum.
1.47 + ///
1.48 + /// \sa SimpleBucketHeap
1.49 template <typename IM, bool MIN = true>
1.50 class BucketHeap {
1.51
1.52 public:
1.53 - /// \e
1.54 - typedef typename IM::Key Item;
1.55 - /// \e
1.56 +
1.57 + /// Type of the item-int map.
1.58 + typedef IM ItemIntMap;
1.59 + /// Type of the priorities.
1.60 typedef int Prio;
1.61 - /// \e
1.62 - typedef std::pair<Item, Prio> Pair;
1.63 - /// \e
1.64 - typedef IM ItemIntMap;
1.65 + /// Type of the items stored in the heap.
1.66 + typedef typename ItemIntMap::Key Item;
1.67 + /// Type of the item-priority pairs.
1.68 + typedef std::pair<Item,Prio> Pair;
1.69
1.70 private:
1.71
1.72 @@ -89,10 +95,10 @@
1.73
1.74 public:
1.75
1.76 - /// \brief Type to represent the items states.
1.77 + /// \brief Type to represent the states of the items.
1.78 ///
1.79 - /// Each Item element have a state associated to it. It may be "in heap",
1.80 - /// "pre heap" or "post heap". The latter two are indifferent from the
1.81 + /// Each item has a state associated to it. It can be "in heap",
1.82 + /// "pre-heap" or "post-heap". The latter two are indifferent from the
1.83 /// heap's point of view, but may be useful to the user.
1.84 ///
1.85 /// The item-int map must be initialized in such way that it assigns
1.86 @@ -104,30 +110,32 @@
1.87 };
1.88
1.89 public:
1.90 - /// \brief The constructor.
1.91 +
1.92 + /// \brief Constructor.
1.93 ///
1.94 - /// The constructor.
1.95 - /// \param map should be given to the constructor, since it is used
1.96 - /// internally to handle the cross references. The value of the map
1.97 - /// should be PRE_HEAP (-1) for each element.
1.98 + /// Constructor.
1.99 + /// \param map A map that assigns \c int values to the items.
1.100 + /// It is used internally to handle the cross references.
1.101 + /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
1.102 explicit BucketHeap(ItemIntMap &map) : _iim(map), _minimum(0) {}
1.103
1.104 - /// The number of items stored in the heap.
1.105 + /// \brief The number of items stored in the heap.
1.106 ///
1.107 - /// \brief Returns the number of items stored in the heap.
1.108 + /// This function returns the number of items stored in the heap.
1.109 int size() const { return _data.size(); }
1.110
1.111 - /// \brief Checks if the heap stores no items.
1.112 + /// \brief Check if the heap is empty.
1.113 ///
1.114 - /// Returns \c true if and only if the heap stores no items.
1.115 + /// This function returns \c true if the heap is empty.
1.116 bool empty() const { return _data.empty(); }
1.117
1.118 - /// \brief Make empty this heap.
1.119 + /// \brief Make the heap empty.
1.120 ///
1.121 - /// Make empty this heap. It does not change the cross reference
1.122 - /// map. If you want to reuse a heap what is not surely empty you
1.123 - /// should first clear the heap and after that you should set the
1.124 - /// cross reference map for each item to \c PRE_HEAP.
1.125 + /// This functon makes the heap empty.
1.126 + /// It does not change the cross reference map. If you want to reuse
1.127 + /// a heap that is not surely empty, you should first clear it and
1.128 + /// then you should set the cross reference map to \c PRE_HEAP
1.129 + /// for each item.
1.130 void clear() {
1.131 _data.clear(); _first.clear(); _minimum = 0;
1.132 }
1.133 @@ -174,19 +182,24 @@
1.134 }
1.135
1.136 public:
1.137 +
1.138 /// \brief Insert a pair of item and priority into the heap.
1.139 ///
1.140 - /// Adds \c p.first to the heap with priority \c p.second.
1.141 + /// This function inserts \c p.first to the heap with priority
1.142 + /// \c p.second.
1.143 /// \param p The pair to insert.
1.144 + /// \pre \c p.first must not be stored in the heap.
1.145 void push(const Pair& p) {
1.146 push(p.first, p.second);
1.147 }
1.148
1.149 /// \brief Insert an item into the heap with the given priority.
1.150 ///
1.151 - /// Adds \c i to the heap with priority \c p.
1.152 + /// This function inserts the given item into the heap with the
1.153 + /// given priority.
1.154 /// \param i The item to insert.
1.155 /// \param p The priority of the item.
1.156 + /// \pre \e i must not be stored in the heap.
1.157 void push(const Item &i, const Prio &p) {
1.158 int idx = _data.size();
1.159 _iim[i] = idx;
1.160 @@ -197,10 +210,10 @@
1.161 }
1.162 }
1.163
1.164 - /// \brief Returns the item with minimum priority.
1.165 + /// \brief Return the item having minimum priority.
1.166 ///
1.167 - /// This method returns the item with minimum priority.
1.168 - /// \pre The heap must be nonempty.
1.169 + /// This function returns the item having minimum priority.
1.170 + /// \pre The heap must be non-empty.
1.171 Item top() const {
1.172 while (_first[_minimum] == -1) {
1.173 Direction::increase(_minimum);
1.174 @@ -208,10 +221,10 @@
1.175 return _data[_first[_minimum]].item;
1.176 }
1.177
1.178 - /// \brief Returns the minimum priority.
1.179 + /// \brief The minimum priority.
1.180 ///
1.181 - /// It returns the minimum priority.
1.182 - /// \pre The heap must be nonempty.
1.183 + /// This function returns the minimum priority.
1.184 + /// \pre The heap must be non-empty.
1.185 Prio prio() const {
1.186 while (_first[_minimum] == -1) {
1.187 Direction::increase(_minimum);
1.188 @@ -219,9 +232,9 @@
1.189 return _minimum;
1.190 }
1.191
1.192 - /// \brief Deletes the item with minimum priority.
1.193 + /// \brief Remove the item having minimum priority.
1.194 ///
1.195 - /// This method deletes the item with minimum priority from the heap.
1.196 + /// This function removes the item having minimum priority.
1.197 /// \pre The heap must be non-empty.
1.198 void pop() {
1.199 while (_first[_minimum] == -1) {
1.200 @@ -233,11 +246,12 @@
1.201 relocate_last(idx);
1.202 }
1.203
1.204 - /// \brief Deletes \c i from the heap.
1.205 + /// \brief Remove the given item from the heap.
1.206 ///
1.207 - /// This method deletes item \c i from the heap, if \c i was
1.208 - /// already stored in the heap.
1.209 - /// \param i The item to erase.
1.210 + /// This function removes the given item from the heap if it is
1.211 + /// already stored.
1.212 + /// \param i The item to delete.
1.213 + /// \pre \e i must be in the heap.
1.214 void erase(const Item &i) {
1.215 int idx = _iim[i];
1.216 _iim[_data[idx].item] = -2;
1.217 @@ -245,22 +259,22 @@
1.218 relocate_last(idx);
1.219 }
1.220
1.221 -
1.222 - /// \brief Returns the priority of \c i.
1.223 + /// \brief The priority of the given item.
1.224 ///
1.225 - /// This function returns the priority of item \c i.
1.226 - /// \pre \c i must be in the heap.
1.227 + /// This function returns the priority of the given item.
1.228 /// \param i The item.
1.229 + /// \pre \e i must be in the heap.
1.230 Prio operator[](const Item &i) const {
1.231 int idx = _iim[i];
1.232 return _data[idx].value;
1.233 }
1.234
1.235 - /// \brief \c i gets to the heap with priority \c p independently
1.236 - /// if \c i was already there.
1.237 + /// \brief Set the priority of an item or insert it, if it is
1.238 + /// not stored in the heap.
1.239 ///
1.240 - /// This method calls \ref push(\c i, \c p) if \c i is not stored
1.241 - /// in the heap and sets the priority of \c i to \c p otherwise.
1.242 + /// This method sets the priority of the given item if it is
1.243 + /// already stored in the heap. Otherwise it inserts the given
1.244 + /// item into the heap with the given priority.
1.245 /// \param i The item.
1.246 /// \param p The priority.
1.247 void set(const Item &i, const Prio &p) {
1.248 @@ -274,13 +288,12 @@
1.249 }
1.250 }
1.251
1.252 - /// \brief Decreases the priority of \c i to \c p.
1.253 + /// \brief Decrease the priority of an item to the given value.
1.254 ///
1.255 - /// This method decreases the priority of item \c i to \c p.
1.256 - /// \pre \c i must be stored in the heap with priority at least \c
1.257 - /// p relative to \c Compare.
1.258 + /// This function decreases the priority of an item to the given value.
1.259 /// \param i The item.
1.260 /// \param p The priority.
1.261 + /// \pre \e i must be stored in the heap with priority at least \e p.
1.262 void decrease(const Item &i, const Prio &p) {
1.263 int idx = _iim[i];
1.264 unlace(idx);
1.265 @@ -291,13 +304,12 @@
1.266 lace(idx);
1.267 }
1.268
1.269 - /// \brief Increases the priority of \c i to \c p.
1.270 + /// \brief Increase the priority of an item to the given value.
1.271 ///
1.272 - /// This method sets the priority of item \c i to \c p.
1.273 - /// \pre \c i must be stored in the heap with priority at most \c
1.274 - /// p relative to \c Compare.
1.275 + /// This function increases the priority of an item to the given value.
1.276 /// \param i The item.
1.277 /// \param p The priority.
1.278 + /// \pre \e i must be stored in the heap with priority at most \e p.
1.279 void increase(const Item &i, const Prio &p) {
1.280 int idx = _iim[i];
1.281 unlace(idx);
1.282 @@ -305,13 +317,13 @@
1.283 lace(idx);
1.284 }
1.285
1.286 - /// \brief Returns if \c item is in, has already been in, or has
1.287 - /// never been in the heap.
1.288 + /// \brief Return the state of an item.
1.289 ///
1.290 - /// This method returns PRE_HEAP if \c item has never been in the
1.291 - /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
1.292 - /// otherwise. In the latter case it is possible that \c item will
1.293 - /// get back to the heap again.
1.294 + /// This method returns \c PRE_HEAP if the given item has never
1.295 + /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
1.296 + /// and \c POST_HEAP otherwise.
1.297 + /// In the latter case it is possible that the item will get back
1.298 + /// to the heap again.
1.299 /// \param i The item.
1.300 State state(const Item &i) const {
1.301 int idx = _iim[i];
1.302 @@ -319,11 +331,11 @@
1.303 return State(idx);
1.304 }
1.305
1.306 - /// \brief Sets the state of the \c item in the heap.
1.307 + /// \brief Set the state of an item in the heap.
1.308 ///
1.309 - /// Sets the state of the \c item in the heap. It can be used to
1.310 - /// manually clear the heap when it is important to achive the
1.311 - /// better time complexity.
1.312 + /// This function sets the state of the given item in the heap.
1.313 + /// It can be used to manually clear the heap when it is important
1.314 + /// to achive better time complexity.
1.315 /// \param i The item.
1.316 /// \param st The state. It should not be \c IN_HEAP.
1.317 void state(const Item& i, State st) {
1.318 @@ -361,31 +373,42 @@
1.319
1.320 /// \ingroup auxdat
1.321 ///
1.322 - /// \brief A Simplified Bucket Heap implementation.
1.323 + /// \brief Simplified bucket heap data structure.
1.324 ///
1.325 /// This class implements a simplified \e bucket \e heap data
1.326 - /// structure. It does not provide some functionality but it faster
1.327 - /// and simplier data structure than the BucketHeap. The main
1.328 - /// difference is that the BucketHeap stores for every key a double
1.329 - /// linked list while this class stores just simple lists. In the
1.330 - /// other way it does not support erasing each elements just the
1.331 - /// minimal and it does not supports key increasing, decreasing.
1.332 + /// structure. It does not provide some functionality, but it is
1.333 + /// faster and simpler than BucketHeap. The main difference is
1.334 + /// that BucketHeap stores a doubly-linked list for each key while
1.335 + /// this class stores only simply-linked lists. It supports erasing
1.336 + /// only for the item having minimum priority and it does not support
1.337 + /// key increasing and decreasing.
1.338 ///
1.339 - /// \param IM A read and write Item int map, used internally
1.340 - /// to handle the cross references.
1.341 - /// \param MIN If the given parameter is false then instead of the
1.342 - /// minimum value the maximum can be retrivied with the top() and
1.343 - /// prio() member functions.
1.344 + /// Note that this implementation does not conform to the
1.345 + /// \ref concepts::Heap "heap concept" due to the lack of some
1.346 + /// functionality.
1.347 + ///
1.348 + /// \tparam IM A read-writable item map with \c int values, used
1.349 + /// internally to handle the cross references.
1.350 + /// \tparam MIN Indicate if the heap is a \e min-heap or a \e max-heap.
1.351 + /// The default is \e min-heap. If this parameter is set to \c false,
1.352 + /// then the comparison is reversed, so the top(), prio() and pop()
1.353 + /// functions deal with the item having maximum priority instead of the
1.354 + /// minimum.
1.355 ///
1.356 /// \sa BucketHeap
1.357 template <typename IM, bool MIN = true >
1.358 class SimpleBucketHeap {
1.359
1.360 public:
1.361 - typedef typename IM::Key Item;
1.362 +
1.363 + /// Type of the item-int map.
1.364 + typedef IM ItemIntMap;
1.365 + /// Type of the priorities.
1.366 typedef int Prio;
1.367 - typedef std::pair<Item, Prio> Pair;
1.368 - typedef IM ItemIntMap;
1.369 + /// Type of the items stored in the heap.
1.370 + typedef typename ItemIntMap::Key Item;
1.371 + /// Type of the item-priority pairs.
1.372 + typedef std::pair<Item,Prio> Pair;
1.373
1.374 private:
1.375
1.376 @@ -393,10 +416,10 @@
1.377
1.378 public:
1.379
1.380 - /// \brief Type to represent the items states.
1.381 + /// \brief Type to represent the states of the items.
1.382 ///
1.383 - /// Each Item element have a state associated to it. It may be "in heap",
1.384 - /// "pre heap" or "post heap". The latter two are indifferent from the
1.385 + /// Each item has a state associated to it. It can be "in heap",
1.386 + /// "pre-heap" or "post-heap". The latter two are indifferent from the
1.387 /// heap's point of view, but may be useful to the user.
1.388 ///
1.389 /// The item-int map must be initialized in such way that it assigns
1.390 @@ -409,48 +432,53 @@
1.391
1.392 public:
1.393
1.394 - /// \brief The constructor.
1.395 + /// \brief Constructor.
1.396 ///
1.397 - /// The constructor.
1.398 - /// \param map should be given to the constructor, since it is used
1.399 - /// internally to handle the cross references. The value of the map
1.400 - /// should be PRE_HEAP (-1) for each element.
1.401 + /// Constructor.
1.402 + /// \param map A map that assigns \c int values to the items.
1.403 + /// It is used internally to handle the cross references.
1.404 + /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
1.405 explicit SimpleBucketHeap(ItemIntMap &map)
1.406 : _iim(map), _free(-1), _num(0), _minimum(0) {}
1.407
1.408 - /// \brief Returns the number of items stored in the heap.
1.409 + /// \brief The number of items stored in the heap.
1.410 ///
1.411 - /// The number of items stored in the heap.
1.412 + /// This function returns the number of items stored in the heap.
1.413 int size() const { return _num; }
1.414
1.415 - /// \brief Checks if the heap stores no items.
1.416 + /// \brief Check if the heap is empty.
1.417 ///
1.418 - /// Returns \c true if and only if the heap stores no items.
1.419 + /// This function returns \c true if the heap is empty.
1.420 bool empty() const { return _num == 0; }
1.421
1.422 - /// \brief Make empty this heap.
1.423 + /// \brief Make the heap empty.
1.424 ///
1.425 - /// Make empty this heap. It does not change the cross reference
1.426 - /// map. If you want to reuse a heap what is not surely empty you
1.427 - /// should first clear the heap and after that you should set the
1.428 - /// cross reference map for each item to \c PRE_HEAP.
1.429 + /// This functon makes the heap empty.
1.430 + /// It does not change the cross reference map. If you want to reuse
1.431 + /// a heap that is not surely empty, you should first clear it and
1.432 + /// then you should set the cross reference map to \c PRE_HEAP
1.433 + /// for each item.
1.434 void clear() {
1.435 _data.clear(); _first.clear(); _free = -1; _num = 0; _minimum = 0;
1.436 }
1.437
1.438 /// \brief Insert a pair of item and priority into the heap.
1.439 ///
1.440 - /// Adds \c p.first to the heap with priority \c p.second.
1.441 + /// This function inserts \c p.first to the heap with priority
1.442 + /// \c p.second.
1.443 /// \param p The pair to insert.
1.444 + /// \pre \c p.first must not be stored in the heap.
1.445 void push(const Pair& p) {
1.446 push(p.first, p.second);
1.447 }
1.448
1.449 /// \brief Insert an item into the heap with the given priority.
1.450 ///
1.451 - /// Adds \c i to the heap with priority \c p.
1.452 + /// This function inserts the given item into the heap with the
1.453 + /// given priority.
1.454 /// \param i The item to insert.
1.455 /// \param p The priority of the item.
1.456 + /// \pre \e i must not be stored in the heap.
1.457 void push(const Item &i, const Prio &p) {
1.458 int idx;
1.459 if (_free == -1) {
1.460 @@ -471,10 +499,10 @@
1.461 ++_num;
1.462 }
1.463
1.464 - /// \brief Returns the item with minimum priority.
1.465 + /// \brief Return the item having minimum priority.
1.466 ///
1.467 - /// This method returns the item with minimum priority.
1.468 - /// \pre The heap must be nonempty.
1.469 + /// This function returns the item having minimum priority.
1.470 + /// \pre The heap must be non-empty.
1.471 Item top() const {
1.472 while (_first[_minimum] == -1) {
1.473 Direction::increase(_minimum);
1.474 @@ -482,10 +510,10 @@
1.475 return _data[_first[_minimum]].item;
1.476 }
1.477
1.478 - /// \brief Returns the minimum priority.
1.479 + /// \brief The minimum priority.
1.480 ///
1.481 - /// It returns the minimum priority.
1.482 - /// \pre The heap must be nonempty.
1.483 + /// This function returns the minimum priority.
1.484 + /// \pre The heap must be non-empty.
1.485 Prio prio() const {
1.486 while (_first[_minimum] == -1) {
1.487 Direction::increase(_minimum);
1.488 @@ -493,9 +521,9 @@
1.489 return _minimum;
1.490 }
1.491
1.492 - /// \brief Deletes the item with minimum priority.
1.493 + /// \brief Remove the item having minimum priority.
1.494 ///
1.495 - /// This method deletes the item with minimum priority from the heap.
1.496 + /// This function removes the item having minimum priority.
1.497 /// \pre The heap must be non-empty.
1.498 void pop() {
1.499 while (_first[_minimum] == -1) {
1.500 @@ -509,16 +537,15 @@
1.501 --_num;
1.502 }
1.503
1.504 - /// \brief Returns the priority of \c i.
1.505 + /// \brief The priority of the given item.
1.506 ///
1.507 - /// This function returns the priority of item \c i.
1.508 - /// \warning This operator is not a constant time function
1.509 - /// because it scans the whole data structure to find the proper
1.510 - /// value.
1.511 - /// \pre \c i must be in the heap.
1.512 + /// This function returns the priority of the given item.
1.513 /// \param i The item.
1.514 + /// \pre \e i must be in the heap.
1.515 + /// \warning This operator is not a constant time function because
1.516 + /// it scans the whole data structure to find the proper value.
1.517 Prio operator[](const Item &i) const {
1.518 - for (int k = 0; k < _first.size(); ++k) {
1.519 + for (int k = 0; k < int(_first.size()); ++k) {
1.520 int idx = _first[k];
1.521 while (idx != -1) {
1.522 if (_data[idx].item == i) {
1.523 @@ -530,13 +557,13 @@
1.524 return -1;
1.525 }
1.526
1.527 - /// \brief Returns if \c item is in, has already been in, or has
1.528 - /// never been in the heap.
1.529 + /// \brief Return the state of an item.
1.530 ///
1.531 - /// This method returns PRE_HEAP if \c item has never been in the
1.532 - /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
1.533 - /// otherwise. In the latter case it is possible that \c item will
1.534 - /// get back to the heap again.
1.535 + /// This method returns \c PRE_HEAP if the given item has never
1.536 + /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
1.537 + /// and \c POST_HEAP otherwise.
1.538 + /// In the latter case it is possible that the item will get back
1.539 + /// to the heap again.
1.540 /// \param i The item.
1.541 State state(const Item &i) const {
1.542 int idx = _iim[i];