1.1 --- a/lemon/concepts/heap.h Mon Jan 12 23:11:39 2009 +0100
1.2 +++ b/lemon/concepts/heap.h Thu Nov 05 15:48:01 2009 +0100
1.3 @@ -16,14 +16,15 @@
1.4 *
1.5 */
1.6
1.7 +#ifndef LEMON_CONCEPTS_HEAP_H
1.8 +#define LEMON_CONCEPTS_HEAP_H
1.9 +
1.10 ///\ingroup concept
1.11 ///\file
1.12 ///\brief The concept of heaps.
1.13
1.14 -#ifndef LEMON_CONCEPT_HEAP_H
1.15 -#define LEMON_CONCEPT_HEAP_H
1.16 -
1.17 #include <lemon/core.h>
1.18 +#include <lemon/concept_check.h>
1.19
1.20 namespace lemon {
1.21
1.22 @@ -34,123 +35,160 @@
1.23
1.24 /// \brief The heap concept.
1.25 ///
1.26 - /// Concept class describing the main interface of heaps.
1.27 - template <typename Priority, typename ItemIntMap>
1.28 + /// This concept class describes the main interface of heaps.
1.29 + /// The various \ref heaps "heap structures" are efficient
1.30 + /// implementations of the abstract data type \e priority \e queue.
1.31 + /// They store items with specified values called \e priorities
1.32 + /// in such a way that finding and removing the item with minimum
1.33 + /// priority are efficient. The basic operations are adding and
1.34 + /// erasing items, changing the priority of an item, etc.
1.35 + ///
1.36 + /// Heaps are crucial in several algorithms, such as Dijkstra and Prim.
1.37 + /// Any class that conforms to this concept can be used easily in such
1.38 + /// algorithms.
1.39 + ///
1.40 + /// \tparam PR Type of the priorities of the items.
1.41 + /// \tparam IM A read-writable item map with \c int values, used
1.42 + /// internally to handle the cross references.
1.43 + /// \tparam CMP A functor class for comparing the priorities.
1.44 + /// The default is \c std::less<PR>.
1.45 +#ifdef DOXYGEN
1.46 + template <typename PR, typename IM, typename CMP>
1.47 +#else
1.48 + template <typename PR, typename IM, typename CMP = std::less<PR> >
1.49 +#endif
1.50 class Heap {
1.51 public:
1.52
1.53 + /// Type of the item-int map.
1.54 + typedef IM ItemIntMap;
1.55 + /// Type of the priorities.
1.56 + typedef PR Prio;
1.57 /// Type of the items stored in the heap.
1.58 typedef typename ItemIntMap::Key Item;
1.59
1.60 - /// Type of the priorities.
1.61 - typedef Priority Prio;
1.62 -
1.63 /// \brief Type to represent the states of the items.
1.64 ///
1.65 /// Each item has a state associated to it. It can be "in heap",
1.66 - /// "pre heap" or "post heap". The later two are indifferent
1.67 - /// from the point of view of the heap, but may be useful for
1.68 - /// the user.
1.69 + /// "pre-heap" or "post-heap". The latter two are indifferent from the
1.70 + /// heap's point of view, but may be useful to the user.
1.71 ///
1.72 - /// The \c ItemIntMap must be initialized in such a way, that it
1.73 - /// assigns \c PRE_HEAP (<tt>-1</tt>) to every item.
1.74 + /// The item-int map must be initialized in such way that it assigns
1.75 + /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
1.76 enum State {
1.77 - IN_HEAP = 0,
1.78 - PRE_HEAP = -1,
1.79 - POST_HEAP = -2
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 };
1.84
1.85 - /// \brief The constructor.
1.86 + /// \brief Constructor.
1.87 ///
1.88 - /// The constructor.
1.89 + /// Constructor.
1.90 /// \param map A map that assigns \c int values to keys of type
1.91 /// \c Item. It is used internally by the heap implementations to
1.92 /// handle the cross references. The assigned value must be
1.93 - /// \c PRE_HEAP (<tt>-1</tt>) for every item.
1.94 + /// \c PRE_HEAP (<tt>-1</tt>) for each item.
1.95 explicit Heap(ItemIntMap &map) {}
1.96
1.97 + /// \brief Constructor.
1.98 + ///
1.99 + /// Constructor.
1.100 + /// \param map A map that assigns \c int values to keys of type
1.101 + /// \c Item. It is used internally by the heap implementations to
1.102 + /// handle the cross references. The assigned value must be
1.103 + /// \c PRE_HEAP (<tt>-1</tt>) for each item.
1.104 + /// \param comp The function object used for comparing the priorities.
1.105 + explicit Heap(ItemIntMap &map, const CMP &comp) {}
1.106 +
1.107 /// \brief The number of items stored in the heap.
1.108 ///
1.109 - /// Returns the number of items stored in the heap.
1.110 + /// This function returns the number of items stored in the heap.
1.111 int size() const { return 0; }
1.112
1.113 - /// \brief Checks if the heap is empty.
1.114 + /// \brief Check if the heap is empty.
1.115 ///
1.116 - /// Returns \c true if the heap is empty.
1.117 + /// This function returns \c true if the heap is empty.
1.118 bool empty() const { return false; }
1.119
1.120 - /// \brief Makes the heap empty.
1.121 + /// \brief Make the heap empty.
1.122 ///
1.123 - /// Makes the heap empty.
1.124 - void clear();
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
1.132 - /// \brief Inserts an item into the heap with the given priority.
1.133 + /// \brief Insert an item into the heap with the given priority.
1.134 ///
1.135 - /// Inserts the given item into the heap with the given priority.
1.136 + /// This function inserts the given item into the heap with the
1.137 + /// given priority.
1.138 /// \param i The item to insert.
1.139 /// \param p The priority of the item.
1.140 + /// \pre \e i must not be stored in the heap.
1.141 void push(const Item &i, const Prio &p) {}
1.142
1.143 - /// \brief Returns the item having minimum priority.
1.144 + /// \brief Return the item having minimum priority.
1.145 ///
1.146 - /// Returns the item having minimum priority.
1.147 + /// This function returns the item having minimum priority.
1.148 /// \pre The heap must be non-empty.
1.149 Item top() const {}
1.150
1.151 /// \brief The minimum priority.
1.152 ///
1.153 - /// Returns the minimum priority.
1.154 + /// This function returns the minimum priority.
1.155 /// \pre The heap must be non-empty.
1.156 Prio prio() const {}
1.157
1.158 - /// \brief Removes the item having minimum priority.
1.159 + /// \brief Remove the item having minimum priority.
1.160 ///
1.161 - /// Removes the item having minimum priority.
1.162 + /// This function removes the item having minimum priority.
1.163 /// \pre The heap must be non-empty.
1.164 void pop() {}
1.165
1.166 - /// \brief Removes an item from the heap.
1.167 + /// \brief Remove the given item from the heap.
1.168 ///
1.169 - /// Removes the given item from the heap if it is already stored.
1.170 + /// This function removes the given item from the heap if it is
1.171 + /// already stored.
1.172 /// \param i The item to delete.
1.173 + /// \pre \e i must be in the heap.
1.174 void erase(const Item &i) {}
1.175
1.176 - /// \brief The priority of an item.
1.177 + /// \brief The priority of the given item.
1.178 ///
1.179 - /// Returns the priority of the given item.
1.180 - /// \pre \c i must be in the heap.
1.181 + /// This function returns the priority of the given item.
1.182 /// \param i The item.
1.183 + /// \pre \e i must be in the heap.
1.184 Prio operator[](const Item &i) const {}
1.185
1.186 - /// \brief Sets the priority of an item or inserts it, if it is
1.187 + /// \brief Set the priority of an item or insert it, if it is
1.188 /// not stored in the heap.
1.189 ///
1.190 /// This method sets the priority of the given item if it is
1.191 - /// already stored in the heap.
1.192 - /// Otherwise it inserts the given item with the given priority.
1.193 + /// already stored in the heap. Otherwise it inserts the given
1.194 + /// item into the heap with the given priority.
1.195 ///
1.196 /// \param i The item.
1.197 /// \param p The priority.
1.198 void set(const Item &i, const Prio &p) {}
1.199
1.200 - /// \brief Decreases the priority of an item to the given value.
1.201 + /// \brief Decrease the priority of an item to the given value.
1.202 ///
1.203 - /// Decreases the priority of an item to the given value.
1.204 - /// \pre \c i must be stored in the heap with priority at least \c p.
1.205 + /// This function decreases the priority of an item to the given value.
1.206 /// \param i The item.
1.207 /// \param p The priority.
1.208 + /// \pre \e i must be stored in the heap with priority at least \e p.
1.209 void decrease(const Item &i, const Prio &p) {}
1.210
1.211 - /// \brief Increases the priority of an item to the given value.
1.212 + /// \brief Increase the priority of an item to the given value.
1.213 ///
1.214 - /// Increases the priority of an item to the given value.
1.215 - /// \pre \c i must be stored in the heap with priority at most \c p.
1.216 + /// This function increases the priority of an item to the given value.
1.217 /// \param i The item.
1.218 /// \param p The priority.
1.219 + /// \pre \e i must be stored in the heap with priority at most \e p.
1.220 void increase(const Item &i, const Prio &p) {}
1.221
1.222 - /// \brief Returns if an item is in, has already been in, or has
1.223 - /// never been in the heap.
1.224 + /// \brief Return the state of an item.
1.225 ///
1.226 /// This method returns \c PRE_HEAP if the given item has never
1.227 /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
1.228 @@ -160,11 +198,11 @@
1.229 /// \param i The item.
1.230 State state(const Item &i) const {}
1.231
1.232 - /// \brief Sets the state of an item in the heap.
1.233 + /// \brief Set the state of an item in the heap.
1.234 ///
1.235 - /// Sets the state of the given item in the heap. It can be used
1.236 - /// to manually clear the heap when it is important to achive the
1.237 - /// better time complexity.
1.238 + /// This function sets the state of the given item in the heap.
1.239 + /// It can be used to manually clear the heap when it is important
1.240 + /// to achive better time complexity.
1.241 /// \param i The item.
1.242 /// \param st The state. It should not be \c IN_HEAP.
1.243 void state(const Item& i, State st) {}
1.244 @@ -242,4 +280,4 @@
1.245 /// @}
1.246 } // namespace lemon
1.247 }
1.248 -#endif // LEMON_CONCEPT_PATH_H
1.249 +#endif