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