1.1 --- a/lemon/concepts/heap.h Fri Aug 09 11:07:27 2013 +0200
1.2 +++ b/lemon/concepts/heap.h Sun Aug 11 15:28:12 2013 +0200
1.3 @@ -2,7 +2,7 @@
1.4 *
1.5 * This file is a part of LEMON, a generic C++ optimization library.
1.6 *
1.7 - * Copyright (C) 2003-2009
1.8 + * Copyright (C) 2003-2010
1.9 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.10 * (Egervary Research Group on Combinatorial Optimization, EGRES).
1.11 *
1.12 @@ -16,13 +16,13 @@
1.13 *
1.14 */
1.15
1.16 +#ifndef LEMON_CONCEPTS_HEAP_H
1.17 +#define LEMON_CONCEPTS_HEAP_H
1.18 +
1.19 ///\ingroup concept
1.20 ///\file
1.21 ///\brief The concept of heaps.
1.22
1.23 -#ifndef LEMON_CONCEPTS_HEAP_H
1.24 -#define LEMON_CONCEPTS_HEAP_H
1.25 -
1.26 #include <lemon/core.h>
1.27 #include <lemon/concept_check.h>
1.28
1.29 @@ -35,21 +35,27 @@
1.30
1.31 /// \brief The heap concept.
1.32 ///
1.33 - /// Concept class describing the main interface of heaps. A \e heap
1.34 - /// is a data structure for storing items with specified values called
1.35 - /// \e priorities in such a way that finding the item with minimum
1.36 - /// priority is efficient. In a heap one can change the priority of an
1.37 - /// item, add or erase an item, etc.
1.38 + /// This concept class describes the main interface of heaps.
1.39 + /// The various \ref heaps "heap structures" are efficient
1.40 + /// implementations of the abstract data type \e priority \e queue.
1.41 + /// They store items with specified values called \e priorities
1.42 + /// in such a way that finding and removing the item with minimum
1.43 + /// priority are efficient. The basic operations are adding and
1.44 + /// erasing items, changing the priority of an item, etc.
1.45 ///
1.46 - /// \tparam PR Type of the priority of the items.
1.47 - /// \tparam IM A read and writable item map with int values, used
1.48 + /// Heaps are crucial in several algorithms, such as Dijkstra and Prim.
1.49 + /// Any class that conforms to this concept can be used easily in such
1.50 + /// algorithms.
1.51 + ///
1.52 + /// \tparam PR Type of the priorities of the items.
1.53 + /// \tparam IM A read-writable item map with \c int values, used
1.54 /// internally to handle the cross references.
1.55 - /// \tparam Comp A functor class for the ordering of the priorities.
1.56 + /// \tparam CMP A functor class for comparing the priorities.
1.57 /// The default is \c std::less<PR>.
1.58 #ifdef DOXYGEN
1.59 - template <typename PR, typename IM, typename Comp = std::less<PR> >
1.60 + template <typename PR, typename IM, typename CMP>
1.61 #else
1.62 - template <typename PR, typename IM>
1.63 + template <typename PR, typename IM, typename CMP = std::less<PR> >
1.64 #endif
1.65 class Heap {
1.66 public:
1.67 @@ -64,109 +70,157 @@
1.68 /// \brief Type to represent the states of the items.
1.69 ///
1.70 /// Each item has a state associated to it. It can be "in heap",
1.71 - /// "pre heap" or "post heap". The later two are indifferent
1.72 - /// from the point of view of the heap, but may be useful for
1.73 - /// the user.
1.74 + /// "pre-heap" or "post-heap". The latter two are indifferent from the
1.75 + /// heap's point of view, but may be useful to the user.
1.76 ///
1.77 /// The item-int map must be initialized in such way that it assigns
1.78 /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
1.79 enum State {
1.80 IN_HEAP = 0, ///< = 0. The "in heap" state constant.
1.81 - PRE_HEAP = -1, ///< = -1. The "pre heap" state constant.
1.82 - POST_HEAP = -2 ///< = -2. The "post heap" state constant.
1.83 + PRE_HEAP = -1, ///< = -1. The "pre-heap" state constant.
1.84 + POST_HEAP = -2 ///< = -2. The "post-heap" state constant.
1.85 };
1.86
1.87 - /// \brief The constructor.
1.88 + /// \brief Constructor.
1.89 ///
1.90 - /// The constructor.
1.91 + /// Constructor.
1.92 /// \param map A map that assigns \c int values to keys of type
1.93 /// \c Item. It is used internally by the heap implementations to
1.94 /// handle the cross references. The assigned value must be
1.95 - /// \c PRE_HEAP (<tt>-1</tt>) for every item.
1.96 + /// \c PRE_HEAP (<tt>-1</tt>) for each item.
1.97 +#ifdef DOXYGEN
1.98 explicit Heap(ItemIntMap &map) {}
1.99 +#else
1.100 + explicit Heap(ItemIntMap&) {}
1.101 +#endif
1.102 +
1.103 + /// \brief Constructor.
1.104 + ///
1.105 + /// Constructor.
1.106 + /// \param map A map that assigns \c int values to keys of type
1.107 + /// \c Item. It is used internally by the heap implementations to
1.108 + /// handle the cross references. The assigned value must be
1.109 + /// \c PRE_HEAP (<tt>-1</tt>) for each item.
1.110 + /// \param comp The function object used for comparing the priorities.
1.111 +#ifdef DOXYGEN
1.112 + explicit Heap(ItemIntMap &map, const CMP &comp) {}
1.113 +#else
1.114 + explicit Heap(ItemIntMap&, const CMP&) {}
1.115 +#endif
1.116
1.117 /// \brief The number of items stored in the heap.
1.118 ///
1.119 - /// 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 0; }
1.122
1.123 - /// \brief Checks if the heap is empty.
1.124 + /// \brief Check if the heap is empty.
1.125 ///
1.126 - /// Returns \c true if the heap is empty.
1.127 + /// This function returns \c true if the heap is empty.
1.128 bool empty() const { return false; }
1.129
1.130 - /// \brief Makes the heap empty.
1.131 + /// \brief Make the heap empty.
1.132 ///
1.133 - /// Makes the heap empty.
1.134 - void clear();
1.135 + /// This functon makes the heap empty.
1.136 + /// It does not change the cross reference map. If you want to reuse
1.137 + /// a heap that is not surely empty, you should first clear it and
1.138 + /// then you should set the cross reference map to \c PRE_HEAP
1.139 + /// for each item.
1.140 + void clear() {}
1.141
1.142 - /// \brief Inserts an item into the heap with the given priority.
1.143 + /// \brief Insert an item into the heap with the given priority.
1.144 ///
1.145 - /// Inserts the given item into the heap with the given priority.
1.146 + /// This function inserts the given item into the heap with the
1.147 + /// given priority.
1.148 /// \param i The item to insert.
1.149 /// \param p The priority of the item.
1.150 + /// \pre \e i must not be stored in the heap.
1.151 +#ifdef DOXYGEN
1.152 void push(const Item &i, const Prio &p) {}
1.153 +#else
1.154 + void push(const Item&, const Prio&) {}
1.155 +#endif
1.156
1.157 - /// \brief Returns the item having minimum priority.
1.158 + /// \brief Return the item having minimum priority.
1.159 ///
1.160 - /// Returns the item having minimum priority.
1.161 + /// This function returns the item having minimum priority.
1.162 /// \pre The heap must be non-empty.
1.163 - Item top() const {}
1.164 + Item top() const { return Item(); }
1.165
1.166 /// \brief The minimum priority.
1.167 ///
1.168 - /// Returns the minimum priority.
1.169 + /// This function returns the minimum priority.
1.170 /// \pre The heap must be non-empty.
1.171 - Prio prio() const {}
1.172 + Prio prio() const { return Prio(); }
1.173
1.174 - /// \brief Removes the item having minimum priority.
1.175 + /// \brief Remove the item having minimum priority.
1.176 ///
1.177 - /// Removes the item having minimum priority.
1.178 + /// This function removes the item having minimum priority.
1.179 /// \pre The heap must be non-empty.
1.180 void pop() {}
1.181
1.182 - /// \brief Removes an item from the heap.
1.183 + /// \brief Remove the given item from the heap.
1.184 ///
1.185 - /// Removes the given item from the heap if it is already stored.
1.186 + /// This function removes the given item from the heap if it is
1.187 + /// already stored.
1.188 /// \param i The item to delete.
1.189 + /// \pre \e i must be in the heap.
1.190 +#ifdef DOXYGEN
1.191 void erase(const Item &i) {}
1.192 +#else
1.193 + void erase(const Item&) {}
1.194 +#endif
1.195
1.196 - /// \brief The priority of an item.
1.197 + /// \brief The priority of the given item.
1.198 ///
1.199 - /// Returns the priority of the given item.
1.200 + /// This function returns the priority of the given item.
1.201 /// \param i The item.
1.202 - /// \pre \c i must be in the heap.
1.203 + /// \pre \e i must be in the heap.
1.204 +#ifdef DOXYGEN
1.205 Prio operator[](const Item &i) const {}
1.206 +#else
1.207 + Prio operator[](const Item&) const { return Prio(); }
1.208 +#endif
1.209
1.210 - /// \brief Sets the priority of an item or inserts it, if it is
1.211 + /// \brief Set the priority of an item or insert it, if it is
1.212 /// not stored in the heap.
1.213 ///
1.214 /// This method sets the priority of the given item if it is
1.215 - /// already stored in the heap.
1.216 - /// Otherwise it inserts the given item with the given priority.
1.217 + /// already stored in the heap. Otherwise it inserts the given
1.218 + /// item into the heap with the given priority.
1.219 ///
1.220 /// \param i The item.
1.221 /// \param p The priority.
1.222 +#ifdef DOXYGEN
1.223 void set(const Item &i, const Prio &p) {}
1.224 +#else
1.225 + void set(const Item&, const Prio&) {}
1.226 +#endif
1.227
1.228 - /// \brief Decreases the priority of an item to the given value.
1.229 + /// \brief Decrease the priority of an item to the given value.
1.230 ///
1.231 - /// Decreases the priority of an item to the given value.
1.232 + /// This function decreases the priority of an item to the given value.
1.233 /// \param i The item.
1.234 /// \param p The priority.
1.235 - /// \pre \c i must be stored in the heap with priority at least \c p.
1.236 + /// \pre \e i must be stored in the heap with priority at least \e p.
1.237 +#ifdef DOXYGEN
1.238 void decrease(const Item &i, const Prio &p) {}
1.239 +#else
1.240 + void decrease(const Item&, const Prio&) {}
1.241 +#endif
1.242
1.243 - /// \brief Increases the priority of an item to the given value.
1.244 + /// \brief Increase the priority of an item to the given value.
1.245 ///
1.246 - /// Increases the priority of an item to the given value.
1.247 + /// This function increases the priority of an item to the given value.
1.248 /// \param i The item.
1.249 /// \param p The priority.
1.250 - /// \pre \c i must be stored in the heap with priority at most \c p.
1.251 + /// \pre \e i must be stored in the heap with priority at most \e p.
1.252 +#ifdef DOXYGEN
1.253 void increase(const Item &i, const Prio &p) {}
1.254 +#else
1.255 + void increase(const Item&, const Prio&) {}
1.256 +#endif
1.257
1.258 - /// \brief Returns if an item is in, has already been in, or has
1.259 - /// never been in the heap.
1.260 + /// \brief Return the state of an item.
1.261 ///
1.262 /// This method returns \c PRE_HEAP if the given item has never
1.263 /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
1.264 @@ -174,16 +228,24 @@
1.265 /// In the latter case it is possible that the item will get back
1.266 /// to the heap again.
1.267 /// \param i The item.
1.268 +#ifdef DOXYGEN
1.269 State state(const Item &i) const {}
1.270 +#else
1.271 + State state(const Item&) const { return PRE_HEAP; }
1.272 +#endif
1.273
1.274 - /// \brief Sets the state of an item in the heap.
1.275 + /// \brief Set the state of an item in the heap.
1.276 ///
1.277 - /// Sets the state of the given item in the heap. It can be used
1.278 - /// to manually clear the heap when it is important to achive the
1.279 - /// better time complexity.
1.280 + /// This function sets the state of the given item in the heap.
1.281 + /// It can be used to manually clear the heap when it is important
1.282 + /// to achive better time complexity.
1.283 /// \param i The item.
1.284 /// \param st The state. It should not be \c IN_HEAP.
1.285 +#ifdef DOXYGEN
1.286 void state(const Item& i, State st) {}
1.287 +#else
1.288 + void state(const Item&, State) {}
1.289 +#endif
1.290
1.291
1.292 template <typename _Heap>