lemon/concepts/heap.h
changeset 100 4f754b4cf82b
child 113 18a7ee8fa56e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lemon/concepts/heap.h	Thu Feb 07 21:37:07 2008 +0000
     1.3 @@ -0,0 +1,226 @@
     1.4 +/* -*- C++ -*-
     1.5 + *
     1.6 + * This file is a part of LEMON, a generic C++ optimization library
     1.7 + *
     1.8 + * Copyright (C) 2003-2008
     1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    1.11 + *
    1.12 + * Permission to use, modify and distribute this software is granted
    1.13 + * provided that this copyright notice appears in all copies. For
    1.14 + * precise terms see the accompanying LICENSE file.
    1.15 + *
    1.16 + * This software is provided "AS IS" with no warranty of any kind,
    1.17 + * express or implied, and with no claim as to its suitability for any
    1.18 + * purpose.
    1.19 + *
    1.20 + */
    1.21 +
    1.22 +///\ingroup concept
    1.23 +///\file
    1.24 +///\brief Classes for representing heaps.
    1.25 +///
    1.26 +
    1.27 +#ifndef LEMON_CONCEPT_HEAP_H
    1.28 +#define LEMON_CONCEPT_HEAP_H
    1.29 +
    1.30 +#include <lemon/bits/invalid.h>
    1.31 +
    1.32 +namespace lemon {
    1.33 +  namespace concepts {
    1.34 +    /// \addtogroup concept
    1.35 +    /// @{
    1.36 +
    1.37 +
    1.38 +    /// \brief A concept structure describes the main interface of heaps.
    1.39 +    ///
    1.40 +    /// A concept structure describes the main interface of heaps.
    1.41 +    ///
    1.42 +    template <typename Prio, typename ItemIntMap>
    1.43 +    class Heap {
    1.44 +    public:
    1.45 +
    1.46 +      ///\brief Type of the items stored in the heap.
    1.47 +      typedef typename ItemIntMap::Key  Item;
    1.48 +  
    1.49 +
    1.50 +      /// \brief Type to represent the items states.
    1.51 +      ///
    1.52 +      /// Each Item element have a state associated to it. It may be "in heap",
    1.53 +      /// "pre heap" or "post heap". The later two are indifferent from the
    1.54 +      /// heap's point of view, but may be useful to the user.
    1.55 +      ///
    1.56 +      /// The ItemIntMap _should_ be initialized in such way, that it maps
    1.57 +      /// PRE_HEAP (-1) to any element to be put in the heap...
    1.58 +      enum State {
    1.59 +	IN_HEAP = 0,
    1.60 +	PRE_HEAP = -1,
    1.61 +	POST_HEAP = -2
    1.62 +      };
    1.63 +      
    1.64 +      /// \brief The constructor.
    1.65 +      ///
    1.66 +      /// The constructor.
    1.67 +      /// \param _iim should be given to the constructor, since it is used
    1.68 +      /// internally to handle the cross references. The value of the map
    1.69 +      /// should be PRE_HEAP (-1) for each element.
    1.70 +      explicit Heap(ItemIntMap &_iim) {}
    1.71 +
    1.72 +      /// \brief The number of items stored in the heap.
    1.73 +      ///
    1.74 +      /// Returns the number of items stored in the heap.
    1.75 +      int size() const { return 0; }
    1.76 +
    1.77 +      /// \brief Checks if the heap stores no items.
    1.78 +      ///
    1.79 +      /// Returns \c true if and only if the heap stores no items.
    1.80 +      bool empty() const { return false; }
    1.81 +
    1.82 +      /// \brief Makes empty this heap.
    1.83 +      ///
    1.84 +      /// Makes this heap empty.
    1.85 +      void clear();
    1.86 +
    1.87 +      /// \brief Insert an item into the heap with the given heap.
    1.88 +      ///    
    1.89 +      /// Adds \c i to the heap with priority \c p. 
    1.90 +      /// \param i The item to insert.
    1.91 +      /// \param p The priority of the item.
    1.92 +      void push(const Item &i, const Prio &p) {}
    1.93 +
    1.94 +      /// \brief Returns the item with minimum priority.
    1.95 +      ///
    1.96 +      /// This method returns the item with minimum priority.  
    1.97 +      /// \pre The heap must be nonempty.  
    1.98 +      Item top() const {}
    1.99 +
   1.100 +      /// \brief Returns the minimum priority.
   1.101 +      ///
   1.102 +      /// It returns the minimum priority.
   1.103 +      /// \pre The heap must be nonempty.
   1.104 +      Prio prio() const {}
   1.105 +
   1.106 +      /// \brief Deletes the item with minimum priority.
   1.107 +      ///
   1.108 +      /// This method deletes the item with minimum priority.
   1.109 +      /// \pre The heap must be non-empty.  
   1.110 +      void pop() {}
   1.111 +
   1.112 +      /// \brief Deletes \c i from the heap.
   1.113 +      ///
   1.114 +      /// This method deletes item \c i from the heap, if \c i was
   1.115 +      /// already stored in the heap.
   1.116 +      /// \param i The item to erase. 
   1.117 +      void erase(const Item &i) {}
   1.118 +
   1.119 +      /// \brief Returns the priority of \c i.
   1.120 +      ///
   1.121 +      /// This function returns the priority of item \c i.  
   1.122 +      /// \pre \c i must be in the heap.
   1.123 +      /// \param i The item.
   1.124 +      Prio operator[](const Item &i) const {}
   1.125 +
   1.126 +      /// \brief \c i gets to the heap with priority \c p independently 
   1.127 +      /// if \c i was already there.
   1.128 +      ///
   1.129 +      /// This method calls \ref push(\c i, \c p) if \c i is not stored
   1.130 +      /// in the heap and sets the priority of \c i to \c p otherwise.
   1.131 +      /// It may throw an \e UnderFlowPriorityException. 
   1.132 +      /// \param i The item.
   1.133 +      /// \param p The priority.
   1.134 +      void set(const Item &i, const Prio &p) {}
   1.135 +      
   1.136 +      /// \brief Decreases the priority of \c i to \c p.
   1.137 +      ///
   1.138 +      /// This method decreases the priority of item \c i to \c p.
   1.139 +      /// \pre \c i must be stored in the heap with priority at least \c p.
   1.140 +      /// \param i The item.
   1.141 +      /// \param p The priority.
   1.142 +      void decrease(const Item &i, const Prio &p) {}
   1.143 +
   1.144 +      /// \brief Increases the priority of \c i to \c p.
   1.145 +      ///
   1.146 +      /// This method sets the priority of item \c i to \c p. 
   1.147 +      /// \pre \c i must be stored in the heap with priority at most \c
   1.148 +      /// p relative to \c Compare.
   1.149 +      /// \param i The item.
   1.150 +      /// \param p The priority.
   1.151 +      void increase(const Item &i, const Prio &p) {}
   1.152 +
   1.153 +      /// \brief Returns if \c item is in, has already been in, or has 
   1.154 +      /// never been in the heap.
   1.155 +      ///
   1.156 +      /// This method returns PRE_HEAP if \c item has never been in the
   1.157 +      /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
   1.158 +      /// otherwise. In the latter case it is possible that \c item will
   1.159 +      /// get back to the heap again.
   1.160 +      /// \param i The item.
   1.161 +      State state(const Item &i) const {}
   1.162 +
   1.163 +      /// \brief Sets the state of the \c item in the heap.
   1.164 +      ///
   1.165 +      /// Sets the state of the \c item in the heap. It can be used to
   1.166 +      /// manually clear the heap when it is important to achive the
   1.167 +      /// better time complexity.
   1.168 +      /// \param i The item.
   1.169 +      /// \param st The state. It should not be \c IN_HEAP. 
   1.170 +      void state(const Item& i, State st) {}
   1.171 +
   1.172 +
   1.173 +      template <typename _Heap>
   1.174 +      struct Constraints {
   1.175 +      public:
   1.176 +    
   1.177 +	void constraints() {
   1.178 +	  Item item;
   1.179 +	  Prio prio;
   1.180 +
   1.181 +	  item=Item();
   1.182 +	  prio=Prio();
   1.183 +
   1.184 +	  ignore_unused_variable_warning(item);
   1.185 +	  ignore_unused_variable_warning(prio);
   1.186 +
   1.187 +	  typedef typename _Heap::State State;
   1.188 +	  State state;
   1.189 +
   1.190 +	  ignore_unused_variable_warning(state);
   1.191 +      
   1.192 +	  _Heap heap1 = _Heap(map);
   1.193 +
   1.194 +	  ignore_unused_variable_warning(heap1);
   1.195 +      
   1.196 +	  heap.push(item, prio);
   1.197 +
   1.198 +	  prio = heap.prio();
   1.199 +	  item = heap.top();
   1.200 +
   1.201 +	  heap.pop();
   1.202 +
   1.203 +	  heap.set(item, prio);
   1.204 +	  heap.decrease(item, prio);
   1.205 +	  heap.increase(item, prio);
   1.206 +	  prio = heap[item];
   1.207 +
   1.208 +	  heap.erase(item);
   1.209 +
   1.210 +	  state = heap.state(item);
   1.211 +
   1.212 +	  state = _Heap::PRE_HEAP;
   1.213 +	  state = _Heap::IN_HEAP;
   1.214 +	  state = _Heap::POST_HEAP;
   1.215 +
   1.216 +	  heap.clear();
   1.217 +	}
   1.218 +    
   1.219 +	_Heap& heap;
   1.220 +	ItemIntMap& map;
   1.221 +
   1.222 +	Constraints() : heap(0), map(0) {}
   1.223 +      };
   1.224 +    };
   1.225 +
   1.226 +    /// @}
   1.227 +  } // namespace lemon
   1.228 +}
   1.229 +#endif // LEMON_CONCEPT_PATH_H