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