[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 | * |
---|
| 5 | * Copyright (C) 2003-2008 |
---|
| 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 | |
---|
| 19 | ///\ingroup concept |
---|
| 20 | ///\file |
---|
[113] | 21 | ///\brief The concept of heaps. |
---|
[100] | 22 | |
---|
| 23 | #ifndef LEMON_CONCEPT_HEAP_H |
---|
| 24 | #define LEMON_CONCEPT_HEAP_H |
---|
| 25 | |
---|
[220] | 26 | #include <lemon/core.h> |
---|
[100] | 27 | |
---|
| 28 | namespace lemon { |
---|
[113] | 29 | |
---|
[100] | 30 | namespace concepts { |
---|
[113] | 31 | |
---|
[100] | 32 | /// \addtogroup concept |
---|
| 33 | /// @{ |
---|
| 34 | |
---|
[113] | 35 | /// \brief The heap concept. |
---|
[100] | 36 | /// |
---|
[113] | 37 | /// Concept class describing the main interface of heaps. |
---|
| 38 | template <typename Priority, typename ItemIntMap> |
---|
[100] | 39 | class Heap { |
---|
| 40 | public: |
---|
| 41 | |
---|
[113] | 42 | /// Type of the items stored in the heap. |
---|
| 43 | typedef typename ItemIntMap::Key Item; |
---|
[100] | 44 | |
---|
[113] | 45 | /// Type of the priorities. |
---|
| 46 | typedef Priority Prio; |
---|
| 47 | |
---|
| 48 | /// \brief Type to represent the states of the items. |
---|
[100] | 49 | /// |
---|
[113] | 50 | /// Each item has a state associated to it. It can be "in heap", |
---|
| 51 | /// "pre heap" or "post heap". The later two are indifferent |
---|
| 52 | /// from the point of view of the heap, but may be useful for |
---|
| 53 | /// the user. |
---|
[100] | 54 | /// |
---|
[209] | 55 | /// The \c ItemIntMap must be initialized in such a way, that it |
---|
[113] | 56 | /// assigns \c PRE_HEAP (<tt>-1</tt>) to every item. |
---|
[100] | 57 | enum State { |
---|
[209] | 58 | IN_HEAP = 0, |
---|
| 59 | PRE_HEAP = -1, |
---|
| 60 | POST_HEAP = -2 |
---|
[100] | 61 | }; |
---|
[209] | 62 | |
---|
[100] | 63 | /// \brief The constructor. |
---|
| 64 | /// |
---|
| 65 | /// The constructor. |
---|
[113] | 66 | /// \param map A map that assigns \c int values to keys of type |
---|
| 67 | /// \c Item. It is used internally by the heap implementations to |
---|
| 68 | /// handle the cross references. The assigned value must be |
---|
| 69 | /// \c PRE_HEAP (<tt>-1</tt>) for every item. |
---|
| 70 | explicit Heap(ItemIntMap &map) {} |
---|
[100] | 71 | |
---|
| 72 | /// \brief The number of items stored in the heap. |
---|
| 73 | /// |
---|
| 74 | /// Returns the number of items stored in the heap. |
---|
| 75 | int size() const { return 0; } |
---|
| 76 | |
---|
[113] | 77 | /// \brief Checks if the heap is empty. |
---|
[100] | 78 | /// |
---|
[113] | 79 | /// Returns \c true if the heap is empty. |
---|
[100] | 80 | bool empty() const { return false; } |
---|
| 81 | |
---|
[113] | 82 | /// \brief Makes the heap empty. |
---|
[100] | 83 | /// |
---|
[113] | 84 | /// Makes the heap empty. |
---|
[100] | 85 | void clear(); |
---|
| 86 | |
---|
[113] | 87 | /// \brief Inserts an item into the heap with the given priority. |
---|
[209] | 88 | /// |
---|
| 89 | /// Inserts the given item into the heap with the given priority. |
---|
[100] | 90 | /// \param i The item to insert. |
---|
| 91 | /// \param p The priority of the item. |
---|
| 92 | void push(const Item &i, const Prio &p) {} |
---|
| 93 | |
---|
[113] | 94 | /// \brief Returns the item having minimum priority. |
---|
[100] | 95 | /// |
---|
[113] | 96 | /// Returns the item having minimum priority. |
---|
| 97 | /// \pre The heap must be non-empty. |
---|
[100] | 98 | Item top() const {} |
---|
| 99 | |
---|
[113] | 100 | /// \brief The minimum priority. |
---|
[100] | 101 | /// |
---|
[113] | 102 | /// Returns the minimum priority. |
---|
| 103 | /// \pre The heap must be non-empty. |
---|
[100] | 104 | Prio prio() const {} |
---|
| 105 | |
---|
[113] | 106 | /// \brief Removes the item having minimum priority. |
---|
[100] | 107 | /// |
---|
[113] | 108 | /// Removes the item having minimum priority. |
---|
| 109 | /// \pre The heap must be non-empty. |
---|
[100] | 110 | void pop() {} |
---|
| 111 | |
---|
[113] | 112 | /// \brief Removes an item from the heap. |
---|
[100] | 113 | /// |
---|
[113] | 114 | /// Removes the given item from the heap if it is already stored. |
---|
[209] | 115 | /// \param i The item to delete. |
---|
[100] | 116 | void erase(const Item &i) {} |
---|
| 117 | |
---|
[113] | 118 | /// \brief The priority of an item. |
---|
[100] | 119 | /// |
---|
[209] | 120 | /// Returns the priority of the given item. |
---|
[100] | 121 | /// \pre \c i must be in the heap. |
---|
| 122 | /// \param i The item. |
---|
| 123 | Prio operator[](const Item &i) const {} |
---|
| 124 | |
---|
[113] | 125 | /// \brief Sets the priority of an item or inserts it, if it is |
---|
| 126 | /// not stored in the heap. |
---|
[100] | 127 | /// |
---|
[113] | 128 | /// This method sets the priority of the given item if it is |
---|
| 129 | /// already stored in the heap. |
---|
| 130 | /// Otherwise it inserts the given item with the given priority. |
---|
| 131 | /// |
---|
[100] | 132 | /// \param i The item. |
---|
| 133 | /// \param p The priority. |
---|
| 134 | void set(const Item &i, const Prio &p) {} |
---|
[209] | 135 | |
---|
[113] | 136 | /// \brief Decreases the priority of an item to the given value. |
---|
[100] | 137 | /// |
---|
[113] | 138 | /// Decreases the priority of an item to the given value. |
---|
[100] | 139 | /// \pre \c i must be stored in the heap with priority at least \c p. |
---|
| 140 | /// \param i The item. |
---|
| 141 | /// \param p The priority. |
---|
| 142 | void decrease(const Item &i, const Prio &p) {} |
---|
| 143 | |
---|
[113] | 144 | /// \brief Increases the priority of an item to the given value. |
---|
[100] | 145 | /// |
---|
[113] | 146 | /// Increases the priority of an item to the given value. |
---|
| 147 | /// \pre \c i must be stored in the heap with priority at most \c p. |
---|
[100] | 148 | /// \param i The item. |
---|
| 149 | /// \param p The priority. |
---|
| 150 | void increase(const Item &i, const Prio &p) {} |
---|
| 151 | |
---|
[113] | 152 | /// \brief Returns if an item is in, has already been in, or has |
---|
[100] | 153 | /// never been in the heap. |
---|
| 154 | /// |
---|
[113] | 155 | /// This method returns \c PRE_HEAP if the given item has never |
---|
| 156 | /// been in the heap, \c IN_HEAP if it is in the heap at the moment, |
---|
| 157 | /// and \c POST_HEAP otherwise. |
---|
| 158 | /// In the latter case it is possible that the item will get back |
---|
| 159 | /// to the heap again. |
---|
[100] | 160 | /// \param i The item. |
---|
| 161 | State state(const Item &i) const {} |
---|
| 162 | |
---|
[113] | 163 | /// \brief Sets the state of an item in the heap. |
---|
[100] | 164 | /// |
---|
[113] | 165 | /// Sets the state of the given item in the heap. It can be used |
---|
| 166 | /// to manually clear the heap when it is important to achive the |
---|
[100] | 167 | /// better time complexity. |
---|
| 168 | /// \param i The item. |
---|
[113] | 169 | /// \param st The state. It should not be \c IN_HEAP. |
---|
[100] | 170 | void state(const Item& i, State st) {} |
---|
| 171 | |
---|
| 172 | |
---|
| 173 | template <typename _Heap> |
---|
| 174 | struct Constraints { |
---|
| 175 | public: |
---|
[209] | 176 | void constraints() { |
---|
| 177 | typedef typename _Heap::Item OwnItem; |
---|
| 178 | typedef typename _Heap::Prio OwnPrio; |
---|
| 179 | typedef typename _Heap::State OwnState; |
---|
[113] | 180 | |
---|
[209] | 181 | Item item; |
---|
| 182 | Prio prio; |
---|
| 183 | item=Item(); |
---|
| 184 | prio=Prio(); |
---|
| 185 | ignore_unused_variable_warning(item); |
---|
| 186 | ignore_unused_variable_warning(prio); |
---|
[100] | 187 | |
---|
[209] | 188 | OwnItem own_item; |
---|
| 189 | OwnPrio own_prio; |
---|
| 190 | OwnState own_state; |
---|
| 191 | own_item=Item(); |
---|
| 192 | own_prio=Prio(); |
---|
| 193 | ignore_unused_variable_warning(own_item); |
---|
| 194 | ignore_unused_variable_warning(own_prio); |
---|
| 195 | ignore_unused_variable_warning(own_state); |
---|
[100] | 196 | |
---|
[209] | 197 | _Heap heap1(map); |
---|
| 198 | _Heap heap2 = heap1; |
---|
| 199 | ignore_unused_variable_warning(heap1); |
---|
| 200 | ignore_unused_variable_warning(heap2); |
---|
[100] | 201 | |
---|
[209] | 202 | int s = heap.size(); |
---|
| 203 | ignore_unused_variable_warning(s); |
---|
| 204 | bool e = heap.empty(); |
---|
| 205 | ignore_unused_variable_warning(e); |
---|
[100] | 206 | |
---|
[209] | 207 | prio = heap.prio(); |
---|
| 208 | item = heap.top(); |
---|
| 209 | prio = heap[item]; |
---|
| 210 | own_prio = heap.prio(); |
---|
| 211 | own_item = heap.top(); |
---|
| 212 | own_prio = heap[own_item]; |
---|
[100] | 213 | |
---|
[209] | 214 | heap.push(item, prio); |
---|
| 215 | heap.push(own_item, own_prio); |
---|
| 216 | heap.pop(); |
---|
[100] | 217 | |
---|
[209] | 218 | heap.set(item, prio); |
---|
| 219 | heap.decrease(item, prio); |
---|
| 220 | heap.increase(item, prio); |
---|
| 221 | heap.set(own_item, own_prio); |
---|
| 222 | heap.decrease(own_item, own_prio); |
---|
| 223 | heap.increase(own_item, own_prio); |
---|
[100] | 224 | |
---|
[209] | 225 | heap.erase(item); |
---|
| 226 | heap.erase(own_item); |
---|
| 227 | heap.clear(); |
---|
[100] | 228 | |
---|
[209] | 229 | own_state = heap.state(own_item); |
---|
| 230 | heap.state(own_item, own_state); |
---|
[100] | 231 | |
---|
[209] | 232 | own_state = _Heap::PRE_HEAP; |
---|
| 233 | own_state = _Heap::IN_HEAP; |
---|
| 234 | own_state = _Heap::POST_HEAP; |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | _Heap& heap; |
---|
| 238 | ItemIntMap& map; |
---|
[100] | 239 | }; |
---|
| 240 | }; |
---|
| 241 | |
---|
| 242 | /// @} |
---|
| 243 | } // namespace lemon |
---|
| 244 | } |
---|
| 245 | #endif // LEMON_CONCEPT_PATH_H |
---|