| 1 | /* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | * |
| 3 | * This file is a part of LEMON, a generic C++ optimization library. |
| 4 | * |
| 5 | * Copyright (C) 2003-2009 |
| 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 | #ifndef LEMON_RADIX_HEAP_H |
| 20 | #define LEMON_RADIX_HEAP_Hcxx . . $ `lemon/radix_heap.h:20:9`! |
| 21 | |
| 22 | ///\ingroup heaps |
| 23 | ///\file |
| 24 | ///\brief Radix heap implementation. |
| 25 | |
| 26 | #include <vector> |
| 27 | #include <lemon/error.h>cxx . . $ `<file>/lemon/error.h`/ |
| 28 | |
| 29 | namespace lemoncxx . . $ lemon/ { |
| 30 | |
| 31 | |
| 32 | /// \ingroup heaps |
| 33 | /// |
| 34 | /// \brief Radix heap data structure. |
| 35 | /// |
| 36 | /// This class implements the \e radix \e heap data structure. |
| 37 | /// It practically conforms to the \ref concepts::Heap "heap concept", |
| 38 | /// but it has some limitations due its special implementation. |
| 39 | /// The type of the priorities must be \c int and the priority of an |
| 40 | /// item cannot be decreased under the priority of the last removed item. |
| 41 | /// |
| 42 | /// \tparam IM A read-writable item map with \c int values, used |
| 43 | /// internally to handle the cross references. |
| 44 | template <typename IM> |
| 45 | class RadixHeapcxx . . $ lemon/RadixHeap# { |
| 46 | |
| 47 | public: |
| 48 | |
| 49 | /// Type of the item-int map. |
| 50 | typedef IM ItemIntMapcxx . . $ lemon/RadixHeap#ItemIntMap#; |
| 51 | /// Type of the priorities. |
| 52 | typedef int Priocxx . . $ lemon/RadixHeap#Prio#; |
| 53 | /// Type of the items stored in the heap. |
| 54 | typedef typename ItemIntMapcxx . . $ lemon/RadixHeap#ItemIntMap#::Key Itemcxx . . $ lemon/RadixHeap#Item#; |
| 55 | |
| 56 | /// \brief Exception thrown by RadixHeap. |
| 57 | /// |
| 58 | /// This exception is thrown when an item is inserted into a |
| 59 | /// RadixHeap with a priority smaller than the last erased one. |
| 60 | /// \see RadixHeap |
| 61 | class PriorityUnderflowErrorcxx . . $ lemon/RadixHeap#PriorityUnderflowError# : public Exceptioncxx . . $ lemon/Exception# { |
| 62 | public: |
| 63 | virtual const char* whatcxx . . $ lemon/RadixHeap#PriorityUnderflowError#what(ba32b354807b2b81).() const throw() { |
| 64 | return "lemon::RadixHeap::PriorityUnderflowError"; |
| 65 | } |
| 66 | }; |
| 67 | |
| 68 | /// \brief Type to represent the states of the items. |
| 69 | /// |
| 70 | /// Each item has a state associated to it. It can be "in heap", |
| 71 | /// "pre-heap" or "post-heap". The latter two are indifferent from the |
| 72 | /// heap's point of view, but may be useful to the user. |
| 73 | /// |
| 74 | /// The item-int map must be initialized in such way that it assigns |
| 75 | /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap. |
| 76 | enum Statecxx . . $ lemon/RadixHeap#State# { |
| 77 | IN_HEAPcxx . . $ lemon/RadixHeap#IN_HEAP. = 0, ///< = 0. |
| 78 | PRE_HEAPcxx . . $ lemon/RadixHeap#PRE_HEAP. = -1, ///< = -1. |
| 79 | POST_HEAPcxx . . $ lemon/RadixHeap#POST_HEAP. = -2 ///< = -2. |
| 80 | }; |
| 81 | |
| 82 | private: |
| 83 | |
| 84 | struct RadixItemcxx . . $ lemon/RadixHeap#RadixItem# { |
| 85 | int prevcxx . . $ lemon/RadixHeap#RadixItem#prev., nextcxx . . $ lemon/RadixHeap#RadixItem#next., boxcxx . . $ lemon/RadixHeap#RadixItem#box.; |
| 86 | Itemcxx . . $ lemon/RadixHeap#Item# itemcxx . . $ lemon/RadixHeap#RadixItem#item.; |
| 87 | int priocxx . . $ lemon/RadixHeap#RadixItem#prio.; |
| 88 | RadixItemcxx . . $ lemon/RadixHeap#RadixItem#RadixItem(bb1defab5f1f5434).(Itemcxx . . $ lemon/RadixHeap#Item# _item, int _prio) : itemcxx . . $ lemon/RadixHeap#RadixItem#item.(_item), priocxx . . $ lemon/RadixHeap#RadixItem#prio.(_prio) {} |
| 89 | }; |
| 90 | |
| 91 | struct RadixBoxcxx . . $ lemon/RadixHeap#RadixBox# { |
| 92 | int firstcxx . . $ lemon/RadixHeap#RadixBox#first.; |
| 93 | int mincxx . . $ lemon/RadixHeap#RadixBox#min., sizecxx . . $ lemon/RadixHeap#RadixBox#size.; |
| 94 | RadixBoxcxx . . $ lemon/RadixHeap#RadixBox#RadixBox(3e454b8ccf442868).(int _min, int _size) : firstcxx . . $ lemon/RadixHeap#RadixBox#first.(-1), mincxx . . $ lemon/RadixHeap#RadixBox#min.(_min), sizecxx . . $ lemon/RadixHeap#RadixBox#size.(_size) {} |
| 95 | }; |
| 96 | |
| 97 | std::vectorcxx . . $ std/vector#<RadixItemcxx . . $ lemon/RadixHeap#RadixItem#> _datacxx . . $ lemon/RadixHeap#_data.; |
| 98 | std::vectorcxx . . $ std/vector#<RadixBoxcxx . . $ lemon/RadixHeap#RadixBox#> _boxescxx . . $ lemon/RadixHeap#_boxes.; |
| 99 | |
| 100 | ItemIntMapcxx . . $ lemon/RadixHeap#ItemIntMap# &_iimcxx . . $ lemon/RadixHeap#_iim.; |
| 101 | |
| 102 | public: |
| 103 | |
| 104 | /// \brief Constructor. |
| 105 | /// |
| 106 | /// Constructor. |
| 107 | /// \param map A map that assigns \c int values to the items. |
| 108 | /// It is used internally to handle the cross references. |
| 109 | /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item. |
| 110 | /// \param minimum The initial minimum value of the heap. |
| 111 | /// \param capacity The initial capacity of the heap. |
| 112 | RadixHeapcxx . . $ lemon/RadixHeap#`RadixHeap<IM>`(fcb4823832f8b527).(ItemIntMapcxx . . $ lemon/RadixHeap#ItemIntMap# &map, int minimum = 0, int capacity = 0) |
| 113 | : _iimcxx . . $ lemon/RadixHeap#_iim.(map) |
| 114 | { |
| 115 | _boxescxx . . $ lemon/RadixHeap#_boxes..push_backcxx . . $ std/vector#push_back(77d9f17553126ecb).(RadixBoxcxx . . $ lemon/RadixHeap#RadixBox#(minimum, 1)); |
| 116 | _boxescxx . . $ lemon/RadixHeap#_boxes..push_backcxx . . $ std/vector#push_back(77d9f17553126ecb).(RadixBoxcxx . . $ lemon/RadixHeap#RadixBox#(minimum + 1, 1)); |
| 117 | while (lowercxx . . $ lemon/RadixHeap#lower(cf9ef193275fa6d8).(_boxescxx . . $ lemon/RadixHeap#_boxes..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).() - 1, capacity + minimum - 1)) { |
| 118 | extendcxx . . $ lemon/RadixHeap#extend(49f6e7a06ebc5aa8).(); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /// \brief The number of items stored in the heap. |
| 123 | /// |
| 124 | /// This function returns the number of items stored in the heap. |
| 125 | int sizecxx . . $ lemon/RadixHeap#size(455f465bc33b4cdf).() const { return _datacxx . . $ lemon/RadixHeap#_data..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).(); } |
| 126 | |
| 127 | /// \brief Check if the heap is empty. |
| 128 | /// |
| 129 | /// This function returns \c true if the heap is empty. |
| 130 | bool emptycxx . . $ lemon/RadixHeap#empty(50ce9a9e25b4a850).() const { return _datacxx . . $ lemon/RadixHeap#_data..emptycxx . . $ std/vector#empty(3482b152b9333168).(); } |
| 131 | |
| 132 | /// \brief Make the heap empty. |
| 133 | /// |
| 134 | /// This functon makes the heap empty. |
| 135 | /// It does not change the cross reference map. If you want to reuse |
| 136 | /// a heap that is not surely empty, you should first clear it and |
| 137 | /// then you should set the cross reference map to \c PRE_HEAP |
| 138 | /// for each item. |
| 139 | /// \param minimum The minimum value of the heap. |
| 140 | /// \param capacity The capacity of the heap. |
| 141 | void clearcxx . . $ lemon/RadixHeap#clear(3e454b8ccf442868).(int minimum = 0, int capacity = 0) { |
| 142 | _datacxx . . $ lemon/RadixHeap#_data..clearcxx . . $ std/vector#clear(ced63f7c635d850d).(); _boxescxx . . $ lemon/RadixHeap#_boxes..clearcxx . . $ std/vector#clear(ced63f7c635d850d).(); |
| 143 | _boxescxx . . $ lemon/RadixHeap#_boxes..push_backcxx . . $ std/vector#push_back(77d9f17553126ecb).(RadixBoxcxx . . $ lemon/RadixHeap#RadixBox#(minimum, 1)); |
| 144 | _boxescxx . . $ lemon/RadixHeap#_boxes..push_backcxx . . $ std/vector#push_back(77d9f17553126ecb).(RadixBoxcxx . . $ lemon/RadixHeap#RadixBox#(minimum + 1, 1)); |
| 145 | while (lowercxx . . $ lemon/RadixHeap#lower(cf9ef193275fa6d8).(_boxescxx . . $ lemon/RadixHeap#_boxes..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).() - 1, capacity + minimum - 1)) { |
| 146 | extendcxx . . $ lemon/RadixHeap#extend(49f6e7a06ebc5aa8).(); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | private: |
| 151 | |
| 152 | bool uppercxx . . $ lemon/RadixHeap#upper(cf9ef193275fa6d8).(int box, Priocxx . . $ lemon/RadixHeap#Prio# pr) { |
| 153 | return pr < _boxescxx . . $ lemon/RadixHeap#_boxes.[box].min; |
| 154 | } |
| 155 | |
| 156 | bool lowercxx . . $ lemon/RadixHeap#lower(cf9ef193275fa6d8).(int box, Priocxx . . $ lemon/RadixHeap#Prio# pr) { |
| 157 | return pr >= _boxescxx . . $ lemon/RadixHeap#_boxes.[box].min + _boxescxx . . $ lemon/RadixHeap#_boxes.[box].size; |
| 158 | } |
| 159 | |
| 160 | // Remove item from the box list |
| 161 | void removecxx . . $ lemon/RadixHeap#remove(d4f767463ce0a6b3).(int index) { |
| 162 | if (_datacxx . . $ lemon/RadixHeap#_data.[index].prev >= 0) { |
| 163 | _datacxx . . $ lemon/RadixHeap#_data.[_datacxx . . $ lemon/RadixHeap#_data.[index].prev].next = _datacxx . . $ lemon/RadixHeap#_data.[index].next; |
| 164 | } else { |
| 165 | _boxescxx . . $ lemon/RadixHeap#_boxes.[_datacxx . . $ lemon/RadixHeap#_data.[index].box].first = _datacxx . . $ lemon/RadixHeap#_data.[index].next; |
| 166 | } |
| 167 | if (_datacxx . . $ lemon/RadixHeap#_data.[index].next >= 0) { |
| 168 | _datacxx . . $ lemon/RadixHeap#_data.[_datacxx . . $ lemon/RadixHeap#_data.[index].next].prev = _datacxx . . $ lemon/RadixHeap#_data.[index].prev; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | // Insert item into the box list |
| 173 | void insertcxx . . $ lemon/RadixHeap#insert(3e454b8ccf442868).(int box, int index) { |
| 174 | if (_boxescxx . . $ lemon/RadixHeap#_boxes.[box].first == -1) { |
| 175 | _boxescxx . . $ lemon/RadixHeap#_boxes.[box].first = index; |
| 176 | _datacxx . . $ lemon/RadixHeap#_data.[index].next = _datacxx . . $ lemon/RadixHeap#_data.[index].prev = -1; |
| 177 | } else { |
| 178 | _datacxx . . $ lemon/RadixHeap#_data.[index].next = _boxescxx . . $ lemon/RadixHeap#_boxes.[box].first; |
| 179 | _datacxx . . $ lemon/RadixHeap#_data.[_boxescxx . . $ lemon/RadixHeap#_boxes.[box].first].prev = index; |
| 180 | _datacxx . . $ lemon/RadixHeap#_data.[index].prev = -1; |
| 181 | _boxescxx . . $ lemon/RadixHeap#_boxes.[box].first = index; |
| 182 | } |
| 183 | _datacxx . . $ lemon/RadixHeap#_data.[index].box = box; |
| 184 | } |
| 185 | |
| 186 | // Add a new box to the box list |
| 187 | void extendcxx . . $ lemon/RadixHeap#extend(49f6e7a06ebc5aa8).() { |
| 188 | int min = _boxescxx . . $ lemon/RadixHeap#_boxes..backcxx . . $ std/vector#back(6baabfa6fc7b3a8e).().min + _boxescxx . . $ lemon/RadixHeap#_boxes..backcxx . . $ std/vector#back(6baabfa6fc7b3a8e).().size; |
| 189 | int bs = 2 * _boxescxx . . $ lemon/RadixHeap#_boxes..backcxx . . $ std/vector#back(6baabfa6fc7b3a8e).().size; |
| 190 | _boxescxx . . $ lemon/RadixHeap#_boxes..push_backcxx . . $ std/vector#push_back(77d9f17553126ecb).(RadixBoxcxx . . $ lemon/RadixHeap#RadixBox#(min, bs)); |
| 191 | } |
| 192 | |
| 193 | // Move an item up into the proper box. |
| 194 | void bubbleUpcxx . . $ lemon/RadixHeap#bubbleUp(d4f767463ce0a6b3).(int index) { |
| 195 | if (!lowercxx . . $ lemon/RadixHeap#lower(cf9ef193275fa6d8).(_datacxx . . $ lemon/RadixHeap#_data.[index].box, _datacxx . . $ lemon/RadixHeap#_data.[index].prio)) return; |
| 196 | removecxx . . $ lemon/RadixHeap#remove(d4f767463ce0a6b3).(index); |
| 197 | int box = findUpcxx . . $ lemon/RadixHeap#findUp(9b79fb6aee4c0440).(_datacxx . . $ lemon/RadixHeap#_data.[index].box, _datacxx . . $ lemon/RadixHeap#_data.[index].prio); |
| 198 | insertcxx . . $ lemon/RadixHeap#insert(3e454b8ccf442868).(box, index); |
| 199 | } |
| 200 | |
| 201 | // Find up the proper box for the item with the given priority |
| 202 | int findUpcxx . . $ lemon/RadixHeap#findUp(9b79fb6aee4c0440).(int start, int pr) { |
| 203 | while (lowercxx . . $ lemon/RadixHeap#lower(cf9ef193275fa6d8).(start, pr)) { |
| 204 | if (++start == int(_boxescxx . . $ lemon/RadixHeap#_boxes..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).())) { |
| 205 | extendcxx . . $ lemon/RadixHeap#extend(49f6e7a06ebc5aa8).(); |
| 206 | } |
| 207 | } |
| 208 | return start; |
| 209 | } |
| 210 | |
| 211 | // Move an item down into the proper box |
| 212 | void bubbleDowncxx . . $ lemon/RadixHeap#bubbleDown(d4f767463ce0a6b3).(int index) { |
| 213 | if (!uppercxx . . $ lemon/RadixHeap#upper(cf9ef193275fa6d8).(_datacxx . . $ lemon/RadixHeap#_data.[index].box, _datacxx . . $ lemon/RadixHeap#_data.[index].prio)) return; |
| 214 | removecxx . . $ lemon/RadixHeap#remove(d4f767463ce0a6b3).(index); |
| 215 | int box = findDowncxx . . $ lemon/RadixHeap#findDown(9b79fb6aee4c0440).(_datacxx . . $ lemon/RadixHeap#_data.[index].box, _datacxx . . $ lemon/RadixHeap#_data.[index].prio); |
| 216 | insertcxx . . $ lemon/RadixHeap#insert(3e454b8ccf442868).(box, index); |
| 217 | } |
| 218 | |
| 219 | // Find down the proper box for the item with the given priority |
| 220 | int findDowncxx . . $ lemon/RadixHeap#findDown(9b79fb6aee4c0440).(int start, int pr) { |
| 221 | while (uppercxx . . $ lemon/RadixHeap#upper(cf9ef193275fa6d8).(start, pr)) { |
| 222 | if (--start < 0) throw PriorityUnderflowErrorcxx . . $ lemon/RadixHeap#PriorityUnderflowError#(); |
| 223 | } |
| 224 | return start; |
| 225 | } |
| 226 | |
| 227 | // Find the first non-empty box |
| 228 | int findFirstcxx . . $ lemon/RadixHeap#findFirst(b126dc7c1de90089).() { |
| 229 | int first = 0; |
| 230 | while (_boxescxx . . $ lemon/RadixHeap#_boxes.[first].first == -1) ++first; |
| 231 | return first; |
| 232 | } |
| 233 | |
| 234 | // Gives back the minimum priority of the given box |
| 235 | int minValuecxx . . $ lemon/RadixHeap#minValue(7864480464b09eea).(int box) { |
| 236 | int min = _datacxx . . $ lemon/RadixHeap#_data.[_boxescxx . . $ lemon/RadixHeap#_boxes.[box].first].prio; |
| 237 | for (int k = _boxescxx . . $ lemon/RadixHeap#_boxes.[box].first; k != -1; k = _datacxx . . $ lemon/RadixHeap#_data.[k].next) { |
| 238 | if (_datacxx . . $ lemon/RadixHeap#_data.[k].prio < min) min = _datacxx . . $ lemon/RadixHeap#_data.[k].prio; |
| 239 | } |
| 240 | return min; |
| 241 | } |
| 242 | |
| 243 | // Rearrange the items of the heap and make the first box non-empty |
| 244 | void moveDowncxx . . $ lemon/RadixHeap#moveDown(49f6e7a06ebc5aa8).() { |
| 245 | int box = findFirstcxx . . $ lemon/RadixHeap#findFirst(b126dc7c1de90089).(); |
| 246 | if (box == 0) return; |
| 247 | int min = minValuecxx . . $ lemon/RadixHeap#minValue(7864480464b09eea).(box); |
| 248 | for (int i = 0; i <= box; ++i) { |
| 249 | _boxescxx . . $ lemon/RadixHeap#_boxes.[i].min = min; |
| 250 | min += _boxescxx . . $ lemon/RadixHeap#_boxes.[i].size; |
| 251 | } |
| 252 | int curr = _boxescxx . . $ lemon/RadixHeap#_boxes.[box].first, next; |
| 253 | while (curr != -1) { |
| 254 | next = _datacxx . . $ lemon/RadixHeap#_data.[curr].next; |
| 255 | bubbleDowncxx . . $ lemon/RadixHeap#bubbleDown(d4f767463ce0a6b3).(curr); |
| 256 | curr = next; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | void relocateLastcxx . . $ lemon/RadixHeap#relocateLast(d4f767463ce0a6b3).(int index) { |
| 261 | if (index != int(_datacxx . . $ lemon/RadixHeap#_data..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).()) - 1) { |
| 262 | _datacxx . . $ lemon/RadixHeap#_data.[index] = _datacxx . . $ lemon/RadixHeap#_data..backcxx . . $ std/vector#back(6baabfa6fc7b3a8e).(); |
| 263 | if (_datacxx . . $ lemon/RadixHeap#_data.[index].prev != -1) { |
| 264 | _datacxx . . $ lemon/RadixHeap#_data.[_datacxx . . $ lemon/RadixHeap#_data.[index].prev].next = index; |
| 265 | } else { |
| 266 | _boxescxx . . $ lemon/RadixHeap#_boxes.[_datacxx . . $ lemon/RadixHeap#_data.[index].box].first = index; |
| 267 | } |
| 268 | if (_datacxx . . $ lemon/RadixHeap#_data.[index].next != -1) { |
| 269 | _datacxx . . $ lemon/RadixHeap#_data.[_datacxx . . $ lemon/RadixHeap#_data.[index].next].prev = index; |
| 270 | } |
| 271 | _iimcxx . . $ lemon/RadixHeap#_iim.[_datacxx . . $ lemon/RadixHeap#_data.[index].item] = index; |
| 272 | } |
| 273 | _datacxx . . $ lemon/RadixHeap#_data..pop_backcxx . . $ std/vector#pop_back(ced63f7c635d850d).(); |
| 274 | } |
| 275 | |
| 276 | public: |
| 277 | |
| 278 | /// \brief Insert an item into the heap with the given priority. |
| 279 | /// |
| 280 | /// This function inserts the given item into the heap with the |
| 281 | /// given priority. |
| 282 | /// \param i The item to insert. |
| 283 | /// \param p The priority of the item. |
| 284 | /// \pre \e i must not be stored in the heap. |
| 285 | /// \warning This method may throw an \c UnderFlowPriorityException. |
| 286 | void pushcxx . . $ lemon/RadixHeap#push(cf2f737abe8b3505).(const Itemcxx . . $ lemon/RadixHeap#Item# &i, const Priocxx . . $ lemon/RadixHeap#Prio# &p) { |
| 287 | int n = _datacxx . . $ lemon/RadixHeap#_data..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).(); |
| 288 | _iimcxx . . $ lemon/RadixHeap#_iim..set(i, n); |
| 289 | _datacxx . . $ lemon/RadixHeap#_data..push_backcxx . . $ std/vector#push_back(77d9f17553126ecb).(RadixItemcxx . . $ lemon/RadixHeap#RadixItem#(i, p)); |
| 290 | while (lowercxx . . $ lemon/RadixHeap#lower(cf9ef193275fa6d8).(_boxescxx . . $ lemon/RadixHeap#_boxes..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).() - 1, p)) { |
| 291 | extendcxx . . $ lemon/RadixHeap#extend(49f6e7a06ebc5aa8).(); |
| 292 | } |
| 293 | int box = findDowncxx . . $ lemon/RadixHeap#findDown(9b79fb6aee4c0440).(_boxescxx . . $ lemon/RadixHeap#_boxes..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).() - 1, p); |
| 294 | insertcxx . . $ lemon/RadixHeap#insert(3e454b8ccf442868).(box, n); |
| 295 | } |
| 296 | |
| 297 | /// \brief Return the item having minimum priority. |
| 298 | /// |
| 299 | /// This function returns the item having minimum priority. |
| 300 | /// \pre The heap must be non-empty. |
| 301 | Itemcxx . . $ lemon/RadixHeap#Item# topcxx . . $ lemon/RadixHeap#top(e6707c32af87d969).() const { |
| 302 | const_cast<RadixHeapcxx . . $ lemon/RadixHeap#<ItemIntMapcxx . . $ lemon/RadixHeap#ItemIntMap#>&>(*this).moveDowncxx . . $ lemon/RadixHeap#moveDown(49f6e7a06ebc5aa8).(); |
| 303 | return _datacxx . . $ lemon/RadixHeap#_data.[_boxescxx . . $ lemon/RadixHeap#_boxes.[0].first].item; |
| 304 | } |
| 305 | |
| 306 | /// \brief The minimum priority. |
| 307 | /// |
| 308 | /// This function returns the minimum priority. |
| 309 | /// \pre The heap must be non-empty. |
| 310 | Priocxx . . $ lemon/RadixHeap#Prio# priocxx . . $ lemon/RadixHeap#prio(455f465bc33b4cdf).() const { |
| 311 | const_cast<RadixHeapcxx . . $ lemon/RadixHeap#<ItemIntMapcxx . . $ lemon/RadixHeap#ItemIntMap#>&>(*this).moveDowncxx . . $ lemon/RadixHeap#moveDown(49f6e7a06ebc5aa8).(); |
| 312 | return _datacxx . . $ lemon/RadixHeap#_data.[_boxescxx . . $ lemon/RadixHeap#_boxes.[0].first].prio; |
| 313 | } |
| 314 | |
| 315 | /// \brief Remove the item having minimum priority. |
| 316 | /// |
| 317 | /// This function removes the item having minimum priority. |
| 318 | /// \pre The heap must be non-empty. |
| 319 | void popcxx . . $ lemon/RadixHeap#pop(49f6e7a06ebc5aa8).() { |
| 320 | moveDowncxx . . $ lemon/RadixHeap#moveDown(49f6e7a06ebc5aa8).(); |
| 321 | int index = _boxescxx . . $ lemon/RadixHeap#_boxes.[0].first; |
| 322 | _iimcxx . . $ lemon/RadixHeap#_iim.[_datacxx . . $ lemon/RadixHeap#_data.[index].item] = POST_HEAPcxx . . $ lemon/RadixHeap#POST_HEAP.; |
| 323 | removecxx . . $ lemon/RadixHeap#remove(d4f767463ce0a6b3).(index); |
| 324 | relocateLastcxx . . $ lemon/RadixHeap#relocateLast(d4f767463ce0a6b3).(index); |
| 325 | } |
| 326 | |
| 327 | /// \brief Remove the given item from the heap. |
| 328 | /// |
| 329 | /// This function removes the given item from the heap if it is |
| 330 | /// already stored. |
| 331 | /// \param i The item to delete. |
| 332 | /// \pre \e i must be in the heap. |
| 333 | void erasecxx . . $ lemon/RadixHeap#erase(8c13afcd6b0c9deb).(const Itemcxx . . $ lemon/RadixHeap#Item# &i) { |
| 334 | int index = _iimcxx . . $ lemon/RadixHeap#_iim.[i]; |
| 335 | _iimcxx . . $ lemon/RadixHeap#_iim.[i] = POST_HEAPcxx . . $ lemon/RadixHeap#POST_HEAP.; |
| 336 | removecxx . . $ lemon/RadixHeap#remove(d4f767463ce0a6b3).(index); |
| 337 | relocateLastcxx . . $ lemon/RadixHeap#relocateLast(d4f767463ce0a6b3).(index); |
| 338 | } |
| 339 | |
| 340 | /// \brief The priority of the given item. |
| 341 | /// |
| 342 | /// This function returns the priority of the given item. |
| 343 | /// \param i The item. |
| 344 | /// \pre \e i must be in the heap. |
| 345 | Priocxx . . $ lemon/RadixHeap#Prio# operatorcxx . . $ lemon/RadixHeap#`operator[]`(8585a36945f51745).[](const Itemcxx . . $ lemon/RadixHeap#Item# &i) const { |
| 346 | int idx = _iimcxx . . $ lemon/RadixHeap#_iim.[i]; |
| 347 | return _datacxx . . $ lemon/RadixHeap#_data.[idx].prio; |
| 348 | } |
| 349 | |
| 350 | /// \brief Set the priority of an item or insert it, if it is |
| 351 | /// not stored in the heap. |
| 352 | /// |
| 353 | /// This method sets the priority of the given item if it is |
| 354 | /// already stored in the heap. Otherwise it inserts the given |
| 355 | /// item into the heap with the given priority. |
| 356 | /// \param i The item. |
| 357 | /// \param p The priority. |
| 358 | /// \pre \e i must be in the heap. |
| 359 | /// \warning This method may throw an \c UnderFlowPriorityException. |
| 360 | void setcxx . . $ lemon/RadixHeap#set(cf2f737abe8b3505).(const Itemcxx . . $ lemon/RadixHeap#Item# &i, const Priocxx . . $ lemon/RadixHeap#Prio# &p) { |
| 361 | int idx = _iimcxx . . $ lemon/RadixHeap#_iim.[i]; |
| 362 | if( idx < 0 ) { |
| 363 | pushcxx . . $ lemon/RadixHeap#push(cf2f737abe8b3505).(i, p); |
| 364 | } |
| 365 | else if( p >= _datacxx . . $ lemon/RadixHeap#_data.[idx].prio ) { |
| 366 | _datacxx . . $ lemon/RadixHeap#_data.[idx].prio = p; |
| 367 | bubbleUpcxx . . $ lemon/RadixHeap#bubbleUp(d4f767463ce0a6b3).(idx); |
| 368 | } else { |
| 369 | _datacxx . . $ lemon/RadixHeap#_data.[idx].prio = p; |
| 370 | bubbleDowncxx . . $ lemon/RadixHeap#bubbleDown(d4f767463ce0a6b3).(idx); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | /// \brief Decrease the priority of an item to the given value. |
| 375 | /// |
| 376 | /// This function decreases the priority of an item to the given value. |
| 377 | /// \param i The item. |
| 378 | /// \param p The priority. |
| 379 | /// \pre \e i must be stored in the heap with priority at least \e p. |
| 380 | /// \warning This method may throw an \c UnderFlowPriorityException. |
| 381 | void decreasecxx . . $ lemon/RadixHeap#decrease(cf2f737abe8b3505).(const Itemcxx . . $ lemon/RadixHeap#Item# &i, const Priocxx . . $ lemon/RadixHeap#Prio# &p) { |
| 382 | int idx = _iimcxx . . $ lemon/RadixHeap#_iim.[i]; |
| 383 | _datacxx . . $ lemon/RadixHeap#_data.[idx].prio = p; |
| 384 | bubbleDowncxx . . $ lemon/RadixHeap#bubbleDown(d4f767463ce0a6b3).(idx); |
| 385 | } |
| 386 | |
| 387 | /// \brief Increase the priority of an item to the given value. |
| 388 | /// |
| 389 | /// This function increases the priority of an item to the given value. |
| 390 | /// \param i The item. |
| 391 | /// \param p The priority. |
| 392 | /// \pre \e i must be stored in the heap with priority at most \e p. |
| 393 | void increasecxx . . $ lemon/RadixHeap#increase(cf2f737abe8b3505).(const Itemcxx . . $ lemon/RadixHeap#Item# &i, const Priocxx . . $ lemon/RadixHeap#Prio# &p) { |
| 394 | int idx = _iimcxx . . $ lemon/RadixHeap#_iim.[i]; |
| 395 | _datacxx . . $ lemon/RadixHeap#_data.[idx].prio = p; |
| 396 | bubbleUpcxx . . $ lemon/RadixHeap#bubbleUp(d4f767463ce0a6b3).(idx); |
| 397 | } |
| 398 | |
| 399 | /// \brief Return the state of an item. |
| 400 | /// |
| 401 | /// This method returns \c PRE_HEAP if the given item has never |
| 402 | /// been in the heap, \c IN_HEAP if it is in the heap at the moment, |
| 403 | /// and \c POST_HEAP otherwise. |
| 404 | /// In the latter case it is possible that the item will get back |
| 405 | /// to the heap again. |
| 406 | /// \param i The item. |
| 407 | Statecxx . . $ lemon/RadixHeap#State# statecxx . . $ lemon/RadixHeap#state(3c01363941109a7a).(const Itemcxx . . $ lemon/RadixHeap#Item# &i) const { |
| 408 | int s = _iimcxx . . $ lemon/RadixHeap#_iim.[i]; |
| 409 | if( s >= 0 ) s = 0; |
| 410 | return Statecxx . . $ lemon/RadixHeap#State#(s); |
| 411 | } |
| 412 | |
| 413 | /// \brief Set the state of an item in the heap. |
| 414 | /// |
| 415 | /// This function sets the state of the given item in the heap. |
| 416 | /// It can be used to manually clear the heap when it is important |
| 417 | /// to achive better time complexity. |
| 418 | /// \param i The item. |
| 419 | /// \param st The state. It should not be \c IN_HEAP. |
| 420 | void statecxx . . $ lemon/RadixHeap#state(2a76018bc34ab73).(const Itemcxx . . $ lemon/RadixHeap#Item#& i, Statecxx . . $ lemon/RadixHeap#State# st) { |
| 421 | switch (st) { |
| 422 | case POST_HEAPcxx . . $ lemon/RadixHeap#POST_HEAP.: |
| 423 | case PRE_HEAPcxx . . $ lemon/RadixHeap#PRE_HEAP.: |
| 424 | if (statecxx . . $ lemon/RadixHeap#state(3c01363941109a7a).(i) == IN_HEAPcxx . . $ lemon/RadixHeap#IN_HEAP.) { |
| 425 | erasecxx . . $ lemon/RadixHeap#erase(8c13afcd6b0c9deb).(i); |
| 426 | } |
| 427 | _iimcxx . . $ lemon/RadixHeap#_iim.[i] = st; |
| 428 | break; |
| 429 | case IN_HEAPcxx . . $ lemon/RadixHeap#IN_HEAP.: |
| 430 | break; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | }; // class RadixHeap |
| 435 | |
| 436 | } // namespace lemon |
| 437 | |
| 438 | #endif // LEMON_RADIX_HEAP_H |