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