Heap concept moved to namespace concept.
authordeba
Sat, 09 Apr 2005 19:27:48 +0000
changeset 133052ef02524468
parent 1329 1bfaec33215b
child 1331 7e93d3f0406d
Heap concept moved to namespace concept.
src/lemon/concept/heap.h
src/test/heap_test.cc
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/lemon/concept/heap.h	Sat Apr 09 19:27:48 2005 +0000
     1.3 @@ -0,0 +1,201 @@
     1.4 +/* -*- C++ -*-
     1.5 + * src/lemon/concept/heap.h - Part of LEMON, a generic C++ optimization library
     1.6 + *
     1.7 + * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     1.8 + * (Egervary Combinatorial Optimization Research Group, EGRES).
     1.9 + *
    1.10 + * Permission to use, modify and distribute this software is granted
    1.11 + * provided that this copyright notice appears in all copies. For
    1.12 + * precise terms see the accompanying LICENSE file.
    1.13 + *
    1.14 + * This software is provided "AS IS" with no warranty of any kind,
    1.15 + * express or implied, and with no claim as to its suitability for any
    1.16 + * purpose.
    1.17 + *
    1.18 + */
    1.19 +
    1.20 +///\ingroup concept
    1.21 +///\file
    1.22 +///\brief Classes for representing heaps.
    1.23 +///
    1.24 +
    1.25 +#ifndef LEMON_CONCEPT_HEAP_H
    1.26 +#define LEMON_CONCEPT_HEAP_H
    1.27 +
    1.28 +#include <lemon/invalid.h>
    1.29 +
    1.30 +namespace lemon {
    1.31 +  namespace concept {
    1.32 +    /// \addtogroup concept
    1.33 +    /// @{
    1.34 +
    1.35 +
    1.36 +    /// \brief A concept structure describes the main interface of heaps.
    1.37 +    ///
    1.38 +    /// A concept structure describes the main interface of heaps.
    1.39 +    ///
    1.40 +    template <typename Item, typename Prio, typename ItemIntMap>
    1.41 +    class Heap {
    1.42 +    public:
    1.43 +  
    1.44 +
    1.45 +      /// \brief Type to represent the items states.
    1.46 +      ///
    1.47 +      /// Each Item element have a state associated to it. It may be "in heap",
    1.48 +      /// "pre heap" or "post heap". The later two are indifferent from the
    1.49 +      /// heap's point of view, but may be useful to the user.
    1.50 +      ///
    1.51 +      /// The ItemIntMap _should_ be initialized in such way, that it maps
    1.52 +      /// PRE_HEAP (-1) to any element to be put in the heap...
    1.53 +      enum state_enum {
    1.54 +	IN_HEAP = 0,
    1.55 +	PRE_HEAP = -1,
    1.56 +	POST_HEAP = -2
    1.57 +      };
    1.58 +      
    1.59 +      /// \brief The constructor.
    1.60 +      ///
    1.61 +      /// The constructor.
    1.62 +      /// \param _iim should be given to the constructor, since it is used
    1.63 +      /// internally to handle the cross references. The value of the map
    1.64 +      /// should be PRE_HEAP (-1) for each element.
    1.65 +      explicit Heap(ItemIntMap &_iim) {}
    1.66 +
    1.67 +      /// The number of items stored in the heap.
    1.68 +      ///
    1.69 +      /// \brief Returns the number of items stored in the heap.
    1.70 +      int size() const { return 0; }
    1.71 +      /// \brief Checks if the heap stores no items.
    1.72 +      ///
    1.73 +      /// Returns \c true if and only if the heap stores no items.
    1.74 +      bool empty() const { return false; }
    1.75 +
    1.76 +      /// \brief Insert an item into the heap with the given heap.
    1.77 +      ///    
    1.78 +      /// Adds \c i to the heap with priority \c p. 
    1.79 +      /// \param i The item to insert.
    1.80 +      /// \param p The priority of the item.
    1.81 +      void push(const Item &i, const Prio &p) {}
    1.82 +
    1.83 +      /// \brief Returns the item with minimum priority.
    1.84 +      ///
    1.85 +      /// This method returns the item with minimum priority.  
    1.86 +      /// \pre The heap must be nonempty.  
    1.87 +      Item top() const {}
    1.88 +
    1.89 +      /// \brief Returns the minimum priority.
    1.90 +      ///
    1.91 +      /// It returns the minimum priority.
    1.92 +      /// \pre The heap must be nonempty.
    1.93 +      Prio prio() const {}
    1.94 +
    1.95 +      /// \brief Deletes the item with minimum priority.
    1.96 +      ///
    1.97 +      /// This method deletes the item with minimum priority.
    1.98 +      /// \pre The heap must be non-empty.  
    1.99 +      void pop() {}
   1.100 +
   1.101 +      /// \brief Deletes \c i from the heap.
   1.102 +      ///
   1.103 +      /// This method deletes item \c i from the heap, if \c i was
   1.104 +      /// already stored in the heap.
   1.105 +      /// \param i The item to erase. 
   1.106 +      void erase(const Item &i) {}
   1.107 +
   1.108 +      /// \brief Returns the priority of \c i.
   1.109 +      ///
   1.110 +      /// This function returns the priority of item \c i.  
   1.111 +      /// \pre \c i must be in the heap.
   1.112 +      /// \param i The item.
   1.113 +      Prio operator[](const Item &i) const {}
   1.114 +
   1.115 +      /// \brief \c i gets to the heap with priority \c p independently 
   1.116 +      /// if \c i was already there.
   1.117 +      ///
   1.118 +      /// This method calls \ref push(\c i, \c p) if \c i is not stored
   1.119 +      /// in the heap and sets the priority of \c i to \c p otherwise.
   1.120 +      /// It may throw an \e UnderFlowPriorityException. 
   1.121 +      /// \param i The item.
   1.122 +      /// \param p The priority.
   1.123 +      void set(const Item &i, const Prio &p) {}
   1.124 +      
   1.125 +      /// \brief Decreases the priority of \c i to \c p.
   1.126 +      ///
   1.127 +      /// This method decreases the priority of item \c i to \c p.
   1.128 +      /// \pre \c i must be stored in the heap with priority at least \c p.
   1.129 +      /// \param i The item.
   1.130 +      /// \param p The priority.
   1.131 +      void decrease(const Item &i, const Prio &p) {}
   1.132 +
   1.133 +      /// \brief Increases the priority of \c i to \c p.
   1.134 +      ///
   1.135 +      /// This method sets the priority of item \c i to \c p. 
   1.136 +      /// \pre \c i must be stored in the heap with priority at most \c
   1.137 +      /// p relative to \c Compare.
   1.138 +      /// \param i The item.
   1.139 +      /// \param p The priority.
   1.140 +      void increase(const Item &i, const Prio &p) {}
   1.141 +
   1.142 +      /// \brief Returns if \c item is in, has already been in, or has 
   1.143 +      /// never been in the heap.
   1.144 +      ///
   1.145 +      /// This method returns PRE_HEAP if \c item has never been in the
   1.146 +      /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
   1.147 +      /// otherwise. In the latter case it is possible that \c item will
   1.148 +      /// get back to the heap again.
   1.149 +      /// \param i The item.
   1.150 +      state_enum state(const Item &i) const {}
   1.151 +
   1.152 +
   1.153 +      template <typename _Heap>
   1.154 +      struct Constraints {
   1.155 +      public:
   1.156 +    
   1.157 +	void constraints() {
   1.158 +	  Item item;
   1.159 +	  Prio prio;
   1.160 +
   1.161 +	  ignore_unused_variable_warning(item);
   1.162 +	  ignore_unused_variable_warning(prio);
   1.163 +
   1.164 +	  typedef typename _Heap::state_enum state_enum;
   1.165 +	  state_enum state;
   1.166 +
   1.167 +	  ignore_unused_variable_warning(state);
   1.168 +      
   1.169 +	  _Heap heap1 = _Heap(map);
   1.170 +
   1.171 +	  ignore_unused_variable_warning(heap1);
   1.172 +      
   1.173 +	  heap.push(item, prio);
   1.174 +
   1.175 +	  prio = heap.prio();
   1.176 +	  item = heap.top();
   1.177 +
   1.178 +	  heap.pop();
   1.179 +
   1.180 +	  heap.set(item, prio);
   1.181 +	  heap.decrease(item, prio);
   1.182 +	  heap.increase(item, prio);
   1.183 +	  prio = heap[item];
   1.184 +
   1.185 +	  heap.erase(item);
   1.186 +
   1.187 +	  state = heap.state(item);
   1.188 +
   1.189 +	  state = _Heap::PRE_HEAP;
   1.190 +	  state = _Heap::IN_HEAP;
   1.191 +	  state = _Heap::POST_HEAP;
   1.192 +	}
   1.193 +    
   1.194 +	_Heap& heap;
   1.195 +	ItemIntMap& map;
   1.196 +
   1.197 +	Constraints() : heap(0), map(0) {}
   1.198 +      };
   1.199 +    };
   1.200 +
   1.201 +    /// @}
   1.202 +  } // namespace lemon
   1.203 +}
   1.204 +#endif // LEMON_CONCEPT_PATH_H
     2.1 --- a/src/test/heap_test.cc	Fri Apr 08 15:46:12 2005 +0000
     2.2 +++ b/src/test/heap_test.cc	Sat Apr 09 19:27:48 2005 +0000
     2.3 @@ -6,6 +6,7 @@
     2.4  #include <vector>
     2.5  
     2.6  #include <lemon/concept_check.h>
     2.7 +#include <lemon/concept/heap.h>
     2.8  
     2.9  #include <lemon/smart_graph.h>
    2.10  
    2.11 @@ -21,58 +22,8 @@
    2.12  
    2.13  
    2.14  using namespace lemon;
    2.15 +using namespace lemon::concept;
    2.16  
    2.17 -template <typename Item, typename Prio, typename ItemIntMap>
    2.18 -class HeapConcept {
    2.19 -public:  
    2.20 -
    2.21 -  template <typename _Heap>
    2.22 -  struct Constraints {
    2.23 -  public:
    2.24 -    
    2.25 -    void constraints() {
    2.26 -      Item item;
    2.27 -      Prio prio;
    2.28 -
    2.29 -      ignore_unused_variable_warning(item);
    2.30 -      ignore_unused_variable_warning(prio);
    2.31 -
    2.32 -      typedef typename _Heap::state_enum state_enum;
    2.33 -      state_enum state;
    2.34 -
    2.35 -      ignore_unused_variable_warning(state);
    2.36 -      
    2.37 -      _Heap heap1 = _Heap(map);
    2.38 -
    2.39 -      ignore_unused_variable_warning(heap1);
    2.40 -      
    2.41 -      heap.push(item, prio);
    2.42 -
    2.43 -      prio = heap.prio();
    2.44 -      item = heap.top();
    2.45 -
    2.46 -      heap.pop();
    2.47 -
    2.48 -      heap.set(item, prio);
    2.49 -      heap.decrease(item, prio);
    2.50 -      heap.increase(item, prio);
    2.51 -      prio = heap[item];
    2.52 -
    2.53 -      heap.erase(item);
    2.54 -
    2.55 -      state = heap.state(item);
    2.56 -
    2.57 -      state = _Heap::PRE_HEAP;
    2.58 -      state = _Heap::IN_HEAP;
    2.59 -      state = _Heap::POST_HEAP;
    2.60 -    }
    2.61 -    
    2.62 -    _Heap& heap;
    2.63 -    ItemIntMap& map;
    2.64 -
    2.65 -    Constraints() : heap(0), map(0) {}
    2.66 -  };
    2.67 -};
    2.68  
    2.69  int main() {
    2.70  
    2.71 @@ -108,36 +59,36 @@
    2.72      std::cerr << "Checking Bin Heap" << std::endl;
    2.73  
    2.74      typedef BinHeap<Item, Prio, ItemIntMap> IntHeap;
    2.75 -    checkConcept<HeapConcept<Item, Prio, ItemIntMap>, IntHeap>();
    2.76 +    checkConcept<Heap<Item, Prio, ItemIntMap>, IntHeap>();
    2.77      heapSortTest<IntHeap>(100);
    2.78      heapIncreaseTest<IntHeap>(100);
    2.79      
    2.80      typedef FibHeap<Node, Prio, Graph::NodeMap<int> > NodeHeap;
    2.81 -    checkConcept<HeapConcept<Node, Prio, Graph::NodeMap<int> >, NodeHeap>();
    2.82 +    checkConcept<Heap<Node, Prio, Graph::NodeMap<int> >, NodeHeap>();
    2.83      dijkstraHeapTest<Graph, LengthMap, NodeHeap>(graph, length, start);
    2.84    }
    2.85    {
    2.86      std::cerr << "Checking Fib Heap" << std::endl;
    2.87  
    2.88      typedef FibHeap<Item, Prio, ItemIntMap> IntHeap;
    2.89 -    checkConcept<HeapConcept<Item, Prio, ItemIntMap>, IntHeap>();
    2.90 +    checkConcept<Heap<Item, Prio, ItemIntMap>, IntHeap>();
    2.91      heapSortTest<IntHeap>(100);
    2.92      heapIncreaseTest<IntHeap>(100);
    2.93  
    2.94      typedef FibHeap<Node, Prio, Graph::NodeMap<int> > NodeHeap;
    2.95 -    checkConcept<HeapConcept<Node, Prio, Graph::NodeMap<int> >, NodeHeap>();
    2.96 +    checkConcept<Heap<Node, Prio, Graph::NodeMap<int> >, NodeHeap>();
    2.97      dijkstraHeapTest<Graph, LengthMap, NodeHeap>(graph, length, start);
    2.98    }
    2.99    {
   2.100      std::cerr << "Checking Radix Heap" << std::endl;
   2.101  
   2.102      typedef RadixHeap<Item, ItemIntMap> IntHeap;
   2.103 -    checkConcept<HeapConcept<Item, Prio, ItemIntMap>, IntHeap>();
   2.104 +    checkConcept<Heap<Item, Prio, ItemIntMap>, IntHeap>();
   2.105      heapSortTest<IntHeap>(100);
   2.106      heapIncreaseTest<IntHeap>(100);
   2.107  
   2.108      typedef RadixHeap<Node, Graph::NodeMap<int> > NodeHeap;
   2.109 -    checkConcept<HeapConcept<Node, Prio, Graph::NodeMap<int> >, NodeHeap>();
   2.110 +    checkConcept<Heap<Node, Prio, Graph::NodeMap<int> >, NodeHeap>();
   2.111      dijkstraHeapTest<Graph, LengthMap, NodeHeap>(graph, length, start);
   2.112    }
   2.113