[Lemon-commits] Balazs Dezso: Unification of names in heaps (#50)
Lemon HG
hg at lemon.cs.elte.hu
Thu Jun 25 14:55:59 CEST 2009
details: http://lemon.cs.elte.hu/hg/lemon/rev/9f529abcaebf
changeset: 731:9f529abcaebf
user: Balazs Dezso <deba [at] inf.elte.hu>
date: Thu Jun 11 23:13:24 2009 +0200
description:
Unification of names in heaps (#50)
diffstat:
lemon/bin_heap.h | 12 +-
lemon/bucket_heap.h | 256 +++++++++++++++++++++---------------------
lemon/fib_heap.h | 285 +++++++++++++++++++++++-----------------------
lemon/radix_heap.h | 38 +++---
4 files changed, 297 insertions(+), 294 deletions(-)
diffs (truncated from 1252 to 300 lines):
diff --git a/lemon/bin_heap.h b/lemon/bin_heap.h
--- a/lemon/bin_heap.h
+++ b/lemon/bin_heap.h
@@ -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<PR>.
///
///\sa FibHeap
///\sa Dijkstra
- template <typename PR, typename IM, typename Comp = std::less<PR> >
+ template <typename PR, typename IM, typename CMP = std::less<PR> >
class BinHeap {
public:
@@ -62,7 +62,7 @@
///\e
typedef std::pair<Item,Prio> Pair;
///\e
- typedef Comp Compare;
+ typedef CMP Compare;
/// \brief Type to represent the items states.
///
diff --git a/lemon/bucket_heap.h b/lemon/bucket_heap.h
--- a/lemon/bucket_heap.h
+++ b/lemon/bucket_heap.h
@@ -31,7 +31,7 @@
namespace _bucket_heap_bits {
- template <bool minimize>
+ template <bool MIN>
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 <typename _ItemIntMap, bool minimize = true>
+ /// \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 <typename IM, bool MIN = true>
class BucketHeap {
public:
/// \e
- typedef typename _ItemIntMap::Key Item;
+ typedef typename IM::Key Item;
/// \e
typedef int Prio;
/// \e
typedef std::pair<Item, Prio> Pair;
/// \e
- typedef _ItemIntMap ItemIntMap;
+ typedef IM ItemIntMap;
private:
- typedef _bucket_heap_bits::DirectionTraits<minimize> Direction;
+ typedef _bucket_heap_bits::DirectionTraits<MIN> 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 (<tt>-1</tt>) 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.
More information about the Lemon-commits
mailing list