deba@2038: /* -*- C++ -*- deba@2038: * deba@2038: * This file is a part of LEMON, a generic C++ optimization library deba@2038: * deba@2038: * Copyright (C) 2003-2006 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@2038: 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@2038: /// only integer priorities and it stores for each priority in the [0..C] deba@2038: /// range a list of items. So it should be used only when the priorities deba@2038: /// are small. It is not intended to use as dijkstra heap. deba@2038: /// deba@2038: /// \param _Item Type of the items to be stored. 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. deba@2038: template deba@2038: class BucketHeap { deba@2038: deba@2038: public: deba@2038: typedef _Item Item; deba@2038: typedef int Prio; deba@2038: typedef std::pair Pair; 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@2038: enum state_enum { 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@2038: /// Make empty this heap. deba@2038: void clear() { deba@2038: for (int i = 0; i < (int)data.size(); ++i) { deba@2038: index[data[i].item] = -2; deba@2038: } 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@2038: 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@2038: 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@2038: 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@2038: state_enum state(const Item &i) const { deba@2038: int idx = index[i]; deba@2038: if (idx >= 0) idx = 0; deba@2038: return state_enum(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@2038: void state(const Item& i, state_enum 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: deba@2038: template deba@2038: class BucketHeap<_Item, _ItemIntMap, false> { deba@2038: deba@2038: public: deba@2038: typedef _Item Item; deba@2038: typedef int Prio; deba@2038: typedef std::pair Pair; deba@2038: typedef _ItemIntMap ItemIntMap; deba@2038: deba@2038: enum state_enum { 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: for (int i = 0; i < (int)data.size(); ++i) { deba@2038: index[data[i].item] = -2; deba@2038: } 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@2038: 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@2038: 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@2038: state_enum state(const Item &i) const { deba@2038: int idx = index[i]; deba@2038: if (idx >= 0) idx = 0; deba@2038: return state_enum(idx); deba@2038: } deba@2038: deba@2038: void state(const Item& i, state_enum 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@2038: } deba@2038: deba@2038: #endif