lemon/concepts/heap.h
author Peter Kovacs <kpeter@inf.elte.hu>
Wed, 01 Oct 2008 11:58:03 +0200
changeset 291 d901321d6555
parent 220 a5d8c039f218
child 440 88ed40ad0d4f
permissions -rw-r--r--
Changing parameter order in exception classes + improvements
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
      ///
alpar@100
   132
      /// \param i The item.
alpar@100
   133
      /// \param p The priority.
alpar@100
   134
      void set(const Item &i, const Prio &p) {}
alpar@209
   135
kpeter@113
   136
      /// \brief Decreases the priority of an item to the given value.
alpar@100
   137
      ///
kpeter@113
   138
      /// Decreases the priority of an item to the given value.
alpar@100
   139
      /// \pre \c i must be stored in the heap with priority at least \c p.
alpar@100
   140
      /// \param i The item.
alpar@100
   141
      /// \param p The priority.
alpar@100
   142
      void decrease(const Item &i, const Prio &p) {}
alpar@100
   143
kpeter@113
   144
      /// \brief Increases the priority of an item to the given value.
alpar@100
   145
      ///
kpeter@113
   146
      /// Increases the priority of an item to the given value.
kpeter@113
   147
      /// \pre \c i must be stored in the heap with priority at most \c p.
alpar@100
   148
      /// \param i The item.
alpar@100
   149
      /// \param p The priority.
alpar@100
   150
      void increase(const Item &i, const Prio &p) {}
alpar@100
   151
kpeter@113
   152
      /// \brief Returns if an item is in, has already been in, or has
alpar@100
   153
      /// never been in the heap.
alpar@100
   154
      ///
kpeter@113
   155
      /// This method returns \c PRE_HEAP if the given item has never
kpeter@113
   156
      /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
kpeter@113
   157
      /// and \c POST_HEAP otherwise.
kpeter@113
   158
      /// In the latter case it is possible that the item will get back
kpeter@113
   159
      /// to the heap again.
alpar@100
   160
      /// \param i The item.
alpar@100
   161
      State state(const Item &i) const {}
alpar@100
   162
kpeter@113
   163
      /// \brief Sets the state of an item in the heap.
alpar@100
   164
      ///
kpeter@113
   165
      /// Sets the state of the given item in the heap. It can be used
kpeter@113
   166
      /// to manually clear the heap when it is important to achive the
alpar@100
   167
      /// better time complexity.
alpar@100
   168
      /// \param i The item.
kpeter@113
   169
      /// \param st The state. It should not be \c IN_HEAP.
alpar@100
   170
      void state(const Item& i, State st) {}
alpar@100
   171
alpar@100
   172
alpar@100
   173
      template <typename _Heap>
alpar@100
   174
      struct Constraints {
alpar@100
   175
      public:
alpar@209
   176
        void constraints() {
alpar@209
   177
          typedef typename _Heap::Item OwnItem;
alpar@209
   178
          typedef typename _Heap::Prio OwnPrio;
alpar@209
   179
          typedef typename _Heap::State OwnState;
kpeter@113
   180
alpar@209
   181
          Item item;
alpar@209
   182
          Prio prio;
alpar@209
   183
          item=Item();
alpar@209
   184
          prio=Prio();
alpar@209
   185
          ignore_unused_variable_warning(item);
alpar@209
   186
          ignore_unused_variable_warning(prio);
alpar@100
   187
alpar@209
   188
          OwnItem own_item;
alpar@209
   189
          OwnPrio own_prio;
alpar@209
   190
          OwnState own_state;
alpar@209
   191
          own_item=Item();
alpar@209
   192
          own_prio=Prio();
alpar@209
   193
          ignore_unused_variable_warning(own_item);
alpar@209
   194
          ignore_unused_variable_warning(own_prio);
alpar@209
   195
          ignore_unused_variable_warning(own_state);
alpar@100
   196
alpar@209
   197
          _Heap heap1(map);
alpar@209
   198
          _Heap heap2 = heap1;
alpar@209
   199
          ignore_unused_variable_warning(heap1);
alpar@209
   200
          ignore_unused_variable_warning(heap2);
alpar@100
   201
alpar@209
   202
          int s = heap.size();
alpar@209
   203
          ignore_unused_variable_warning(s);
alpar@209
   204
          bool e = heap.empty();
alpar@209
   205
          ignore_unused_variable_warning(e);
alpar@100
   206
alpar@209
   207
          prio = heap.prio();
alpar@209
   208
          item = heap.top();
alpar@209
   209
          prio = heap[item];
alpar@209
   210
          own_prio = heap.prio();
alpar@209
   211
          own_item = heap.top();
alpar@209
   212
          own_prio = heap[own_item];
alpar@100
   213
alpar@209
   214
          heap.push(item, prio);
alpar@209
   215
          heap.push(own_item, own_prio);
alpar@209
   216
          heap.pop();
alpar@100
   217
alpar@209
   218
          heap.set(item, prio);
alpar@209
   219
          heap.decrease(item, prio);
alpar@209
   220
          heap.increase(item, prio);
alpar@209
   221
          heap.set(own_item, own_prio);
alpar@209
   222
          heap.decrease(own_item, own_prio);
alpar@209
   223
          heap.increase(own_item, own_prio);
alpar@100
   224
alpar@209
   225
          heap.erase(item);
alpar@209
   226
          heap.erase(own_item);
alpar@209
   227
          heap.clear();
alpar@100
   228
alpar@209
   229
          own_state = heap.state(own_item);
alpar@209
   230
          heap.state(own_item, own_state);
alpar@100
   231
alpar@209
   232
          own_state = _Heap::PRE_HEAP;
alpar@209
   233
          own_state = _Heap::IN_HEAP;
alpar@209
   234
          own_state = _Heap::POST_HEAP;
alpar@209
   235
        }
alpar@209
   236
alpar@209
   237
        _Heap& heap;
alpar@209
   238
        ItemIntMap& map;
alpar@100
   239
      };
alpar@100
   240
    };
alpar@100
   241
alpar@100
   242
    /// @}
alpar@100
   243
  } // namespace lemon
alpar@100
   244
}
alpar@100
   245
#endif // LEMON_CONCEPT_PATH_H