lemon/concepts/heap.h
author Peter Kovacs <kpeter@inf.elte.hu>
Mon, 23 Feb 2009 14:51:10 +0100
changeset 532 997a75bac45a
parent 519 c786cd201266
child 559 c5fd2d996909
permissions -rw-r--r--
Small improvements in DIMACS solver (#226)
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@440
     5
 * Copyright (C) 2003-2009
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
deba@529
    23
#ifndef LEMON_CONCEPTS_HEAP_H
deba@529
    24
#define LEMON_CONCEPTS_HEAP_H
alpar@100
    25
deba@220
    26
#include <lemon/core.h>
deba@519
    27
#include <lemon/concept_check.h>
alpar@100
    28
alpar@100
    29
namespace lemon {
kpeter@113
    30
alpar@100
    31
  namespace concepts {
kpeter@113
    32
alpar@100
    33
    /// \addtogroup concept
alpar@100
    34
    /// @{
alpar@100
    35
kpeter@113
    36
    /// \brief The heap concept.
alpar@100
    37
    ///
kpeter@113
    38
    /// Concept class describing the main interface of heaps.
kpeter@113
    39
    template <typename Priority, typename ItemIntMap>
alpar@100
    40
    class Heap {
alpar@100
    41
    public:
alpar@100
    42
kpeter@113
    43
      /// Type of the items stored in the heap.
kpeter@113
    44
      typedef typename ItemIntMap::Key Item;
alpar@100
    45
kpeter@113
    46
      /// Type of the priorities.
kpeter@113
    47
      typedef Priority Prio;
kpeter@113
    48
kpeter@113
    49
      /// \brief Type to represent the states of the items.
alpar@100
    50
      ///
kpeter@113
    51
      /// Each item has a state associated to it. It can be "in heap",
kpeter@113
    52
      /// "pre heap" or "post heap". The later two are indifferent
kpeter@113
    53
      /// from the point of view of the heap, but may be useful for
kpeter@113
    54
      /// the user.
alpar@100
    55
      ///
alpar@209
    56
      /// The \c ItemIntMap must be initialized in such a way, that it
kpeter@113
    57
      /// assigns \c PRE_HEAP (<tt>-1</tt>) to every item.
alpar@100
    58
      enum State {
alpar@209
    59
        IN_HEAP = 0,
alpar@209
    60
        PRE_HEAP = -1,
alpar@209
    61
        POST_HEAP = -2
alpar@100
    62
      };
alpar@209
    63
alpar@100
    64
      /// \brief The constructor.
alpar@100
    65
      ///
alpar@100
    66
      /// The constructor.
kpeter@113
    67
      /// \param map A map that assigns \c int values to keys of type
kpeter@113
    68
      /// \c Item. It is used internally by the heap implementations to
kpeter@113
    69
      /// handle the cross references. The assigned value must be
kpeter@113
    70
      /// \c PRE_HEAP (<tt>-1</tt>) for every item.
kpeter@113
    71
      explicit Heap(ItemIntMap &map) {}
alpar@100
    72
alpar@100
    73
      /// \brief The number of items stored in the heap.
alpar@100
    74
      ///
alpar@100
    75
      /// Returns the number of items stored in the heap.
alpar@100
    76
      int size() const { return 0; }
alpar@100
    77
kpeter@113
    78
      /// \brief Checks if the heap is empty.
alpar@100
    79
      ///
kpeter@113
    80
      /// Returns \c true if the heap is empty.
alpar@100
    81
      bool empty() const { return false; }
alpar@100
    82
kpeter@113
    83
      /// \brief Makes the heap empty.
alpar@100
    84
      ///
kpeter@113
    85
      /// Makes the heap empty.
alpar@100
    86
      void clear();
alpar@100
    87
kpeter@113
    88
      /// \brief Inserts an item into the heap with the given priority.
alpar@209
    89
      ///
alpar@209
    90
      /// Inserts the given item into the heap with the given priority.
alpar@100
    91
      /// \param i The item to insert.
alpar@100
    92
      /// \param p The priority of the item.
alpar@100
    93
      void push(const Item &i, const Prio &p) {}
alpar@100
    94
kpeter@113
    95
      /// \brief Returns the item having minimum priority.
alpar@100
    96
      ///
kpeter@113
    97
      /// Returns the item having minimum priority.
kpeter@113
    98
      /// \pre The heap must be non-empty.
alpar@100
    99
      Item top() const {}
alpar@100
   100
kpeter@113
   101
      /// \brief The minimum priority.
alpar@100
   102
      ///
kpeter@113
   103
      /// Returns the minimum priority.
kpeter@113
   104
      /// \pre The heap must be non-empty.
alpar@100
   105
      Prio prio() const {}
alpar@100
   106
kpeter@113
   107
      /// \brief Removes the item having minimum priority.
alpar@100
   108
      ///
kpeter@113
   109
      /// Removes the item having minimum priority.
kpeter@113
   110
      /// \pre The heap must be non-empty.
alpar@100
   111
      void pop() {}
alpar@100
   112
kpeter@113
   113
      /// \brief Removes an item from the heap.
alpar@100
   114
      ///
kpeter@113
   115
      /// Removes the given item from the heap if it is already stored.
alpar@209
   116
      /// \param i The item to delete.
alpar@100
   117
      void erase(const Item &i) {}
alpar@100
   118
kpeter@113
   119
      /// \brief The priority of an item.
alpar@100
   120
      ///
alpar@209
   121
      /// Returns the priority of the given item.
alpar@100
   122
      /// \pre \c i must be in the heap.
alpar@100
   123
      /// \param i The item.
alpar@100
   124
      Prio operator[](const Item &i) const {}
alpar@100
   125
kpeter@113
   126
      /// \brief Sets the priority of an item or inserts it, if it is
kpeter@113
   127
      /// not stored in the heap.
alpar@100
   128
      ///
kpeter@113
   129
      /// This method sets the priority of the given item if it is
kpeter@113
   130
      /// already stored in the heap.
kpeter@113
   131
      /// Otherwise it inserts the given item with the given priority.
kpeter@113
   132
      ///
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
}
deba@529
   246
#endif