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: kpeter@757: ///\ingroup heaps deba@728: ///\file kpeter@756: ///\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: kpeter@757: /// \ingroup heaps deba@728: /// kpeter@756: /// \brief Bucket heap data structure. deba@728: /// kpeter@756: /// This class implements the \e bucket \e heap data structure. kpeter@756: /// It practically conforms to the \ref concepts::Heap "heap concept", kpeter@756: /// but it has some limitations. deba@728: /// kpeter@756: /// The bucket heap is a very simple structure. It can store only kpeter@756: /// \c int priorities and it maintains a list of items for each priority kpeter@756: /// in the range [0..C). So it should only be used when the kpeter@756: /// priorities are small. It is not intended to use as a Dijkstra heap. kpeter@756: /// kpeter@756: /// \tparam IM A read-writable item map with \c int values, used kpeter@756: /// internally to handle the cross references. kpeter@756: /// \tparam MIN Indicate if the heap is a \e min-heap or a \e max-heap. kpeter@756: /// The default is \e min-heap. If this parameter is set to \c false, kpeter@756: /// then the comparison is reversed, so the top(), prio() and pop() kpeter@756: /// functions deal with the item having maximum priority instead of the kpeter@756: /// minimum. kpeter@756: /// kpeter@756: /// \sa SimpleBucketHeap deba@730: template deba@728: class BucketHeap { deba@728: deba@728: public: kpeter@756: kpeter@756: /// Type of the item-int map. kpeter@756: typedef IM ItemIntMap; kpeter@756: /// Type of the priorities. deba@728: typedef int Prio; kpeter@756: /// Type of the items stored in the heap. kpeter@756: typedef typename ItemIntMap::Key Item; kpeter@756: /// Type of the item-priority pairs. kpeter@756: typedef std::pair Pair; deba@728: deba@729: private: deba@729: deba@730: typedef _bucket_heap_bits::DirectionTraits Direction; deba@729: deba@729: public: deba@729: kpeter@756: /// \brief Type to represent the states of the items. deba@728: /// kpeter@756: /// Each item has a state associated to it. It can be "in heap", kpeter@756: /// "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: kpeter@756: kpeter@756: /// \brief Constructor. deba@728: /// kpeter@756: /// Constructor. kpeter@756: /// \param map A map that assigns \c int values to the items. kpeter@756: /// It is used internally to handle the cross references. kpeter@756: /// The assigned value must be \c PRE_HEAP (-1) for each item. deba@730: explicit BucketHeap(ItemIntMap &map) : _iim(map), _minimum(0) {} deba@728: kpeter@756: /// \brief The number of items stored in the heap. deba@728: /// kpeter@756: /// This function returns the number of items stored in the heap. deba@730: int size() const { return _data.size(); } deba@728: kpeter@756: /// \brief Check if the heap is empty. deba@728: /// kpeter@756: /// This function returns \c true if the heap is empty. deba@730: bool empty() const { return _data.empty(); } deba@728: kpeter@756: /// \brief Make the heap empty. deba@728: /// kpeter@756: /// This functon makes the heap empty. kpeter@756: /// It does not change the cross reference map. If you want to reuse kpeter@756: /// a heap that is not surely empty, you should first clear it and kpeter@756: /// then you should set the cross reference map to \c PRE_HEAP kpeter@756: /// for each item. 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: kpeter@756: deba@728: /// \brief Insert a pair of item and priority into the heap. deba@728: /// kpeter@756: /// This function inserts \c p.first to the heap with priority kpeter@756: /// \c p.second. deba@728: /// \param p The pair to insert. kpeter@756: /// \pre \c p.first must not be stored in the heap. 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: /// kpeter@756: /// This function inserts the given item into the heap with the kpeter@756: /// given priority. deba@728: /// \param i The item to insert. deba@728: /// \param p The priority of the item. kpeter@756: /// \pre \e i must not be stored in the heap. 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: kpeter@756: /// \brief Return the item having minimum priority. deba@728: /// kpeter@756: /// This function returns the item having minimum priority. kpeter@756: /// \pre The heap must be non-empty. 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: kpeter@756: /// \brief The minimum priority. deba@728: /// kpeter@756: /// This function returns the minimum priority. kpeter@756: /// \pre The heap must be non-empty. 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: kpeter@756: /// \brief Remove the item having minimum priority. deba@728: /// kpeter@756: /// This function removes the item having minimum priority. 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: kpeter@756: /// \brief Remove the given item from the heap. deba@728: /// kpeter@756: /// This function removes the given item from the heap if it is kpeter@756: /// already stored. kpeter@756: /// \param i The item to delete. kpeter@756: /// \pre \e i must be in the heap. 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: kpeter@756: /// \brief The priority of the given item. deba@728: /// kpeter@756: /// This function returns the priority of the given item. deba@728: /// \param i The item. kpeter@756: /// \pre \e i must be in the heap. deba@728: Prio operator[](const Item &i) const { deba@730: int idx = _iim[i]; deba@730: return _data[idx].value; deba@728: } deba@728: kpeter@756: /// \brief Set the priority of an item or insert it, if it is kpeter@756: /// not stored in the heap. deba@728: /// kpeter@756: /// This method sets the priority of the given item if it is kpeter@756: /// already stored in the heap. Otherwise it inserts the given kpeter@756: /// item into the heap with the given priority. 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: kpeter@756: /// \brief Decrease the priority of an item to the given value. deba@728: /// kpeter@756: /// This function decreases the priority of an item to the given value. deba@728: /// \param i The item. deba@728: /// \param p The priority. kpeter@756: /// \pre \e i must be stored in the heap with priority at least \e p. 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: kpeter@756: /// \brief Increase the priority of an item to the given value. deba@728: /// kpeter@756: /// This function increases the priority of an item to the given value. deba@728: /// \param i The item. deba@728: /// \param p The priority. kpeter@756: /// \pre \e i must be stored in the heap with priority at most \e p. 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: kpeter@756: /// \brief Return the state of an item. deba@728: /// kpeter@756: /// This method returns \c PRE_HEAP if the given item has never kpeter@756: /// been in the heap, \c IN_HEAP if it is in the heap at the moment, kpeter@756: /// and \c POST_HEAP otherwise. kpeter@756: /// In the latter case it is possible that the item will get back kpeter@756: /// 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: kpeter@756: /// \brief Set the state of an item in the heap. deba@728: /// kpeter@756: /// This function sets the state of the given item in the heap. kpeter@756: /// It can be used to manually clear the heap when it is important kpeter@756: /// to achive 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: kpeter@757: /// \ingroup heaps deba@728: /// kpeter@756: /// \brief Simplified bucket heap data structure. deba@728: /// deba@728: /// This class implements a simplified \e bucket \e heap data kpeter@756: /// structure. It does not provide some functionality, but it is kpeter@756: /// faster and simpler than BucketHeap. The main difference is kpeter@756: /// that BucketHeap stores a doubly-linked list for each key while kpeter@756: /// this class stores only simply-linked lists. It supports erasing kpeter@756: /// only for the item having minimum priority and it does not support kpeter@756: /// key increasing and decreasing. deba@728: /// kpeter@756: /// Note that this implementation does not conform to the kpeter@756: /// \ref concepts::Heap "heap concept" due to the lack of some kpeter@756: /// functionality. kpeter@756: /// kpeter@756: /// \tparam IM A read-writable item map with \c int values, used kpeter@756: /// internally to handle the cross references. kpeter@756: /// \tparam MIN Indicate if the heap is a \e min-heap or a \e max-heap. kpeter@756: /// The default is \e min-heap. If this parameter is set to \c false, kpeter@756: /// then the comparison is reversed, so the top(), prio() and pop() kpeter@756: /// functions deal with the item having maximum priority instead of the kpeter@756: /// minimum. deba@728: /// deba@728: /// \sa BucketHeap deba@730: template deba@728: class SimpleBucketHeap { deba@728: deba@728: public: kpeter@756: kpeter@756: /// Type of the item-int map. kpeter@756: typedef IM ItemIntMap; kpeter@756: /// Type of the priorities. deba@728: typedef int Prio; kpeter@756: /// Type of the items stored in the heap. kpeter@756: typedef typename ItemIntMap::Key Item; kpeter@756: /// Type of the item-priority pairs. kpeter@756: typedef std::pair Pair; deba@728: deba@729: private: deba@729: deba@730: typedef _bucket_heap_bits::DirectionTraits Direction; deba@729: deba@729: public: deba@729: kpeter@756: /// \brief Type to represent the states of the items. deba@728: /// kpeter@756: /// Each item has a state associated to it. It can be "in heap", kpeter@756: /// "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: kpeter@756: /// \brief Constructor. deba@728: /// kpeter@756: /// Constructor. kpeter@756: /// \param map A map that assigns \c int values to the items. kpeter@756: /// It is used internally to handle the cross references. kpeter@756: /// The assigned value must be \c PRE_HEAP (-1) for each item. deba@730: explicit SimpleBucketHeap(ItemIntMap &map) deba@730: : _iim(map), _free(-1), _num(0), _minimum(0) {} deba@728: kpeter@756: /// \brief The number of items stored in the heap. deba@728: /// kpeter@756: /// This function returns the number of items stored in the heap. deba@730: int size() const { return _num; } deba@728: kpeter@756: /// \brief Check if the heap is empty. deba@728: /// kpeter@756: /// This function returns \c true if the heap is empty. deba@730: bool empty() const { return _num == 0; } deba@728: kpeter@756: /// \brief Make the heap empty. deba@728: /// kpeter@756: /// This functon makes the heap empty. kpeter@756: /// It does not change the cross reference map. If you want to reuse kpeter@756: /// a heap that is not surely empty, you should first clear it and kpeter@756: /// then you should set the cross reference map to \c PRE_HEAP kpeter@756: /// for each item. 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: /// kpeter@756: /// This function inserts \c p.first to the heap with priority kpeter@756: /// \c p.second. deba@728: /// \param p The pair to insert. kpeter@756: /// \pre \c p.first must not be stored in the heap. 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: /// kpeter@756: /// This function inserts the given item into the heap with the kpeter@756: /// given priority. deba@728: /// \param i The item to insert. deba@728: /// \param p The priority of the item. kpeter@756: /// \pre \e i must not be stored in the heap. 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: kpeter@756: /// \brief Return the item having minimum priority. deba@728: /// kpeter@756: /// This function returns the item having minimum priority. kpeter@756: /// \pre The heap must be non-empty. 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: kpeter@756: /// \brief The minimum priority. deba@728: /// kpeter@756: /// This function returns the minimum priority. kpeter@756: /// \pre The heap must be non-empty. 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: kpeter@756: /// \brief Remove the item having minimum priority. deba@728: /// kpeter@756: /// This function removes the item having minimum priority. 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: kpeter@756: /// \brief The priority of the given item. deba@728: /// kpeter@756: /// This function returns the priority of the given item. deba@728: /// \param i The item. kpeter@756: /// \pre \e i must be in the heap. kpeter@756: /// \warning This operator is not a constant time function because kpeter@756: /// it scans the whole data structure to find the proper value. deba@728: Prio operator[](const Item &i) const { kpeter@756: for (int k = 0; k < int(_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: kpeter@756: /// \brief Return the state of an item. deba@728: /// kpeter@756: /// This method returns \c PRE_HEAP if the given item has never kpeter@756: /// been in the heap, \c IN_HEAP if it is in the heap at the moment, kpeter@756: /// and \c POST_HEAP otherwise. kpeter@756: /// In the latter case it is possible that the item will get back kpeter@756: /// 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