lemon/concepts/heap.h
author Peter Kovacs <kpeter@inf.elte.hu>
Sat, 05 Jul 2008 00:14:27 +0200
changeset 192 7bf5f97d574f
parent 100 4f754b4cf82b
child 203 215bfc30b14f
permissions -rw-r--r--
Doc improvements in LGF related files
alpar@100
     1
/* -*- C++ -*-
alpar@100
     2
 *
alpar@100
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@100
     4
 *
alpar@100
     5
 * Copyright (C) 2003-2008
alpar@100
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@100
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@100
     8
 *
alpar@100
     9
 * Permission to use, modify and distribute this software is granted
alpar@100
    10
 * provided that this copyright notice appears in all copies. For
alpar@100
    11
 * precise terms see the accompanying LICENSE file.
alpar@100
    12
 *
alpar@100
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@100
    14
 * express or implied, and with no claim as to its suitability for any
alpar@100
    15
 * purpose.
alpar@100
    16
 *
alpar@100
    17
 */
alpar@100
    18
alpar@100
    19
///\ingroup concept
alpar@100
    20
///\file
kpeter@113
    21
///\brief The concept of heaps.
alpar@100
    22
alpar@100
    23
#ifndef LEMON_CONCEPT_HEAP_H
alpar@100
    24
#define LEMON_CONCEPT_HEAP_H
alpar@100
    25
alpar@100
    26
#include <lemon/bits/invalid.h>
alpar@100
    27
alpar@100
    28
namespace lemon {
kpeter@113
    29
alpar@100
    30
  namespace concepts {
kpeter@113
    31
alpar@100
    32
    /// \addtogroup concept
alpar@100
    33
    /// @{
alpar@100
    34
kpeter@113
    35
    /// \brief The heap concept.
alpar@100
    36
    ///
kpeter@113
    37
    /// Concept class describing the main interface of heaps.
kpeter@113
    38
    template <typename Priority, typename ItemIntMap>
alpar@100
    39
    class Heap {
alpar@100
    40
    public:
alpar@100
    41
kpeter@113
    42
      /// Type of the items stored in the heap.
kpeter@113
    43
      typedef typename ItemIntMap::Key Item;
alpar@100
    44
kpeter@113
    45
      /// Type of the priorities.
kpeter@113
    46
      typedef Priority Prio;
kpeter@113
    47
kpeter@113
    48
      /// \brief Type to represent the states of the items.
alpar@100
    49
      ///
kpeter@113
    50
      /// Each item has a state associated to it. It can be "in heap",
kpeter@113
    51
      /// "pre heap" or "post heap". The later two are indifferent
kpeter@113
    52
      /// from the point of view of the heap, but may be useful for
kpeter@113
    53
      /// the user.
alpar@100
    54
      ///
kpeter@113
    55
      /// The \c ItemIntMap must be initialized in such a way, that it 
kpeter@113
    56
      /// assigns \c PRE_HEAP (<tt>-1</tt>) to every item.
alpar@100
    57
      enum State {
alpar@100
    58
	IN_HEAP = 0,
alpar@100
    59
	PRE_HEAP = -1,
alpar@100
    60
	POST_HEAP = -2
alpar@100
    61
      };
alpar@100
    62
      
alpar@100
    63
      /// \brief The constructor.
alpar@100
    64
      ///
alpar@100
    65
      /// The constructor.
kpeter@113
    66
      /// \param map A map that assigns \c int values to keys of type
kpeter@113
    67
      /// \c Item. It is used internally by the heap implementations to
kpeter@113
    68
      /// handle the cross references. The assigned value must be
kpeter@113
    69
      /// \c PRE_HEAP (<tt>-1</tt>) for every item.
kpeter@113
    70
      explicit Heap(ItemIntMap &map) {}
alpar@100
    71
alpar@100
    72
      /// \brief The number of items stored in the heap.
alpar@100
    73
      ///
alpar@100
    74
      /// Returns the number of items stored in the heap.
alpar@100
    75
      int size() const { return 0; }
alpar@100
    76
kpeter@113
    77
      /// \brief Checks if the heap is empty.
alpar@100
    78
      ///
kpeter@113
    79
      /// Returns \c true if the heap is empty.
alpar@100
    80
      bool empty() const { return false; }
alpar@100
    81
kpeter@113
    82
      /// \brief Makes the heap empty.
alpar@100
    83
      ///
kpeter@113
    84
      /// Makes the heap empty.
alpar@100
    85
      void clear();
alpar@100
    86
kpeter@113
    87
      /// \brief Inserts an item into the heap with the given priority.
alpar@100
    88
      ///    
kpeter@113
    89
      /// Inserts the given item into the heap with the given priority. 
alpar@100
    90
      /// \param i The item to insert.
alpar@100
    91
      /// \param p The priority of the item.
alpar@100
    92
      void push(const Item &i, const Prio &p) {}
alpar@100
    93
kpeter@113
    94
      /// \brief Returns the item having minimum priority.
alpar@100
    95
      ///
kpeter@113
    96
      /// Returns the item having minimum priority.
kpeter@113
    97
      /// \pre The heap must be non-empty.
alpar@100
    98
      Item top() const {}
alpar@100
    99
kpeter@113
   100
      /// \brief The minimum priority.
alpar@100
   101
      ///
kpeter@113
   102
      /// Returns the minimum priority.
kpeter@113
   103
      /// \pre The heap must be non-empty.
alpar@100
   104
      Prio prio() const {}
alpar@100
   105
kpeter@113
   106
      /// \brief Removes the item having minimum priority.
alpar@100
   107
      ///
kpeter@113
   108
      /// Removes the item having minimum priority.
kpeter@113
   109
      /// \pre The heap must be non-empty.
alpar@100
   110
      void pop() {}
alpar@100
   111
kpeter@113
   112
      /// \brief Removes an item from the heap.
alpar@100
   113
      ///
kpeter@113
   114
      /// Removes the given item from the heap if it is already stored.
kpeter@113
   115
      /// \param i The item to delete. 
alpar@100
   116
      void erase(const Item &i) {}
alpar@100
   117
kpeter@113
   118
      /// \brief The priority of an item.
alpar@100
   119
      ///
kpeter@113
   120
      /// Returns the priority of the given item.  
alpar@100
   121
      /// \pre \c i must be in the heap.
alpar@100
   122
      /// \param i The item.
alpar@100
   123
      Prio operator[](const Item &i) const {}
alpar@100
   124
kpeter@113
   125
      /// \brief Sets the priority of an item or inserts it, if it is
kpeter@113
   126
      /// not stored in the heap.
alpar@100
   127
      ///
kpeter@113
   128
      /// This method sets the priority of the given item if it is
kpeter@113
   129
      /// already stored in the heap.
kpeter@113
   130
      /// Otherwise it inserts the given item with the given priority.
kpeter@113
   131
      ///
kpeter@113
   132
      /// It may throw an \ref UnderflowPriorityException.
alpar@100
   133
      /// \param i The item.
alpar@100
   134
      /// \param p The priority.
alpar@100
   135
      void set(const Item &i, const Prio &p) {}
alpar@100
   136
      
kpeter@113
   137
      /// \brief Decreases the priority of an item to the given value.
alpar@100
   138
      ///
kpeter@113
   139
      /// Decreases the priority of an item to the given value.
alpar@100
   140
      /// \pre \c i must be stored in the heap with priority at least \c p.
alpar@100
   141
      /// \param i The item.
alpar@100
   142
      /// \param p The priority.
alpar@100
   143
      void decrease(const Item &i, const Prio &p) {}
alpar@100
   144
kpeter@113
   145
      /// \brief Increases the priority of an item to the given value.
alpar@100
   146
      ///
kpeter@113
   147
      /// Increases the priority of an item to the given value.
kpeter@113
   148
      /// \pre \c i must be stored in the heap with priority at most \c p.
alpar@100
   149
      /// \param i The item.
alpar@100
   150
      /// \param p The priority.
alpar@100
   151
      void increase(const Item &i, const Prio &p) {}
alpar@100
   152
kpeter@113
   153
      /// \brief Returns if an item is in, has already been in, or has
alpar@100
   154
      /// never been in the heap.
alpar@100
   155
      ///
kpeter@113
   156
      /// This method returns \c PRE_HEAP if the given item has never
kpeter@113
   157
      /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
kpeter@113
   158
      /// and \c POST_HEAP otherwise.
kpeter@113
   159
      /// In the latter case it is possible that the item will get back
kpeter@113
   160
      /// to the heap again.
alpar@100
   161
      /// \param i The item.
alpar@100
   162
      State state(const Item &i) const {}
alpar@100
   163
kpeter@113
   164
      /// \brief Sets the state of an item in the heap.
alpar@100
   165
      ///
kpeter@113
   166
      /// Sets the state of the given item in the heap. It can be used
kpeter@113
   167
      /// to manually clear the heap when it is important to achive the
alpar@100
   168
      /// better time complexity.
alpar@100
   169
      /// \param i The item.
kpeter@113
   170
      /// \param st The state. It should not be \c IN_HEAP.
alpar@100
   171
      void state(const Item& i, State st) {}
alpar@100
   172
alpar@100
   173
alpar@100
   174
      template <typename _Heap>
alpar@100
   175
      struct Constraints {
alpar@100
   176
      public:
alpar@100
   177
	void constraints() {
kpeter@113
   178
	  typedef typename _Heap::Item OwnItem;
kpeter@113
   179
	  typedef typename _Heap::Prio OwnPrio;
kpeter@113
   180
	  typedef typename _Heap::State OwnState;
kpeter@113
   181
alpar@100
   182
	  Item item;
alpar@100
   183
	  Prio prio;
kpeter@113
   184
	  State state;
alpar@100
   185
	  item=Item();
alpar@100
   186
	  prio=Prio();
alpar@100
   187
	  ignore_unused_variable_warning(item);
alpar@100
   188
	  ignore_unused_variable_warning(prio);
kpeter@113
   189
	  ignore_unused_variable_warning(state);
alpar@100
   190
kpeter@113
   191
	  OwnItem own_item;
kpeter@113
   192
	  OwnPrio own_prio;
kpeter@113
   193
	  OwnState own_state;
kpeter@113
   194
	  own_item=Item();
kpeter@113
   195
	  own_prio=Prio();
kpeter@113
   196
	  ignore_unused_variable_warning(own_item);
kpeter@113
   197
	  ignore_unused_variable_warning(own_prio);
kpeter@113
   198
	  ignore_unused_variable_warning(own_state);
alpar@100
   199
kpeter@113
   200
	  _Heap heap1(map);
kpeter@113
   201
	  _Heap heap2 = heap1;
alpar@100
   202
	  ignore_unused_variable_warning(heap1);
kpeter@113
   203
	  ignore_unused_variable_warning(heap2);
kpeter@113
   204
	  
kpeter@113
   205
	  int s = heap.size();
kpeter@113
   206
	  bool e = heap.empty();
alpar@100
   207
alpar@100
   208
	  prio = heap.prio();
alpar@100
   209
	  item = heap.top();
kpeter@113
   210
	  prio = heap[item];
kpeter@113
   211
	  own_prio = heap.prio();
kpeter@113
   212
	  own_item = heap.top();
kpeter@113
   213
	  own_prio = heap[own_item];
alpar@100
   214
kpeter@113
   215
	  heap.push(item, prio);
kpeter@113
   216
	  heap.push(own_item, own_prio);
alpar@100
   217
	  heap.pop();
alpar@100
   218
alpar@100
   219
	  heap.set(item, prio);
alpar@100
   220
	  heap.decrease(item, prio);
alpar@100
   221
	  heap.increase(item, prio);
kpeter@113
   222
	  heap.set(own_item, own_prio);
kpeter@113
   223
	  heap.decrease(own_item, own_prio);
kpeter@113
   224
	  heap.increase(own_item, own_prio);
alpar@100
   225
alpar@100
   226
	  heap.erase(item);
kpeter@113
   227
	  heap.erase(own_item);
kpeter@113
   228
	  heap.clear();
alpar@100
   229
alpar@100
   230
	  state = heap.state(item);
kpeter@113
   231
	  heap.state(item, state);
kpeter@113
   232
	  state = heap.state(own_item);
kpeter@113
   233
	  heap.state(own_item, own_state);
alpar@100
   234
alpar@100
   235
	  state = _Heap::PRE_HEAP;
alpar@100
   236
	  state = _Heap::IN_HEAP;
alpar@100
   237
	  state = _Heap::POST_HEAP;
kpeter@113
   238
	  own_state = _Heap::PRE_HEAP;
kpeter@113
   239
	  own_state = _Heap::IN_HEAP;
kpeter@113
   240
	  own_state = _Heap::POST_HEAP;
kpeter@113
   241
	}
alpar@100
   242
alpar@100
   243
	_Heap& heap;
alpar@100
   244
	ItemIntMap& map;
alpar@100
   245
      };
alpar@100
   246
    };
alpar@100
   247
alpar@100
   248
    /// @}
alpar@100
   249
  } // namespace lemon
alpar@100
   250
}
alpar@100
   251
#endif // LEMON_CONCEPT_PATH_H