[Lemon-commits] Peter Kovacs: Make K a template parameter in Kar...

Lemon HG hg at lemon.cs.elte.hu
Mon Aug 31 12:21:04 CEST 2009


details:   http://lemon.cs.elte.hu/hg/lemon/rev/7124b2581f72
changeset: 757:7124b2581f72
user:      Peter Kovacs <kpeter [at] inf.elte.hu>
date:      Fri Jul 10 09:15:22 2009 +0200
description:
	Make K a template parameter in KaryHeap (#301)

diffstat:

 lemon/kary_heap.h |  22 +++++++++++++---------
 1 files changed, 13 insertions(+), 9 deletions(-)

diffs (74 lines):

diff --git a/lemon/kary_heap.h b/lemon/kary_heap.h
--- a/lemon/kary_heap.h
+++ b/lemon/kary_heap.h
@@ -45,15 +45,20 @@
   /// \tparam PR Type of the priorities of the items.
   /// \tparam IM A read-writable item map with \c int values, used
   /// internally to handle the cross references.
+  /// \tparam K The degree of the heap, each node have at most \e K
+  /// children. The default is 16. Powers of two are suggested to use
+  /// so that the multiplications and divisions needed to traverse the
+  /// nodes of the heap could be performed faster.
   /// \tparam CMP A functor class for comparing the priorities.
   /// The default is \c std::less<PR>.
   ///
   ///\sa BinHeap
   ///\sa FouraryHeap
 #ifdef DOXYGEN
-  template <typename PR, typename IM, typename CMP>
+  template <typename PR, typename IM, int K, typename CMP>
 #else
-  template <typename PR, typename IM, typename CMP = std::less<PR> >
+  template <typename PR, typename IM, int K = 16,
+            typename CMP = std::less<PR> >
 #endif
   class KaryHeap {
   public:
@@ -86,7 +91,6 @@
     std::vector<Pair> _data;
     Compare _comp;
     ItemIntMap &_iim;
-    int _K;
 
   public:
     /// \brief Constructor.
@@ -95,7 +99,7 @@
     /// \param map A map that assigns \c int values to the items.
     /// It is used internally to handle the cross references.
     /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
-    explicit KaryHeap(ItemIntMap &map, int K=32) : _iim(map), _K(K) {}
+    explicit KaryHeap(ItemIntMap &map) : _iim(map) {}
 
     /// \brief Constructor.
     ///
@@ -104,8 +108,8 @@
     /// It is used internally to handle the cross references.
     /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
     /// \param comp The function object used for comparing the priorities.
-    KaryHeap(ItemIntMap &map, const Compare &comp, int K=32)
-      : _iim(map), _comp(comp), _K(K) {}
+    KaryHeap(ItemIntMap &map, const Compare &comp)
+      : _iim(map), _comp(comp) {}
 
     /// \brief The number of items stored in the heap.
     ///
@@ -127,8 +131,8 @@
     void clear() { _data.clear(); }
 
   private:
-    int parent(int i) { return (i-1)/_K; }
-    int firstChild(int i) { return _K*i+1; }
+    int parent(int i) { return (i-1)/K; }
+    int firstChild(int i) { return K*i+1; }
 
     bool less(const Pair &p1, const Pair &p2) const {
       return _comp(p1.second, p2.second);
@@ -136,7 +140,7 @@
 
     int findMin(const int child, const int length) {
       int min=child, i=1;
-      while( i<_K && child+i<length ) {
+      while( i<K && child+i<length ) {
         if( less(_data[child+i], _data[min]) )
           min=child+i;
         ++i;



More information about the Lemon-commits mailing list