1.1 --- a/lemon/radix_heap.h Fri Sep 25 12:24:16 2009 +0200
1.2 +++ b/lemon/radix_heap.h Sat Sep 26 07:08:10 2009 +0200
1.3 @@ -19,9 +19,9 @@
1.4 #ifndef LEMON_RADIX_HEAP_H
1.5 #define LEMON_RADIX_HEAP_H
1.6
1.7 -///\ingroup auxdat
1.8 +///\ingroup heaps
1.9 ///\file
1.10 -///\brief Radix Heap implementation.
1.11 +///\brief Radix heap implementation.
1.12
1.13 #include <vector>
1.14 #include <lemon/error.h>
1.15 @@ -29,56 +29,54 @@
1.16 namespace lemon {
1.17
1.18
1.19 - /// \ingroup auxdata
1.20 + /// \ingroup heaps
1.21 ///
1.22 - /// \brief A Radix Heap implementation.
1.23 + /// \brief Radix heap data structure.
1.24 ///
1.25 - /// This class implements the \e radix \e heap data structure. A \e heap
1.26 - /// is a data structure for storing items with specified values called \e
1.27 - /// priorities in such a way that finding the item with minimum priority is
1.28 - /// efficient. This heap type can store only items with \e int priority.
1.29 - /// In a heap one can change the priority of an item, add or erase an
1.30 - /// item, but the priority cannot be decreased under the last removed
1.31 - /// item's priority.
1.32 + /// This class implements the \e radix \e heap data structure.
1.33 + /// It practically conforms to the \ref concepts::Heap "heap concept",
1.34 + /// but it has some limitations due its special implementation.
1.35 + /// The type of the priorities must be \c int and the priority of an
1.36 + /// item cannot be decreased under the priority of the last removed item.
1.37 ///
1.38 - /// \param IM A read and writable Item int map, used internally
1.39 - /// to handle the cross references.
1.40 - ///
1.41 - /// \see BinHeap
1.42 - /// \see Dijkstra
1.43 + /// \tparam IM A read-writable item map with \c int values, used
1.44 + /// internally to handle the cross references.
1.45 template <typename IM>
1.46 class RadixHeap {
1.47
1.48 public:
1.49 - typedef typename IM::Key Item;
1.50 +
1.51 + /// Type of the item-int map.
1.52 + typedef IM ItemIntMap;
1.53 + /// Type of the priorities.
1.54 typedef int Prio;
1.55 - typedef IM ItemIntMap;
1.56 + /// Type of the items stored in the heap.
1.57 + typedef typename ItemIntMap::Key Item;
1.58
1.59 /// \brief Exception thrown by RadixHeap.
1.60 ///
1.61 - /// This Exception is thrown when a smaller priority
1.62 - /// is inserted into the \e RadixHeap then the last time erased.
1.63 + /// This exception is thrown when an item is inserted into a
1.64 + /// RadixHeap with a priority smaller than the last erased one.
1.65 /// \see RadixHeap
1.66 -
1.67 - class UnderFlowPriorityError : public Exception {
1.68 + class PriorityUnderflowError : public Exception {
1.69 public:
1.70 virtual const char* what() const throw() {
1.71 - return "lemon::RadixHeap::UnderFlowPriorityError";
1.72 + return "lemon::RadixHeap::PriorityUnderflowError";
1.73 }
1.74 };
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 ItemIntMap \e should be initialized in such way that it maps
1.86 - /// PRE_HEAP (-1) to any element to be put in the heap...
1.87 + /// The item-int map must be initialized in such way that it assigns
1.88 + /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
1.89 enum State {
1.90 - IN_HEAP = 0,
1.91 - PRE_HEAP = -1,
1.92 - POST_HEAP = -2
1.93 + IN_HEAP = 0, ///< = 0.
1.94 + PRE_HEAP = -1, ///< = -1.
1.95 + POST_HEAP = -2 ///< = -2.
1.96 };
1.97
1.98 private:
1.99 @@ -96,52 +94,55 @@
1.100 RadixBox(int _min, int _size) : first(-1), min(_min), size(_size) {}
1.101 };
1.102
1.103 - std::vector<RadixItem> data;
1.104 - std::vector<RadixBox> boxes;
1.105 + std::vector<RadixItem> _data;
1.106 + std::vector<RadixBox> _boxes;
1.107
1.108 ItemIntMap &_iim;
1.109
1.110 + public:
1.111
1.112 - public:
1.113 - /// \brief The constructor.
1.114 + /// \brief Constructor.
1.115 ///
1.116 - /// The constructor.
1.117 - ///
1.118 - /// \param map It should be given to the constructor, since it is used
1.119 - /// internally to handle the cross references. The value of the map
1.120 - /// should be PRE_HEAP (-1) for each element.
1.121 - ///
1.122 - /// \param minimal The initial minimal value of the heap.
1.123 - /// \param capacity It determines the initial capacity of the heap.
1.124 - RadixHeap(ItemIntMap &map, int minimal = 0, int capacity = 0)
1.125 - : _iim(map) {
1.126 - boxes.push_back(RadixBox(minimal, 1));
1.127 - boxes.push_back(RadixBox(minimal + 1, 1));
1.128 - while (lower(boxes.size() - 1, capacity + minimal - 1)) {
1.129 + /// Constructor.
1.130 + /// \param map A map that assigns \c int values to the items.
1.131 + /// It is used internally to handle the cross references.
1.132 + /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
1.133 + /// \param minimum The initial minimum value of the heap.
1.134 + /// \param capacity The initial capacity of the heap.
1.135 + RadixHeap(ItemIntMap &map, int minimum = 0, int capacity = 0)
1.136 + : _iim(map)
1.137 + {
1.138 + _boxes.push_back(RadixBox(minimum, 1));
1.139 + _boxes.push_back(RadixBox(minimum + 1, 1));
1.140 + while (lower(_boxes.size() - 1, capacity + minimum - 1)) {
1.141 extend();
1.142 }
1.143 }
1.144
1.145 - /// The number of items stored in the heap.
1.146 + /// \brief The number of items stored in the heap.
1.147 ///
1.148 - /// \brief Returns the number of items stored in the heap.
1.149 - int size() const { return data.size(); }
1.150 - /// \brief Checks if the heap stores no items.
1.151 + /// This function returns the number of items stored in the heap.
1.152 + int size() const { return _data.size(); }
1.153 +
1.154 + /// \brief Check if the heap is empty.
1.155 ///
1.156 - /// Returns \c true if and only if the heap stores no items.
1.157 - bool empty() const { return data.empty(); }
1.158 + /// This function returns \c true if the heap is empty.
1.159 + bool empty() const { return _data.empty(); }
1.160
1.161 - /// \brief Make empty this heap.
1.162 + /// \brief Make the heap empty.
1.163 ///
1.164 - /// Make empty this heap. It does not change the cross reference
1.165 - /// map. If you want to reuse a heap what is not surely empty you
1.166 - /// should first clear the heap and after that you should set the
1.167 - /// cross reference map for each item to \c PRE_HEAP.
1.168 - void clear(int minimal = 0, int capacity = 0) {
1.169 - data.clear(); boxes.clear();
1.170 - boxes.push_back(RadixBox(minimal, 1));
1.171 - boxes.push_back(RadixBox(minimal + 1, 1));
1.172 - while (lower(boxes.size() - 1, capacity + minimal - 1)) {
1.173 + /// This functon makes the heap empty.
1.174 + /// It does not change the cross reference map. If you want to reuse
1.175 + /// a heap that is not surely empty, you should first clear it and
1.176 + /// then you should set the cross reference map to \c PRE_HEAP
1.177 + /// for each item.
1.178 + /// \param minimum The minimum value of the heap.
1.179 + /// \param capacity The capacity of the heap.
1.180 + void clear(int minimum = 0, int capacity = 0) {
1.181 + _data.clear(); _boxes.clear();
1.182 + _boxes.push_back(RadixBox(minimum, 1));
1.183 + _boxes.push_back(RadixBox(minimum + 1, 1));
1.184 + while (lower(_boxes.size() - 1, capacity + minimum - 1)) {
1.185 extend();
1.186 }
1.187 }
1.188 @@ -149,255 +150,259 @@
1.189 private:
1.190
1.191 bool upper(int box, Prio pr) {
1.192 - return pr < boxes[box].min;
1.193 + return pr < _boxes[box].min;
1.194 }
1.195
1.196 bool lower(int box, Prio pr) {
1.197 - return pr >= boxes[box].min + boxes[box].size;
1.198 + return pr >= _boxes[box].min + _boxes[box].size;
1.199 }
1.200
1.201 - /// \brief Remove item from the box list.
1.202 + // Remove item from the box list
1.203 void remove(int index) {
1.204 - if (data[index].prev >= 0) {
1.205 - data[data[index].prev].next = data[index].next;
1.206 + if (_data[index].prev >= 0) {
1.207 + _data[_data[index].prev].next = _data[index].next;
1.208 } else {
1.209 - boxes[data[index].box].first = data[index].next;
1.210 + _boxes[_data[index].box].first = _data[index].next;
1.211 }
1.212 - if (data[index].next >= 0) {
1.213 - data[data[index].next].prev = data[index].prev;
1.214 + if (_data[index].next >= 0) {
1.215 + _data[_data[index].next].prev = _data[index].prev;
1.216 }
1.217 }
1.218
1.219 - /// \brief Insert item into the box list.
1.220 + // Insert item into the box list
1.221 void insert(int box, int index) {
1.222 - if (boxes[box].first == -1) {
1.223 - boxes[box].first = index;
1.224 - data[index].next = data[index].prev = -1;
1.225 + if (_boxes[box].first == -1) {
1.226 + _boxes[box].first = index;
1.227 + _data[index].next = _data[index].prev = -1;
1.228 } else {
1.229 - data[index].next = boxes[box].first;
1.230 - data[boxes[box].first].prev = index;
1.231 - data[index].prev = -1;
1.232 - boxes[box].first = index;
1.233 + _data[index].next = _boxes[box].first;
1.234 + _data[_boxes[box].first].prev = index;
1.235 + _data[index].prev = -1;
1.236 + _boxes[box].first = index;
1.237 }
1.238 - data[index].box = box;
1.239 + _data[index].box = box;
1.240 }
1.241
1.242 - /// \brief Add a new box to the box list.
1.243 + // Add a new box to the box list
1.244 void extend() {
1.245 - int min = boxes.back().min + boxes.back().size;
1.246 - int bs = 2 * boxes.back().size;
1.247 - boxes.push_back(RadixBox(min, bs));
1.248 + int min = _boxes.back().min + _boxes.back().size;
1.249 + int bs = 2 * _boxes.back().size;
1.250 + _boxes.push_back(RadixBox(min, bs));
1.251 }
1.252
1.253 - /// \brief Move an item up into the proper box.
1.254 - void bubble_up(int index) {
1.255 - if (!lower(data[index].box, data[index].prio)) return;
1.256 + // Move an item up into the proper box.
1.257 + void bubbleUp(int index) {
1.258 + if (!lower(_data[index].box, _data[index].prio)) return;
1.259 remove(index);
1.260 - int box = findUp(data[index].box, data[index].prio);
1.261 + int box = findUp(_data[index].box, _data[index].prio);
1.262 insert(box, index);
1.263 }
1.264
1.265 - /// \brief Find up the proper box for the item with the given prio.
1.266 + // Find up the proper box for the item with the given priority
1.267 int findUp(int start, int pr) {
1.268 while (lower(start, pr)) {
1.269 - if (++start == int(boxes.size())) {
1.270 + if (++start == int(_boxes.size())) {
1.271 extend();
1.272 }
1.273 }
1.274 return start;
1.275 }
1.276
1.277 - /// \brief Move an item down into the proper box.
1.278 - void bubble_down(int index) {
1.279 - if (!upper(data[index].box, data[index].prio)) return;
1.280 + // Move an item down into the proper box
1.281 + void bubbleDown(int index) {
1.282 + if (!upper(_data[index].box, _data[index].prio)) return;
1.283 remove(index);
1.284 - int box = findDown(data[index].box, data[index].prio);
1.285 + int box = findDown(_data[index].box, _data[index].prio);
1.286 insert(box, index);
1.287 }
1.288
1.289 - /// \brief Find up the proper box for the item with the given prio.
1.290 + // Find down the proper box for the item with the given priority
1.291 int findDown(int start, int pr) {
1.292 while (upper(start, pr)) {
1.293 - if (--start < 0) throw UnderFlowPriorityError();
1.294 + if (--start < 0) throw PriorityUnderflowError();
1.295 }
1.296 return start;
1.297 }
1.298
1.299 - /// \brief Find the first not empty box.
1.300 + // Find the first non-empty box
1.301 int findFirst() {
1.302 int first = 0;
1.303 - while (boxes[first].first == -1) ++first;
1.304 + while (_boxes[first].first == -1) ++first;
1.305 return first;
1.306 }
1.307
1.308 - /// \brief Gives back the minimal prio of the box.
1.309 + // Gives back the minimum priority of the given box
1.310 int minValue(int box) {
1.311 - int min = data[boxes[box].first].prio;
1.312 - for (int k = boxes[box].first; k != -1; k = data[k].next) {
1.313 - if (data[k].prio < min) min = data[k].prio;
1.314 + int min = _data[_boxes[box].first].prio;
1.315 + for (int k = _boxes[box].first; k != -1; k = _data[k].next) {
1.316 + if (_data[k].prio < min) min = _data[k].prio;
1.317 }
1.318 return min;
1.319 }
1.320
1.321 - /// \brief Rearrange the items of the heap and makes the
1.322 - /// first box not empty.
1.323 + // Rearrange the items of the heap and make the first box non-empty
1.324 void moveDown() {
1.325 int box = findFirst();
1.326 if (box == 0) return;
1.327 int min = minValue(box);
1.328 for (int i = 0; i <= box; ++i) {
1.329 - boxes[i].min = min;
1.330 - min += boxes[i].size;
1.331 + _boxes[i].min = min;
1.332 + min += _boxes[i].size;
1.333 }
1.334 - int curr = boxes[box].first, next;
1.335 + int curr = _boxes[box].first, next;
1.336 while (curr != -1) {
1.337 - next = data[curr].next;
1.338 - bubble_down(curr);
1.339 + next = _data[curr].next;
1.340 + bubbleDown(curr);
1.341 curr = next;
1.342 }
1.343 }
1.344
1.345 - void relocate_last(int index) {
1.346 - if (index != int(data.size()) - 1) {
1.347 - data[index] = data.back();
1.348 - if (data[index].prev != -1) {
1.349 - data[data[index].prev].next = index;
1.350 + void relocateLast(int index) {
1.351 + if (index != int(_data.size()) - 1) {
1.352 + _data[index] = _data.back();
1.353 + if (_data[index].prev != -1) {
1.354 + _data[_data[index].prev].next = index;
1.355 } else {
1.356 - boxes[data[index].box].first = index;
1.357 + _boxes[_data[index].box].first = index;
1.358 }
1.359 - if (data[index].next != -1) {
1.360 - data[data[index].next].prev = index;
1.361 + if (_data[index].next != -1) {
1.362 + _data[_data[index].next].prev = index;
1.363 }
1.364 - _iim[data[index].item] = index;
1.365 + _iim[_data[index].item] = index;
1.366 }
1.367 - data.pop_back();
1.368 + _data.pop_back();
1.369 }
1.370
1.371 public:
1.372
1.373 /// \brief Insert an item into the heap with the given priority.
1.374 ///
1.375 - /// Adds \c i to the heap with priority \c p.
1.376 + /// This function inserts the given item into the heap with the
1.377 + /// given priority.
1.378 /// \param i The item to insert.
1.379 /// \param p The priority of the item.
1.380 + /// \pre \e i must not be stored in the heap.
1.381 + /// \warning This method may throw an \c UnderFlowPriorityException.
1.382 void push(const Item &i, const Prio &p) {
1.383 - int n = data.size();
1.384 + int n = _data.size();
1.385 _iim.set(i, n);
1.386 - data.push_back(RadixItem(i, p));
1.387 - while (lower(boxes.size() - 1, p)) {
1.388 + _data.push_back(RadixItem(i, p));
1.389 + while (lower(_boxes.size() - 1, p)) {
1.390 extend();
1.391 }
1.392 - int box = findDown(boxes.size() - 1, p);
1.393 + int box = findDown(_boxes.size() - 1, p);
1.394 insert(box, n);
1.395 }
1.396
1.397 - /// \brief Returns the item with minimum priority.
1.398 + /// \brief Return the item having minimum priority.
1.399 ///
1.400 - /// This method returns the item with minimum priority.
1.401 - /// \pre The heap must be nonempty.
1.402 + /// This function returns the item having minimum priority.
1.403 + /// \pre The heap must be non-empty.
1.404 Item top() const {
1.405 const_cast<RadixHeap<ItemIntMap>&>(*this).moveDown();
1.406 - return data[boxes[0].first].item;
1.407 + return _data[_boxes[0].first].item;
1.408 }
1.409
1.410 - /// \brief Returns the minimum priority.
1.411 + /// \brief The minimum priority.
1.412 ///
1.413 - /// It returns the minimum priority.
1.414 - /// \pre The heap must be nonempty.
1.415 + /// This function returns the minimum priority.
1.416 + /// \pre The heap must be non-empty.
1.417 Prio prio() const {
1.418 const_cast<RadixHeap<ItemIntMap>&>(*this).moveDown();
1.419 - return data[boxes[0].first].prio;
1.420 + return _data[_boxes[0].first].prio;
1.421 }
1.422
1.423 - /// \brief Deletes the item with minimum priority.
1.424 + /// \brief Remove the item having minimum priority.
1.425 ///
1.426 - /// This method deletes the item with minimum priority.
1.427 + /// This function removes the item having minimum priority.
1.428 /// \pre The heap must be non-empty.
1.429 void pop() {
1.430 moveDown();
1.431 - int index = boxes[0].first;
1.432 - _iim[data[index].item] = POST_HEAP;
1.433 + int index = _boxes[0].first;
1.434 + _iim[_data[index].item] = POST_HEAP;
1.435 remove(index);
1.436 - relocate_last(index);
1.437 + relocateLast(index);
1.438 }
1.439
1.440 - /// \brief Deletes \c i from the heap.
1.441 + /// \brief Remove the given item from the heap.
1.442 ///
1.443 - /// This method deletes item \c i from the heap, if \c i was
1.444 - /// already stored in the heap.
1.445 - /// \param i The item to erase.
1.446 + /// This function removes the given item from the heap if it is
1.447 + /// already stored.
1.448 + /// \param i The item to delete.
1.449 + /// \pre \e i must be in the heap.
1.450 void erase(const Item &i) {
1.451 int index = _iim[i];
1.452 _iim[i] = POST_HEAP;
1.453 remove(index);
1.454 - relocate_last(index);
1.455 + relocateLast(index);
1.456 }
1.457
1.458 - /// \brief Returns the priority of \c i.
1.459 + /// \brief The priority of the given item.
1.460 ///
1.461 - /// This function returns the priority of item \c i.
1.462 - /// \pre \c i must be in the heap.
1.463 + /// This function returns the priority of the given item.
1.464 /// \param i The item.
1.465 + /// \pre \e i must be in the heap.
1.466 Prio operator[](const Item &i) const {
1.467 int idx = _iim[i];
1.468 - return data[idx].prio;
1.469 + return _data[idx].prio;
1.470 }
1.471
1.472 - /// \brief \c i gets to the heap with priority \c p independently
1.473 - /// if \c i was already there.
1.474 + /// \brief Set the priority of an item or insert it, if it is
1.475 + /// not stored in the heap.
1.476 ///
1.477 - /// This method calls \ref push(\c i, \c p) if \c i is not stored
1.478 - /// in the heap and sets the priority of \c i to \c p otherwise.
1.479 - /// It may throw an \e UnderFlowPriorityException.
1.480 + /// This method sets the priority of the given item if it is
1.481 + /// already stored in the heap. Otherwise it inserts the given
1.482 + /// item into the heap with the given priority.
1.483 /// \param i The item.
1.484 /// \param p The priority.
1.485 + /// \pre \e i must be in the heap.
1.486 + /// \warning This method may throw an \c UnderFlowPriorityException.
1.487 void set(const Item &i, const Prio &p) {
1.488 int idx = _iim[i];
1.489 if( idx < 0 ) {
1.490 push(i, p);
1.491 }
1.492 - else if( p >= data[idx].prio ) {
1.493 - data[idx].prio = p;
1.494 - bubble_up(idx);
1.495 + else if( p >= _data[idx].prio ) {
1.496 + _data[idx].prio = p;
1.497 + bubbleUp(idx);
1.498 } else {
1.499 - data[idx].prio = p;
1.500 - bubble_down(idx);
1.501 + _data[idx].prio = p;
1.502 + bubbleDown(idx);
1.503 }
1.504 }
1.505
1.506 -
1.507 - /// \brief Decreases the priority of \c i to \c p.
1.508 + /// \brief Decrease the priority of an item to the given value.
1.509 ///
1.510 - /// This method decreases the priority of item \c i to \c p.
1.511 - /// \pre \c i must be stored in the heap with priority at least \c p, and
1.512 - /// \c should be greater or equal to the last removed item's priority.
1.513 + /// This function decreases the priority of an item to the given value.
1.514 /// \param i The item.
1.515 /// \param p The priority.
1.516 + /// \pre \e i must be stored in the heap with priority at least \e p.
1.517 + /// \warning This method may throw an \c UnderFlowPriorityException.
1.518 void decrease(const Item &i, const Prio &p) {
1.519 int idx = _iim[i];
1.520 - data[idx].prio = p;
1.521 - bubble_down(idx);
1.522 + _data[idx].prio = p;
1.523 + bubbleDown(idx);
1.524 }
1.525
1.526 - /// \brief Increases the priority of \c i to \c p.
1.527 + /// \brief Increase the priority of an item to the given value.
1.528 ///
1.529 - /// This method sets the priority of item \c i to \c p.
1.530 - /// \pre \c i must be stored in the heap with priority at most \c p
1.531 + /// This function increases the priority of an item to the given value.
1.532 /// \param i The item.
1.533 /// \param p The priority.
1.534 + /// \pre \e i must be stored in the heap with priority at most \e p.
1.535 void increase(const Item &i, const Prio &p) {
1.536 int idx = _iim[i];
1.537 - data[idx].prio = p;
1.538 - bubble_up(idx);
1.539 + _data[idx].prio = p;
1.540 + bubbleUp(idx);
1.541 }
1.542
1.543 - /// \brief Returns if \c item is in, has already been in, or has
1.544 - /// never been in the heap.
1.545 + /// \brief Return the state of an item.
1.546 ///
1.547 - /// This method returns PRE_HEAP if \c item has never been in the
1.548 - /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
1.549 - /// otherwise. In the latter case it is possible that \c item will
1.550 - /// get back to the heap again.
1.551 + /// This method returns \c PRE_HEAP if the given item has never
1.552 + /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
1.553 + /// and \c POST_HEAP otherwise.
1.554 + /// In the latter case it is possible that the item will get back
1.555 + /// to the heap again.
1.556 /// \param i The item.
1.557 State state(const Item &i) const {
1.558 int s = _iim[i];
1.559 @@ -405,11 +410,11 @@
1.560 return State(s);
1.561 }
1.562
1.563 - /// \brief Sets the state of the \c item in the heap.
1.564 + /// \brief Set the state of an item in the heap.
1.565 ///
1.566 - /// Sets the state of the \c item in the heap. It can be used to
1.567 - /// manually clear the heap when it is important to achive the
1.568 - /// better time complexity.
1.569 + /// This function sets the state of the given item in the heap.
1.570 + /// It can be used to manually clear the heap when it is important
1.571 + /// to achive better time complexity.
1.572 /// \param i The item.
1.573 /// \param st The state. It should not be \c IN_HEAP.
1.574 void state(const Item& i, State st) {