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