deba@681: /* -*- mode: C++; indent-tabs-mode: nil; -*- deba@681: * deba@681: * This file is a part of LEMON, a generic C++ optimization library. deba@681: * alpar@877: * Copyright (C) 2003-2010 deba@681: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport deba@681: * (Egervary Research Group on Combinatorial Optimization, EGRES). deba@681: * deba@681: * Permission to use, modify and distribute this software is granted deba@681: * provided that this copyright notice appears in all copies. For deba@681: * precise terms see the accompanying LICENSE file. deba@681: * deba@681: * This software is provided "AS IS" with no warranty of any kind, deba@681: * express or implied, and with no claim as to its suitability for any deba@681: * purpose. deba@681: * deba@681: */ deba@681: deba@681: #ifndef LEMON_BUCKET_HEAP_H deba@681: #define LEMON_BUCKET_HEAP_H deba@681: kpeter@710: ///\ingroup heaps deba@681: ///\file kpeter@709: ///\brief Bucket heap implementation. deba@681: deba@681: #include deba@681: #include deba@681: #include deba@681: deba@681: namespace lemon { deba@681: deba@682: namespace _bucket_heap_bits { deba@682: deba@683: template deba@682: struct DirectionTraits { deba@682: static bool less(int left, int right) { deba@682: return left < right; deba@682: } deba@682: static void increase(int& value) { deba@682: ++value; deba@682: } deba@682: }; deba@682: deba@682: template <> deba@682: struct DirectionTraits { deba@682: static bool less(int left, int right) { deba@682: return left > right; deba@682: } deba@682: static void increase(int& value) { deba@682: --value; deba@682: } deba@682: }; deba@682: deba@682: } deba@682: kpeter@710: /// \ingroup heaps deba@681: /// kpeter@709: /// \brief Bucket heap data structure. deba@681: /// kpeter@709: /// This class implements the \e bucket \e heap data structure. kpeter@709: /// It practically conforms to the \ref concepts::Heap "heap concept", kpeter@709: /// but it has some limitations. deba@681: /// kpeter@709: /// The bucket heap is a very simple structure. It can store only kpeter@709: /// \c int priorities and it maintains a list of items for each priority kpeter@709: /// in the range [0..C). So it should only be used when the kpeter@709: /// priorities are small. It is not intended to use as a Dijkstra heap. kpeter@709: /// kpeter@709: /// \tparam IM A read-writable item map with \c int values, used kpeter@709: /// internally to handle the cross references. kpeter@709: /// \tparam MIN Indicate if the heap is a \e min-heap or a \e max-heap. kpeter@709: /// The default is \e min-heap. If this parameter is set to \c false, kpeter@709: /// then the comparison is reversed, so the top(), prio() and pop() kpeter@709: /// functions deal with the item having maximum priority instead of the kpeter@709: /// minimum. kpeter@709: /// kpeter@709: /// \sa SimpleBucketHeap deba@683: template deba@681: class BucketHeap { deba@681: deba@681: public: kpeter@709: kpeter@709: /// Type of the item-int map. kpeter@709: typedef IM ItemIntMap; kpeter@709: /// Type of the priorities. deba@681: typedef int Prio; kpeter@709: /// Type of the items stored in the heap. kpeter@709: typedef typename ItemIntMap::Key Item; kpeter@709: /// Type of the item-priority pairs. kpeter@709: typedef std::pair Pair; deba@681: deba@682: private: deba@682: deba@683: typedef _bucket_heap_bits::DirectionTraits Direction; deba@682: deba@682: public: deba@682: kpeter@709: /// \brief Type to represent the states of the items. deba@681: /// kpeter@709: /// Each item has a state associated to it. It can be "in heap", kpeter@709: /// "pre-heap" or "post-heap". The latter two are indifferent from the deba@681: /// heap's point of view, but may be useful to the user. deba@681: /// deba@683: /// The item-int map must be initialized in such way that it assigns deba@683: /// \c PRE_HEAP (-1) to any element to be put in the heap. deba@681: enum State { deba@683: IN_HEAP = 0, ///< = 0. deba@683: PRE_HEAP = -1, ///< = -1. deba@683: POST_HEAP = -2 ///< = -2. deba@681: }; deba@681: deba@681: public: kpeter@709: kpeter@709: /// \brief Constructor. deba@681: /// kpeter@709: /// Constructor. kpeter@709: /// \param map A map that assigns \c int values to the items. kpeter@709: /// It is used internally to handle the cross references. kpeter@709: /// The assigned value must be \c PRE_HEAP (-1) for each item. deba@683: explicit BucketHeap(ItemIntMap &map) : _iim(map), _minimum(0) {} deba@681: kpeter@709: /// \brief The number of items stored in the heap. deba@681: /// kpeter@709: /// This function returns the number of items stored in the heap. deba@683: int size() const { return _data.size(); } deba@681: kpeter@709: /// \brief Check if the heap is empty. deba@681: /// kpeter@709: /// This function returns \c true if the heap is empty. deba@683: bool empty() const { return _data.empty(); } deba@681: kpeter@709: /// \brief Make the heap empty. deba@681: /// kpeter@709: /// This functon makes the heap empty. kpeter@709: /// It does not change the cross reference map. If you want to reuse kpeter@709: /// a heap that is not surely empty, you should first clear it and kpeter@709: /// then you should set the cross reference map to \c PRE_HEAP kpeter@709: /// for each item. deba@681: void clear() { deba@683: _data.clear(); _first.clear(); _minimum = 0; deba@681: } deba@681: deba@681: private: deba@681: kpeter@711: void relocateLast(int idx) { deba@683: if (idx + 1 < int(_data.size())) { deba@683: _data[idx] = _data.back(); deba@683: if (_data[idx].prev != -1) { deba@683: _data[_data[idx].prev].next = idx; deba@681: } else { deba@683: _first[_data[idx].value] = idx; deba@681: } deba@683: if (_data[idx].next != -1) { deba@683: _data[_data[idx].next].prev = idx; deba@681: } deba@683: _iim[_data[idx].item] = idx; deba@681: } deba@683: _data.pop_back(); deba@681: } deba@681: deba@681: void unlace(int idx) { deba@683: if (_data[idx].prev != -1) { deba@683: _data[_data[idx].prev].next = _data[idx].next; deba@681: } else { deba@683: _first[_data[idx].value] = _data[idx].next; deba@681: } deba@683: if (_data[idx].next != -1) { deba@683: _data[_data[idx].next].prev = _data[idx].prev; deba@681: } deba@681: } deba@681: deba@681: void lace(int idx) { deba@683: if (int(_first.size()) <= _data[idx].value) { deba@683: _first.resize(_data[idx].value + 1, -1); deba@681: } deba@683: _data[idx].next = _first[_data[idx].value]; deba@683: if (_data[idx].next != -1) { deba@683: _data[_data[idx].next].prev = idx; deba@681: } deba@683: _first[_data[idx].value] = idx; deba@683: _data[idx].prev = -1; deba@681: } deba@681: deba@681: public: kpeter@709: deba@681: /// \brief Insert a pair of item and priority into the heap. deba@681: /// kpeter@709: /// This function inserts \c p.first to the heap with priority kpeter@709: /// \c p.second. deba@681: /// \param p The pair to insert. kpeter@709: /// \pre \c p.first must not be stored in the heap. deba@681: void push(const Pair& p) { deba@681: push(p.first, p.second); deba@681: } deba@681: deba@681: /// \brief Insert an item into the heap with the given priority. deba@681: /// kpeter@709: /// This function inserts the given item into the heap with the kpeter@709: /// given priority. deba@681: /// \param i The item to insert. deba@681: /// \param p The priority of the item. kpeter@709: /// \pre \e i must not be stored in the heap. deba@681: void push(const Item &i, const Prio &p) { deba@683: int idx = _data.size(); deba@683: _iim[i] = idx; deba@683: _data.push_back(BucketItem(i, p)); deba@681: lace(idx); deba@683: if (Direction::less(p, _minimum)) { deba@683: _minimum = p; deba@681: } deba@681: } deba@681: kpeter@709: /// \brief Return the item having minimum priority. deba@681: /// kpeter@709: /// This function returns the item having minimum priority. kpeter@709: /// \pre The heap must be non-empty. deba@681: Item top() const { deba@683: while (_first[_minimum] == -1) { deba@683: Direction::increase(_minimum); deba@681: } deba@683: return _data[_first[_minimum]].item; deba@681: } deba@681: kpeter@709: /// \brief The minimum priority. deba@681: /// kpeter@709: /// This function returns the minimum priority. kpeter@709: /// \pre The heap must be non-empty. deba@681: Prio prio() const { deba@683: while (_first[_minimum] == -1) { deba@683: Direction::increase(_minimum); deba@681: } deba@683: return _minimum; deba@681: } deba@681: kpeter@709: /// \brief Remove the item having minimum priority. deba@681: /// kpeter@709: /// This function removes the item having minimum priority. deba@681: /// \pre The heap must be non-empty. deba@681: void pop() { deba@683: while (_first[_minimum] == -1) { deba@683: Direction::increase(_minimum); deba@681: } deba@683: int idx = _first[_minimum]; deba@683: _iim[_data[idx].item] = -2; deba@681: unlace(idx); kpeter@711: relocateLast(idx); deba@681: } deba@681: kpeter@709: /// \brief Remove the given item from the heap. deba@681: /// kpeter@709: /// This function removes the given item from the heap if it is kpeter@709: /// already stored. kpeter@709: /// \param i The item to delete. kpeter@709: /// \pre \e i must be in the heap. deba@681: void erase(const Item &i) { deba@683: int idx = _iim[i]; deba@683: _iim[_data[idx].item] = -2; deba@681: unlace(idx); kpeter@711: relocateLast(idx); deba@681: } deba@681: kpeter@709: /// \brief The priority of the given item. deba@681: /// kpeter@709: /// This function returns the priority of the given item. deba@681: /// \param i The item. kpeter@709: /// \pre \e i must be in the heap. deba@681: Prio operator[](const Item &i) const { deba@683: int idx = _iim[i]; deba@683: return _data[idx].value; deba@681: } deba@681: kpeter@709: /// \brief Set the priority of an item or insert it, if it is kpeter@709: /// not stored in the heap. deba@681: /// kpeter@709: /// This method sets the priority of the given item if it is kpeter@709: /// already stored in the heap. Otherwise it inserts the given kpeter@709: /// item into the heap with the given priority. deba@681: /// \param i The item. deba@681: /// \param p The priority. deba@681: void set(const Item &i, const Prio &p) { deba@683: int idx = _iim[i]; deba@681: if (idx < 0) { deba@682: push(i, p); deba@683: } else if (Direction::less(p, _data[idx].value)) { deba@682: decrease(i, p); deba@682: } else { deba@681: increase(i, p); deba@681: } deba@681: } deba@681: kpeter@709: /// \brief Decrease the priority of an item to the given value. deba@681: /// kpeter@709: /// This function decreases the priority of an item to the given value. deba@681: /// \param i The item. deba@681: /// \param p The priority. kpeter@709: /// \pre \e i must be stored in the heap with priority at least \e p. deba@681: void decrease(const Item &i, const Prio &p) { deba@683: int idx = _iim[i]; deba@681: unlace(idx); deba@683: _data[idx].value = p; deba@683: if (Direction::less(p, _minimum)) { deba@683: _minimum = p; deba@681: } deba@681: lace(idx); deba@681: } deba@681: kpeter@709: /// \brief Increase the priority of an item to the given value. deba@681: /// kpeter@709: /// This function increases the priority of an item to the given value. deba@681: /// \param i The item. deba@681: /// \param p The priority. kpeter@709: /// \pre \e i must be stored in the heap with priority at most \e p. deba@681: void increase(const Item &i, const Prio &p) { deba@683: int idx = _iim[i]; deba@681: unlace(idx); deba@683: _data[idx].value = p; deba@681: lace(idx); deba@681: } deba@681: kpeter@709: /// \brief Return the state of an item. deba@681: /// kpeter@709: /// This method returns \c PRE_HEAP if the given item has never kpeter@709: /// been in the heap, \c IN_HEAP if it is in the heap at the moment, kpeter@709: /// and \c POST_HEAP otherwise. kpeter@709: /// In the latter case it is possible that the item will get back kpeter@709: /// to the heap again. deba@681: /// \param i The item. deba@681: State state(const Item &i) const { deba@683: int idx = _iim[i]; deba@681: if (idx >= 0) idx = 0; deba@681: return State(idx); deba@681: } deba@681: kpeter@709: /// \brief Set the state of an item in the heap. deba@681: /// kpeter@709: /// This function sets the state of the given item in the heap. kpeter@709: /// It can be used to manually clear the heap when it is important kpeter@709: /// to achive better time complexity. deba@681: /// \param i The item. deba@681: /// \param st The state. It should not be \c IN_HEAP. deba@681: void state(const Item& i, State st) { deba@681: switch (st) { deba@681: case POST_HEAP: deba@681: case PRE_HEAP: deba@681: if (state(i) == IN_HEAP) { deba@681: erase(i); deba@681: } deba@683: _iim[i] = st; deba@681: break; deba@681: case IN_HEAP: deba@681: break; deba@681: } deba@681: } deba@681: deba@681: private: deba@681: deba@681: struct BucketItem { deba@681: BucketItem(const Item& _item, int _value) deba@681: : item(_item), value(_value) {} deba@681: deba@681: Item item; deba@681: int value; deba@681: deba@681: int prev, next; deba@681: }; deba@681: deba@683: ItemIntMap& _iim; deba@683: std::vector _first; deba@683: std::vector _data; deba@683: mutable int _minimum; deba@681: deba@681: }; // class BucketHeap deba@681: kpeter@710: /// \ingroup heaps deba@681: /// kpeter@709: /// \brief Simplified bucket heap data structure. deba@681: /// deba@681: /// This class implements a simplified \e bucket \e heap data kpeter@709: /// structure. It does not provide some functionality, but it is kpeter@709: /// faster and simpler than BucketHeap. The main difference is kpeter@709: /// that BucketHeap stores a doubly-linked list for each key while kpeter@709: /// this class stores only simply-linked lists. It supports erasing kpeter@709: /// only for the item having minimum priority and it does not support kpeter@709: /// key increasing and decreasing. deba@681: /// kpeter@709: /// Note that this implementation does not conform to the alpar@877: /// \ref concepts::Heap "heap concept" due to the lack of some kpeter@709: /// functionality. kpeter@709: /// kpeter@709: /// \tparam IM A read-writable item map with \c int values, used kpeter@709: /// internally to handle the cross references. kpeter@709: /// \tparam MIN Indicate if the heap is a \e min-heap or a \e max-heap. kpeter@709: /// The default is \e min-heap. If this parameter is set to \c false, kpeter@709: /// then the comparison is reversed, so the top(), prio() and pop() kpeter@709: /// functions deal with the item having maximum priority instead of the kpeter@709: /// minimum. deba@681: /// deba@681: /// \sa BucketHeap deba@683: template deba@681: class SimpleBucketHeap { deba@681: deba@681: public: kpeter@709: kpeter@709: /// Type of the item-int map. kpeter@709: typedef IM ItemIntMap; kpeter@709: /// Type of the priorities. deba@681: typedef int Prio; kpeter@709: /// Type of the items stored in the heap. kpeter@709: typedef typename ItemIntMap::Key Item; kpeter@709: /// Type of the item-priority pairs. kpeter@709: typedef std::pair Pair; deba@681: deba@682: private: deba@682: deba@683: typedef _bucket_heap_bits::DirectionTraits Direction; deba@682: deba@682: public: deba@682: kpeter@709: /// \brief Type to represent the states of the items. deba@681: /// kpeter@709: /// Each item has a state associated to it. It can be "in heap", kpeter@709: /// "pre-heap" or "post-heap". The latter two are indifferent from the deba@681: /// heap's point of view, but may be useful to the user. deba@681: /// deba@683: /// The item-int map must be initialized in such way that it assigns deba@683: /// \c PRE_HEAP (-1) to any element to be put in the heap. deba@681: enum State { deba@683: IN_HEAP = 0, ///< = 0. deba@683: PRE_HEAP = -1, ///< = -1. deba@683: POST_HEAP = -2 ///< = -2. deba@681: }; deba@681: deba@681: public: deba@681: kpeter@709: /// \brief Constructor. deba@681: /// kpeter@709: /// Constructor. kpeter@709: /// \param map A map that assigns \c int values to the items. kpeter@709: /// It is used internally to handle the cross references. kpeter@709: /// The assigned value must be \c PRE_HEAP (-1) for each item. deba@683: explicit SimpleBucketHeap(ItemIntMap &map) deba@683: : _iim(map), _free(-1), _num(0), _minimum(0) {} deba@681: kpeter@709: /// \brief The number of items stored in the heap. deba@681: /// kpeter@709: /// This function returns the number of items stored in the heap. deba@683: int size() const { return _num; } deba@681: kpeter@709: /// \brief Check if the heap is empty. deba@681: /// kpeter@709: /// This function returns \c true if the heap is empty. deba@683: bool empty() const { return _num == 0; } deba@681: kpeter@709: /// \brief Make the heap empty. deba@681: /// kpeter@709: /// This functon makes the heap empty. kpeter@709: /// It does not change the cross reference map. If you want to reuse kpeter@709: /// a heap that is not surely empty, you should first clear it and kpeter@709: /// then you should set the cross reference map to \c PRE_HEAP kpeter@709: /// for each item. deba@681: void clear() { deba@683: _data.clear(); _first.clear(); _free = -1; _num = 0; _minimum = 0; deba@681: } deba@681: deba@681: /// \brief Insert a pair of item and priority into the heap. deba@681: /// kpeter@709: /// This function inserts \c p.first to the heap with priority kpeter@709: /// \c p.second. deba@681: /// \param p The pair to insert. kpeter@709: /// \pre \c p.first must not be stored in the heap. deba@681: void push(const Pair& p) { deba@681: push(p.first, p.second); deba@681: } deba@681: deba@681: /// \brief Insert an item into the heap with the given priority. deba@681: /// kpeter@709: /// This function inserts the given item into the heap with the kpeter@709: /// given priority. deba@681: /// \param i The item to insert. deba@681: /// \param p The priority of the item. kpeter@709: /// \pre \e i must not be stored in the heap. deba@681: void push(const Item &i, const Prio &p) { deba@681: int idx; deba@683: if (_free == -1) { deba@683: idx = _data.size(); deba@683: _data.push_back(BucketItem(i)); deba@681: } else { deba@683: idx = _free; deba@683: _free = _data[idx].next; deba@683: _data[idx].item = i; deba@681: } deba@683: _iim[i] = idx; deba@683: if (p >= int(_first.size())) _first.resize(p + 1, -1); deba@683: _data[idx].next = _first[p]; deba@683: _first[p] = idx; deba@683: if (Direction::less(p, _minimum)) { deba@683: _minimum = p; deba@681: } deba@683: ++_num; deba@681: } deba@681: kpeter@709: /// \brief Return the item having minimum priority. deba@681: /// kpeter@709: /// This function returns the item having minimum priority. kpeter@709: /// \pre The heap must be non-empty. deba@681: Item top() const { deba@683: while (_first[_minimum] == -1) { deba@683: Direction::increase(_minimum); deba@681: } deba@683: return _data[_first[_minimum]].item; deba@681: } deba@681: kpeter@709: /// \brief The minimum priority. deba@681: /// kpeter@709: /// This function returns the minimum priority. kpeter@709: /// \pre The heap must be non-empty. deba@681: Prio prio() const { deba@683: while (_first[_minimum] == -1) { deba@683: Direction::increase(_minimum); deba@681: } deba@683: return _minimum; deba@681: } deba@681: kpeter@709: /// \brief Remove the item having minimum priority. deba@681: /// kpeter@709: /// This function removes the item having minimum priority. deba@681: /// \pre The heap must be non-empty. deba@681: void pop() { deba@683: while (_first[_minimum] == -1) { deba@683: Direction::increase(_minimum); deba@681: } deba@683: int idx = _first[_minimum]; deba@683: _iim[_data[idx].item] = -2; deba@683: _first[_minimum] = _data[idx].next; deba@683: _data[idx].next = _free; deba@683: _free = idx; deba@683: --_num; deba@681: } deba@681: kpeter@709: /// \brief The priority of the given item. deba@681: /// kpeter@709: /// This function returns the priority of the given item. deba@681: /// \param i The item. kpeter@709: /// \pre \e i must be in the heap. kpeter@709: /// \warning This operator is not a constant time function because kpeter@709: /// it scans the whole data structure to find the proper value. deba@681: Prio operator[](const Item &i) const { kpeter@709: for (int k = 0; k < int(_first.size()); ++k) { deba@683: int idx = _first[k]; deba@681: while (idx != -1) { deba@683: if (_data[idx].item == i) { deba@681: return k; deba@681: } deba@683: idx = _data[idx].next; deba@681: } deba@681: } deba@681: return -1; deba@681: } deba@681: kpeter@709: /// \brief Return the state of an item. deba@681: /// kpeter@709: /// This method returns \c PRE_HEAP if the given item has never kpeter@709: /// been in the heap, \c IN_HEAP if it is in the heap at the moment, kpeter@709: /// and \c POST_HEAP otherwise. kpeter@709: /// In the latter case it is possible that the item will get back kpeter@709: /// to the heap again. deba@681: /// \param i The item. deba@681: State state(const Item &i) const { deba@683: int idx = _iim[i]; deba@681: if (idx >= 0) idx = 0; deba@681: return State(idx); deba@681: } deba@681: deba@681: private: deba@681: deba@681: struct BucketItem { deba@681: BucketItem(const Item& _item) deba@681: : item(_item) {} deba@681: deba@681: Item item; deba@681: int next; deba@681: }; deba@681: deba@683: ItemIntMap& _iim; deba@683: std::vector _first; deba@683: std::vector _data; deba@683: int _free, _num; deba@683: mutable int _minimum; deba@681: deba@681: }; // class SimpleBucketHeap deba@681: deba@681: } deba@681: deba@681: #endif