deba@1186: /* -*- C++ -*-
deba@1186:  *
alpar@1956:  * This file is a part of LEMON, a generic C++ optimization library
alpar@1956:  *
alpar@1956:  * Copyright (C) 2003-2006
alpar@1956:  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1359:  * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@1186:  *
deba@1186:  * Permission to use, modify and distribute this software is granted
deba@1186:  * provided that this copyright notice appears in all copies. For
deba@1186:  * precise terms see the accompanying LICENSE file.
deba@1186:  *
deba@1186:  * This software is provided "AS IS" with no warranty of any kind,
deba@1186:  * express or implied, and with no claim as to its suitability for any
deba@1186:  * purpose.
deba@1186:  *
deba@1186:  */
deba@1186: 
deba@1186: #ifndef LEMON_RADIX_HEAP_H
deba@1186: #define LEMON_RADIX_HEAP_H
deba@1186: 
deba@1186: ///\ingroup auxdat
deba@1186: ///\file
deba@1186: ///\brief Radix Heap implementation.
deba@1186: 
deba@1186: #include <vector>
deba@1186: #include <lemon/error.h>
deba@1186: 
deba@1186: namespace lemon {
deba@1186: 
deba@1331:   /// \brief Exception thrown by RadixHeap.
deba@1331:   ///  
deba@1331:   /// This Exception is thrown when a smaller priority
deba@1331:   /// is inserted into the \e RadixHeap then the last time erased.
deba@1331:   /// \see RadixHeap
deba@1331:   /// \author Balazs Dezso
deba@1186: 
deba@1331:   class UnderFlowPriorityError : public RuntimeError {
deba@1186:   public:
alpar@2151:     virtual const char* what() const throw() {
deba@1331:       return "lemon::UnderFlowPriorityError";
deba@1186:     }  
deba@1186:   };
deba@1186: 
deba@1717:   /// \ingroup auxdata
deba@1717:   ///
deba@1331:   /// \brief A Radix Heap implementation.
deba@1331:   ///
deba@1331:   /// This class implements the \e radix \e heap data structure. A \e heap
deba@1331:   /// is a data structure for storing items with specified values called \e
deba@1331:   /// priorities in such a way that finding the item with minimum priority is
deba@1331:   /// efficient. This heap type can store only items with \e int priority.
deba@1331:   /// In a heap one can change the priority of an item, add or erase an 
deba@1331:   /// item, but the priority cannot be decreased under the last removed 
deba@1331:   /// item's priority.
deba@1331:   ///
deba@1331:   /// \param _ItemIntMap A read and writable Item int map, used internally
deba@1331:   /// to handle the cross references.
deba@1331:   ///
deba@1331:   /// \see BinHeap
deba@1331:   /// \see Dijkstra
deba@1331:   /// \author Balazs Dezso
deba@1331: 
mqrelly@2263:   template <typename _ItemIntMap>
deba@1186:   class RadixHeap {
deba@1186: 
deba@1186:   public:
mqrelly@2263:     typedef typename _ItemIntMap::Key Item;
deba@1186:     typedef int Prio;
deba@1186:     typedef _ItemIntMap ItemIntMap;
deba@1186: 
deba@1331:     /// \brief Type to represent the items states.
deba@1186:     ///
deba@1331:     /// Each Item element have a state associated to it. It may be "in heap",
alpar@1336:     /// "pre heap" or "post heap". The latter two are indifferent from the
deba@1331:     /// heap's point of view, but may be useful to the user.
deba@1331:     ///
alpar@1336:     /// The ItemIntMap \e should be initialized in such way that it maps
deba@1331:     /// PRE_HEAP (-1) to any element to be put in the heap...
deba@1186:     enum state_enum {
deba@1186:       IN_HEAP = 0,
deba@1186:       PRE_HEAP = -1,
deba@1186:       POST_HEAP = -2
deba@1186:     };
deba@1186: 
deba@1186:   private:
deba@1186:     
deba@1186:     struct RadixItem {
deba@1186:       int prev, next, box;
deba@1186:       Item item;
deba@1186:       int prio;
deba@1186:       RadixItem(Item _item, int _prio) : item(_item), prio(_prio) {}
deba@1186:     };
deba@1186: 
deba@1186:     struct RadixBox {
deba@1186:       int first;
deba@1186:       int min, size;
deba@1186:       RadixBox(int _min, int _size) : first(-1), min(_min), size(_size) {}
deba@1186:     };
deba@1186: 
deba@1186:     std::vector<RadixItem> data;
deba@1186:     std::vector<RadixBox> boxes;
deba@1186: 
deba@1186:     ItemIntMap &iim;
deba@1186: 
deba@1186: 
deba@1186:   public:
deba@1331:     /// \brief The constructor.
deba@1331:     ///
deba@1331:     /// The constructor.
deba@1331:     ///
deba@1331:     /// \param _iim It should be given to the constructor, since it is used
deba@1331:     /// internally to handle the cross references. The value of the map
deba@1331:     /// should be PRE_HEAP (-1) for each element.
deba@1331:     ///
deba@1717:     /// \param minimal The initial minimal value of the heap.
deba@1331:     /// \param capacity It determines the initial capacity of the heap. 
deba@1717:     RadixHeap(ItemIntMap &_iim, int minimal = 0, int capacity = 0) 
deba@1717:       : iim(_iim) {
deba@1717:       boxes.push_back(RadixBox(minimal, 1));
deba@1717:       boxes.push_back(RadixBox(minimal + 1, 1));
deba@1717:       while (lower(boxes.size() - 1, capacity + minimal - 1)) {
deba@1186: 	extend();
deba@1186:       }
deba@1186:     }
deba@1186: 
deba@1331:     /// The number of items stored in the heap.
deba@1331:     ///
deba@1331:     /// \brief Returns the number of items stored in the heap.
deba@1186:     int size() const { return data.size(); }
deba@1331:     /// \brief Checks if the heap stores no items.
deba@1331:     ///
deba@1331:     /// Returns \c true if and only if the heap stores no items.
deba@1186:     bool empty() const { return data.empty(); }
deba@1186: 
deba@1717:     /// \brief Make empty this heap.
deba@1717:     /// 
deba@2050:     /// Make empty this heap. It does not change the cross reference
deba@2050:     /// map.  If you want to reuse a heap what is not surely empty you
deba@2050:     /// should first clear the heap and after that you should set the
deba@2050:     /// cross reference map for each item to \c PRE_HEAP.
deba@1717:     void clear(int minimal = 0, int capacity = 0) { 
deba@1717:       data.clear(); boxes.clear(); 
deba@1717:       boxes.push_back(RadixBox(minimal, 1));
deba@1717:       boxes.push_back(RadixBox(minimal + 1, 1));
deba@1717:       while (lower(boxes.size() - 1, capacity + minimal - 1)) {
deba@1717: 	extend();
deba@1717:       }
deba@1717:     }
deba@1717: 
deba@1186:   private:
deba@1186: 
deba@1186:     bool upper(int box, Prio prio) {
deba@1186:       return prio < boxes[box].min;
deba@1186:     }
deba@1186: 
deba@1186:     bool lower(int box, Prio prio) {
deba@1186:       return prio >= boxes[box].min + boxes[box].size;
deba@1186:     }
deba@1186: 
deba@1186:     /// \brief Remove item from the box list.
deba@1186:     void remove(int index) {
deba@1186:       if (data[index].prev >= 0) {
deba@1186: 	data[data[index].prev].next = data[index].next;
deba@1186:       } else {
deba@1186: 	boxes[data[index].box].first = data[index].next;
deba@1186:       }
deba@1186:       if (data[index].next >= 0) {
deba@1186: 	data[data[index].next].prev = data[index].prev;
deba@1186:       }
deba@1186:     }
deba@1186: 
deba@1186:     /// \brief Insert item into the box list.
deba@1186:     void insert(int box, int index) {
deba@1186:       if (boxes[box].first == -1) {
deba@1186: 	boxes[box].first = index;
deba@1186: 	data[index].next = data[index].prev = -1;
deba@1186:       } else {
deba@1186: 	data[index].next = boxes[box].first;
deba@1186: 	data[boxes[box].first].prev = index;
deba@1186: 	data[index].prev = -1;
deba@1186: 	boxes[box].first = index;
deba@1186:       }
deba@1186:       data[index].box = box;
deba@1186:     }
deba@1186: 
deba@1186:     /// \brief Add a new box to the box list.
deba@1186:     void extend() {
deba@1186:       int min = boxes.back().min + boxes.back().size;
deba@1186:       int size = 2 * boxes.back().size;
deba@1186:       boxes.push_back(RadixBox(min, size));
deba@1186:     }
deba@1186: 
deba@1186:     /// \brief Move an item up into the proper box.
deba@1186:     void bubble_up(int index) {
deba@1205:       if (!lower(data[index].box, data[index].prio)) return;
deba@1186:       remove(index);
deba@1186:       int box = findUp(data[index].box, data[index].prio);
deba@1186:       insert(box, index);      
deba@1186:     }
deba@1186: 
deba@1186:     /// \brief Find up the proper box for the item with the given prio.
deba@1186:     int findUp(int start, int prio) {
deba@1186:       while (lower(start, prio)) {
deba@1186: 	if (++start == (int)boxes.size()) {
deba@1186: 	  extend();
deba@1186: 	}
deba@1186:       }
deba@1186:       return start;
deba@1186:     }
deba@1186: 
deba@1186:     /// \brief Move an item down into the proper box.
deba@1186:     void bubble_down(int index) {
deba@1186:       if (!upper(data[index].box, data[index].prio)) return;
deba@1186:       remove(index);
deba@1186:       int box = findDown(data[index].box, data[index].prio);
deba@1186:       insert(box, index);
deba@1186:     }
deba@1186: 
deba@1186:     /// \brief Find up the proper box for the item with the given prio.
deba@1186:     int findDown(int start, int prio) {
deba@1186:       while (upper(start, prio)) {
deba@1331: 	if (--start < 0) throw UnderFlowPriorityError();
deba@1186:       }
deba@1186:       return start;
deba@1186:     }
deba@1186: 
deba@1186:     /// \brief Find the first not empty box.
deba@1186:     int findFirst() {
deba@1186:       int first = 0;
deba@1186:       while (boxes[first].first == -1) ++first;
deba@1186:       return first;
deba@1186:     }
deba@1186: 
deba@1186:     /// \brief Gives back the minimal prio of the box.
deba@1186:     int minValue(int box) {
deba@1186:       int min = data[boxes[box].first].prio;
deba@1186:       for (int k = boxes[box].first; k != -1; k = data[k].next) {
deba@1186: 	if (data[k].prio < min) min = data[k].prio;
deba@1186:       }
deba@1186:       return min;
deba@1186:     }
deba@1186: 
deba@1186:     /// \brief Rearrange the items of the heap and makes the 
deba@1186:     /// first box not empty.
deba@1186:     void moveDown() {
deba@1186:       int box = findFirst();
deba@1186:       if (box == 0) return;
deba@1186:       int min = minValue(box);
deba@1186:       for (int i = 0; i <= box; ++i) {
deba@1186: 	boxes[i].min = min;
deba@1186: 	min += boxes[i].size;
deba@1186:       }
deba@1186:       int curr = boxes[box].first, next;
deba@1186:       while (curr != -1) {
deba@1186: 	next = data[curr].next;
deba@1186: 	bubble_down(curr);
deba@1186: 	curr = next;
deba@1186:       }      
deba@1186:     }
deba@1186: 
deba@1186:     void relocate_last(int index) {
deba@1186:       if (index != (int)data.size() - 1) {
deba@1186: 	data[index] = data.back();
deba@1186: 	if (data[index].prev != -1) {
deba@1186: 	  data[data[index].prev].next = index;
deba@1186: 	} else {
deba@1186: 	  boxes[data[index].box].first = index;
deba@1186: 	}
deba@1186: 	if (data[index].next != -1) {
deba@1186: 	  data[data[index].next].prev = index;
deba@1186: 	}
deba@1186: 	iim[data[index].item] = index;
deba@1186:       }
deba@1186:       data.pop_back();
deba@1186:     }
deba@1186: 
deba@1186:   public:
deba@1186: 
deba@1717:     /// \brief Insert an item into the heap with the given priority.
deba@1331:     ///    
deba@1331:     /// Adds \c i to the heap with priority \c p. 
deba@1331:     /// \param i The item to insert.
deba@1331:     /// \param p The priority of the item.
deba@1186:     void push(const Item &i, const Prio &p) {
deba@1186:       int n = data.size();
deba@1186:       iim.set(i, n);
deba@1186:       data.push_back(RadixItem(i, p));
deba@1186:       while (lower(boxes.size() - 1, p)) {
deba@1186: 	extend();
deba@1186:       }
deba@1186:       int box = findDown(boxes.size() - 1, p);
deba@1186:       insert(box, n);
deba@1186:     }
deba@1186: 
deba@1331:     /// \brief Returns the item with minimum priority.
deba@1331:     ///
deba@1331:     /// This method returns the item with minimum priority.  
deba@1331:     /// \pre The heap must be nonempty.  
deba@1186:     Item top() const {
mqrelly@2263:       const_cast<RadixHeap<ItemIntMap>&>(*this).moveDown();
deba@1186:       return data[boxes[0].first].item;
deba@1186:     }
deba@1186: 
deba@1331:     /// \brief Returns the minimum priority.
deba@1331:     ///
deba@1331:     /// It returns the minimum priority.
deba@1331:     /// \pre The heap must be nonempty.
deba@1186:     Prio prio() const {
mqrelly@2263:       const_cast<RadixHeap<ItemIntMap>&>(*this).moveDown();
deba@1186:       return data[boxes[0].first].prio;
deba@1186:      }
deba@1186: 
deba@1331:     /// \brief Deletes the item with minimum priority.
deba@1331:     ///
deba@1331:     /// This method deletes the item with minimum priority.
deba@1331:     /// \pre The heap must be non-empty.  
deba@1186:     void pop() {
deba@1186:       moveDown();
deba@1186:       int index = boxes[0].first;
deba@1186:       iim[data[index].item] = POST_HEAP;
deba@1186:       remove(index);
deba@1186:       relocate_last(index);
deba@1186:     }
deba@1186: 
deba@1331:     /// \brief Deletes \c i from the heap.
deba@1331:     ///
deba@1331:     /// This method deletes item \c i from the heap, if \c i was
deba@1331:     /// already stored in the heap.
deba@1331:     /// \param i The item to erase. 
deba@1186:     void erase(const Item &i) {
deba@1186:       int index = iim[i];
deba@1186:       iim[i] = POST_HEAP;
deba@1186:       remove(index);
deba@1186:       relocate_last(index);
deba@1186:    }
deba@1186: 
deba@1331:     /// \brief Returns the priority of \c i.
deba@1331:     ///
deba@1331:     /// This function returns the priority of item \c i.  
deba@1331:     /// \pre \c i must be in the heap.
deba@1331:     /// \param i The item.
deba@1186:     Prio operator[](const Item &i) const {
deba@1186:       int idx = iim[i];
deba@1186:       return data[idx].prio;
deba@1186:     }
deba@1186: 
deba@1331:     /// \brief \c i gets to the heap with priority \c p independently 
deba@1331:     /// if \c i was already there.
deba@1331:     ///
deba@1331:     /// This method calls \ref push(\c i, \c p) if \c i is not stored
deba@1331:     /// in the heap and sets the priority of \c i to \c p otherwise.
deba@1331:     /// It may throw an \e UnderFlowPriorityException. 
deba@1331:     /// \param i The item.
deba@1331:     /// \param p The priority.
deba@1186:     void set(const Item &i, const Prio &p) {
deba@1186:       int idx = iim[i];
deba@1186:       if( idx < 0 ) {
deba@1186: 	push(i, p);
deba@1186:       }
deba@1186:       else if( p >= data[idx].prio ) {
deba@1186: 	data[idx].prio = p;
deba@1186: 	bubble_up(idx);
deba@1186:       } else {
deba@1186: 	data[idx].prio = p;
deba@1186: 	bubble_down(idx);
deba@1186:       }
deba@1186:     }
deba@1186: 
deba@1331: 
deba@1331:     /// \brief Decreases the priority of \c i to \c p.
deba@1331:     ///
deba@1331:     /// This method decreases the priority of item \c i to \c p.
deba@1331:     /// \pre \c i must be stored in the heap with priority at least \c p, and
deba@1758:     /// \c should be greater or equal to the last removed item's priority.
deba@1331:     /// \param i The item.
deba@1331:     /// \param p The priority.
deba@1186:     void decrease(const Item &i, const Prio &p) {
deba@1186:       int idx = iim[i];
deba@1186:       data[idx].prio = p;
deba@1186:       bubble_down(idx);
deba@1186:     }
deba@1186: 
deba@1331:     /// \brief Increases the priority of \c i to \c p.
deba@1331:     ///
deba@1331:     /// This method sets the priority of item \c i to \c p. 
deba@1758:     /// \pre \c i must be stored in the heap with priority at most \c p
deba@1331:     /// \param i The item.
deba@1331:     /// \param p The priority.
deba@1186:     void increase(const Item &i, const Prio &p) {
deba@1186:       int idx = iim[i];
deba@1186:       data[idx].prio = p;
deba@1186:       bubble_up(idx);
deba@1186:     }
deba@1186: 
deba@1331:     /// \brief Returns if \c item is in, has already been in, or has 
deba@1331:     /// never been in the heap.
deba@1331:     ///
deba@1331:     /// This method returns PRE_HEAP if \c item has never been in the
deba@1331:     /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
deba@1331:     /// otherwise. In the latter case it is possible that \c item will
deba@1331:     /// get back to the heap again.
deba@1331:     /// \param i The item.
deba@1186:     state_enum state(const Item &i) const {
deba@1186:       int s = iim[i];
deba@1186:       if( s >= 0 ) s = 0;
deba@1186:       return state_enum(s);
deba@1186:     }
deba@1186: 
deba@1902:     /// \brief Sets the state of the \c item in the heap.
deba@1902:     ///
deba@1902:     /// Sets the state of the \c item in the heap. It can be used to
deba@1902:     /// manually clear the heap when it is important to achive the
deba@1902:     /// better time complexity.
deba@1902:     /// \param i The item.
deba@1902:     /// \param st The state. It should not be \c IN_HEAP. 
deba@1902:     void state(const Item& i, state_enum st) {
deba@1902:       switch (st) {
deba@1902:       case POST_HEAP:
deba@1902:       case PRE_HEAP:
deba@1902:         if (state(i) == IN_HEAP) {
deba@1902:           erase(i);
deba@1902:         }
deba@1903:         iim[i] = st;
deba@1902:         break;
deba@1906:       case IN_HEAP:
deba@1906:         break;
deba@1902:       }
deba@1902:     }
deba@1902: 
deba@1186:   }; // class RadixHeap
deba@1186: 
deba@1186: } // namespace lemon
deba@1186: 
deba@1186: #endif // LEMON_RADIX_HEAP_H