deba@2038: /* -*- C++ -*- deba@2038: * deba@2038: * This file is a part of LEMON, a generic C++ optimization library deba@2038: * alpar@2553: * Copyright (C) 2003-2008 deba@2038: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport deba@2038: * (Egervary Research Group on Combinatorial Optimization, EGRES). deba@2038: * deba@2038: * Permission to use, modify and distribute this software is granted deba@2038: * provided that this copyright notice appears in all copies. For deba@2038: * precise terms see the accompanying LICENSE file. deba@2038: * deba@2038: * This software is provided "AS IS" with no warranty of any kind, deba@2038: * express or implied, and with no claim as to its suitability for any deba@2038: * purpose. deba@2038: * deba@2038: */ deba@2038: deba@2038: #ifndef LEMON_BUCKET_HEAP_H deba@2038: #define LEMON_BUCKET_HEAP_H deba@2038: deba@2038: ///\ingroup auxdat deba@2038: ///\file deba@2038: ///\brief Bucket Heap implementation. deba@2038: deba@2038: #include deba@2038: #include deba@2038: #include deba@2038: deba@2038: namespace lemon { deba@2038: deba@2038: /// \ingroup auxdat deba@2089: /// deba@2038: /// \brief A Bucket Heap implementation. deba@2038: /// deba@2038: /// This class implements the \e bucket \e heap data structure. A \e heap deba@2038: /// is a data structure for storing items with specified values called \e deba@2038: /// priorities in such a way that finding the item with minimum priority is deba@2038: /// efficient. The bucket heap is very simple implementation, it can store deba@2042: /// only integer priorities and it stores for each priority in the deba@2042: /// \f$ [0..C) \f$ range a list of items. So it should be used only when deba@2042: /// the priorities are small. It is not intended to use as dijkstra heap. deba@2038: /// deba@2038: /// \param _ItemIntMap A read and writable Item int map, used internally deba@2038: /// to handle the cross references. deba@2038: /// \param minimize If the given parameter is true then the heap gives back deba@2038: /// the lowest priority. mqrelly@2263: template deba@2038: class BucketHeap { deba@2038: deba@2038: public: deba@2547: /// \e mqrelly@2263: typedef typename _ItemIntMap::Key Item; deba@2547: /// \e deba@2038: typedef int Prio; deba@2547: /// \e deba@2038: typedef std::pair Pair; deba@2547: /// \e deba@2038: typedef _ItemIntMap ItemIntMap; deba@2038: deba@2038: /// \brief Type to represent the items states. deba@2038: /// deba@2038: /// Each Item element have a state associated to it. It may be "in heap", deba@2038: /// "pre heap" or "post heap". The latter two are indifferent from the deba@2038: /// heap's point of view, but may be useful to the user. deba@2038: /// deba@2038: /// The ItemIntMap \e should be initialized in such way that it maps deba@2038: /// PRE_HEAP (-1) to any element to be put in the heap... deba@2547: enum State { deba@2038: IN_HEAP = 0, deba@2038: PRE_HEAP = -1, deba@2038: POST_HEAP = -2 deba@2038: }; deba@2038: deba@2038: public: deba@2038: /// \brief The constructor. deba@2038: /// deba@2038: /// The constructor. deba@2038: /// \param _index should be given to the constructor, since it is used deba@2038: /// internally to handle the cross references. The value of the map deba@2038: /// should be PRE_HEAP (-1) for each element. deba@2038: explicit BucketHeap(ItemIntMap &_index) : index(_index), minimal(0) {} deba@2038: deba@2038: /// The number of items stored in the heap. deba@2038: /// deba@2038: /// \brief Returns the number of items stored in the heap. deba@2038: int size() const { return data.size(); } deba@2038: deba@2038: /// \brief Checks if the heap stores no items. deba@2038: /// deba@2038: /// Returns \c true if and only if the heap stores no items. deba@2038: bool empty() const { return data.empty(); } deba@2038: deba@2038: /// \brief Make empty this heap. deba@2038: /// deba@2050: /// Make empty this heap. It does not change the cross reference deba@2050: /// map. If you want to reuse a heap what is not surely empty you deba@2050: /// should first clear the heap and after that you should set the deba@2050: /// cross reference map for each item to \c PRE_HEAP. deba@2038: void clear() { deba@2038: data.clear(); first.clear(); minimal = 0; deba@2038: } deba@2038: deba@2038: private: deba@2038: deba@2038: void relocate_last(int idx) { deba@2386: if (idx + 1 < int(data.size())) { deba@2038: data[idx] = data.back(); deba@2038: if (data[idx].prev != -1) { deba@2038: data[data[idx].prev].next = idx; deba@2038: } else { deba@2038: first[data[idx].value] = idx; deba@2038: } deba@2038: if (data[idx].next != -1) { deba@2038: data[data[idx].next].prev = idx; deba@2038: } deba@2038: index[data[idx].item] = idx; deba@2038: } deba@2038: data.pop_back(); deba@2038: } deba@2038: deba@2038: void unlace(int idx) { deba@2038: if (data[idx].prev != -1) { deba@2038: data[data[idx].prev].next = data[idx].next; deba@2038: } else { deba@2038: first[data[idx].value] = data[idx].next; deba@2038: } deba@2038: if (data[idx].next != -1) { deba@2038: data[data[idx].next].prev = data[idx].prev; deba@2038: } deba@2038: } deba@2038: deba@2038: void lace(int idx) { deba@2386: if (int(first.size()) <= data[idx].value) { deba@2038: first.resize(data[idx].value + 1, -1); deba@2038: } deba@2038: data[idx].next = first[data[idx].value]; deba@2038: if (data[idx].next != -1) { deba@2038: data[data[idx].next].prev = idx; deba@2038: } deba@2038: first[data[idx].value] = idx; deba@2038: data[idx].prev = -1; deba@2038: } deba@2038: deba@2038: public: deba@2038: /// \brief Insert a pair of item and priority into the heap. deba@2038: /// deba@2038: /// Adds \c p.first to the heap with priority \c p.second. deba@2038: /// \param p The pair to insert. deba@2038: void push(const Pair& p) { deba@2038: push(p.first, p.second); deba@2038: } deba@2038: deba@2038: /// \brief Insert an item into the heap with the given priority. deba@2038: /// deba@2038: /// Adds \c i to the heap with priority \c p. deba@2038: /// \param i The item to insert. deba@2038: /// \param p The priority of the item. deba@2038: void push(const Item &i, const Prio &p) { deba@2038: int idx = data.size(); deba@2038: index[i] = idx; deba@2038: data.push_back(BucketItem(i, p)); deba@2038: lace(idx); deba@2038: if (p < minimal) { deba@2038: minimal = p; deba@2038: } deba@2038: } deba@2038: deba@2038: /// \brief Returns the item with minimum priority. deba@2038: /// deba@2038: /// This method returns the item with minimum priority. deba@2038: /// \pre The heap must be nonempty. deba@2038: Item top() const { deba@2038: while (first[minimal] == -1) { deba@2038: ++minimal; deba@2038: } deba@2038: return data[first[minimal]].item; deba@2038: } deba@2038: deba@2038: /// \brief Returns the minimum priority. deba@2038: /// deba@2038: /// It returns the minimum priority. deba@2038: /// \pre The heap must be nonempty. deba@2038: Prio prio() const { deba@2038: while (first[minimal] == -1) { deba@2038: ++minimal; deba@2038: } deba@2038: return minimal; deba@2038: } deba@2038: deba@2038: /// \brief Deletes the item with minimum priority. deba@2038: /// deba@2038: /// This method deletes the item with minimum priority from the heap. deba@2038: /// \pre The heap must be non-empty. deba@2038: void pop() { deba@2038: while (first[minimal] == -1) { deba@2038: ++minimal; deba@2038: } deba@2038: int idx = first[minimal]; deba@2038: index[data[idx].item] = -2; deba@2038: unlace(idx); deba@2038: relocate_last(idx); deba@2038: } deba@2038: deba@2038: /// \brief Deletes \c i from the heap. deba@2038: /// deba@2038: /// This method deletes item \c i from the heap, if \c i was deba@2038: /// already stored in the heap. deba@2038: /// \param i The item to erase. deba@2038: void erase(const Item &i) { deba@2038: int idx = index[i]; deba@2038: index[data[idx].item] = -2; deba@2038: unlace(idx); deba@2038: relocate_last(idx); deba@2038: } deba@2038: deba@2038: deba@2038: /// \brief Returns the priority of \c i. deba@2038: /// deba@2038: /// This function returns the priority of item \c i. deba@2038: /// \pre \c i must be in the heap. deba@2038: /// \param i The item. deba@2038: Prio operator[](const Item &i) const { deba@2038: int idx = index[i]; deba@2038: return data[idx].value; deba@2038: } deba@2038: deba@2038: /// \brief \c i gets to the heap with priority \c p independently deba@2038: /// if \c i was already there. deba@2038: /// deba@2038: /// This method calls \ref push(\c i, \c p) if \c i is not stored deba@2038: /// in the heap and sets the priority of \c i to \c p otherwise. deba@2038: /// \param i The item. deba@2038: /// \param p The priority. deba@2038: void set(const Item &i, const Prio &p) { deba@2038: int idx = index[i]; deba@2038: if (idx < 0) { deba@2038: push(i,p); deba@2038: } else if (p > data[idx].value) { deba@2038: increase(i, p); deba@2038: } else { deba@2038: decrease(i, p); deba@2038: } deba@2038: } deba@2038: deba@2038: /// \brief Decreases the priority of \c i to \c p. deba@2089: /// deba@2038: /// This method decreases the priority of item \c i to \c p. deba@2038: /// \pre \c i must be stored in the heap with priority at least \c deba@2038: /// p relative to \c Compare. deba@2038: /// \param i The item. deba@2038: /// \param p The priority. deba@2038: void decrease(const Item &i, const Prio &p) { deba@2038: int idx = index[i]; deba@2038: unlace(idx); deba@2038: data[idx].value = p; deba@2038: if (p < minimal) { deba@2038: minimal = p; deba@2038: } deba@2038: lace(idx); deba@2038: } deba@2038: deba@2038: /// \brief Increases the priority of \c i to \c p. deba@2038: /// deba@2038: /// This method sets the priority of item \c i to \c p. deba@2038: /// \pre \c i must be stored in the heap with priority at most \c deba@2038: /// p relative to \c Compare. deba@2038: /// \param i The item. deba@2038: /// \param p The priority. deba@2038: void increase(const Item &i, const Prio &p) { deba@2038: int idx = index[i]; deba@2038: unlace(idx); deba@2038: data[idx].value = p; deba@2038: lace(idx); deba@2038: } deba@2038: deba@2038: /// \brief Returns if \c item is in, has already been in, or has deba@2038: /// never been in the heap. deba@2038: /// deba@2038: /// This method returns PRE_HEAP if \c item has never been in the deba@2038: /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP deba@2038: /// otherwise. In the latter case it is possible that \c item will deba@2038: /// get back to the heap again. deba@2038: /// \param i The item. deba@2547: State state(const Item &i) const { deba@2038: int idx = index[i]; deba@2038: if (idx >= 0) idx = 0; deba@2547: return State(idx); deba@2038: } deba@2038: deba@2038: /// \brief Sets the state of the \c item in the heap. deba@2038: /// deba@2038: /// Sets the state of the \c item in the heap. It can be used to deba@2038: /// manually clear the heap when it is important to achive the deba@2038: /// better time complexity. deba@2038: /// \param i The item. deba@2038: /// \param st The state. It should not be \c IN_HEAP. deba@2547: void state(const Item& i, State st) { deba@2038: switch (st) { deba@2038: case POST_HEAP: deba@2038: case PRE_HEAP: deba@2038: if (state(i) == IN_HEAP) { deba@2038: erase(i); deba@2038: } deba@2038: index[i] = st; deba@2038: break; deba@2038: case IN_HEAP: deba@2038: break; deba@2038: } deba@2038: } deba@2038: deba@2038: private: deba@2038: deba@2038: struct BucketItem { deba@2038: BucketItem(const Item& _item, int _value) deba@2038: : item(_item), value(_value) {} deba@2038: deba@2038: Item item; deba@2038: int value; deba@2038: deba@2038: int prev, next; deba@2038: }; deba@2038: deba@2038: ItemIntMap& index; deba@2038: std::vector first; deba@2038: std::vector data; deba@2038: mutable int minimal; deba@2038: deba@2038: }; // class BucketHeap deba@2038: deba@2038: mqrelly@2263: template mqrelly@2263: class BucketHeap<_ItemIntMap, false> { deba@2038: deba@2038: public: mqrelly@2263: typedef typename _ItemIntMap::Key Item; deba@2038: typedef int Prio; deba@2038: typedef std::pair Pair; deba@2038: typedef _ItemIntMap ItemIntMap; deba@2038: deba@2547: enum State { deba@2038: IN_HEAP = 0, deba@2038: PRE_HEAP = -1, deba@2038: POST_HEAP = -2 deba@2038: }; deba@2038: deba@2038: public: deba@2038: deba@2038: explicit BucketHeap(ItemIntMap &_index) : index(_index), maximal(-1) {} deba@2038: deba@2038: int size() const { return data.size(); } deba@2038: bool empty() const { return data.empty(); } deba@2038: deba@2038: void clear() { deba@2038: data.clear(); first.clear(); maximal = -1; deba@2038: } deba@2038: deba@2038: private: deba@2038: deba@2038: void relocate_last(int idx) { deba@2386: if (idx + 1 != int(data.size())) { deba@2038: data[idx] = data.back(); deba@2038: if (data[idx].prev != -1) { deba@2038: data[data[idx].prev].next = idx; deba@2038: } else { deba@2038: first[data[idx].value] = idx; deba@2038: } deba@2038: if (data[idx].next != -1) { deba@2038: data[data[idx].next].prev = idx; deba@2038: } deba@2038: index[data[idx].item] = idx; deba@2038: } deba@2038: data.pop_back(); deba@2038: } deba@2038: deba@2038: void unlace(int idx) { deba@2038: if (data[idx].prev != -1) { deba@2038: data[data[idx].prev].next = data[idx].next; deba@2038: } else { deba@2038: first[data[idx].value] = data[idx].next; deba@2038: } deba@2038: if (data[idx].next != -1) { deba@2038: data[data[idx].next].prev = data[idx].prev; deba@2038: } deba@2038: } deba@2038: deba@2038: void lace(int idx) { deba@2386: if (int(first.size()) <= data[idx].value) { deba@2038: first.resize(data[idx].value + 1, -1); deba@2038: } deba@2038: data[idx].next = first[data[idx].value]; deba@2038: if (data[idx].next != -1) { deba@2038: data[data[idx].next].prev = idx; deba@2038: } deba@2038: first[data[idx].value] = idx; deba@2038: data[idx].prev = -1; deba@2038: } deba@2038: deba@2038: public: deba@2038: deba@2038: void push(const Pair& p) { deba@2038: push(p.first, p.second); deba@2038: } deba@2038: deba@2038: void push(const Item &i, const Prio &p) { deba@2038: int idx = data.size(); deba@2038: index[i] = idx; deba@2038: data.push_back(BucketItem(i, p)); deba@2038: lace(idx); deba@2038: if (data[idx].value > maximal) { deba@2038: maximal = data[idx].value; deba@2038: } deba@2038: } deba@2038: deba@2038: Item top() const { deba@2038: while (first[maximal] == -1) { deba@2038: --maximal; deba@2038: } deba@2038: return data[first[maximal]].item; deba@2038: } deba@2038: deba@2038: Prio prio() const { deba@2038: while (first[maximal] == -1) { deba@2038: --maximal; deba@2038: } deba@2038: return maximal; deba@2038: } deba@2038: deba@2038: void pop() { deba@2038: while (first[maximal] == -1) { deba@2038: --maximal; deba@2038: } deba@2038: int idx = first[maximal]; deba@2038: index[data[idx].item] = -2; deba@2038: unlace(idx); deba@2038: relocate_last(idx); deba@2038: } deba@2038: deba@2038: void erase(const Item &i) { deba@2038: int idx = index[i]; deba@2038: index[data[idx].item] = -2; deba@2038: unlace(idx); deba@2038: relocate_last(idx); deba@2038: } deba@2038: deba@2038: Prio operator[](const Item &i) const { deba@2038: int idx = index[i]; deba@2038: return data[idx].value; deba@2038: } deba@2038: deba@2038: void set(const Item &i, const Prio &p) { deba@2038: int idx = index[i]; deba@2038: if (idx < 0) { deba@2038: push(i,p); deba@2038: } else if (p > data[idx].value) { deba@2038: decrease(i, p); deba@2038: } else { deba@2038: increase(i, p); deba@2038: } deba@2038: } deba@2038: deba@2038: void decrease(const Item &i, const Prio &p) { deba@2038: int idx = index[i]; deba@2038: unlace(idx); deba@2038: data[idx].value = p; deba@2038: if (p > maximal) { deba@2038: maximal = p; deba@2038: } deba@2038: lace(idx); deba@2038: } deba@2038: deba@2038: void increase(const Item &i, const Prio &p) { deba@2038: int idx = index[i]; deba@2038: unlace(idx); deba@2038: data[idx].value = p; deba@2038: lace(idx); deba@2038: } deba@2038: deba@2547: State state(const Item &i) const { deba@2038: int idx = index[i]; deba@2038: if (idx >= 0) idx = 0; deba@2547: return State(idx); deba@2038: } deba@2038: deba@2547: void state(const Item& i, State st) { deba@2038: switch (st) { deba@2038: case POST_HEAP: deba@2038: case PRE_HEAP: deba@2038: if (state(i) == IN_HEAP) { deba@2038: erase(i); deba@2038: } deba@2038: index[i] = st; deba@2038: break; deba@2038: case IN_HEAP: deba@2038: break; deba@2038: } deba@2038: } deba@2038: deba@2038: private: deba@2038: deba@2038: struct BucketItem { deba@2038: BucketItem(const Item& _item, int _value) deba@2038: : item(_item), value(_value) {} deba@2038: deba@2038: Item item; deba@2038: int value; deba@2038: deba@2038: int prev, next; deba@2038: }; deba@2038: deba@2038: ItemIntMap& index; deba@2038: std::vector first; deba@2038: std::vector data; deba@2038: mutable int maximal; deba@2038: deba@2038: }; // class BucketHeap deba@2038: deba@2089: /// \ingroup auxdat deba@2089: /// deba@2089: /// \brief A Simplified Bucket Heap implementation. deba@2089: /// deba@2089: /// This class implements a simplified \e bucket \e heap data deba@2089: /// structure. It does not provide some functionality but it faster deba@2089: /// and simplier data structure than the BucketHeap. The main deba@2089: /// difference is that the BucketHeap stores for every key a double deba@2089: /// linked list while this class stores just simple lists. In the deba@2089: /// other way it does not supports erasing each elements just the deba@2089: /// minimal and it does not supports key increasing, decreasing. deba@2089: /// deba@2089: /// \param _ItemIntMap A read and writable Item int map, used internally deba@2089: /// to handle the cross references. deba@2089: /// \param minimize If the given parameter is true then the heap gives back deba@2089: /// the lowest priority. deba@2089: /// deba@2089: /// \sa BucketHeap mqrelly@2263: template deba@2089: class SimpleBucketHeap { deba@2089: deba@2089: public: mqrelly@2263: typedef typename _ItemIntMap::Key Item; deba@2089: typedef int Prio; deba@2089: typedef std::pair Pair; deba@2089: typedef _ItemIntMap ItemIntMap; deba@2089: deba@2089: /// \brief Type to represent the items states. deba@2089: /// deba@2089: /// Each Item element have a state associated to it. It may be "in heap", deba@2089: /// "pre heap" or "post heap". The latter two are indifferent from the deba@2089: /// heap's point of view, but may be useful to the user. deba@2089: /// deba@2089: /// The ItemIntMap \e should be initialized in such way that it maps deba@2089: /// PRE_HEAP (-1) to any element to be put in the heap... deba@2547: enum State { deba@2089: IN_HEAP = 0, deba@2089: PRE_HEAP = -1, deba@2089: POST_HEAP = -2 deba@2089: }; deba@2089: deba@2089: public: deba@2089: deba@2089: /// \brief The constructor. deba@2089: /// deba@2089: /// The constructor. deba@2089: /// \param _index should be given to the constructor, since it is used deba@2089: /// internally to handle the cross references. The value of the map deba@2089: /// should be PRE_HEAP (-1) for each element. deba@2089: explicit SimpleBucketHeap(ItemIntMap &_index) deba@2089: : index(_index), free(-1), num(0), minimal(0) {} deba@2089: deba@2089: /// \brief Returns the number of items stored in the heap. deba@2089: /// deba@2089: /// The number of items stored in the heap. deba@2089: int size() const { return num; } deba@2089: deba@2089: /// \brief Checks if the heap stores no items. deba@2089: /// deba@2089: /// Returns \c true if and only if the heap stores no items. deba@2089: bool empty() const { return num == 0; } deba@2089: deba@2089: /// \brief Make empty this heap. deba@2089: /// deba@2089: /// Make empty this heap. It does not change the cross reference deba@2089: /// map. If you want to reuse a heap what is not surely empty you deba@2089: /// should first clear the heap and after that you should set the deba@2089: /// cross reference map for each item to \c PRE_HEAP. deba@2089: void clear() { deba@2089: data.clear(); first.clear(); free = -1; num = 0; minimal = 0; deba@2089: } deba@2089: deba@2089: /// \brief Insert a pair of item and priority into the heap. deba@2089: /// deba@2089: /// Adds \c p.first to the heap with priority \c p.second. deba@2089: /// \param p The pair to insert. deba@2089: void push(const Pair& p) { deba@2089: push(p.first, p.second); deba@2089: } deba@2089: deba@2089: /// \brief Insert an item into the heap with the given priority. deba@2089: /// deba@2089: /// Adds \c i to the heap with priority \c p. deba@2089: /// \param i The item to insert. deba@2089: /// \param p The priority of the item. deba@2089: void push(const Item &i, const Prio &p) { deba@2089: int idx; deba@2089: if (free == -1) { deba@2089: idx = data.size(); deba@2110: data.push_back(BucketItem(i)); deba@2089: } else { deba@2089: idx = free; deba@2089: free = data[idx].next; deba@2110: data[idx].item = i; deba@2089: } deba@2089: index[i] = idx; deba@2386: if (p >= int(first.size())) first.resize(p + 1, -1); deba@2089: data[idx].next = first[p]; deba@2089: first[p] = idx; deba@2089: if (p < minimal) { deba@2089: minimal = p; deba@2089: } deba@2089: ++num; deba@2089: } deba@2089: deba@2089: /// \brief Returns the item with minimum priority. deba@2089: /// deba@2089: /// This method returns the item with minimum priority. deba@2089: /// \pre The heap must be nonempty. deba@2089: Item top() const { deba@2089: while (first[minimal] == -1) { deba@2089: ++minimal; deba@2089: } deba@2089: return data[first[minimal]].item; deba@2089: } deba@2089: deba@2089: /// \brief Returns the minimum priority. deba@2089: /// deba@2089: /// It returns the minimum priority. deba@2089: /// \pre The heap must be nonempty. deba@2089: Prio prio() const { deba@2089: while (first[minimal] == -1) { deba@2089: ++minimal; deba@2089: } deba@2089: return minimal; deba@2089: } deba@2089: deba@2089: /// \brief Deletes the item with minimum priority. deba@2089: /// deba@2089: /// This method deletes the item with minimum priority from the heap. deba@2089: /// \pre The heap must be non-empty. deba@2089: void pop() { deba@2089: while (first[minimal] == -1) { deba@2089: ++minimal; deba@2089: } deba@2089: int idx = first[minimal]; deba@2089: index[data[idx].item] = -2; deba@2089: first[minimal] = data[idx].next; deba@2089: data[idx].next = free; deba@2089: free = idx; deba@2089: --num; deba@2089: } deba@2089: deba@2089: /// \brief Returns the priority of \c i. deba@2089: /// deba@2110: /// This function returns the priority of item \c i. deba@2110: /// \warning This operator is not a constant time function deba@2110: /// because it scans the whole data structure to find the proper deba@2110: /// value. deba@2089: /// \pre \c i must be in the heap. deba@2089: /// \param i The item. deba@2089: Prio operator[](const Item &i) const { deba@2110: for (int k = 0; k < first.size(); ++k) { deba@2110: int idx = first[k]; deba@2110: while (idx != -1) { deba@2110: if (data[idx].item == i) { deba@2110: return k; deba@2110: } deba@2110: idx = data[idx].next; deba@2110: } deba@2110: } deba@2110: return -1; deba@2089: } deba@2089: deba@2089: /// \brief Returns if \c item is in, has already been in, or has deba@2089: /// never been in the heap. deba@2089: /// deba@2089: /// This method returns PRE_HEAP if \c item has never been in the deba@2089: /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP deba@2089: /// otherwise. In the latter case it is possible that \c item will deba@2089: /// get back to the heap again. deba@2089: /// \param i The item. deba@2547: State state(const Item &i) const { deba@2089: int idx = index[i]; deba@2089: if (idx >= 0) idx = 0; deba@2547: return State(idx); deba@2089: } deba@2089: deba@2089: private: deba@2089: deba@2089: struct BucketItem { deba@2110: BucketItem(const Item& _item) deba@2110: : item(_item) {} deba@2089: deba@2089: Item item; deba@2089: int next; deba@2089: }; deba@2089: deba@2089: ItemIntMap& index; deba@2089: std::vector first; deba@2089: std::vector data; deba@2089: int free, num; deba@2089: mutable int minimal; deba@2089: deba@2089: }; // class SimpleBucketHeap deba@2089: mqrelly@2263: template mqrelly@2263: class SimpleBucketHeap<_ItemIntMap, false> { deba@2089: deba@2089: public: mqrelly@2263: typedef typename _ItemIntMap::Key Item; deba@2089: typedef int Prio; deba@2089: typedef std::pair Pair; deba@2089: typedef _ItemIntMap ItemIntMap; deba@2089: deba@2547: enum State { deba@2089: IN_HEAP = 0, deba@2089: PRE_HEAP = -1, deba@2089: POST_HEAP = -2 deba@2089: }; deba@2089: deba@2089: public: deba@2089: deba@2089: explicit SimpleBucketHeap(ItemIntMap &_index) deba@2089: : index(_index), free(-1), num(0), maximal(0) {} deba@2089: deba@2089: int size() const { return num; } deba@2089: deba@2089: bool empty() const { return num == 0; } deba@2089: deba@2089: void clear() { deba@2089: data.clear(); first.clear(); free = -1; num = 0; maximal = 0; deba@2089: } deba@2089: deba@2089: void push(const Pair& p) { deba@2089: push(p.first, p.second); deba@2089: } deba@2089: deba@2089: void push(const Item &i, const Prio &p) { deba@2089: int idx; deba@2089: if (free == -1) { deba@2089: idx = data.size(); deba@2110: data.push_back(BucketItem(i)); deba@2089: } else { deba@2089: idx = free; deba@2089: free = data[idx].next; deba@2110: data[idx].item = i; deba@2089: } deba@2089: index[i] = idx; deba@2386: if (p >= int(first.size())) first.resize(p + 1, -1); deba@2089: data[idx].next = first[p]; deba@2089: first[p] = idx; deba@2089: if (p > maximal) { deba@2089: maximal = p; deba@2089: } deba@2089: ++num; deba@2089: } deba@2089: deba@2089: Item top() const { deba@2089: while (first[maximal] == -1) { deba@2089: --maximal; deba@2089: } deba@2089: return data[first[maximal]].item; deba@2089: } deba@2089: deba@2089: Prio prio() const { deba@2089: while (first[maximal] == -1) { deba@2089: --maximal; deba@2089: } deba@2089: return maximal; deba@2089: } deba@2089: deba@2089: void pop() { deba@2089: while (first[maximal] == -1) { deba@2089: --maximal; deba@2089: } deba@2089: int idx = first[maximal]; deba@2089: index[data[idx].item] = -2; deba@2089: first[maximal] = data[idx].next; deba@2089: data[idx].next = free; deba@2089: free = idx; deba@2089: --num; deba@2089: } deba@2089: deba@2089: Prio operator[](const Item &i) const { deba@2110: for (int k = 0; k < first.size(); ++k) { deba@2110: int idx = first[k]; deba@2110: while (idx != -1) { deba@2110: if (data[idx].item == i) { deba@2110: return k; deba@2110: } deba@2110: idx = data[idx].next; deba@2110: } deba@2110: } deba@2110: return -1; deba@2089: } deba@2089: deba@2547: State state(const Item &i) const { deba@2089: int idx = index[i]; deba@2089: if (idx >= 0) idx = 0; deba@2547: return State(idx); deba@2089: } deba@2089: deba@2089: private: deba@2089: deba@2089: struct BucketItem { deba@2110: BucketItem(const Item& _item) : item(_item) {} deba@2089: deba@2089: Item item; deba@2089: deba@2089: int next; deba@2089: }; deba@2089: deba@2089: ItemIntMap& index; deba@2089: std::vector first; deba@2089: std::vector data; deba@2089: int free, num; deba@2089: mutable int maximal; deba@2089: deba@2089: }; deba@2089: deba@2038: } deba@2038: deba@2038: #endif