lemon/concept/heap.h
author alpar
Wed, 13 Jul 2005 20:02:29 +0000
changeset 1556 caf0f91e16a7
parent 1435 8e85e6bbefdf
child 1717 75fe24093ded
permissions -rw-r--r--
Avoid ambiguity.
deba@1330
     1
/* -*- C++ -*-
ladanyi@1435
     2
 * lemon/concept/heap.h - Part of LEMON, a generic C++ optimization library
deba@1330
     3
 *
deba@1330
     4
 * Copyright (C) 2005 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@1330
    64
      /// The number of items stored in the heap.
deba@1330
    65
      ///
deba@1330
    66
      /// \brief Returns the number of items stored in the heap.
deba@1330
    67
      int size() const { return 0; }
deba@1330
    68
      /// \brief Checks if the heap stores no items.
deba@1330
    69
      ///
deba@1330
    70
      /// Returns \c true if and only if the heap stores no items.
deba@1330
    71
      bool empty() const { return false; }
deba@1330
    72
deba@1330
    73
      /// \brief Insert an item into the heap with the given heap.
deba@1330
    74
      ///    
deba@1330
    75
      /// Adds \c i to the heap with priority \c p. 
deba@1330
    76
      /// \param i The item to insert.
deba@1330
    77
      /// \param p The priority of the item.
deba@1330
    78
      void push(const Item &i, const Prio &p) {}
deba@1330
    79
deba@1330
    80
      /// \brief Returns the item with minimum priority.
deba@1330
    81
      ///
deba@1330
    82
      /// This method returns the item with minimum priority.  
deba@1330
    83
      /// \pre The heap must be nonempty.  
deba@1330
    84
      Item top() const {}
deba@1330
    85
deba@1330
    86
      /// \brief Returns the minimum priority.
deba@1330
    87
      ///
deba@1330
    88
      /// It returns the minimum priority.
deba@1330
    89
      /// \pre The heap must be nonempty.
deba@1330
    90
      Prio prio() const {}
deba@1330
    91
deba@1330
    92
      /// \brief Deletes the item with minimum priority.
deba@1330
    93
      ///
deba@1330
    94
      /// This method deletes the item with minimum priority.
deba@1330
    95
      /// \pre The heap must be non-empty.  
deba@1330
    96
      void pop() {}
deba@1330
    97
deba@1330
    98
      /// \brief Deletes \c i from the heap.
deba@1330
    99
      ///
deba@1330
   100
      /// This method deletes item \c i from the heap, if \c i was
deba@1330
   101
      /// already stored in the heap.
deba@1330
   102
      /// \param i The item to erase. 
deba@1330
   103
      void erase(const Item &i) {}
deba@1330
   104
deba@1330
   105
      /// \brief Returns the priority of \c i.
deba@1330
   106
      ///
deba@1330
   107
      /// This function returns the priority of item \c i.  
deba@1330
   108
      /// \pre \c i must be in the heap.
deba@1330
   109
      /// \param i The item.
deba@1330
   110
      Prio operator[](const Item &i) const {}
deba@1330
   111
deba@1330
   112
      /// \brief \c i gets to the heap with priority \c p independently 
deba@1330
   113
      /// if \c i was already there.
deba@1330
   114
      ///
deba@1330
   115
      /// This method calls \ref push(\c i, \c p) if \c i is not stored
deba@1330
   116
      /// in the heap and sets the priority of \c i to \c p otherwise.
deba@1330
   117
      /// It may throw an \e UnderFlowPriorityException. 
deba@1330
   118
      /// \param i The item.
deba@1330
   119
      /// \param p The priority.
deba@1330
   120
      void set(const Item &i, const Prio &p) {}
deba@1330
   121
      
deba@1330
   122
      /// \brief Decreases the priority of \c i to \c p.
deba@1330
   123
      ///
deba@1330
   124
      /// This method decreases the priority of item \c i to \c p.
deba@1330
   125
      /// \pre \c i must be stored in the heap with priority at least \c p.
deba@1330
   126
      /// \param i The item.
deba@1330
   127
      /// \param p The priority.
deba@1330
   128
      void decrease(const Item &i, const Prio &p) {}
deba@1330
   129
deba@1330
   130
      /// \brief Increases the priority of \c i to \c p.
deba@1330
   131
      ///
deba@1330
   132
      /// This method sets the priority of item \c i to \c p. 
deba@1330
   133
      /// \pre \c i must be stored in the heap with priority at most \c
deba@1330
   134
      /// p relative to \c Compare.
deba@1330
   135
      /// \param i The item.
deba@1330
   136
      /// \param p The priority.
deba@1330
   137
      void increase(const Item &i, const Prio &p) {}
deba@1330
   138
deba@1330
   139
      /// \brief Returns if \c item is in, has already been in, or has 
deba@1330
   140
      /// never been in the heap.
deba@1330
   141
      ///
deba@1330
   142
      /// This method returns PRE_HEAP if \c item has never been in the
deba@1330
   143
      /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
deba@1330
   144
      /// otherwise. In the latter case it is possible that \c item will
deba@1330
   145
      /// get back to the heap again.
deba@1330
   146
      /// \param i The item.
deba@1330
   147
      state_enum state(const Item &i) const {}
deba@1330
   148
deba@1330
   149
deba@1330
   150
      template <typename _Heap>
deba@1330
   151
      struct Constraints {
deba@1330
   152
      public:
deba@1330
   153
    
deba@1330
   154
	void constraints() {
deba@1330
   155
	  Item item;
deba@1330
   156
	  Prio prio;
deba@1330
   157
alpar@1494
   158
	  item=Item();
alpar@1494
   159
	  prio=Prio();
alpar@1494
   160
deba@1330
   161
	  ignore_unused_variable_warning(item);
deba@1330
   162
	  ignore_unused_variable_warning(prio);
deba@1330
   163
deba@1330
   164
	  typedef typename _Heap::state_enum state_enum;
deba@1330
   165
	  state_enum state;
deba@1330
   166
deba@1330
   167
	  ignore_unused_variable_warning(state);
deba@1330
   168
      
deba@1330
   169
	  _Heap heap1 = _Heap(map);
deba@1330
   170
deba@1330
   171
	  ignore_unused_variable_warning(heap1);
deba@1330
   172
      
deba@1330
   173
	  heap.push(item, prio);
deba@1330
   174
deba@1330
   175
	  prio = heap.prio();
deba@1330
   176
	  item = heap.top();
deba@1330
   177
deba@1330
   178
	  heap.pop();
deba@1330
   179
deba@1330
   180
	  heap.set(item, prio);
deba@1330
   181
	  heap.decrease(item, prio);
deba@1330
   182
	  heap.increase(item, prio);
deba@1330
   183
	  prio = heap[item];
deba@1330
   184
deba@1330
   185
	  heap.erase(item);
deba@1330
   186
deba@1330
   187
	  state = heap.state(item);
deba@1330
   188
deba@1330
   189
	  state = _Heap::PRE_HEAP;
deba@1330
   190
	  state = _Heap::IN_HEAP;
deba@1330
   191
	  state = _Heap::POST_HEAP;
deba@1330
   192
	}
deba@1330
   193
    
deba@1330
   194
	_Heap& heap;
deba@1330
   195
	ItemIntMap& map;
deba@1330
   196
deba@1330
   197
	Constraints() : heap(0), map(0) {}
deba@1330
   198
      };
deba@1330
   199
    };
deba@1330
   200
deba@1330
   201
    /// @}
deba@1330
   202
  } // namespace lemon
deba@1330
   203
}
deba@1330
   204
#endif // LEMON_CONCEPT_PATH_H