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