1.1 --- a/src/lemon/radix_heap.h Sat Apr 09 19:27:48 2005 +0000
1.2 +++ b/src/lemon/radix_heap.h Sat Apr 09 19:30:49 2005 +0000
1.3 @@ -1,5 +1,5 @@
1.4 /* -*- C++ -*-
1.5 - * src/lemon/bin_heap.h - Part of LEMON, a generic C++ optimization library
1.6 + * src/lemon/radix_heap.h - Part of LEMON, a generic C++ optimization library
1.7 *
1.8 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.9 * (Egervary Combinatorial Optimization Research Group, EGRES).
1.10 @@ -30,39 +30,54 @@
1.11 /// \addtogroup auxdat
1.12 /// @{
1.13
1.14 - /// A Radix Heap implementation.
1.15 -
1.16 - ///\todo Please document...
1.17 - ///
1.18 - ///\sa BinHeap
1.19 - ///\sa Dijkstra
1.20 + /// \brief Exception thrown by RadixHeap.
1.21 + ///
1.22 + /// This Exception is thrown when a smaller priority
1.23 + /// is inserted into the \e RadixHeap then the last time erased.
1.24 + /// \see RadixHeap
1.25 + /// \author Balazs Dezso
1.26
1.27 - class UnderFlowPriorityException : public RuntimeError {
1.28 + class UnderFlowPriorityError : public RuntimeError {
1.29 public:
1.30 virtual const char* exceptionName() const {
1.31 - return "lemon::UnderFlowPriorityException";
1.32 + return "lemon::UnderFlowPriorityError";
1.33 }
1.34 };
1.35
1.36 + /// \brief A Radix Heap implementation.
1.37 + ///
1.38 + /// This class implements the \e radix \e heap data structure. A \e heap
1.39 + /// is a data structure for storing items with specified values called \e
1.40 + /// priorities in such a way that finding the item with minimum priority is
1.41 + /// efficient. This heap type can store only items with \e int priority.
1.42 + /// In a heap one can change the priority of an item, add or erase an
1.43 + /// item, but the priority cannot be decreased under the last removed
1.44 + /// item's priority.
1.45 + ///
1.46 + /// \param _Item Type of the items to be stored.
1.47 + /// \param _ItemIntMap A read and writable Item int map, used internally
1.48 + /// to handle the cross references.
1.49 + ///
1.50 + /// \see BinHeap
1.51 + /// \see Dijkstra
1.52 + /// \author Balazs Dezso
1.53 +
1.54 template <typename _Item, typename _ItemIntMap>
1.55 class RadixHeap {
1.56
1.57 public:
1.58 typedef _Item Item;
1.59 - // FIXME: stl-ben nem ezt hivjak value_type -nak, hanem a kovetkezot...
1.60 typedef int Prio;
1.61 typedef _ItemIntMap ItemIntMap;
1.62
1.63 - /**
1.64 - * Each Item element have a state associated to it. It may be "in heap",
1.65 - * "pre heap" or "post heap". The later two are indifferent from the
1.66 - * heap's point of view, but may be useful to the user.
1.67 - *
1.68 - * The ItemIntMap _should_ be initialized in such way, that it maps
1.69 - * PRE_HEAP (-1) to any element to be put in the heap...
1.70 - */
1.71 - ///\todo it is used nowhere
1.72 + /// \brief Type to represent the items states.
1.73 ///
1.74 + /// Each Item element have a state associated to it. It may be "in heap",
1.75 + /// "pre heap" or "post heap". The later two are indifferent from the
1.76 + /// heap's point of view, but may be useful to the user.
1.77 + ///
1.78 + /// The ItemIntMap _should_ be initialized in such way, that it maps
1.79 + /// PRE_HEAP (-1) to any element to be put in the heap...
1.80 enum state_enum {
1.81 IN_HEAP = 0,
1.82 PRE_HEAP = -1,
1.83 @@ -91,13 +106,26 @@
1.84
1.85
1.86 public:
1.87 - ///\e
1.88 + /// \brief The constructor.
1.89 + ///
1.90 + /// The constructor.
1.91 + /// \param _iim should be given to the constructor, since it is used
1.92 + /// internally to handle the cross references. The value of the map
1.93 + /// should be PRE_HEAP (-1) for each element.
1.94 explicit RadixHeap(ItemIntMap &_iim) : iim(_iim) {
1.95 boxes.push_back(RadixBox(0, 1));
1.96 boxes.push_back(RadixBox(1, 1));
1.97 }
1.98
1.99 - ///\e
1.100 + /// \brief The constructor.
1.101 + ///
1.102 + /// The constructor.
1.103 + ///
1.104 + /// \param _iim It should be given to the constructor, since it is used
1.105 + /// internally to handle the cross references. The value of the map
1.106 + /// should be PRE_HEAP (-1) for each element.
1.107 + ///
1.108 + /// \param capacity It determines the initial capacity of the heap.
1.109 RadixHeap(ItemIntMap &_iim, int capacity) : iim(_iim) {
1.110 boxes.push_back(RadixBox(0, 1));
1.111 boxes.push_back(RadixBox(1, 1));
1.112 @@ -106,9 +134,13 @@
1.113 }
1.114 }
1.115
1.116 - ///\e
1.117 + /// The number of items stored in the heap.
1.118 + ///
1.119 + /// \brief Returns the number of items stored in the heap.
1.120 int size() const { return data.size(); }
1.121 - ///\e
1.122 + /// \brief Checks if the heap stores no items.
1.123 + ///
1.124 + /// Returns \c true if and only if the heap stores no items.
1.125 bool empty() const { return data.empty(); }
1.126
1.127 private:
1.128 @@ -183,7 +215,7 @@
1.129 /// \brief Find up the proper box for the item with the given prio.
1.130 int findDown(int start, int prio) {
1.131 while (upper(start, prio)) {
1.132 - if (--start < 0) throw UnderFlowPriorityException();
1.133 + if (--start < 0) throw UnderFlowPriorityError();
1.134 }
1.135 return start;
1.136 }
1.137 @@ -207,7 +239,6 @@
1.138 /// \brief Rearrange the items of the heap and makes the
1.139 /// first box not empty.
1.140 void moveDown() {
1.141 - // print(); printf("moveDown\n"); fflush(stdout);
1.142 int box = findFirst();
1.143 if (box == 0) return;
1.144 int min = minValue(box);
1.145 @@ -241,9 +272,12 @@
1.146
1.147 public:
1.148
1.149 - ///\e
1.150 + /// \brief Insert an item into the heap with the given heap.
1.151 + ///
1.152 + /// Adds \c i to the heap with priority \c p.
1.153 + /// \param i The item to insert.
1.154 + /// \param p The priority of the item.
1.155 void push(const Item &i, const Prio &p) {
1.156 - fflush(stdout);
1.157 int n = data.size();
1.158 iim.set(i, n);
1.159 data.push_back(RadixItem(i, p));
1.160 @@ -252,38 +286,43 @@
1.161 }
1.162 int box = findDown(boxes.size() - 1, p);
1.163 insert(box, n);
1.164 - // printf("Push %d\n", p);
1.165 - //print();
1.166 }
1.167
1.168 - ///\e
1.169 + /// \brief Returns the item with minimum priority.
1.170 + ///
1.171 + /// This method returns the item with minimum priority.
1.172 + /// \pre The heap must be nonempty.
1.173 Item top() const {
1.174 - // print(); printf("top\n"); fflush(stdout);
1.175 const_cast<RadixHeap<Item, ItemIntMap>*>(this)->moveDown();
1.176 return data[boxes[0].first].item;
1.177 - // print(); printf("top_end\n"); fflush(stdout);
1.178 }
1.179
1.180 - /// Returns the prio of the top element of the heap.
1.181 + /// \brief Returns the minimum priority.
1.182 + ///
1.183 + /// It returns the minimum priority.
1.184 + /// \pre The heap must be nonempty.
1.185 Prio prio() const {
1.186 - // print(); printf("prio\n"); fflush(stdout);
1.187 const_cast<RadixHeap<Item, ItemIntMap>*>(this)->moveDown();
1.188 return data[boxes[0].first].prio;
1.189 }
1.190
1.191 - ///\e
1.192 + /// \brief Deletes the item with minimum priority.
1.193 + ///
1.194 + /// This method deletes the item with minimum priority.
1.195 + /// \pre The heap must be non-empty.
1.196 void pop() {
1.197 - // print(); printf("pop\n"); fflush(stdout);
1.198 moveDown();
1.199 int index = boxes[0].first;
1.200 iim[data[index].item] = POST_HEAP;
1.201 remove(index);
1.202 relocate_last(index);
1.203 - // printf("Pop \n");
1.204 - //print();
1.205 }
1.206
1.207 - ///\e
1.208 + /// \brief Deletes \c i from the heap.
1.209 + ///
1.210 + /// This method deletes item \c i from the heap, if \c i was
1.211 + /// already stored in the heap.
1.212 + /// \param i The item to erase.
1.213 void erase(const Item &i) {
1.214 int index = iim[i];
1.215 iim[i] = POST_HEAP;
1.216 @@ -291,13 +330,24 @@
1.217 relocate_last(index);
1.218 }
1.219
1.220 - ///\e
1.221 + /// \brief Returns the priority of \c i.
1.222 + ///
1.223 + /// This function returns the priority of item \c i.
1.224 + /// \pre \c i must be in the heap.
1.225 + /// \param i The item.
1.226 Prio operator[](const Item &i) const {
1.227 int idx = iim[i];
1.228 return data[idx].prio;
1.229 }
1.230
1.231 - ///\e
1.232 + /// \brief \c i gets to the heap with priority \c p independently
1.233 + /// if \c i was already there.
1.234 + ///
1.235 + /// This method calls \ref push(\c i, \c p) if \c i is not stored
1.236 + /// in the heap and sets the priority of \c i to \c p otherwise.
1.237 + /// It may throw an \e UnderFlowPriorityException.
1.238 + /// \param i The item.
1.239 + /// \param p The priority.
1.240 void set(const Item &i, const Prio &p) {
1.241 int idx = iim[i];
1.242 if( idx < 0 ) {
1.243 @@ -312,39 +362,47 @@
1.244 }
1.245 }
1.246
1.247 - ///\e
1.248 +
1.249 + /// \brief Decreases the priority of \c i to \c p.
1.250 + ///
1.251 + /// This method decreases the priority of item \c i to \c p.
1.252 + /// \pre \c i must be stored in the heap with priority at least \c p, and
1.253 + /// \c should be greater then the last removed item's priority.
1.254 + /// \param i The item.
1.255 + /// \param p The priority.
1.256 void decrease(const Item &i, const Prio &p) {
1.257 - // print(); printf("decrease\n"); fflush(stdout);
1.258 int idx = iim[i];
1.259 data[idx].prio = p;
1.260 bubble_down(idx);
1.261 }
1.262
1.263 - ///\e
1.264 + /// \brief Increases the priority of \c i to \c p.
1.265 + ///
1.266 + /// This method sets the priority of item \c i to \c p.
1.267 + /// \pre \c i must be stored in the heap with priority at most \c
1.268 + /// p relative to \c Compare.
1.269 + /// \param i The item.
1.270 + /// \param p The priority.
1.271 void increase(const Item &i, const Prio &p) {
1.272 int idx = iim[i];
1.273 data[idx].prio = p;
1.274 bubble_up(idx);
1.275 }
1.276
1.277 - ///\e
1.278 + /// \brief Returns if \c item is in, has already been in, or has
1.279 + /// never been in the heap.
1.280 + ///
1.281 + /// This method returns PRE_HEAP if \c item has never been in the
1.282 + /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
1.283 + /// otherwise. In the latter case it is possible that \c item will
1.284 + /// get back to the heap again.
1.285 + /// \param i The item.
1.286 state_enum state(const Item &i) const {
1.287 int s = iim[i];
1.288 if( s >= 0 ) s = 0;
1.289 return state_enum(s);
1.290 }
1.291
1.292 -// void print() const {
1.293 -// for (int i = 0; i < boxes.size(); ++i) {
1.294 -// printf("(%d, %d) ", boxes[i].min, boxes[i].size);
1.295 -// for (int k = boxes[i].first; k != -1; k = data[k].next) {
1.296 -// printf("%d ", data[k].prio);
1.297 -// }
1.298 -// printf("\n");
1.299 -// }
1.300 -// fflush(stdout);
1.301 -// }
1.302 -
1.303 }; // class RadixHeap
1.304
1.305