lemon/concept/heap.h
author deba
Wed, 01 Mar 2006 10:17:25 +0000
changeset 1990 15fb7a4ea6be
parent 1902 e9af75c90c28
child 1993 2115143eceea
permissions -rw-r--r--
Some classes assumed that the GraphMaps should be inherited
from an ObserverBase. These classes parents replaced with
DefaultMap which cause that the graph maps should not be
inherited from the ObserverBase.
     1 /* -*- C++ -*-
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library
     4  *
     5  * Copyright (C) 2003-2006
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     9  * Permission to use, modify and distribute this software is granted
    10  * provided that this copyright notice appears in all copies. For
    11  * precise terms see the accompanying LICENSE file.
    12  *
    13  * This software is provided "AS IS" with no warranty of any kind,
    14  * express or implied, and with no claim as to its suitability for any
    15  * purpose.
    16  *
    17  */
    18 
    19 ///\ingroup concept
    20 ///\file
    21 ///\brief Classes for representing heaps.
    22 ///
    23 
    24 #ifndef LEMON_CONCEPT_HEAP_H
    25 #define LEMON_CONCEPT_HEAP_H
    26 
    27 #include <lemon/invalid.h>
    28 
    29 namespace lemon {
    30   namespace concept {
    31     /// \addtogroup concept
    32     /// @{
    33 
    34 
    35     /// \brief A concept structure describes the main interface of heaps.
    36     ///
    37     /// A concept structure describes the main interface of heaps.
    38     ///
    39     template <typename Item, typename Prio, typename ItemIntMap>
    40     class Heap {
    41     public:
    42   
    43 
    44       /// \brief Type to represent the items states.
    45       ///
    46       /// Each Item element have a state associated to it. It may be "in heap",
    47       /// "pre heap" or "post heap". The later two are indifferent from the
    48       /// heap's point of view, but may be useful to the user.
    49       ///
    50       /// The ItemIntMap _should_ be initialized in such way, that it maps
    51       /// PRE_HEAP (-1) to any element to be put in the heap...
    52       enum state_enum {
    53 	IN_HEAP = 0,
    54 	PRE_HEAP = -1,
    55 	POST_HEAP = -2
    56       };
    57       
    58       /// \brief The constructor.
    59       ///
    60       /// The constructor.
    61       /// \param _iim should be given to the constructor, since it is used
    62       /// internally to handle the cross references. The value of the map
    63       /// should be PRE_HEAP (-1) for each element.
    64       explicit Heap(ItemIntMap &_iim) {}
    65 
    66       /// \brief The number of items stored in the heap.
    67       ///
    68       /// Returns the number of items stored in the heap.
    69       int size() const { return 0; }
    70 
    71       /// \brief Checks if the heap stores no items.
    72       ///
    73       /// Returns \c true if and only if the heap stores no items.
    74       bool empty() const { return false; }
    75 
    76       /// \brief Makes empty this heap.
    77       ///
    78       /// Makes this heap empty.
    79       void clear();
    80 
    81       /// \brief Insert an item into the heap with the given heap.
    82       ///    
    83       /// Adds \c i to the heap with priority \c p. 
    84       /// \param i The item to insert.
    85       /// \param p The priority of the item.
    86       void push(const Item &i, const Prio &p) {}
    87 
    88       /// \brief Returns the item with minimum priority.
    89       ///
    90       /// This method returns the item with minimum priority.  
    91       /// \pre The heap must be nonempty.  
    92       Item top() const {}
    93 
    94       /// \brief Returns the minimum priority.
    95       ///
    96       /// It returns the minimum priority.
    97       /// \pre The heap must be nonempty.
    98       Prio prio() const {}
    99 
   100       /// \brief Deletes the item with minimum priority.
   101       ///
   102       /// This method deletes the item with minimum priority.
   103       /// \pre The heap must be non-empty.  
   104       void pop() {}
   105 
   106       /// \brief Deletes \c i from the heap.
   107       ///
   108       /// This method deletes item \c i from the heap, if \c i was
   109       /// already stored in the heap.
   110       /// \param i The item to erase. 
   111       void erase(const Item &i) {}
   112 
   113       /// \brief Returns the priority of \c i.
   114       ///
   115       /// This function returns the priority of item \c i.  
   116       /// \pre \c i must be in the heap.
   117       /// \param i The item.
   118       Prio operator[](const Item &i) const {}
   119 
   120       /// \brief \c i gets to the heap with priority \c p independently 
   121       /// if \c i was already there.
   122       ///
   123       /// This method calls \ref push(\c i, \c p) if \c i is not stored
   124       /// in the heap and sets the priority of \c i to \c p otherwise.
   125       /// It may throw an \e UnderFlowPriorityException. 
   126       /// \param i The item.
   127       /// \param p The priority.
   128       void set(const Item &i, const Prio &p) {}
   129       
   130       /// \brief Decreases the priority of \c i to \c p.
   131       ///
   132       /// This method decreases the priority of item \c i to \c p.
   133       /// \pre \c i must be stored in the heap with priority at least \c p.
   134       /// \param i The item.
   135       /// \param p The priority.
   136       void decrease(const Item &i, const Prio &p) {}
   137 
   138       /// \brief Increases the priority of \c i to \c p.
   139       ///
   140       /// This method sets the priority of item \c i to \c p. 
   141       /// \pre \c i must be stored in the heap with priority at most \c
   142       /// p relative to \c Compare.
   143       /// \param i The item.
   144       /// \param p The priority.
   145       void increase(const Item &i, const Prio &p) {}
   146 
   147       /// \brief Returns if \c item is in, has already been in, or has 
   148       /// never been in the heap.
   149       ///
   150       /// This method returns PRE_HEAP if \c item has never been in the
   151       /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
   152       /// otherwise. In the latter case it is possible that \c item will
   153       /// get back to the heap again.
   154       /// \param i The item.
   155       state_enum state(const Item &i) const {}
   156 
   157       /// \brief Sets the state of the \c item in the heap.
   158       ///
   159       /// Sets the state of the \c item in the heap. It can be used to
   160       /// manually clear the heap when it is important to achive the
   161       /// better time complexity.
   162       /// \param i The item.
   163       /// \param st The state. It should not be \c IN_HEAP. 
   164       void state(const Item& i, state_enum st) {}
   165 
   166 
   167       template <typename _Heap>
   168       struct Constraints {
   169       public:
   170     
   171 	void constraints() {
   172 	  Item item;
   173 	  Prio prio;
   174 
   175 	  item=Item();
   176 	  prio=Prio();
   177 
   178 	  ignore_unused_variable_warning(item);
   179 	  ignore_unused_variable_warning(prio);
   180 
   181 	  typedef typename _Heap::state_enum state_enum;
   182 	  state_enum state;
   183 
   184 	  ignore_unused_variable_warning(state);
   185       
   186 	  _Heap heap1 = _Heap(map);
   187 
   188 	  ignore_unused_variable_warning(heap1);
   189       
   190 	  heap.push(item, prio);
   191 
   192 	  prio = heap.prio();
   193 	  item = heap.top();
   194 
   195 	  heap.pop();
   196 
   197 	  heap.set(item, prio);
   198 	  heap.decrease(item, prio);
   199 	  heap.increase(item, prio);
   200 	  prio = heap[item];
   201 
   202 	  heap.erase(item);
   203 
   204 	  state = heap.state(item);
   205 
   206 	  state = _Heap::PRE_HEAP;
   207 	  state = _Heap::IN_HEAP;
   208 	  state = _Heap::POST_HEAP;
   209 
   210 	  heap.clear();
   211 	}
   212     
   213 	_Heap& heap;
   214 	ItemIntMap& map;
   215 
   216 	Constraints() : heap(0), map(0) {}
   217       };
   218     };
   219 
   220     /// @}
   221   } // namespace lemon
   222 }
   223 #endif // LEMON_CONCEPT_PATH_H