COIN-OR::LEMON - Graph Library

source: lemon-main/lemon/concepts/heap.h @ 710:f1fe0ddad6f7

Last change on this file since 710:f1fe0ddad6f7 was 710:f1fe0ddad6f7, checked in by Peter Kovacs <kpeter@…>, 15 years ago

Move the heaps to a separate group (#299)

File size: 9.6 KB
RevLine 
[209]1/* -*- mode: C++; indent-tabs-mode: nil; -*-
[100]2 *
[209]3 * This file is a part of LEMON, a generic C++ optimization library.
[100]4 *
[440]5 * Copyright (C) 2003-2009
[100]6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 *
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
12 *
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
15 * purpose.
16 *
17 */
18
[709]19#ifndef LEMON_CONCEPTS_HEAP_H
20#define LEMON_CONCEPTS_HEAP_H
21
[100]22///\ingroup concept
23///\file
[113]24///\brief The concept of heaps.
[100]25
[220]26#include <lemon/core.h>
[519]27#include <lemon/concept_check.h>
[100]28
29namespace lemon {
[113]30
[100]31  namespace concepts {
[113]32
[100]33    /// \addtogroup concept
34    /// @{
35
[113]36    /// \brief The heap concept.
[100]37    ///
[709]38    /// This concept class describes the main interface of heaps.
[710]39    /// The various \ref heaps "heap structures" are efficient
[709]40    /// implementations of the abstract data type \e priority \e queue.
41    /// They store items with specified values called \e priorities
42    /// in such a way that finding and removing the item with minimum
43    /// priority are efficient. The basic operations are adding and
44    /// erasing items, changing the priority of an item, etc.
[559]45    ///
[709]46    /// Heaps are crucial in several algorithms, such as Dijkstra and Prim.
47    /// Any class that conforms to this concept can be used easily in such
48    /// algorithms.
49    ///
50    /// \tparam PR Type of the priorities of the items.
51    /// \tparam IM A read-writable item map with \c int values, used
[559]52    /// internally to handle the cross references.
[709]53    /// \tparam CMP A functor class for comparing the priorities.
[559]54    /// The default is \c std::less<PR>.
55#ifdef DOXYGEN
[709]56    template <typename PR, typename IM, typename CMP>
[559]57#else
[709]58    template <typename PR, typename IM, typename CMP = std::less<PR> >
[559]59#endif
[100]60    class Heap {
61    public:
62
[559]63      /// Type of the item-int map.
64      typedef IM ItemIntMap;
65      /// Type of the priorities.
66      typedef PR Prio;
[113]67      /// Type of the items stored in the heap.
68      typedef typename ItemIntMap::Key Item;
[100]69
[113]70      /// \brief Type to represent the states of the items.
[100]71      ///
[113]72      /// Each item has a state associated to it. It can be "in heap",
[709]73      /// "pre-heap" or "post-heap". The latter two are indifferent from the
74      /// heap's point of view, but may be useful to the user.
[100]75      ///
[559]76      /// The item-int map must be initialized in such way that it assigns
77      /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
[100]78      enum State {
[584]79        IN_HEAP = 0,    ///< = 0. The "in heap" state constant.
[709]80        PRE_HEAP = -1,  ///< = -1. The "pre-heap" state constant.
81        POST_HEAP = -2  ///< = -2. The "post-heap" state constant.
[100]82      };
[209]83
[709]84      /// \brief Constructor.
[100]85      ///
[709]86      /// Constructor.
[113]87      /// \param map A map that assigns \c int values to keys of type
88      /// \c Item. It is used internally by the heap implementations to
89      /// handle the cross references. The assigned value must be
[709]90      /// \c PRE_HEAP (<tt>-1</tt>) for each item.
[113]91      explicit Heap(ItemIntMap &map) {}
[100]92
[709]93      /// \brief Constructor.
94      ///
95      /// Constructor.
96      /// \param map A map that assigns \c int values to keys of type
97      /// \c Item. It is used internally by the heap implementations to
98      /// handle the cross references. The assigned value must be
99      /// \c PRE_HEAP (<tt>-1</tt>) for each item.
100      /// \param comp The function object used for comparing the priorities.
101      explicit Heap(ItemIntMap &map, const CMP &comp) {}
102
[100]103      /// \brief The number of items stored in the heap.
104      ///
[709]105      /// This function returns the number of items stored in the heap.
[100]106      int size() const { return 0; }
107
[709]108      /// \brief Check if the heap is empty.
[100]109      ///
[709]110      /// This function returns \c true if the heap is empty.
[100]111      bool empty() const { return false; }
112
[709]113      /// \brief Make the heap empty.
[100]114      ///
[709]115      /// This functon makes the heap empty.
116      /// It does not change the cross reference map. If you want to reuse
117      /// a heap that is not surely empty, you should first clear it and
118      /// then you should set the cross reference map to \c PRE_HEAP
119      /// for each item.
120      void clear() {}
[100]121
[709]122      /// \brief Insert an item into the heap with the given priority.
[209]123      ///
[709]124      /// This function inserts the given item into the heap with the
125      /// given priority.
[100]126      /// \param i The item to insert.
127      /// \param p The priority of the item.
[709]128      /// \pre \e i must not be stored in the heap.
[100]129      void push(const Item &i, const Prio &p) {}
130
[709]131      /// \brief Return the item having minimum priority.
[100]132      ///
[709]133      /// This function returns the item having minimum priority.
[113]134      /// \pre The heap must be non-empty.
[100]135      Item top() const {}
136
[113]137      /// \brief The minimum priority.
[100]138      ///
[709]139      /// This function returns the minimum priority.
[113]140      /// \pre The heap must be non-empty.
[100]141      Prio prio() const {}
142
[709]143      /// \brief Remove the item having minimum priority.
[100]144      ///
[709]145      /// This function removes the item having minimum priority.
[113]146      /// \pre The heap must be non-empty.
[100]147      void pop() {}
148
[709]149      /// \brief Remove the given item from the heap.
[100]150      ///
[709]151      /// This function removes the given item from the heap if it is
152      /// already stored.
[209]153      /// \param i The item to delete.
[709]154      /// \pre \e i must be in the heap.
[100]155      void erase(const Item &i) {}
156
[709]157      /// \brief The priority of the given item.
[100]158      ///
[709]159      /// This function returns the priority of the given item.
[559]160      /// \param i The item.
[709]161      /// \pre \e i must be in the heap.
[100]162      Prio operator[](const Item &i) const {}
163
[709]164      /// \brief Set the priority of an item or insert it, if it is
[113]165      /// not stored in the heap.
[100]166      ///
[113]167      /// This method sets the priority of the given item if it is
[709]168      /// already stored in the heap. Otherwise it inserts the given
169      /// item into the heap with the given priority.
[113]170      ///
[100]171      /// \param i The item.
172      /// \param p The priority.
173      void set(const Item &i, const Prio &p) {}
[209]174
[709]175      /// \brief Decrease the priority of an item to the given value.
[100]176      ///
[709]177      /// This function decreases the priority of an item to the given value.
[100]178      /// \param i The item.
179      /// \param p The priority.
[709]180      /// \pre \e i must be stored in the heap with priority at least \e p.
[100]181      void decrease(const Item &i, const Prio &p) {}
182
[709]183      /// \brief Increase the priority of an item to the given value.
[100]184      ///
[709]185      /// This function increases the priority of an item to the given value.
[100]186      /// \param i The item.
187      /// \param p The priority.
[709]188      /// \pre \e i must be stored in the heap with priority at most \e p.
[100]189      void increase(const Item &i, const Prio &p) {}
190
[709]191      /// \brief Return the state of an item.
[100]192      ///
[113]193      /// This method returns \c PRE_HEAP if the given item has never
194      /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
195      /// and \c POST_HEAP otherwise.
196      /// In the latter case it is possible that the item will get back
197      /// to the heap again.
[100]198      /// \param i The item.
199      State state(const Item &i) const {}
200
[709]201      /// \brief Set the state of an item in the heap.
[100]202      ///
[709]203      /// This function sets the state of the given item in the heap.
204      /// It can be used to manually clear the heap when it is important
205      /// to achive better time complexity.
[100]206      /// \param i The item.
[113]207      /// \param st The state. It should not be \c IN_HEAP.
[100]208      void state(const Item& i, State st) {}
209
210
211      template <typename _Heap>
212      struct Constraints {
213      public:
[209]214        void constraints() {
215          typedef typename _Heap::Item OwnItem;
216          typedef typename _Heap::Prio OwnPrio;
217          typedef typename _Heap::State OwnState;
[113]218
[209]219          Item item;
220          Prio prio;
221          item=Item();
222          prio=Prio();
223          ignore_unused_variable_warning(item);
224          ignore_unused_variable_warning(prio);
[100]225
[209]226          OwnItem own_item;
227          OwnPrio own_prio;
228          OwnState own_state;
229          own_item=Item();
230          own_prio=Prio();
231          ignore_unused_variable_warning(own_item);
232          ignore_unused_variable_warning(own_prio);
233          ignore_unused_variable_warning(own_state);
[100]234
[209]235          _Heap heap1(map);
236          _Heap heap2 = heap1;
237          ignore_unused_variable_warning(heap1);
238          ignore_unused_variable_warning(heap2);
[100]239
[209]240          int s = heap.size();
241          ignore_unused_variable_warning(s);
242          bool e = heap.empty();
243          ignore_unused_variable_warning(e);
[100]244
[209]245          prio = heap.prio();
246          item = heap.top();
247          prio = heap[item];
248          own_prio = heap.prio();
249          own_item = heap.top();
250          own_prio = heap[own_item];
[100]251
[209]252          heap.push(item, prio);
253          heap.push(own_item, own_prio);
254          heap.pop();
[100]255
[209]256          heap.set(item, prio);
257          heap.decrease(item, prio);
258          heap.increase(item, prio);
259          heap.set(own_item, own_prio);
260          heap.decrease(own_item, own_prio);
261          heap.increase(own_item, own_prio);
[100]262
[209]263          heap.erase(item);
264          heap.erase(own_item);
265          heap.clear();
[100]266
[209]267          own_state = heap.state(own_item);
268          heap.state(own_item, own_state);
[100]269
[209]270          own_state = _Heap::PRE_HEAP;
271          own_state = _Heap::IN_HEAP;
272          own_state = _Heap::POST_HEAP;
273        }
274
275        _Heap& heap;
276        ItemIntMap& map;
[100]277      };
278    };
279
280    /// @}
281  } // namespace lemon
282}
[529]283#endif
Note: See TracBrowser for help on using the repository browser.