lemon/concept/heap.h
author hegyi
Thu, 05 Jan 2006 12:30:09 +0000
changeset 1878 409a31271efd
parent 1717 75fe24093ded
child 1902 e9af75c90c28
permissions -rw-r--r--
Several changes. \n If new map is added to mapstorage it emits signal with the name of the new map. This was important, because from now on not only tha mapwin should be updated. \n Furthermore algobox gets a pointer to mapstorage instead of only the mapnames from it. This is important because without it it would be complicated to pass all of the required maps to algobox.
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@1330
   155
deba@1330
   156
      template <typename _Heap>
deba@1330
   157
      struct Constraints {
deba@1330
   158
      public:
deba@1330
   159
    
deba@1330
   160
	void constraints() {
deba@1330
   161
	  Item item;
deba@1330
   162
	  Prio prio;
deba@1330
   163
alpar@1494
   164
	  item=Item();
alpar@1494
   165
	  prio=Prio();
alpar@1494
   166
deba@1330
   167
	  ignore_unused_variable_warning(item);
deba@1330
   168
	  ignore_unused_variable_warning(prio);
deba@1330
   169
deba@1330
   170
	  typedef typename _Heap::state_enum state_enum;
deba@1330
   171
	  state_enum state;
deba@1330
   172
deba@1330
   173
	  ignore_unused_variable_warning(state);
deba@1330
   174
      
deba@1330
   175
	  _Heap heap1 = _Heap(map);
deba@1330
   176
deba@1330
   177
	  ignore_unused_variable_warning(heap1);
deba@1330
   178
      
deba@1330
   179
	  heap.push(item, prio);
deba@1330
   180
deba@1330
   181
	  prio = heap.prio();
deba@1330
   182
	  item = heap.top();
deba@1330
   183
deba@1330
   184
	  heap.pop();
deba@1330
   185
deba@1330
   186
	  heap.set(item, prio);
deba@1330
   187
	  heap.decrease(item, prio);
deba@1330
   188
	  heap.increase(item, prio);
deba@1330
   189
	  prio = heap[item];
deba@1330
   190
deba@1330
   191
	  heap.erase(item);
deba@1330
   192
deba@1330
   193
	  state = heap.state(item);
deba@1330
   194
deba@1330
   195
	  state = _Heap::PRE_HEAP;
deba@1330
   196
	  state = _Heap::IN_HEAP;
deba@1330
   197
	  state = _Heap::POST_HEAP;
deba@1717
   198
deba@1717
   199
	  heap.clear();
deba@1330
   200
	}
deba@1330
   201
    
deba@1330
   202
	_Heap& heap;
deba@1330
   203
	ItemIntMap& map;
deba@1330
   204
deba@1330
   205
	Constraints() : heap(0), map(0) {}
deba@1330
   206
      };
deba@1330
   207
    };
deba@1330
   208
deba@1330
   209
    /// @}
deba@1330
   210
  } // namespace lemon
deba@1330
   211
}
deba@1330
   212
#endif // LEMON_CONCEPT_PATH_H