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:  *
deba@681:  * Copyright (C) 2003-2009
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: 
deba@681: ///\ingroup auxdat
deba@681: ///\file
deba@681: ///\brief Bucket Heap implementation.
deba@681: 
deba@681: #include <vector>
deba@681: #include <utility>
deba@681: #include <functional>
deba@681: 
deba@681: namespace lemon {
deba@681: 
deba@682:   namespace _bucket_heap_bits {
deba@682: 
deba@683:     template <bool MIN>
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<false> {
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: 
deba@681:   /// \ingroup auxdat
deba@681:   ///
deba@681:   /// \brief A Bucket Heap implementation.
deba@681:   ///
deba@681:   /// This class implements the \e bucket \e heap data structure. A \e heap
deba@681:   /// is a data structure for storing items with specified values called \e
deba@681:   /// priorities in such a way that finding the item with minimum priority is
deba@681:   /// efficient. The bucket heap is very simple implementation, it can store
deba@681:   /// only integer priorities and it stores for each priority in the
deba@681:   /// \f$ [0..C) \f$ range a list of items. So it should be used only when
deba@681:   /// the priorities are small. It is not intended to use as dijkstra heap.
deba@681:   ///
deba@683:   /// \param IM A read and write Item int map, used internally
deba@681:   /// to handle the cross references.
deba@683:   /// \param MIN If the given parameter is false then instead of the
deba@683:   /// minimum value the maximum can be retrivied with the top() and
deba@683:   /// prio() member functions.
deba@683:   template <typename IM, bool MIN = true>
deba@681:   class BucketHeap {
deba@681: 
deba@681:   public:
deba@681:     /// \e
deba@683:     typedef typename IM::Key Item;
deba@681:     /// \e
deba@681:     typedef int Prio;
deba@681:     /// \e
deba@681:     typedef std::pair<Item, Prio> Pair;
deba@681:     /// \e
deba@683:     typedef IM ItemIntMap;
deba@681: 
deba@682:   private:
deba@682: 
deba@683:     typedef _bucket_heap_bits::DirectionTraits<MIN> Direction;
deba@682: 
deba@682:   public:
deba@682: 
deba@681:     /// \brief Type to represent the items states.
deba@681:     ///
deba@681:     /// Each Item element have a state associated to it. It may be "in heap",
deba@681:     /// "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 (<tt>-1</tt>) 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:     /// \brief The constructor.
deba@681:     ///
deba@681:     /// The constructor.
deba@683:     /// \param map should be given to the constructor, since it is used
deba@681:     /// internally to handle the cross references. The value of the map
deba@681:     /// should be PRE_HEAP (-1) for each element.
deba@683:     explicit BucketHeap(ItemIntMap &map) : _iim(map), _minimum(0) {}
deba@681: 
deba@681:     /// The number of items stored in the heap.
deba@681:     ///
deba@681:     /// \brief Returns the number of items stored in the heap.
deba@683:     int size() const { return _data.size(); }
deba@681: 
deba@681:     /// \brief Checks if the heap stores no items.
deba@681:     ///
deba@681:     /// Returns \c true if and only if the heap stores no items.
deba@683:     bool empty() const { return _data.empty(); }
deba@681: 
deba@681:     /// \brief Make empty this heap.
deba@681:     ///
deba@681:     /// Make empty this heap. It does not change the cross reference
deba@681:     /// map.  If you want to reuse a heap what is not surely empty you
deba@681:     /// should first clear the heap and after that you should set the
deba@681:     /// cross reference map for each item to \c PRE_HEAP.
deba@681:     void clear() {
deba@683:       _data.clear(); _first.clear(); _minimum = 0;
deba@681:     }
deba@681: 
deba@681:   private:
deba@681: 
deba@681:     void relocate_last(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:
deba@681:     /// \brief Insert a pair of item and priority into the heap.
deba@681:     ///
deba@681:     /// Adds \c p.first to the heap with priority \c p.second.
deba@681:     /// \param p The pair to insert.
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:     ///
deba@681:     /// Adds \c i to the heap with priority \c p.
deba@681:     /// \param i The item to insert.
deba@681:     /// \param p The priority of the item.
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: 
deba@681:     /// \brief Returns the item with minimum priority.
deba@681:     ///
deba@681:     /// This method returns the item with minimum priority.
deba@681:     /// \pre The heap must be nonempty.
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: 
deba@681:     /// \brief Returns the minimum priority.
deba@681:     ///
deba@681:     /// It returns the minimum priority.
deba@681:     /// \pre The heap must be nonempty.
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: 
deba@681:     /// \brief Deletes the item with minimum priority.
deba@681:     ///
deba@681:     /// This method deletes the item with minimum priority from the heap.
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);
deba@681:       relocate_last(idx);
deba@681:     }
deba@681: 
deba@681:     /// \brief Deletes \c i from the heap.
deba@681:     ///
deba@681:     /// This method deletes item \c i from the heap, if \c i was
deba@681:     /// already stored in the heap.
deba@681:     /// \param i The item to erase.
deba@681:     void erase(const Item &i) {
deba@683:       int idx = _iim[i];
deba@683:       _iim[_data[idx].item] = -2;
deba@681:       unlace(idx);
deba@681:       relocate_last(idx);
deba@681:     }
deba@681: 
deba@681: 
deba@681:     /// \brief Returns the priority of \c i.
deba@681:     ///
deba@681:     /// This function returns the priority of item \c i.
deba@681:     /// \pre \c i must be in the heap.
deba@681:     /// \param i The item.
deba@681:     Prio operator[](const Item &i) const {
deba@683:       int idx = _iim[i];
deba@683:       return _data[idx].value;
deba@681:     }
deba@681: 
deba@681:     /// \brief \c i gets to the heap with priority \c p independently
deba@681:     /// if \c i was already there.
deba@681:     ///
deba@681:     /// This method calls \ref push(\c i, \c p) if \c i is not stored
deba@681:     /// in the heap and sets the priority of \c i to \c p otherwise.
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: 
deba@681:     /// \brief Decreases the priority of \c i to \c p.
deba@681:     ///
deba@681:     /// This method decreases the priority of item \c i to \c p.
deba@681:     /// \pre \c i must be stored in the heap with priority at least \c
deba@681:     /// p relative to \c Compare.
deba@681:     /// \param i The item.
deba@681:     /// \param p The priority.
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: 
deba@681:     /// \brief Increases the priority of \c i to \c p.
deba@681:     ///
deba@681:     /// This method sets the priority of item \c i to \c p.
deba@681:     /// \pre \c i must be stored in the heap with priority at most \c
deba@681:     /// p relative to \c Compare.
deba@681:     /// \param i The item.
deba@681:     /// \param p The priority.
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: 
deba@681:     /// \brief Returns if \c item is in, has already been in, or has
deba@681:     /// never been in the heap.
deba@681:     ///
deba@681:     /// This method returns PRE_HEAP if \c item has never been in the
deba@681:     /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
deba@681:     /// otherwise. In the latter case it is possible that \c item will
deba@681:     /// get back 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:     /// \brief Sets the state of the \c item in the heap.
deba@681:     ///
deba@681:     /// Sets the state of the \c item in the heap. It can be used to
deba@681:     /// manually clear the heap when it is important to achive the
deba@681:     /// 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<int> _first;
deba@683:     std::vector<BucketItem> _data;
deba@683:     mutable int _minimum;
deba@681: 
deba@681:   }; // class BucketHeap
deba@681: 
deba@681:   /// \ingroup auxdat
deba@681:   ///
deba@681:   /// \brief A Simplified Bucket Heap implementation.
deba@681:   ///
deba@681:   /// This class implements a simplified \e bucket \e heap data
deba@681:   /// structure.  It does not provide some functionality but it faster
deba@681:   /// and simplier data structure than the BucketHeap. The main
deba@681:   /// difference is that the BucketHeap stores for every key a double
deba@681:   /// linked list while this class stores just simple lists. In the
deba@682:   /// other way it does not support erasing each elements just the
deba@681:   /// minimal and it does not supports key increasing, decreasing.
deba@681:   ///
deba@683:   /// \param IM A read and write Item int map, used internally
deba@681:   /// to handle the cross references.
deba@683:   /// \param MIN If the given parameter is false then instead of the
deba@683:   /// minimum value the maximum can be retrivied with the top() and
deba@683:   /// prio() member functions.
deba@681:   ///
deba@681:   /// \sa BucketHeap
deba@683:   template <typename IM, bool MIN = true >
deba@681:   class SimpleBucketHeap {
deba@681: 
deba@681:   public:
deba@683:     typedef typename IM::Key Item;
deba@681:     typedef int Prio;
deba@681:     typedef std::pair<Item, Prio> Pair;
deba@683:     typedef IM ItemIntMap;
deba@681: 
deba@682:   private:
deba@682: 
deba@683:     typedef _bucket_heap_bits::DirectionTraits<MIN> Direction;
deba@682: 
deba@682:   public:
deba@682: 
deba@681:     /// \brief Type to represent the items states.
deba@681:     ///
deba@681:     /// Each Item element have a state associated to it. It may be "in heap",
deba@681:     /// "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 (<tt>-1</tt>) 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: 
deba@681:     /// \brief The constructor.
deba@681:     ///
deba@681:     /// The constructor.
deba@683:     /// \param map should be given to the constructor, since it is used
deba@681:     /// internally to handle the cross references. The value of the map
deba@681:     /// should be PRE_HEAP (-1) for each element.
deba@683:     explicit SimpleBucketHeap(ItemIntMap &map)
deba@683:       : _iim(map), _free(-1), _num(0), _minimum(0) {}
deba@681: 
deba@681:     /// \brief Returns the number of items stored in the heap.
deba@681:     ///
deba@681:     /// The number of items stored in the heap.
deba@683:     int size() const { return _num; }
deba@681: 
deba@681:     /// \brief Checks if the heap stores no items.
deba@681:     ///
deba@681:     /// Returns \c true if and only if the heap stores no items.
deba@683:     bool empty() const { return _num == 0; }
deba@681: 
deba@681:     /// \brief Make empty this heap.
deba@681:     ///
deba@681:     /// Make empty this heap. It does not change the cross reference
deba@681:     /// map.  If you want to reuse a heap what is not surely empty you
deba@681:     /// should first clear the heap and after that you should set the
deba@681:     /// cross reference map for each item to \c PRE_HEAP.
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:     ///
deba@681:     /// Adds \c p.first to the heap with priority \c p.second.
deba@681:     /// \param p The pair to insert.
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:     ///
deba@681:     /// Adds \c i to the heap with priority \c p.
deba@681:     /// \param i The item to insert.
deba@681:     /// \param p The priority of the item.
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: 
deba@681:     /// \brief Returns the item with minimum priority.
deba@681:     ///
deba@681:     /// This method returns the item with minimum priority.
deba@681:     /// \pre The heap must be nonempty.
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: 
deba@681:     /// \brief Returns the minimum priority.
deba@681:     ///
deba@681:     /// It returns the minimum priority.
deba@681:     /// \pre The heap must be nonempty.
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: 
deba@681:     /// \brief Deletes the item with minimum priority.
deba@681:     ///
deba@681:     /// This method deletes the item with minimum priority from the heap.
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: 
deba@681:     /// \brief Returns the priority of \c i.
deba@681:     ///
deba@681:     /// This function returns the priority of item \c i.
deba@681:     /// \warning This operator is not a constant time function
deba@681:     /// because it scans the whole data structure to find the proper
deba@681:     /// value.
deba@681:     /// \pre \c i must be in the heap.
deba@681:     /// \param i The item.
deba@681:     Prio operator[](const Item &i) const {
deba@683:       for (int k = 0; k < _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: 
deba@681:     /// \brief Returns if \c item is in, has already been in, or has
deba@681:     /// never been in the heap.
deba@681:     ///
deba@681:     /// This method returns PRE_HEAP if \c item has never been in the
deba@681:     /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
deba@681:     /// otherwise. In the latter case it is possible that \c item will
deba@681:     /// get back 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<int> _first;
deba@683:     std::vector<BucketItem> _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