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