1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/lemon/bucket_heap.h Thu Jun 11 22:11:29 2009 +0200
1.3 @@ -0,0 +1,831 @@
1.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
1.5 + *
1.6 + * This file is a part of LEMON, a generic C++ optimization library.
1.7 + *
1.8 + * Copyright (C) 2003-2009
1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
1.11 + *
1.12 + * Permission to use, modify and distribute this software is granted
1.13 + * provided that this copyright notice appears in all copies. For
1.14 + * precise terms see the accompanying LICENSE file.
1.15 + *
1.16 + * This software is provided "AS IS" with no warranty of any kind,
1.17 + * express or implied, and with no claim as to its suitability for any
1.18 + * purpose.
1.19 + *
1.20 + */
1.21 +
1.22 +#ifndef LEMON_BUCKET_HEAP_H
1.23 +#define LEMON_BUCKET_HEAP_H
1.24 +
1.25 +///\ingroup auxdat
1.26 +///\file
1.27 +///\brief Bucket Heap implementation.
1.28 +
1.29 +#include <vector>
1.30 +#include <utility>
1.31 +#include <functional>
1.32 +
1.33 +namespace lemon {
1.34 +
1.35 + /// \ingroup auxdat
1.36 + ///
1.37 + /// \brief A Bucket Heap implementation.
1.38 + ///
1.39 + /// This class implements the \e bucket \e heap data structure. A \e heap
1.40 + /// is a data structure for storing items with specified values called \e
1.41 + /// priorities in such a way that finding the item with minimum priority is
1.42 + /// efficient. The bucket heap is very simple implementation, it can store
1.43 + /// only integer priorities and it stores for each priority in the
1.44 + /// \f$ [0..C) \f$ range a list of items. So it should be used only when
1.45 + /// the priorities are small. It is not intended to use as dijkstra heap.
1.46 + ///
1.47 + /// \param _ItemIntMap A read and writable Item int map, used internally
1.48 + /// to handle the cross references.
1.49 + /// \param minimize If the given parameter is true then the heap gives back
1.50 + /// the lowest priority.
1.51 + template <typename _ItemIntMap, bool minimize = true >
1.52 + class BucketHeap {
1.53 +
1.54 + public:
1.55 + /// \e
1.56 + typedef typename _ItemIntMap::Key Item;
1.57 + /// \e
1.58 + typedef int Prio;
1.59 + /// \e
1.60 + typedef std::pair<Item, Prio> Pair;
1.61 + /// \e
1.62 + typedef _ItemIntMap ItemIntMap;
1.63 +
1.64 + /// \brief Type to represent the items states.
1.65 + ///
1.66 + /// Each Item element have a state associated to it. It may be "in heap",
1.67 + /// "pre heap" or "post heap". The latter two are indifferent from the
1.68 + /// heap's point of view, but may be useful to the user.
1.69 + ///
1.70 + /// The ItemIntMap \e should be initialized in such way that it maps
1.71 + /// PRE_HEAP (-1) to any element to be put in the heap...
1.72 + enum State {
1.73 + IN_HEAP = 0,
1.74 + PRE_HEAP = -1,
1.75 + POST_HEAP = -2
1.76 + };
1.77 +
1.78 + public:
1.79 + /// \brief The constructor.
1.80 + ///
1.81 + /// The constructor.
1.82 + /// \param _index should be given to the constructor, since it is used
1.83 + /// internally to handle the cross references. The value of the map
1.84 + /// should be PRE_HEAP (-1) for each element.
1.85 + explicit BucketHeap(ItemIntMap &_index) : index(_index), minimal(0) {}
1.86 +
1.87 + /// The number of items stored in the heap.
1.88 + ///
1.89 + /// \brief Returns the number of items stored in the heap.
1.90 + int size() const { return data.size(); }
1.91 +
1.92 + /// \brief Checks if the heap stores no items.
1.93 + ///
1.94 + /// Returns \c true if and only if the heap stores no items.
1.95 + bool empty() const { return data.empty(); }
1.96 +
1.97 + /// \brief Make empty this heap.
1.98 + ///
1.99 + /// Make empty this heap. It does not change the cross reference
1.100 + /// map. If you want to reuse a heap what is not surely empty you
1.101 + /// should first clear the heap and after that you should set the
1.102 + /// cross reference map for each item to \c PRE_HEAP.
1.103 + void clear() {
1.104 + data.clear(); first.clear(); minimal = 0;
1.105 + }
1.106 +
1.107 + private:
1.108 +
1.109 + void relocate_last(int idx) {
1.110 + if (idx + 1 < int(data.size())) {
1.111 + data[idx] = data.back();
1.112 + if (data[idx].prev != -1) {
1.113 + data[data[idx].prev].next = idx;
1.114 + } else {
1.115 + first[data[idx].value] = idx;
1.116 + }
1.117 + if (data[idx].next != -1) {
1.118 + data[data[idx].next].prev = idx;
1.119 + }
1.120 + index[data[idx].item] = idx;
1.121 + }
1.122 + data.pop_back();
1.123 + }
1.124 +
1.125 + void unlace(int idx) {
1.126 + if (data[idx].prev != -1) {
1.127 + data[data[idx].prev].next = data[idx].next;
1.128 + } else {
1.129 + first[data[idx].value] = data[idx].next;
1.130 + }
1.131 + if (data[idx].next != -1) {
1.132 + data[data[idx].next].prev = data[idx].prev;
1.133 + }
1.134 + }
1.135 +
1.136 + void lace(int idx) {
1.137 + if (int(first.size()) <= data[idx].value) {
1.138 + first.resize(data[idx].value + 1, -1);
1.139 + }
1.140 + data[idx].next = first[data[idx].value];
1.141 + if (data[idx].next != -1) {
1.142 + data[data[idx].next].prev = idx;
1.143 + }
1.144 + first[data[idx].value] = idx;
1.145 + data[idx].prev = -1;
1.146 + }
1.147 +
1.148 + public:
1.149 + /// \brief Insert a pair of item and priority into the heap.
1.150 + ///
1.151 + /// Adds \c p.first to the heap with priority \c p.second.
1.152 + /// \param p The pair to insert.
1.153 + void push(const Pair& p) {
1.154 + push(p.first, p.second);
1.155 + }
1.156 +
1.157 + /// \brief Insert an item into the heap with the given priority.
1.158 + ///
1.159 + /// Adds \c i to the heap with priority \c p.
1.160 + /// \param i The item to insert.
1.161 + /// \param p The priority of the item.
1.162 + void push(const Item &i, const Prio &p) {
1.163 + int idx = data.size();
1.164 + index[i] = idx;
1.165 + data.push_back(BucketItem(i, p));
1.166 + lace(idx);
1.167 + if (p < minimal) {
1.168 + minimal = p;
1.169 + }
1.170 + }
1.171 +
1.172 + /// \brief Returns the item with minimum priority.
1.173 + ///
1.174 + /// This method returns the item with minimum priority.
1.175 + /// \pre The heap must be nonempty.
1.176 + Item top() const {
1.177 + while (first[minimal] == -1) {
1.178 + ++minimal;
1.179 + }
1.180 + return data[first[minimal]].item;
1.181 + }
1.182 +
1.183 + /// \brief Returns the minimum priority.
1.184 + ///
1.185 + /// It returns the minimum priority.
1.186 + /// \pre The heap must be nonempty.
1.187 + Prio prio() const {
1.188 + while (first[minimal] == -1) {
1.189 + ++minimal;
1.190 + }
1.191 + return minimal;
1.192 + }
1.193 +
1.194 + /// \brief Deletes the item with minimum priority.
1.195 + ///
1.196 + /// This method deletes the item with minimum priority from the heap.
1.197 + /// \pre The heap must be non-empty.
1.198 + void pop() {
1.199 + while (first[minimal] == -1) {
1.200 + ++minimal;
1.201 + }
1.202 + int idx = first[minimal];
1.203 + index[data[idx].item] = -2;
1.204 + unlace(idx);
1.205 + relocate_last(idx);
1.206 + }
1.207 +
1.208 + /// \brief Deletes \c i from the heap.
1.209 + ///
1.210 + /// This method deletes item \c i from the heap, if \c i was
1.211 + /// already stored in the heap.
1.212 + /// \param i The item to erase.
1.213 + void erase(const Item &i) {
1.214 + int idx = index[i];
1.215 + index[data[idx].item] = -2;
1.216 + unlace(idx);
1.217 + relocate_last(idx);
1.218 + }
1.219 +
1.220 +
1.221 + /// \brief Returns the priority of \c i.
1.222 + ///
1.223 + /// This function returns the priority of item \c i.
1.224 + /// \pre \c i must be in the heap.
1.225 + /// \param i The item.
1.226 + Prio operator[](const Item &i) const {
1.227 + int idx = index[i];
1.228 + return data[idx].value;
1.229 + }
1.230 +
1.231 + /// \brief \c i gets to the heap with priority \c p independently
1.232 + /// if \c i was already there.
1.233 + ///
1.234 + /// This method calls \ref push(\c i, \c p) if \c i is not stored
1.235 + /// in the heap and sets the priority of \c i to \c p otherwise.
1.236 + /// \param i The item.
1.237 + /// \param p The priority.
1.238 + void set(const Item &i, const Prio &p) {
1.239 + int idx = index[i];
1.240 + if (idx < 0) {
1.241 + push(i,p);
1.242 + } else if (p > data[idx].value) {
1.243 + increase(i, p);
1.244 + } else {
1.245 + decrease(i, p);
1.246 + }
1.247 + }
1.248 +
1.249 + /// \brief Decreases the priority of \c i to \c p.
1.250 + ///
1.251 + /// This method decreases the priority of item \c i to \c p.
1.252 + /// \pre \c i must be stored in the heap with priority at least \c
1.253 + /// p relative to \c Compare.
1.254 + /// \param i The item.
1.255 + /// \param p The priority.
1.256 + void decrease(const Item &i, const Prio &p) {
1.257 + int idx = index[i];
1.258 + unlace(idx);
1.259 + data[idx].value = p;
1.260 + if (p < minimal) {
1.261 + minimal = p;
1.262 + }
1.263 + lace(idx);
1.264 + }
1.265 +
1.266 + /// \brief Increases the priority of \c i to \c p.
1.267 + ///
1.268 + /// This method sets the priority of item \c i to \c p.
1.269 + /// \pre \c i must be stored in the heap with priority at most \c
1.270 + /// p relative to \c Compare.
1.271 + /// \param i The item.
1.272 + /// \param p The priority.
1.273 + void increase(const Item &i, const Prio &p) {
1.274 + int idx = index[i];
1.275 + unlace(idx);
1.276 + data[idx].value = p;
1.277 + lace(idx);
1.278 + }
1.279 +
1.280 + /// \brief Returns if \c item is in, has already been in, or has
1.281 + /// never been in the heap.
1.282 + ///
1.283 + /// This method returns PRE_HEAP if \c item has never been in the
1.284 + /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
1.285 + /// otherwise. In the latter case it is possible that \c item will
1.286 + /// get back to the heap again.
1.287 + /// \param i The item.
1.288 + State state(const Item &i) const {
1.289 + int idx = index[i];
1.290 + if (idx >= 0) idx = 0;
1.291 + return State(idx);
1.292 + }
1.293 +
1.294 + /// \brief Sets the state of the \c item in the heap.
1.295 + ///
1.296 + /// Sets the state of the \c item in the heap. It can be used to
1.297 + /// manually clear the heap when it is important to achive the
1.298 + /// better time complexity.
1.299 + /// \param i The item.
1.300 + /// \param st The state. It should not be \c IN_HEAP.
1.301 + void state(const Item& i, State st) {
1.302 + switch (st) {
1.303 + case POST_HEAP:
1.304 + case PRE_HEAP:
1.305 + if (state(i) == IN_HEAP) {
1.306 + erase(i);
1.307 + }
1.308 + index[i] = st;
1.309 + break;
1.310 + case IN_HEAP:
1.311 + break;
1.312 + }
1.313 + }
1.314 +
1.315 + private:
1.316 +
1.317 + struct BucketItem {
1.318 + BucketItem(const Item& _item, int _value)
1.319 + : item(_item), value(_value) {}
1.320 +
1.321 + Item item;
1.322 + int value;
1.323 +
1.324 + int prev, next;
1.325 + };
1.326 +
1.327 + ItemIntMap& index;
1.328 + std::vector<int> first;
1.329 + std::vector<BucketItem> data;
1.330 + mutable int minimal;
1.331 +
1.332 + }; // class BucketHeap
1.333 +
1.334 +
1.335 + template <typename _ItemIntMap>
1.336 + class BucketHeap<_ItemIntMap, false> {
1.337 +
1.338 + public:
1.339 + typedef typename _ItemIntMap::Key Item;
1.340 + typedef int Prio;
1.341 + typedef std::pair<Item, Prio> Pair;
1.342 + typedef _ItemIntMap ItemIntMap;
1.343 +
1.344 + enum State {
1.345 + IN_HEAP = 0,
1.346 + PRE_HEAP = -1,
1.347 + POST_HEAP = -2
1.348 + };
1.349 +
1.350 + public:
1.351 +
1.352 + explicit BucketHeap(ItemIntMap &_index) : index(_index), maximal(-1) {}
1.353 +
1.354 + int size() const { return data.size(); }
1.355 + bool empty() const { return data.empty(); }
1.356 +
1.357 + void clear() {
1.358 + data.clear(); first.clear(); maximal = -1;
1.359 + }
1.360 +
1.361 + private:
1.362 +
1.363 + void relocate_last(int idx) {
1.364 + if (idx + 1 != int(data.size())) {
1.365 + data[idx] = data.back();
1.366 + if (data[idx].prev != -1) {
1.367 + data[data[idx].prev].next = idx;
1.368 + } else {
1.369 + first[data[idx].value] = idx;
1.370 + }
1.371 + if (data[idx].next != -1) {
1.372 + data[data[idx].next].prev = idx;
1.373 + }
1.374 + index[data[idx].item] = idx;
1.375 + }
1.376 + data.pop_back();
1.377 + }
1.378 +
1.379 + void unlace(int idx) {
1.380 + if (data[idx].prev != -1) {
1.381 + data[data[idx].prev].next = data[idx].next;
1.382 + } else {
1.383 + first[data[idx].value] = data[idx].next;
1.384 + }
1.385 + if (data[idx].next != -1) {
1.386 + data[data[idx].next].prev = data[idx].prev;
1.387 + }
1.388 + }
1.389 +
1.390 + void lace(int idx) {
1.391 + if (int(first.size()) <= data[idx].value) {
1.392 + first.resize(data[idx].value + 1, -1);
1.393 + }
1.394 + data[idx].next = first[data[idx].value];
1.395 + if (data[idx].next != -1) {
1.396 + data[data[idx].next].prev = idx;
1.397 + }
1.398 + first[data[idx].value] = idx;
1.399 + data[idx].prev = -1;
1.400 + }
1.401 +
1.402 + public:
1.403 +
1.404 + void push(const Pair& p) {
1.405 + push(p.first, p.second);
1.406 + }
1.407 +
1.408 + void push(const Item &i, const Prio &p) {
1.409 + int idx = data.size();
1.410 + index[i] = idx;
1.411 + data.push_back(BucketItem(i, p));
1.412 + lace(idx);
1.413 + if (data[idx].value > maximal) {
1.414 + maximal = data[idx].value;
1.415 + }
1.416 + }
1.417 +
1.418 + Item top() const {
1.419 + while (first[maximal] == -1) {
1.420 + --maximal;
1.421 + }
1.422 + return data[first[maximal]].item;
1.423 + }
1.424 +
1.425 + Prio prio() const {
1.426 + while (first[maximal] == -1) {
1.427 + --maximal;
1.428 + }
1.429 + return maximal;
1.430 + }
1.431 +
1.432 + void pop() {
1.433 + while (first[maximal] == -1) {
1.434 + --maximal;
1.435 + }
1.436 + int idx = first[maximal];
1.437 + index[data[idx].item] = -2;
1.438 + unlace(idx);
1.439 + relocate_last(idx);
1.440 + }
1.441 +
1.442 + void erase(const Item &i) {
1.443 + int idx = index[i];
1.444 + index[data[idx].item] = -2;
1.445 + unlace(idx);
1.446 + relocate_last(idx);
1.447 + }
1.448 +
1.449 + Prio operator[](const Item &i) const {
1.450 + int idx = index[i];
1.451 + return data[idx].value;
1.452 + }
1.453 +
1.454 + void set(const Item &i, const Prio &p) {
1.455 + int idx = index[i];
1.456 + if (idx < 0) {
1.457 + push(i,p);
1.458 + } else if (p > data[idx].value) {
1.459 + decrease(i, p);
1.460 + } else {
1.461 + increase(i, p);
1.462 + }
1.463 + }
1.464 +
1.465 + void decrease(const Item &i, const Prio &p) {
1.466 + int idx = index[i];
1.467 + unlace(idx);
1.468 + data[idx].value = p;
1.469 + if (p > maximal) {
1.470 + maximal = p;
1.471 + }
1.472 + lace(idx);
1.473 + }
1.474 +
1.475 + void increase(const Item &i, const Prio &p) {
1.476 + int idx = index[i];
1.477 + unlace(idx);
1.478 + data[idx].value = p;
1.479 + lace(idx);
1.480 + }
1.481 +
1.482 + State state(const Item &i) const {
1.483 + int idx = index[i];
1.484 + if (idx >= 0) idx = 0;
1.485 + return State(idx);
1.486 + }
1.487 +
1.488 + void state(const Item& i, State st) {
1.489 + switch (st) {
1.490 + case POST_HEAP:
1.491 + case PRE_HEAP:
1.492 + if (state(i) == IN_HEAP) {
1.493 + erase(i);
1.494 + }
1.495 + index[i] = st;
1.496 + break;
1.497 + case IN_HEAP:
1.498 + break;
1.499 + }
1.500 + }
1.501 +
1.502 + private:
1.503 +
1.504 + struct BucketItem {
1.505 + BucketItem(const Item& _item, int _value)
1.506 + : item(_item), value(_value) {}
1.507 +
1.508 + Item item;
1.509 + int value;
1.510 +
1.511 + int prev, next;
1.512 + };
1.513 +
1.514 + ItemIntMap& index;
1.515 + std::vector<int> first;
1.516 + std::vector<BucketItem> data;
1.517 + mutable int maximal;
1.518 +
1.519 + }; // class BucketHeap
1.520 +
1.521 + /// \ingroup auxdat
1.522 + ///
1.523 + /// \brief A Simplified Bucket Heap implementation.
1.524 + ///
1.525 + /// This class implements a simplified \e bucket \e heap data
1.526 + /// structure. It does not provide some functionality but it faster
1.527 + /// and simplier data structure than the BucketHeap. The main
1.528 + /// difference is that the BucketHeap stores for every key a double
1.529 + /// linked list while this class stores just simple lists. In the
1.530 + /// other way it does not supports erasing each elements just the
1.531 + /// minimal and it does not supports key increasing, decreasing.
1.532 + ///
1.533 + /// \param _ItemIntMap A read and writable Item int map, used internally
1.534 + /// to handle the cross references.
1.535 + /// \param minimize If the given parameter is true then the heap gives back
1.536 + /// the lowest priority.
1.537 + ///
1.538 + /// \sa BucketHeap
1.539 + template <typename _ItemIntMap, bool minimize = true >
1.540 + class SimpleBucketHeap {
1.541 +
1.542 + public:
1.543 + typedef typename _ItemIntMap::Key Item;
1.544 + typedef int Prio;
1.545 + typedef std::pair<Item, Prio> Pair;
1.546 + typedef _ItemIntMap ItemIntMap;
1.547 +
1.548 + /// \brief Type to represent the items states.
1.549 + ///
1.550 + /// Each Item element have a state associated to it. It may be "in heap",
1.551 + /// "pre heap" or "post heap". The latter two are indifferent from the
1.552 + /// heap's point of view, but may be useful to the user.
1.553 + ///
1.554 + /// The ItemIntMap \e should be initialized in such way that it maps
1.555 + /// PRE_HEAP (-1) to any element to be put in the heap...
1.556 + enum State {
1.557 + IN_HEAP = 0,
1.558 + PRE_HEAP = -1,
1.559 + POST_HEAP = -2
1.560 + };
1.561 +
1.562 + public:
1.563 +
1.564 + /// \brief The constructor.
1.565 + ///
1.566 + /// The constructor.
1.567 + /// \param _index should be given to the constructor, since it is used
1.568 + /// internally to handle the cross references. The value of the map
1.569 + /// should be PRE_HEAP (-1) for each element.
1.570 + explicit SimpleBucketHeap(ItemIntMap &_index)
1.571 + : index(_index), free(-1), num(0), minimal(0) {}
1.572 +
1.573 + /// \brief Returns the number of items stored in the heap.
1.574 + ///
1.575 + /// The number of items stored in the heap.
1.576 + int size() const { return num; }
1.577 +
1.578 + /// \brief Checks if the heap stores no items.
1.579 + ///
1.580 + /// Returns \c true if and only if the heap stores no items.
1.581 + bool empty() const { return num == 0; }
1.582 +
1.583 + /// \brief Make empty this heap.
1.584 + ///
1.585 + /// Make empty this heap. It does not change the cross reference
1.586 + /// map. If you want to reuse a heap what is not surely empty you
1.587 + /// should first clear the heap and after that you should set the
1.588 + /// cross reference map for each item to \c PRE_HEAP.
1.589 + void clear() {
1.590 + data.clear(); first.clear(); free = -1; num = 0; minimal = 0;
1.591 + }
1.592 +
1.593 + /// \brief Insert a pair of item and priority into the heap.
1.594 + ///
1.595 + /// Adds \c p.first to the heap with priority \c p.second.
1.596 + /// \param p The pair to insert.
1.597 + void push(const Pair& p) {
1.598 + push(p.first, p.second);
1.599 + }
1.600 +
1.601 + /// \brief Insert an item into the heap with the given priority.
1.602 + ///
1.603 + /// Adds \c i to the heap with priority \c p.
1.604 + /// \param i The item to insert.
1.605 + /// \param p The priority of the item.
1.606 + void push(const Item &i, const Prio &p) {
1.607 + int idx;
1.608 + if (free == -1) {
1.609 + idx = data.size();
1.610 + data.push_back(BucketItem(i));
1.611 + } else {
1.612 + idx = free;
1.613 + free = data[idx].next;
1.614 + data[idx].item = i;
1.615 + }
1.616 + index[i] = idx;
1.617 + if (p >= int(first.size())) first.resize(p + 1, -1);
1.618 + data[idx].next = first[p];
1.619 + first[p] = idx;
1.620 + if (p < minimal) {
1.621 + minimal = p;
1.622 + }
1.623 + ++num;
1.624 + }
1.625 +
1.626 + /// \brief Returns the item with minimum priority.
1.627 + ///
1.628 + /// This method returns the item with minimum priority.
1.629 + /// \pre The heap must be nonempty.
1.630 + Item top() const {
1.631 + while (first[minimal] == -1) {
1.632 + ++minimal;
1.633 + }
1.634 + return data[first[minimal]].item;
1.635 + }
1.636 +
1.637 + /// \brief Returns the minimum priority.
1.638 + ///
1.639 + /// It returns the minimum priority.
1.640 + /// \pre The heap must be nonempty.
1.641 + Prio prio() const {
1.642 + while (first[minimal] == -1) {
1.643 + ++minimal;
1.644 + }
1.645 + return minimal;
1.646 + }
1.647 +
1.648 + /// \brief Deletes the item with minimum priority.
1.649 + ///
1.650 + /// This method deletes the item with minimum priority from the heap.
1.651 + /// \pre The heap must be non-empty.
1.652 + void pop() {
1.653 + while (first[minimal] == -1) {
1.654 + ++minimal;
1.655 + }
1.656 + int idx = first[minimal];
1.657 + index[data[idx].item] = -2;
1.658 + first[minimal] = data[idx].next;
1.659 + data[idx].next = free;
1.660 + free = idx;
1.661 + --num;
1.662 + }
1.663 +
1.664 + /// \brief Returns the priority of \c i.
1.665 + ///
1.666 + /// This function returns the priority of item \c i.
1.667 + /// \warning This operator is not a constant time function
1.668 + /// because it scans the whole data structure to find the proper
1.669 + /// value.
1.670 + /// \pre \c i must be in the heap.
1.671 + /// \param i The item.
1.672 + Prio operator[](const Item &i) const {
1.673 + for (int k = 0; k < first.size(); ++k) {
1.674 + int idx = first[k];
1.675 + while (idx != -1) {
1.676 + if (data[idx].item == i) {
1.677 + return k;
1.678 + }
1.679 + idx = data[idx].next;
1.680 + }
1.681 + }
1.682 + return -1;
1.683 + }
1.684 +
1.685 + /// \brief Returns if \c item is in, has already been in, or has
1.686 + /// never been in the heap.
1.687 + ///
1.688 + /// This method returns PRE_HEAP if \c item has never been in the
1.689 + /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
1.690 + /// otherwise. In the latter case it is possible that \c item will
1.691 + /// get back to the heap again.
1.692 + /// \param i The item.
1.693 + State state(const Item &i) const {
1.694 + int idx = index[i];
1.695 + if (idx >= 0) idx = 0;
1.696 + return State(idx);
1.697 + }
1.698 +
1.699 + private:
1.700 +
1.701 + struct BucketItem {
1.702 + BucketItem(const Item& _item)
1.703 + : item(_item) {}
1.704 +
1.705 + Item item;
1.706 + int next;
1.707 + };
1.708 +
1.709 + ItemIntMap& index;
1.710 + std::vector<int> first;
1.711 + std::vector<BucketItem> data;
1.712 + int free, num;
1.713 + mutable int minimal;
1.714 +
1.715 + }; // class SimpleBucketHeap
1.716 +
1.717 + template <typename _ItemIntMap>
1.718 + class SimpleBucketHeap<_ItemIntMap, false> {
1.719 +
1.720 + public:
1.721 + typedef typename _ItemIntMap::Key Item;
1.722 + typedef int Prio;
1.723 + typedef std::pair<Item, Prio> Pair;
1.724 + typedef _ItemIntMap ItemIntMap;
1.725 +
1.726 + enum State {
1.727 + IN_HEAP = 0,
1.728 + PRE_HEAP = -1,
1.729 + POST_HEAP = -2
1.730 + };
1.731 +
1.732 + public:
1.733 +
1.734 + explicit SimpleBucketHeap(ItemIntMap &_index)
1.735 + : index(_index), free(-1), num(0), maximal(0) {}
1.736 +
1.737 + int size() const { return num; }
1.738 +
1.739 + bool empty() const { return num == 0; }
1.740 +
1.741 + void clear() {
1.742 + data.clear(); first.clear(); free = -1; num = 0; maximal = 0;
1.743 + }
1.744 +
1.745 + void push(const Pair& p) {
1.746 + push(p.first, p.second);
1.747 + }
1.748 +
1.749 + void push(const Item &i, const Prio &p) {
1.750 + int idx;
1.751 + if (free == -1) {
1.752 + idx = data.size();
1.753 + data.push_back(BucketItem(i));
1.754 + } else {
1.755 + idx = free;
1.756 + free = data[idx].next;
1.757 + data[idx].item = i;
1.758 + }
1.759 + index[i] = idx;
1.760 + if (p >= int(first.size())) first.resize(p + 1, -1);
1.761 + data[idx].next = first[p];
1.762 + first[p] = idx;
1.763 + if (p > maximal) {
1.764 + maximal = p;
1.765 + }
1.766 + ++num;
1.767 + }
1.768 +
1.769 + Item top() const {
1.770 + while (first[maximal] == -1) {
1.771 + --maximal;
1.772 + }
1.773 + return data[first[maximal]].item;
1.774 + }
1.775 +
1.776 + Prio prio() const {
1.777 + while (first[maximal] == -1) {
1.778 + --maximal;
1.779 + }
1.780 + return maximal;
1.781 + }
1.782 +
1.783 + void pop() {
1.784 + while (first[maximal] == -1) {
1.785 + --maximal;
1.786 + }
1.787 + int idx = first[maximal];
1.788 + index[data[idx].item] = -2;
1.789 + first[maximal] = data[idx].next;
1.790 + data[idx].next = free;
1.791 + free = idx;
1.792 + --num;
1.793 + }
1.794 +
1.795 + Prio operator[](const Item &i) const {
1.796 + for (int k = 0; k < first.size(); ++k) {
1.797 + int idx = first[k];
1.798 + while (idx != -1) {
1.799 + if (data[idx].item == i) {
1.800 + return k;
1.801 + }
1.802 + idx = data[idx].next;
1.803 + }
1.804 + }
1.805 + return -1;
1.806 + }
1.807 +
1.808 + State state(const Item &i) const {
1.809 + int idx = index[i];
1.810 + if (idx >= 0) idx = 0;
1.811 + return State(idx);
1.812 + }
1.813 +
1.814 + private:
1.815 +
1.816 + struct BucketItem {
1.817 + BucketItem(const Item& _item) : item(_item) {}
1.818 +
1.819 + Item item;
1.820 +
1.821 + int next;
1.822 + };
1.823 +
1.824 + ItemIntMap& index;
1.825 + std::vector<int> first;
1.826 + std::vector<BucketItem> data;
1.827 + int free, num;
1.828 + mutable int maximal;
1.829 +
1.830 + };
1.831 +
1.832 +}
1.833 +
1.834 +#endif