lemon/concepts/heap.h
author Balazs Dezso <deba@inf.elte.hu>
Tue, 29 Jul 2008 10:27:36 +0200
changeset 237 9c8efad5f587
parent 209 765619b7cbb2
child 290 f6899946c1ac
permissions -rw-r--r--
Fixing constness of Graph in GraphWriter
alpar@209
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@100
     2
 *
alpar@209
     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
deba@220
    26
#include <lemon/core.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
      ///
alpar@209
    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@209
    58
        IN_HEAP = 0,
alpar@209
    59
        PRE_HEAP = -1,
alpar@209
    60
        POST_HEAP = -2
alpar@100
    61
      };
alpar@209
    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@209
    88
      ///
alpar@209
    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.
alpar@209
   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
      ///
alpar@209
   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@209
   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@209
   177
        void constraints() {
alpar@209
   178
          typedef typename _Heap::Item OwnItem;
alpar@209
   179
          typedef typename _Heap::Prio OwnPrio;
alpar@209
   180
          typedef typename _Heap::State OwnState;
kpeter@113
   181
alpar@209
   182
          Item item;
alpar@209
   183
          Prio prio;
alpar@209
   184
          item=Item();
alpar@209
   185
          prio=Prio();
alpar@209
   186
          ignore_unused_variable_warning(item);
alpar@209
   187
          ignore_unused_variable_warning(prio);
alpar@100
   188
alpar@209
   189
          OwnItem own_item;
alpar@209
   190
          OwnPrio own_prio;
alpar@209
   191
          OwnState own_state;
alpar@209
   192
          own_item=Item();
alpar@209
   193
          own_prio=Prio();
alpar@209
   194
          ignore_unused_variable_warning(own_item);
alpar@209
   195
          ignore_unused_variable_warning(own_prio);
alpar@209
   196
          ignore_unused_variable_warning(own_state);
alpar@100
   197
alpar@209
   198
          _Heap heap1(map);
alpar@209
   199
          _Heap heap2 = heap1;
alpar@209
   200
          ignore_unused_variable_warning(heap1);
alpar@209
   201
          ignore_unused_variable_warning(heap2);
alpar@100
   202
alpar@209
   203
          int s = heap.size();
alpar@209
   204
          ignore_unused_variable_warning(s);
alpar@209
   205
          bool e = heap.empty();
alpar@209
   206
          ignore_unused_variable_warning(e);
alpar@100
   207
alpar@209
   208
          prio = heap.prio();
alpar@209
   209
          item = heap.top();
alpar@209
   210
          prio = heap[item];
alpar@209
   211
          own_prio = heap.prio();
alpar@209
   212
          own_item = heap.top();
alpar@209
   213
          own_prio = heap[own_item];
alpar@100
   214
alpar@209
   215
          heap.push(item, prio);
alpar@209
   216
          heap.push(own_item, own_prio);
alpar@209
   217
          heap.pop();
alpar@100
   218
alpar@209
   219
          heap.set(item, prio);
alpar@209
   220
          heap.decrease(item, prio);
alpar@209
   221
          heap.increase(item, prio);
alpar@209
   222
          heap.set(own_item, own_prio);
alpar@209
   223
          heap.decrease(own_item, own_prio);
alpar@209
   224
          heap.increase(own_item, own_prio);
alpar@100
   225
alpar@209
   226
          heap.erase(item);
alpar@209
   227
          heap.erase(own_item);
alpar@209
   228
          heap.clear();
alpar@100
   229
alpar@209
   230
          own_state = heap.state(own_item);
alpar@209
   231
          heap.state(own_item, own_state);
alpar@100
   232
alpar@209
   233
          own_state = _Heap::PRE_HEAP;
alpar@209
   234
          own_state = _Heap::IN_HEAP;
alpar@209
   235
          own_state = _Heap::POST_HEAP;
alpar@209
   236
        }
alpar@209
   237
alpar@209
   238
        _Heap& heap;
alpar@209
   239
        ItemIntMap& map;
alpar@100
   240
      };
alpar@100
   241
    };
alpar@100
   242
alpar@100
   243
    /// @}
alpar@100
   244
  } // namespace lemon
alpar@100
   245
}
alpar@100
   246
#endif // LEMON_CONCEPT_PATH_H