Documentation improvments.
1.1 --- a/src/lemon/bin_heap.h Sat Apr 09 19:27:48 2005 +0000
1.2 +++ b/src/lemon/bin_heap.h Sat Apr 09 19:30:49 2005 +0000
1.3 @@ -59,16 +59,14 @@
1.4 typedef ItemIntMap ItemIntMapType;
1.5 typedef Compare PrioCompare;
1.6
1.7 - /**
1.8 - * Each Item element have a state associated to it. It may be "in heap",
1.9 - * "pre heap" or "post heap". The later two are indifferent from the
1.10 - * heap's point of view, but may be useful to the user.
1.11 - *
1.12 - * The ItemIntMap _should_ be initialized in such way, that it maps
1.13 - * PRE_HEAP (-1) to any element to be put in the heap...
1.14 - */
1.15 - ///\todo it is used nowhere
1.16 + /// \brief Type to represent the items states.
1.17 ///
1.18 + /// Each Item element have a state associated to it. It may be "in heap",
1.19 + /// "pre heap" or "post heap". The later two are indifferent from the
1.20 + /// heap's point of view, but may be useful to the user.
1.21 + ///
1.22 + /// The ItemIntMap _should_ be initialized in such way, that it maps
1.23 + /// PRE_HEAP (-1) to any element to be put in the heap...
1.24 enum state_enum {
1.25 IN_HEAP = 0,
1.26 PRE_HEAP = -1,
1.27 @@ -78,41 +76,37 @@
1.28 private:
1.29 std::vector<PairType> data;
1.30 Compare comp;
1.31 - // FIXME: jo ez igy???
1.32 ItemIntMap &iim;
1.33
1.34 public:
1.35 - ///The constructor
1.36 -
1.37 - /**
1.38 - \c _iim should be given to the constructor, since it is used
1.39 - internally to handle the cross references.
1.40 - */
1.41 + /// \brief The constructor.
1.42 + ///
1.43 + /// The constructor.
1.44 + /// \param _iim should be given to the constructor, since it is used
1.45 + /// internally to handle the cross references. The value of the map
1.46 + /// should be PRE_HEAP (-1) for each element.
1.47 explicit BinHeap(ItemIntMap &_iim) : iim(_iim) {}
1.48
1.49 - ///The constructor
1.50 -
1.51 - /**
1.52 - \c _iim should be given to the constructor, since it is used
1.53 - internally to handle the cross references. \c _comp is an
1.54 - object for ordering of the priorities.
1.55 - */
1.56 + /// \brief The constructor.
1.57 + ///
1.58 + /// The constructor.
1.59 + /// \param _iim should be given to the constructor, since it is used
1.60 + /// internally to handle the cross references. The value of the map
1.61 + /// should be PRE_HEAP (-1) for each element.
1.62 + ///
1.63 + /// \param _comp The comparator function object.
1.64 BinHeap(ItemIntMap &_iim, const Compare &_comp)
1.65 : iim(_iim), comp(_comp) {}
1.66
1.67
1.68 - ///The number of items stored in the heap.
1.69 -
1.70 - /**
1.71 - Returns the number of items stored in the heap.
1.72 - */
1.73 + /// The number of items stored in the heap.
1.74 + ///
1.75 + /// \brief Returns the number of items stored in the heap.
1.76 int size() const { return data.size(); }
1.77
1.78 - ///Checks if the heap stores no items.
1.79 -
1.80 - /**
1.81 - Returns \c true if and only if the heap stores no items.
1.82 - */
1.83 + /// \brief Checks if the heap stores no items.
1.84 + ///
1.85 + /// Returns \c true if and only if the heap stores no items.
1.86 bool empty() const { return data.empty(); }
1.87
1.88 private:
1.89 @@ -142,86 +136,76 @@
1.90 }
1.91
1.92 public:
1.93 - ///Adds \c p.first to the heap with priority \c p.second.
1.94 -
1.95 - /**
1.96 - Adds \c p.first to the heap with priority \c p.second.
1.97 - \c p.first must not be stored in the heap.
1.98 - */
1.99 + /// \brief Insert a pair of item and priority into the heap.
1.100 + ///
1.101 + /// Adds \c p.first to the heap with priority \c p.second.
1.102 + /// \param p The pair to insert.
1.103 void push(const PairType &p) {
1.104 int n = data.size();
1.105 data.resize(n+1);
1.106 bubble_up(n, p);
1.107 }
1.108
1.109 - ///Adds \c i to the heap with priority \c p.
1.110 -
1.111 - /**
1.112 - Adds \c i to the heap with priority \c p.
1.113 - \pre \c i must not be stored in the heap.
1.114 - */
1.115 + /// \brief Insert an item into the heap with the given heap.
1.116 + ///
1.117 + /// Adds \c i to the heap with priority \c p.
1.118 + /// \param i The item to insert.
1.119 + /// \param p The priority of the item.
1.120 void push(const Item &i, const Prio &p) { push(PairType(i,p)); }
1.121
1.122 - ///Returns the item with minimum priority relative to \c Compare.
1.123 -
1.124 - /**
1.125 - This method returns the item with minimum priority relative to \c
1.126 - Compare.
1.127 - \pre The heap must be nonempty.
1.128 - */
1.129 + /// \brief Returns the item with minimum priority relative to \c Compare.
1.130 + ///
1.131 + /// This method returns the item with minimum priority relative to \c
1.132 + /// Compare.
1.133 + /// \pre The heap must be nonempty.
1.134 Item top() const {
1.135 return data[0].first;
1.136 }
1.137
1.138 - ///Returns the minimum priority relative to \c Compare.
1.139 -
1.140 - /**
1.141 - It returns the minimum priority relative to \c Compare.
1.142 - \pre The heap must be nonempty.
1.143 - */
1.144 + /// \brief Returns the minimum priority relative to \c Compare.
1.145 + ///
1.146 + /// It returns the minimum priority relative to \c Compare.
1.147 + /// \pre The heap must be nonempty.
1.148 Prio prio() const {
1.149 return data[0].second;
1.150 }
1.151
1.152 - ///Deletes the item with minimum priority relative to \c Compare.
1.153 -
1.154 - /**
1.155 - This method deletes the item with minimum priority relative to \c
1.156 - Compare from the heap.
1.157 - \pre The heap must be non-empty.
1.158 - */
1.159 + /// \brief Deletes the item with minimum priority relative to \c Compare.
1.160 + ///
1.161 + /// This method deletes the item with minimum priority relative to \c
1.162 + /// Compare from the heap.
1.163 + /// \pre The heap must be non-empty.
1.164 void pop() {
1.165 rmidx(0);
1.166 }
1.167
1.168 - ///Deletes \c i from the heap.
1.169 -
1.170 - /**
1.171 - This method deletes item \c i from the heap, if \c i was
1.172 - already stored in the heap.
1.173 - */
1.174 + /// \brief Deletes \c i from the heap.
1.175 + ///
1.176 + /// This method deletes item \c i from the heap, if \c i was
1.177 + /// already stored in the heap.
1.178 + /// \param i The item to erase.
1.179 void erase(const Item &i) {
1.180 rmidx(iim[i]);
1.181 }
1.182
1.183
1.184 - ///Returns the priority of \c i.
1.185 -
1.186 - /**
1.187 - This function returns the priority of item \c i.
1.188 - \pre \c i must be in the heap.
1.189 - */
1.190 + /// \brief Returns the priority of \c i.
1.191 + ///
1.192 + /// This function returns the priority of item \c i.
1.193 + /// \pre \c i must be in the heap.
1.194 + /// \param i The item.
1.195 Prio operator[](const Item &i) const {
1.196 int idx = iim[i];
1.197 return data[idx].second;
1.198 }
1.199
1.200 - ///\c i gets to the heap with priority \c p independently if \c i was already there.
1.201 -
1.202 - /**
1.203 - This method calls \ref push(\c i, \c p) if \c i is not stored
1.204 - in the heap and sets the priority of \c i to \c p otherwise.
1.205 - */
1.206 + /// \brief \c i gets to the heap with priority \c p independently
1.207 + /// if \c i was already there.
1.208 + ///
1.209 + /// This method calls \ref push(\c i, \c p) if \c i is not stored
1.210 + /// in the heap and sets the priority of \c i to \c p otherwise.
1.211 + /// \param i The item.
1.212 + /// \param p The priority.
1.213 void set(const Item &i, const Prio &p) {
1.214 int idx = iim[i];
1.215 if( idx < 0 ) {
1.216 @@ -235,38 +219,38 @@
1.217 }
1.218 }
1.219
1.220 - ///Decreases the priority of \c i to \c p.
1.221 + /// \brief Decreases the priority of \c i to \c p.
1.222
1.223 - /**
1.224 - This method decreases the priority of item \c i to \c p.
1.225 - \pre \c i must be stored in the heap with priority at least \c
1.226 - p relative to \c Compare.
1.227 - */
1.228 + /// This method decreases the priority of item \c i to \c p.
1.229 + /// \pre \c i must be stored in the heap with priority at least \c
1.230 + /// p relative to \c Compare.
1.231 + /// \param i The item.
1.232 + /// \param p The priority.
1.233 void decrease(const Item &i, const Prio &p) {
1.234 int idx = iim[i];
1.235 bubble_up(idx, PairType(i,p));
1.236 }
1.237
1.238 - ///Increases the priority of \c i to \c p.
1.239 -
1.240 - /**
1.241 - This method sets the priority of item \c i to \c p.
1.242 - \pre \c i must be stored in the heap with priority at most \c
1.243 - p relative to \c Compare.
1.244 - */
1.245 + /// \brief Increases the priority of \c i to \c p.
1.246 + ///
1.247 + /// This method sets the priority of item \c i to \c p.
1.248 + /// \pre \c i must be stored in the heap with priority at most \c
1.249 + /// p relative to \c Compare.
1.250 + /// \param i The item.
1.251 + /// \param p The priority.
1.252 void increase(const Item &i, const Prio &p) {
1.253 int idx = iim[i];
1.254 bubble_down(idx, PairType(i,p), data.size());
1.255 }
1.256
1.257 - ///Returns if \c item is in, has already been in, or has never been in the heap.
1.258 -
1.259 - /**
1.260 - This method returns PRE_HEAP if \c item has never been in the
1.261 - heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
1.262 - otherwise. In the latter case it is possible that \c item will
1.263 - get back to the heap again.
1.264 - */
1.265 + /// \brief Returns if \c item is in, has already been in, or has
1.266 + /// never been in the heap.
1.267 + ///
1.268 + /// This method returns PRE_HEAP if \c item has never been in the
1.269 + /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
1.270 + /// otherwise. In the latter case it is possible that \c item will
1.271 + /// get back to the heap again.
1.272 + /// \param i The item.
1.273 state_enum state(const Item &i) const {
1.274 int s = iim[i];
1.275 if( s>=0 )
2.1 --- a/src/lemon/radix_heap.h Sat Apr 09 19:27:48 2005 +0000
2.2 +++ b/src/lemon/radix_heap.h Sat Apr 09 19:30:49 2005 +0000
2.3 @@ -1,5 +1,5 @@
2.4 /* -*- C++ -*-
2.5 - * src/lemon/bin_heap.h - Part of LEMON, a generic C++ optimization library
2.6 + * src/lemon/radix_heap.h - Part of LEMON, a generic C++ optimization library
2.7 *
2.8 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
2.9 * (Egervary Combinatorial Optimization Research Group, EGRES).
2.10 @@ -30,39 +30,54 @@
2.11 /// \addtogroup auxdat
2.12 /// @{
2.13
2.14 - /// A Radix Heap implementation.
2.15 -
2.16 - ///\todo Please document...
2.17 - ///
2.18 - ///\sa BinHeap
2.19 - ///\sa Dijkstra
2.20 + /// \brief Exception thrown by RadixHeap.
2.21 + ///
2.22 + /// This Exception is thrown when a smaller priority
2.23 + /// is inserted into the \e RadixHeap then the last time erased.
2.24 + /// \see RadixHeap
2.25 + /// \author Balazs Dezso
2.26
2.27 - class UnderFlowPriorityException : public RuntimeError {
2.28 + class UnderFlowPriorityError : public RuntimeError {
2.29 public:
2.30 virtual const char* exceptionName() const {
2.31 - return "lemon::UnderFlowPriorityException";
2.32 + return "lemon::UnderFlowPriorityError";
2.33 }
2.34 };
2.35
2.36 + /// \brief A Radix Heap implementation.
2.37 + ///
2.38 + /// This class implements the \e radix \e heap data structure. A \e heap
2.39 + /// is a data structure for storing items with specified values called \e
2.40 + /// priorities in such a way that finding the item with minimum priority is
2.41 + /// efficient. This heap type can store only items with \e int priority.
2.42 + /// In a heap one can change the priority of an item, add or erase an
2.43 + /// item, but the priority cannot be decreased under the last removed
2.44 + /// item's priority.
2.45 + ///
2.46 + /// \param _Item Type of the items to be stored.
2.47 + /// \param _ItemIntMap A read and writable Item int map, used internally
2.48 + /// to handle the cross references.
2.49 + ///
2.50 + /// \see BinHeap
2.51 + /// \see Dijkstra
2.52 + /// \author Balazs Dezso
2.53 +
2.54 template <typename _Item, typename _ItemIntMap>
2.55 class RadixHeap {
2.56
2.57 public:
2.58 typedef _Item Item;
2.59 - // FIXME: stl-ben nem ezt hivjak value_type -nak, hanem a kovetkezot...
2.60 typedef int Prio;
2.61 typedef _ItemIntMap ItemIntMap;
2.62
2.63 - /**
2.64 - * Each Item element have a state associated to it. It may be "in heap",
2.65 - * "pre heap" or "post heap". The later two are indifferent from the
2.66 - * heap's point of view, but may be useful to the user.
2.67 - *
2.68 - * The ItemIntMap _should_ be initialized in such way, that it maps
2.69 - * PRE_HEAP (-1) to any element to be put in the heap...
2.70 - */
2.71 - ///\todo it is used nowhere
2.72 + /// \brief Type to represent the items states.
2.73 ///
2.74 + /// Each Item element have a state associated to it. It may be "in heap",
2.75 + /// "pre heap" or "post heap". The later two are indifferent from the
2.76 + /// heap's point of view, but may be useful to the user.
2.77 + ///
2.78 + /// The ItemIntMap _should_ be initialized in such way, that it maps
2.79 + /// PRE_HEAP (-1) to any element to be put in the heap...
2.80 enum state_enum {
2.81 IN_HEAP = 0,
2.82 PRE_HEAP = -1,
2.83 @@ -91,13 +106,26 @@
2.84
2.85
2.86 public:
2.87 - ///\e
2.88 + /// \brief The constructor.
2.89 + ///
2.90 + /// The constructor.
2.91 + /// \param _iim should be given to the constructor, since it is used
2.92 + /// internally to handle the cross references. The value of the map
2.93 + /// should be PRE_HEAP (-1) for each element.
2.94 explicit RadixHeap(ItemIntMap &_iim) : iim(_iim) {
2.95 boxes.push_back(RadixBox(0, 1));
2.96 boxes.push_back(RadixBox(1, 1));
2.97 }
2.98
2.99 - ///\e
2.100 + /// \brief The constructor.
2.101 + ///
2.102 + /// The constructor.
2.103 + ///
2.104 + /// \param _iim It should be given to the constructor, since it is used
2.105 + /// internally to handle the cross references. The value of the map
2.106 + /// should be PRE_HEAP (-1) for each element.
2.107 + ///
2.108 + /// \param capacity It determines the initial capacity of the heap.
2.109 RadixHeap(ItemIntMap &_iim, int capacity) : iim(_iim) {
2.110 boxes.push_back(RadixBox(0, 1));
2.111 boxes.push_back(RadixBox(1, 1));
2.112 @@ -106,9 +134,13 @@
2.113 }
2.114 }
2.115
2.116 - ///\e
2.117 + /// The number of items stored in the heap.
2.118 + ///
2.119 + /// \brief Returns the number of items stored in the heap.
2.120 int size() const { return data.size(); }
2.121 - ///\e
2.122 + /// \brief Checks if the heap stores no items.
2.123 + ///
2.124 + /// Returns \c true if and only if the heap stores no items.
2.125 bool empty() const { return data.empty(); }
2.126
2.127 private:
2.128 @@ -183,7 +215,7 @@
2.129 /// \brief Find up the proper box for the item with the given prio.
2.130 int findDown(int start, int prio) {
2.131 while (upper(start, prio)) {
2.132 - if (--start < 0) throw UnderFlowPriorityException();
2.133 + if (--start < 0) throw UnderFlowPriorityError();
2.134 }
2.135 return start;
2.136 }
2.137 @@ -207,7 +239,6 @@
2.138 /// \brief Rearrange the items of the heap and makes the
2.139 /// first box not empty.
2.140 void moveDown() {
2.141 - // print(); printf("moveDown\n"); fflush(stdout);
2.142 int box = findFirst();
2.143 if (box == 0) return;
2.144 int min = minValue(box);
2.145 @@ -241,9 +272,12 @@
2.146
2.147 public:
2.148
2.149 - ///\e
2.150 + /// \brief Insert an item into the heap with the given heap.
2.151 + ///
2.152 + /// Adds \c i to the heap with priority \c p.
2.153 + /// \param i The item to insert.
2.154 + /// \param p The priority of the item.
2.155 void push(const Item &i, const Prio &p) {
2.156 - fflush(stdout);
2.157 int n = data.size();
2.158 iim.set(i, n);
2.159 data.push_back(RadixItem(i, p));
2.160 @@ -252,38 +286,43 @@
2.161 }
2.162 int box = findDown(boxes.size() - 1, p);
2.163 insert(box, n);
2.164 - // printf("Push %d\n", p);
2.165 - //print();
2.166 }
2.167
2.168 - ///\e
2.169 + /// \brief Returns the item with minimum priority.
2.170 + ///
2.171 + /// This method returns the item with minimum priority.
2.172 + /// \pre The heap must be nonempty.
2.173 Item top() const {
2.174 - // print(); printf("top\n"); fflush(stdout);
2.175 const_cast<RadixHeap<Item, ItemIntMap>*>(this)->moveDown();
2.176 return data[boxes[0].first].item;
2.177 - // print(); printf("top_end\n"); fflush(stdout);
2.178 }
2.179
2.180 - /// Returns the prio of the top element of the heap.
2.181 + /// \brief Returns the minimum priority.
2.182 + ///
2.183 + /// It returns the minimum priority.
2.184 + /// \pre The heap must be nonempty.
2.185 Prio prio() const {
2.186 - // print(); printf("prio\n"); fflush(stdout);
2.187 const_cast<RadixHeap<Item, ItemIntMap>*>(this)->moveDown();
2.188 return data[boxes[0].first].prio;
2.189 }
2.190
2.191 - ///\e
2.192 + /// \brief Deletes the item with minimum priority.
2.193 + ///
2.194 + /// This method deletes the item with minimum priority.
2.195 + /// \pre The heap must be non-empty.
2.196 void pop() {
2.197 - // print(); printf("pop\n"); fflush(stdout);
2.198 moveDown();
2.199 int index = boxes[0].first;
2.200 iim[data[index].item] = POST_HEAP;
2.201 remove(index);
2.202 relocate_last(index);
2.203 - // printf("Pop \n");
2.204 - //print();
2.205 }
2.206
2.207 - ///\e
2.208 + /// \brief Deletes \c i from the heap.
2.209 + ///
2.210 + /// This method deletes item \c i from the heap, if \c i was
2.211 + /// already stored in the heap.
2.212 + /// \param i The item to erase.
2.213 void erase(const Item &i) {
2.214 int index = iim[i];
2.215 iim[i] = POST_HEAP;
2.216 @@ -291,13 +330,24 @@
2.217 relocate_last(index);
2.218 }
2.219
2.220 - ///\e
2.221 + /// \brief Returns the priority of \c i.
2.222 + ///
2.223 + /// This function returns the priority of item \c i.
2.224 + /// \pre \c i must be in the heap.
2.225 + /// \param i The item.
2.226 Prio operator[](const Item &i) const {
2.227 int idx = iim[i];
2.228 return data[idx].prio;
2.229 }
2.230
2.231 - ///\e
2.232 + /// \brief \c i gets to the heap with priority \c p independently
2.233 + /// if \c i was already there.
2.234 + ///
2.235 + /// This method calls \ref push(\c i, \c p) if \c i is not stored
2.236 + /// in the heap and sets the priority of \c i to \c p otherwise.
2.237 + /// It may throw an \e UnderFlowPriorityException.
2.238 + /// \param i The item.
2.239 + /// \param p The priority.
2.240 void set(const Item &i, const Prio &p) {
2.241 int idx = iim[i];
2.242 if( idx < 0 ) {
2.243 @@ -312,39 +362,47 @@
2.244 }
2.245 }
2.246
2.247 - ///\e
2.248 +
2.249 + /// \brief Decreases the priority of \c i to \c p.
2.250 + ///
2.251 + /// This method decreases the priority of item \c i to \c p.
2.252 + /// \pre \c i must be stored in the heap with priority at least \c p, and
2.253 + /// \c should be greater then the last removed item's priority.
2.254 + /// \param i The item.
2.255 + /// \param p The priority.
2.256 void decrease(const Item &i, const Prio &p) {
2.257 - // print(); printf("decrease\n"); fflush(stdout);
2.258 int idx = iim[i];
2.259 data[idx].prio = p;
2.260 bubble_down(idx);
2.261 }
2.262
2.263 - ///\e
2.264 + /// \brief Increases the priority of \c i to \c p.
2.265 + ///
2.266 + /// This method sets the priority of item \c i to \c p.
2.267 + /// \pre \c i must be stored in the heap with priority at most \c
2.268 + /// p relative to \c Compare.
2.269 + /// \param i The item.
2.270 + /// \param p The priority.
2.271 void increase(const Item &i, const Prio &p) {
2.272 int idx = iim[i];
2.273 data[idx].prio = p;
2.274 bubble_up(idx);
2.275 }
2.276
2.277 - ///\e
2.278 + /// \brief Returns if \c item is in, has already been in, or has
2.279 + /// never been in the heap.
2.280 + ///
2.281 + /// This method returns PRE_HEAP if \c item has never been in the
2.282 + /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
2.283 + /// otherwise. In the latter case it is possible that \c item will
2.284 + /// get back to the heap again.
2.285 + /// \param i The item.
2.286 state_enum state(const Item &i) const {
2.287 int s = iim[i];
2.288 if( s >= 0 ) s = 0;
2.289 return state_enum(s);
2.290 }
2.291
2.292 -// void print() const {
2.293 -// for (int i = 0; i < boxes.size(); ++i) {
2.294 -// printf("(%d, %d) ", boxes[i].min, boxes[i].size);
2.295 -// for (int k = boxes[i].first; k != -1; k = data[k].next) {
2.296 -// printf("%d ", data[k].prio);
2.297 -// }
2.298 -// printf("\n");
2.299 -// }
2.300 -// fflush(stdout);
2.301 -// }
2.302 -
2.303 }; // class RadixHeap
2.304
2.305