# HG changeset patch # User Balazs Dezso # Date 1244754804 -7200 # Node ID 9f529abcaebf13f19e61ba24fdd2c3631860af91 # Parent bb8c4cd57900e4b3052fd3f5b20263cf4da343de Unification of names in heaps (#50) diff -r bb8c4cd57900 -r 9f529abcaebf lemon/bin_heap.h --- a/lemon/bin_heap.h Thu Jun 11 22:16:11 2009 +0200 +++ b/lemon/bin_heap.h Thu Jun 11 23:13:24 2009 +0200 @@ -33,23 +33,23 @@ /// ///\brief A Binary Heap implementation. /// - ///This class implements the \e binary \e heap data structure. - /// + ///This class implements the \e binary \e heap data structure. + /// ///A \e heap is a data structure for storing items with specified values ///called \e priorities in such a way that finding the item with minimum - ///priority is efficient. \c Comp specifies the ordering of the priorities. + ///priority is efficient. \c CMP specifies the ordering of the priorities. ///In a heap one can change the priority of an item, add or erase an ///item, etc. /// ///\tparam PR Type of the priority of the items. ///\tparam IM A read and writable item map with int values, used internally ///to handle the cross references. - ///\tparam Comp A functor class for the ordering of the priorities. + ///\tparam CMP A functor class for the ordering of the priorities. ///The default is \c std::less. /// ///\sa FibHeap ///\sa Dijkstra - template > + template > class BinHeap { public: @@ -62,7 +62,7 @@ ///\e typedef std::pair Pair; ///\e - typedef Comp Compare; + typedef CMP Compare; /// \brief Type to represent the items states. /// diff -r bb8c4cd57900 -r 9f529abcaebf lemon/bucket_heap.h --- a/lemon/bucket_heap.h Thu Jun 11 22:16:11 2009 +0200 +++ b/lemon/bucket_heap.h Thu Jun 11 23:13:24 2009 +0200 @@ -31,7 +31,7 @@ namespace _bucket_heap_bits { - template + template struct DirectionTraits { static bool less(int left, int right) { return left < right; @@ -65,26 +65,27 @@ /// \f$ [0..C) \f$ range a list of items. So it should be used only when /// the priorities are small. It is not intended to use as dijkstra heap. /// - /// \param _ItemIntMap A read and writable Item int map, used internally + /// \param IM A read and write Item int map, used internally /// to handle the cross references. - /// \param minimize If the given parameter is true then the heap gives back - /// the lowest priority. - template + /// \param MIN If the given parameter is false then instead of the + /// minimum value the maximum can be retrivied with the top() and + /// prio() member functions. + template class BucketHeap { public: /// \e - typedef typename _ItemIntMap::Key Item; + typedef typename IM::Key Item; /// \e typedef int Prio; /// \e typedef std::pair Pair; /// \e - typedef _ItemIntMap ItemIntMap; + typedef IM ItemIntMap; private: - typedef _bucket_heap_bits::DirectionTraits Direction; + typedef _bucket_heap_bits::DirectionTraits Direction; public: @@ -94,32 +95,32 @@ /// "pre heap" or "post heap". The latter two are indifferent from the /// heap's point of view, but may be useful to the user. /// - /// The ItemIntMap \e should be initialized in such way that it maps - /// PRE_HEAP (-1) to any element to be put in the heap... + /// The item-int map must be initialized in such way that it assigns + /// \c PRE_HEAP (-1) to any element to be put in the heap. enum State { - IN_HEAP = 0, - PRE_HEAP = -1, - POST_HEAP = -2 + IN_HEAP = 0, ///< = 0. + PRE_HEAP = -1, ///< = -1. + POST_HEAP = -2 ///< = -2. }; public: /// \brief The constructor. /// /// The constructor. - /// \param _index should be given to the constructor, since it is used + /// \param map should be given to the constructor, since it is used /// internally to handle the cross references. The value of the map /// should be PRE_HEAP (-1) for each element. - explicit BucketHeap(ItemIntMap &_index) : index(_index), minimal(0) {} + explicit BucketHeap(ItemIntMap &map) : _iim(map), _minimum(0) {} /// The number of items stored in the heap. /// /// \brief Returns the number of items stored in the heap. - int size() const { return data.size(); } + int size() const { return _data.size(); } /// \brief Checks if the heap stores no items. /// /// Returns \c true if and only if the heap stores no items. - bool empty() const { return data.empty(); } + bool empty() const { return _data.empty(); } /// \brief Make empty this heap. /// @@ -128,48 +129,48 @@ /// should first clear the heap and after that you should set the /// cross reference map for each item to \c PRE_HEAP. void clear() { - data.clear(); first.clear(); minimal = 0; + _data.clear(); _first.clear(); _minimum = 0; } private: void relocate_last(int idx) { - if (idx + 1 < int(data.size())) { - data[idx] = data.back(); - if (data[idx].prev != -1) { - data[data[idx].prev].next = idx; + if (idx + 1 < int(_data.size())) { + _data[idx] = _data.back(); + if (_data[idx].prev != -1) { + _data[_data[idx].prev].next = idx; } else { - first[data[idx].value] = idx; + _first[_data[idx].value] = idx; } - if (data[idx].next != -1) { - data[data[idx].next].prev = idx; + if (_data[idx].next != -1) { + _data[_data[idx].next].prev = idx; } - index[data[idx].item] = idx; + _iim[_data[idx].item] = idx; } - data.pop_back(); + _data.pop_back(); } void unlace(int idx) { - if (data[idx].prev != -1) { - data[data[idx].prev].next = data[idx].next; + if (_data[idx].prev != -1) { + _data[_data[idx].prev].next = _data[idx].next; } else { - first[data[idx].value] = data[idx].next; + _first[_data[idx].value] = _data[idx].next; } - if (data[idx].next != -1) { - data[data[idx].next].prev = data[idx].prev; + if (_data[idx].next != -1) { + _data[_data[idx].next].prev = _data[idx].prev; } } void lace(int idx) { - if (int(first.size()) <= data[idx].value) { - first.resize(data[idx].value + 1, -1); + if (int(_first.size()) <= _data[idx].value) { + _first.resize(_data[idx].value + 1, -1); } - data[idx].next = first[data[idx].value]; - if (data[idx].next != -1) { - data[data[idx].next].prev = idx; + _data[idx].next = _first[_data[idx].value]; + if (_data[idx].next != -1) { + _data[_data[idx].next].prev = idx; } - first[data[idx].value] = idx; - data[idx].prev = -1; + _first[_data[idx].value] = idx; + _data[idx].prev = -1; } public: @@ -187,12 +188,12 @@ /// \param i The item to insert. /// \param p The priority of the item. void push(const Item &i, const Prio &p) { - int idx = data.size(); - index[i] = idx; - data.push_back(BucketItem(i, p)); + int idx = _data.size(); + _iim[i] = idx; + _data.push_back(BucketItem(i, p)); lace(idx); - if (Direction::less(p, minimal)) { - minimal = p; + if (Direction::less(p, _minimum)) { + _minimum = p; } } @@ -201,10 +202,10 @@ /// This method returns the item with minimum priority. /// \pre The heap must be nonempty. Item top() const { - while (first[minimal] == -1) { - Direction::increase(minimal); + while (_first[_minimum] == -1) { + Direction::increase(_minimum); } - return data[first[minimal]].item; + return _data[_first[_minimum]].item; } /// \brief Returns the minimum priority. @@ -212,10 +213,10 @@ /// It returns the minimum priority. /// \pre The heap must be nonempty. Prio prio() const { - while (first[minimal] == -1) { - Direction::increase(minimal); + while (_first[_minimum] == -1) { + Direction::increase(_minimum); } - return minimal; + return _minimum; } /// \brief Deletes the item with minimum priority. @@ -223,11 +224,11 @@ /// This method deletes the item with minimum priority from the heap. /// \pre The heap must be non-empty. void pop() { - while (first[minimal] == -1) { - Direction::increase(minimal); + while (_first[_minimum] == -1) { + Direction::increase(_minimum); } - int idx = first[minimal]; - index[data[idx].item] = -2; + int idx = _first[_minimum]; + _iim[_data[idx].item] = -2; unlace(idx); relocate_last(idx); } @@ -238,8 +239,8 @@ /// already stored in the heap. /// \param i The item to erase. void erase(const Item &i) { - int idx = index[i]; - index[data[idx].item] = -2; + int idx = _iim[i]; + _iim[_data[idx].item] = -2; unlace(idx); relocate_last(idx); } @@ -251,8 +252,8 @@ /// \pre \c i must be in the heap. /// \param i The item. Prio operator[](const Item &i) const { - int idx = index[i]; - return data[idx].value; + int idx = _iim[i]; + return _data[idx].value; } /// \brief \c i gets to the heap with priority \c p independently @@ -263,10 +264,10 @@ /// \param i The item. /// \param p The priority. void set(const Item &i, const Prio &p) { - int idx = index[i]; + int idx = _iim[i]; if (idx < 0) { push(i, p); - } else if (Direction::less(p, data[idx].value)) { + } else if (Direction::less(p, _data[idx].value)) { decrease(i, p); } else { increase(i, p); @@ -281,11 +282,11 @@ /// \param i The item. /// \param p The priority. void decrease(const Item &i, const Prio &p) { - int idx = index[i]; + int idx = _iim[i]; unlace(idx); - data[idx].value = p; - if (Direction::less(p, minimal)) { - minimal = p; + _data[idx].value = p; + if (Direction::less(p, _minimum)) { + _minimum = p; } lace(idx); } @@ -298,9 +299,9 @@ /// \param i The item. /// \param p The priority. void increase(const Item &i, const Prio &p) { - int idx = index[i]; + int idx = _iim[i]; unlace(idx); - data[idx].value = p; + _data[idx].value = p; lace(idx); } @@ -313,7 +314,7 @@ /// get back to the heap again. /// \param i The item. State state(const Item &i) const { - int idx = index[i]; + int idx = _iim[i]; if (idx >= 0) idx = 0; return State(idx); } @@ -332,7 +333,7 @@ if (state(i) == IN_HEAP) { erase(i); } - index[i] = st; + _iim[i] = st; break; case IN_HEAP: break; @@ -351,10 +352,10 @@ int prev, next; }; - ItemIntMap& index; - std::vector first; - std::vector data; - mutable int minimal; + ItemIntMap& _iim; + std::vector _first; + std::vector _data; + mutable int _minimum; }; // class BucketHeap @@ -370,24 +371,25 @@ /// other way it does not support erasing each elements just the /// minimal and it does not supports key increasing, decreasing. /// - /// \param _ItemIntMap A read and writable Item int map, used internally + /// \param IM A read and write Item int map, used internally /// to handle the cross references. - /// \param minimize If the given parameter is true then the heap gives back - /// the lowest priority. + /// \param MIN If the given parameter is false then instead of the + /// minimum value the maximum can be retrivied with the top() and + /// prio() member functions. /// /// \sa BucketHeap - template + template class SimpleBucketHeap { public: - typedef typename _ItemIntMap::Key Item; + typedef typename IM::Key Item; typedef int Prio; typedef std::pair Pair; - typedef _ItemIntMap ItemIntMap; + typedef IM ItemIntMap; private: - typedef _bucket_heap_bits::DirectionTraits Direction; + typedef _bucket_heap_bits::DirectionTraits Direction; public: @@ -397,12 +399,12 @@ /// "pre heap" or "post heap". The latter two are indifferent from the /// heap's point of view, but may be useful to the user. /// - /// The ItemIntMap \e should be initialized in such way that it maps - /// PRE_HEAP (-1) to any element to be put in the heap... + /// The item-int map must be initialized in such way that it assigns + /// \c PRE_HEAP (-1) to any element to be put in the heap. enum State { - IN_HEAP = 0, - PRE_HEAP = -1, - POST_HEAP = -2 + IN_HEAP = 0, ///< = 0. + PRE_HEAP = -1, ///< = -1. + POST_HEAP = -2 ///< = -2. }; public: @@ -410,21 +412,21 @@ /// \brief The constructor. /// /// The constructor. - /// \param _index should be given to the constructor, since it is used + /// \param map should be given to the constructor, since it is used /// internally to handle the cross references. The value of the map /// should be PRE_HEAP (-1) for each element. - explicit SimpleBucketHeap(ItemIntMap &_index) - : index(_index), free(-1), num(0), minimal(0) {} + explicit SimpleBucketHeap(ItemIntMap &map) + : _iim(map), _free(-1), _num(0), _minimum(0) {} /// \brief Returns the number of items stored in the heap. /// /// The number of items stored in the heap. - int size() const { return num; } + int size() const { return _num; } /// \brief Checks if the heap stores no items. /// /// Returns \c true if and only if the heap stores no items. - bool empty() const { return num == 0; } + bool empty() const { return _num == 0; } /// \brief Make empty this heap. /// @@ -433,7 +435,7 @@ /// should first clear the heap and after that you should set the /// cross reference map for each item to \c PRE_HEAP. void clear() { - data.clear(); first.clear(); free = -1; num = 0; minimal = 0; + _data.clear(); _first.clear(); _free = -1; _num = 0; _minimum = 0; } /// \brief Insert a pair of item and priority into the heap. @@ -451,22 +453,22 @@ /// \param p The priority of the item. void push(const Item &i, const Prio &p) { int idx; - if (free == -1) { - idx = data.size(); - data.push_back(BucketItem(i)); + if (_free == -1) { + idx = _data.size(); + _data.push_back(BucketItem(i)); } else { - idx = free; - free = data[idx].next; - data[idx].item = i; + idx = _free; + _free = _data[idx].next; + _data[idx].item = i; } - index[i] = idx; - if (p >= int(first.size())) first.resize(p + 1, -1); - data[idx].next = first[p]; - first[p] = idx; - if (Direction::less(p, minimal)) { - minimal = p; + _iim[i] = idx; + if (p >= int(_first.size())) _first.resize(p + 1, -1); + _data[idx].next = _first[p]; + _first[p] = idx; + if (Direction::less(p, _minimum)) { + _minimum = p; } - ++num; + ++_num; } /// \brief Returns the item with minimum priority. @@ -474,10 +476,10 @@ /// This method returns the item with minimum priority. /// \pre The heap must be nonempty. Item top() const { - while (first[minimal] == -1) { - Direction::increase(minimal); + while (_first[_minimum] == -1) { + Direction::increase(_minimum); } - return data[first[minimal]].item; + return _data[_first[_minimum]].item; } /// \brief Returns the minimum priority. @@ -485,10 +487,10 @@ /// It returns the minimum priority. /// \pre The heap must be nonempty. Prio prio() const { - while (first[minimal] == -1) { - Direction::increase(minimal); + while (_first[_minimum] == -1) { + Direction::increase(_minimum); } - return minimal; + return _minimum; } /// \brief Deletes the item with minimum priority. @@ -496,15 +498,15 @@ /// This method deletes the item with minimum priority from the heap. /// \pre The heap must be non-empty. void pop() { - while (first[minimal] == -1) { - Direction::increase(minimal); + while (_first[_minimum] == -1) { + Direction::increase(_minimum); } - int idx = first[minimal]; - index[data[idx].item] = -2; - first[minimal] = data[idx].next; - data[idx].next = free; - free = idx; - --num; + int idx = _first[_minimum]; + _iim[_data[idx].item] = -2; + _first[_minimum] = _data[idx].next; + _data[idx].next = _free; + _free = idx; + --_num; } /// \brief Returns the priority of \c i. @@ -516,13 +518,13 @@ /// \pre \c i must be in the heap. /// \param i The item. Prio operator[](const Item &i) const { - for (int k = 0; k < first.size(); ++k) { - int idx = first[k]; + for (int k = 0; k < _first.size(); ++k) { + int idx = _first[k]; while (idx != -1) { - if (data[idx].item == i) { + if (_data[idx].item == i) { return k; } - idx = data[idx].next; + idx = _data[idx].next; } } return -1; @@ -537,7 +539,7 @@ /// get back to the heap again. /// \param i The item. State state(const Item &i) const { - int idx = index[i]; + int idx = _iim[i]; if (idx >= 0) idx = 0; return State(idx); } @@ -552,11 +554,11 @@ int next; }; - ItemIntMap& index; - std::vector first; - std::vector data; - int free, num; - mutable int minimal; + ItemIntMap& _iim; + std::vector _first; + std::vector _data; + int _free, _num; + mutable int _minimum; }; // class SimpleBucketHeap diff -r bb8c4cd57900 -r 9f529abcaebf lemon/fib_heap.h --- a/lemon/fib_heap.h Thu Jun 11 22:16:11 2009 +0200 +++ b/lemon/fib_heap.h Thu Jun 11 23:13:24 2009 +0200 @@ -36,87 +36,88 @@ ///This class implements the \e Fibonacci \e heap data structure. A \e heap ///is a data structure for storing items with specified values called \e ///priorities in such a way that finding the item with minimum priority is - ///efficient. \c Compare specifies the ordering of the priorities. In a heap + ///efficient. \c CMP specifies the ordering of the priorities. In a heap ///one can change the priority of an item, add or erase an item, etc. /// ///The methods \ref increase and \ref erase are not efficient in a Fibonacci ///heap. In case of many calls to these operations, it is better to use a ///\ref BinHeap "binary heap". /// - ///\param _Prio Type of the priority of the items. - ///\param _ItemIntMap A read and writable Item int map, used internally + ///\param PRIO Type of the priority of the items. + ///\param IM A read and writable Item int map, used internally ///to handle the cross references. - ///\param _Compare A class for the ordering of the priorities. The - ///default is \c std::less<_Prio>. + ///\param CMP A class for the ordering of the priorities. The + ///default is \c std::less. /// ///\sa BinHeap ///\sa Dijkstra #ifdef DOXYGEN - template + template #else - template > + template > #endif class FibHeap { public: ///\e - typedef _ItemIntMap ItemIntMap; + typedef IM ItemIntMap; ///\e - typedef _Prio Prio; + typedef PRIO Prio; ///\e typedef typename ItemIntMap::Key Item; ///\e typedef std::pair Pair; ///\e - typedef _Compare Compare; + typedef CMP Compare; private: - class store; + class Store; - std::vector container; - int minimum; - ItemIntMap &iimap; - Compare comp; - int num_items; + std::vector _data; + int _minimum; + ItemIntMap &_iim; + Compare _comp; + int _num; public: - ///Status of the nodes + + /// \brief Type to represent the items states. + /// + /// Each Item element have a state associated to it. It may be "in heap", + /// "pre heap" or "post heap". The latter two are indifferent from the + /// heap's point of view, but may be useful to the user. + /// + /// The item-int map must be initialized in such way that it assigns + /// \c PRE_HEAP (-1) to any element to be put in the heap. enum State { - ///The node is in the heap - IN_HEAP = 0, - ///The node has never been in the heap - PRE_HEAP = -1, - ///The node was in the heap but it got out of it - POST_HEAP = -2 + IN_HEAP = 0, ///< = 0. + PRE_HEAP = -1, ///< = -1. + POST_HEAP = -2 ///< = -2. }; /// \brief The constructor /// - /// \c _iimap should be given to the constructor, since it is + /// \c map should be given to the constructor, since it is /// used internally to handle the cross references. - explicit FibHeap(ItemIntMap &_iimap) - : minimum(0), iimap(_iimap), num_items() {} + explicit FibHeap(ItemIntMap &map) + : _minimum(0), _iim(map), _num() {} /// \brief The constructor /// - /// \c _iimap should be given to the constructor, since it is used - /// internally to handle the cross references. \c _comp is an + /// \c map should be given to the constructor, since it is used + /// internally to handle the cross references. \c comp is an /// object for ordering of the priorities. - FibHeap(ItemIntMap &_iimap, const Compare &_comp) - : minimum(0), iimap(_iimap), comp(_comp), num_items() {} + FibHeap(ItemIntMap &map, const Compare &comp) + : _minimum(0), _iim(map), _comp(comp), _num() {} /// \brief The number of items stored in the heap. /// /// Returns the number of items stored in the heap. - int size() const { return num_items; } + int size() const { return _num; } /// \brief Checks if the heap stores no items. /// /// Returns \c true if and only if the heap stores no items. - bool empty() const { return num_items==0; } + bool empty() const { return _num==0; } /// \brief Make empty this heap. /// @@ -125,7 +126,7 @@ /// should first clear the heap and after that you should set the /// cross reference map for each item to \c PRE_HEAP. void clear() { - container.clear(); minimum = 0; num_items = 0; + _data.clear(); _minimum = 0; _num = 0; } /// \brief \c item gets to the heap with priority \c value independently @@ -135,10 +136,10 @@ /// stored in the heap and it calls \ref decrease(\c item, \c value) or /// \ref increase(\c item, \c value) otherwise. void set (const Item& item, const Prio& value) { - int i=iimap[item]; - if ( i >= 0 && container[i].in ) { - if ( comp(value, container[i].prio) ) decrease(item, value); - if ( comp(container[i].prio, value) ) increase(item, value); + int i=_iim[item]; + if ( i >= 0 && _data[i].in ) { + if ( _comp(value, _data[i].prio) ) decrease(item, value); + if ( _comp(_data[i].prio, value) ) increase(item, value); } else push(item, value); } @@ -147,33 +148,33 @@ /// Adds \c item to the heap with priority \c value. /// \pre \c item must not be stored in the heap. void push (const Item& item, const Prio& value) { - int i=iimap[item]; + int i=_iim[item]; if ( i < 0 ) { - int s=container.size(); - iimap.set( item, s ); - store st; + int s=_data.size(); + _iim.set( item, s ); + Store st; st.name=item; - container.push_back(st); + _data.push_back(st); i=s; } else { - container[i].parent=container[i].child=-1; - container[i].degree=0; - container[i].in=true; - container[i].marked=false; + _data[i].parent=_data[i].child=-1; + _data[i].degree=0; + _data[i].in=true; + _data[i].marked=false; } - if ( num_items ) { - container[container[minimum].right_neighbor].left_neighbor=i; - container[i].right_neighbor=container[minimum].right_neighbor; - container[minimum].right_neighbor=i; - container[i].left_neighbor=minimum; - if ( comp( value, container[minimum].prio) ) minimum=i; + if ( _num ) { + _data[_data[_minimum].right_neighbor].left_neighbor=i; + _data[i].right_neighbor=_data[_minimum].right_neighbor; + _data[_minimum].right_neighbor=i; + _data[i].left_neighbor=_minimum; + if ( _comp( value, _data[_minimum].prio) ) _minimum=i; } else { - container[i].right_neighbor=container[i].left_neighbor=i; - minimum=i; + _data[i].right_neighbor=_data[i].left_neighbor=i; + _minimum=i; } - container[i].prio=value; - ++num_items; + _data[i].prio=value; + ++_num; } /// \brief Returns the item with minimum priority relative to \c Compare. @@ -181,20 +182,20 @@ /// This method returns the item with minimum priority relative to \c /// Compare. /// \pre The heap must be nonempty. - Item top() const { return container[minimum].name; } + Item top() const { return _data[_minimum].name; } /// \brief Returns the minimum priority relative to \c Compare. /// /// It returns the minimum priority relative to \c Compare. /// \pre The heap must be nonempty. - const Prio& prio() const { return container[minimum].prio; } + const Prio& prio() const { return _data[_minimum].prio; } /// \brief Returns the priority of \c item. /// /// It returns the priority of \c item. /// \pre \c item must be in the heap. const Prio& operator[](const Item& item) const { - return container[iimap[item]].prio; + return _data[_iim[item]].prio; } /// \brief Deletes the item with minimum priority relative to \c Compare. @@ -204,33 +205,33 @@ /// \pre The heap must be non-empty. void pop() { /*The first case is that there are only one root.*/ - if ( container[minimum].left_neighbor==minimum ) { - container[minimum].in=false; - if ( container[minimum].degree!=0 ) { - makeroot(container[minimum].child); - minimum=container[minimum].child; + if ( _data[_minimum].left_neighbor==_minimum ) { + _data[_minimum].in=false; + if ( _data[_minimum].degree!=0 ) { + makeroot(_data[_minimum].child); + _minimum=_data[_minimum].child; balance(); } } else { - int right=container[minimum].right_neighbor; - unlace(minimum); - container[minimum].in=false; - if ( container[minimum].degree > 0 ) { - int left=container[minimum].left_neighbor; - int child=container[minimum].child; - int last_child=container[child].left_neighbor; + int right=_data[_minimum].right_neighbor; + unlace(_minimum); + _data[_minimum].in=false; + if ( _data[_minimum].degree > 0 ) { + int left=_data[_minimum].left_neighbor; + int child=_data[_minimum].child; + int last_child=_data[child].left_neighbor; makeroot(child); - container[left].right_neighbor=child; - container[child].left_neighbor=left; - container[right].left_neighbor=last_child; - container[last_child].right_neighbor=right; + _data[left].right_neighbor=child; + _data[child].left_neighbor=left; + _data[right].left_neighbor=last_child; + _data[last_child].right_neighbor=right; } - minimum=right; + _minimum=right; balance(); } // the case where there are more roots - --num_items; + --_num; } /// \brief Deletes \c item from the heap. @@ -238,15 +239,15 @@ /// This method deletes \c item from the heap, if \c item was already /// stored in the heap. It is quite inefficient in Fibonacci heaps. void erase (const Item& item) { - int i=iimap[item]; + int i=_iim[item]; - if ( i >= 0 && container[i].in ) { - if ( container[i].parent!=-1 ) { - int p=container[i].parent; + if ( i >= 0 && _data[i].in ) { + if ( _data[i].parent!=-1 ) { + int p=_data[i].parent; cut(i,p); cascade(p); } - minimum=i; //As if its prio would be -infinity + _minimum=i; //As if its prio would be -infinity pop(); } } @@ -257,15 +258,15 @@ /// \pre \c item must be stored in the heap with priority at least \c /// value relative to \c Compare. void decrease (Item item, const Prio& value) { - int i=iimap[item]; - container[i].prio=value; - int p=container[i].parent; + int i=_iim[item]; + _data[i].prio=value; + int p=_data[i].parent; - if ( p!=-1 && comp(value, container[p].prio) ) { + if ( p!=-1 && _comp(value, _data[p].prio) ) { cut(i,p); cascade(p); } - if ( comp(value, container[minimum].prio) ) minimum=i; + if ( _comp(value, _data[_minimum].prio) ) _minimum=i; } /// \brief Increases the priority of \c item to \c value. @@ -289,9 +290,9 @@ /// otherwise. In the latter case it is possible that \c item will /// get back to the heap again. State state(const Item &item) const { - int i=iimap[item]; + int i=_iim[item]; if( i>=0 ) { - if ( container[i].in ) i=0; + if ( _data[i].in ) i=0; else i=-2; } return State(i); @@ -301,7 +302,7 @@ /// /// Sets the state of the \c item in the heap. It can be used to /// manually clear the heap when it is important to achive the - /// better time complexity. + /// better time _complexity. /// \param i The item. /// \param st The state. It should not be \c IN_HEAP. void state(const Item& i, State st) { @@ -311,7 +312,7 @@ if (state(i) == IN_HEAP) { erase(i); } - iimap[i] = st; + _iim[i] = st; break; case IN_HEAP: break; @@ -322,7 +323,7 @@ void balance() { - int maxdeg=int( std::floor( 2.08*log(double(container.size()))))+1; + int maxdeg=int( std::floor( 2.08*log(double(_data.size()))))+1; std::vector A(maxdeg,-1); @@ -330,18 +331,18 @@ *Recall that now minimum does not point to the minimum prio element. *We set minimum to this during balance(). */ - int anchor=container[minimum].left_neighbor; - int next=minimum; + int anchor=_data[_minimum].left_neighbor; + int next=_minimum; bool end=false; do { int active=next; if ( anchor==active ) end=true; - int d=container[active].degree; - next=container[active].right_neighbor; + int d=_data[active].degree; + next=_data[active].right_neighbor; while (A[d]!=-1) { - if( comp(container[active].prio, container[A[d]].prio) ) { + if( _comp(_data[active].prio, _data[A[d]].prio) ) { fuse(active,A[d]); } else { fuse(A[d],active); @@ -354,21 +355,21 @@ } while ( !end ); - while ( container[minimum].parent >=0 ) - minimum=container[minimum].parent; - int s=minimum; - int m=minimum; + while ( _data[_minimum].parent >=0 ) + _minimum=_data[_minimum].parent; + int s=_minimum; + int m=_minimum; do { - if ( comp(container[s].prio, container[minimum].prio) ) minimum=s; - s=container[s].right_neighbor; + if ( _comp(_data[s].prio, _data[_minimum].prio) ) _minimum=s; + s=_data[s].right_neighbor; } while ( s != m ); } void makeroot(int c) { int s=c; do { - container[s].parent=-1; - s=container[s].right_neighbor; + _data[s].parent=-1; + s=_data[s].right_neighbor; } while ( s != c ); } @@ -376,32 +377,32 @@ /* *Replacing a from the children of b. */ - --container[b].degree; + --_data[b].degree; - if ( container[b].degree !=0 ) { - int child=container[b].child; + if ( _data[b].degree !=0 ) { + int child=_data[b].child; if ( child==a ) - container[b].child=container[child].right_neighbor; + _data[b].child=_data[child].right_neighbor; unlace(a); } /*Lacing a to the roots.*/ - int right=container[minimum].right_neighbor; - container[minimum].right_neighbor=a; - container[a].left_neighbor=minimum; - container[a].right_neighbor=right; - container[right].left_neighbor=a; + int right=_data[_minimum].right_neighbor; + _data[_minimum].right_neighbor=a; + _data[a].left_neighbor=_minimum; + _data[a].right_neighbor=right; + _data[right].left_neighbor=a; - container[a].parent=-1; - container[a].marked=false; + _data[a].parent=-1; + _data[a].marked=false; } void cascade(int a) { - if ( container[a].parent!=-1 ) { - int p=container[a].parent; + if ( _data[a].parent!=-1 ) { + int p=_data[a].parent; - if ( container[a].marked==false ) container[a].marked=true; + if ( _data[a].marked==false ) _data[a].marked=true; else { cut(a,p); cascade(p); @@ -413,38 +414,38 @@ unlace(b); /*Lacing b under a.*/ - container[b].parent=a; + _data[b].parent=a; - if (container[a].degree==0) { - container[b].left_neighbor=b; - container[b].right_neighbor=b; - container[a].child=b; + if (_data[a].degree==0) { + _data[b].left_neighbor=b; + _data[b].right_neighbor=b; + _data[a].child=b; } else { - int child=container[a].child; - int last_child=container[child].left_neighbor; - container[child].left_neighbor=b; - container[b].right_neighbor=child; - container[last_child].right_neighbor=b; - container[b].left_neighbor=last_child; + int child=_data[a].child; + int last_child=_data[child].left_neighbor; + _data[child].left_neighbor=b; + _data[b].right_neighbor=child; + _data[last_child].right_neighbor=b; + _data[b].left_neighbor=last_child; } - ++container[a].degree; + ++_data[a].degree; - container[b].marked=false; + _data[b].marked=false; } /* *It is invoked only if a has siblings. */ void unlace(int a) { - int leftn=container[a].left_neighbor; - int rightn=container[a].right_neighbor; - container[leftn].right_neighbor=rightn; - container[rightn].left_neighbor=leftn; + int leftn=_data[a].left_neighbor; + int rightn=_data[a].right_neighbor; + _data[leftn].right_neighbor=rightn; + _data[rightn].left_neighbor=leftn; } - class store { + class Store { friend class FibHeap; Item name; @@ -457,7 +458,7 @@ bool in; Prio prio; - store() : parent(-1), child(-1), degree(), marked(false), in(true) {} + Store() : parent(-1), child(-1), degree(), marked(false), in(true) {} }; }; diff -r bb8c4cd57900 -r 9f529abcaebf lemon/radix_heap.h --- a/lemon/radix_heap.h Thu Jun 11 22:16:11 2009 +0200 +++ b/lemon/radix_heap.h Thu Jun 11 23:13:24 2009 +0200 @@ -41,18 +41,18 @@ /// item, but the priority cannot be decreased under the last removed /// item's priority. /// - /// \param _ItemIntMap A read and writable Item int map, used internally + /// \param IM A read and writable Item int map, used internally /// to handle the cross references. /// /// \see BinHeap /// \see Dijkstra - template + template class RadixHeap { public: - typedef typename _ItemIntMap::Key Item; + typedef typename IM::Key Item; typedef int Prio; - typedef _ItemIntMap ItemIntMap; + typedef IM ItemIntMap; /// \brief Exception thrown by RadixHeap. /// @@ -99,7 +99,7 @@ std::vector data; std::vector boxes; - ItemIntMap &iim; + ItemIntMap &_iim; public: @@ -107,14 +107,14 @@ /// /// The constructor. /// - /// \param _iim It should be given to the constructor, since it is used + /// \param map It should be given to the constructor, since it is used /// internally to handle the cross references. The value of the map /// should be PRE_HEAP (-1) for each element. /// /// \param minimal The initial minimal value of the heap. /// \param capacity It determines the initial capacity of the heap. - RadixHeap(ItemIntMap &_iim, int minimal = 0, int capacity = 0) - : iim(_iim) { + RadixHeap(ItemIntMap &map, int minimal = 0, int capacity = 0) + : _iim(map) { boxes.push_back(RadixBox(minimal, 1)); boxes.push_back(RadixBox(minimal + 1, 1)); while (lower(boxes.size() - 1, capacity + minimal - 1)) { @@ -268,7 +268,7 @@ if (data[index].next != -1) { data[data[index].next].prev = index; } - iim[data[index].item] = index; + _iim[data[index].item] = index; } data.pop_back(); } @@ -282,7 +282,7 @@ /// \param p The priority of the item. void push(const Item &i, const Prio &p) { int n = data.size(); - iim.set(i, n); + _iim.set(i, n); data.push_back(RadixItem(i, p)); while (lower(boxes.size() - 1, p)) { extend(); @@ -316,7 +316,7 @@ void pop() { moveDown(); int index = boxes[0].first; - iim[data[index].item] = POST_HEAP; + _iim[data[index].item] = POST_HEAP; remove(index); relocate_last(index); } @@ -327,8 +327,8 @@ /// already stored in the heap. /// \param i The item to erase. void erase(const Item &i) { - int index = iim[i]; - iim[i] = POST_HEAP; + int index = _iim[i]; + _iim[i] = POST_HEAP; remove(index); relocate_last(index); } @@ -339,7 +339,7 @@ /// \pre \c i must be in the heap. /// \param i The item. Prio operator[](const Item &i) const { - int idx = iim[i]; + int idx = _iim[i]; return data[idx].prio; } @@ -352,7 +352,7 @@ /// \param i The item. /// \param p The priority. void set(const Item &i, const Prio &p) { - int idx = iim[i]; + int idx = _iim[i]; if( idx < 0 ) { push(i, p); } @@ -374,7 +374,7 @@ /// \param i The item. /// \param p The priority. void decrease(const Item &i, const Prio &p) { - int idx = iim[i]; + int idx = _iim[i]; data[idx].prio = p; bubble_down(idx); } @@ -386,7 +386,7 @@ /// \param i The item. /// \param p The priority. void increase(const Item &i, const Prio &p) { - int idx = iim[i]; + int idx = _iim[i]; data[idx].prio = p; bubble_up(idx); } @@ -400,7 +400,7 @@ /// get back to the heap again. /// \param i The item. State state(const Item &i) const { - int s = iim[i]; + int s = _iim[i]; if( s >= 0 ) s = 0; return State(s); } @@ -419,7 +419,7 @@ if (state(i) == IN_HEAP) { erase(i); } - iim[i] = st; + _iim[i] = st; break; case IN_HEAP: break;