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