proj home

Files   Classes   Functions   Hierarchy  

buckethybrid< T, HFN, C > Class Template Reference

Hybrid bucket sort which in worst case sorts with the default sorter. When the chain reaches a maximum length all elements on the chain are inserted into the second default sorting container C. I expect the client to supply a O(logn) default sorter. More...

#include <buckethybrid.h>

Inheritance diagram for buckethybrid< T, HFN, C >:
Collaboration diagram for buckethybrid< T, HFN, C >:

List of all members.

Public Member Functions

 buckethybrid (T *data_, uintc datasz_, HFN fn_, uintc maxchainlength_)
 Pre allocates memory.
void eval ()
 Sort the data.
void copy (T *data2) const
 Extract the result by copying to another vector.

Public Attributes

defaultsorter
 STL's multiset<T> has log(n) insertion and can handle multiple data entry.
uintc maxchainlength
 The maximum number of elements inserted into a chain/list.

Protected Member Functions

void mergeuptoelement (T *&data2, typename C::const_iterator &i, T const &x) const
 Copies the default sorter to data2 upto x in value.
void move (bucketlink< T > *b)
 Move the bucket into the defaultsorter.


Detailed Description

template<typename T, typename HFN, typename C>
class buckethybrid< T, HFN, C >

Hybrid bucket sort which in worst case sorts with the default sorter. When the chain reaches a maximum length all elements on the chain are inserted into the second default sorting container C. I expect the client to supply a O(logn) default sorter.

Warning: It is important that I and C template parameters have the same sorting operator.

Interface for template HFN parameter

class I
{
  public:

  // Configures the number of buckets.
  uint bucketsize; 
  // Decide which bucket the data belongs.
  uintc index(T const &) const;  
  // Comparision for sorting
  bool const operator()(T const & a, T const & b);
};

Interface for template C parameter
 
C is assumed to be a STL container as C has the following references.

C::const_iterator i
*i to get the data
defaultsorter.begin();
defaultsorter.end()
defaultsorter.insert(x)
defaultsorter.empty()

Example
  vector<double> v(n);
  ... fill v.
  unitdouble f1(n*2);
  typedef buckethybrid<double,unitdouble, multiset<double> > bhd;
  bhd b(chainmax,&v[0],n,f1);
  b.eval();

  vector<double> v2(n);
  b.copy(&v2[0]);

Definition at line 70 of file buckethybrid.h.


Constructor & Destructor Documentation

template<typename T , typename HFN , typename C >
buckethybrid< T, HFN, C >::buckethybrid ( T data_,
uintc  datasz_,
HFN  fn_,
uintc  maxchainlength_ 
) [inline]

Pre allocates memory.

Pass in the data, data size, hash function and the maximum chain length before the data is inserted to the default container.

Definition at line 85 of file buckethybrid.h.

00090     : bucket<T,HFN>(data_,datasz_,fn_), maxchainlength(maxchainlength_)
00091     {}


Member Function Documentation

template<typename T , typename HFN , typename C >
void buckethybrid< T, HFN, C >::copy ( T data2  )  const [inline]

Extract the result by copying to another vector.

Merges the two data structures.

Reimplemented from bucket< T, HFN >.

Definition at line 151 of file buckethybrid.h.

References bucketlink< T >::data, buckethybrid< T, HFN, C >::defaultsorter, buckethybrid< T, HFN, C >::mergeuptoelement(), and bucketlink< T >::next.

00152 {
00153   if (defaultsorter.empty())
00154   {
00155     bucket<T,HFN>::copy(data2);
00156     return;
00157   }
00158 
00159   typename C::const_iterator pos = defaultsorter.begin();
00160   bucketlink<T> * cp;
00161 
00162 #ifdef DEBUG_BUCKETHYBRID
00163   // Test code
00164   cout << "Printing the bucket first" << endl;
00165   for (uint i=0; i < bucket<T,HFN>::fn.bucketsize; ++i)
00166   {
00167     cp = bucket<T,HFN>::vb[i];
00168     if (cp==0)
00169       continue;
00170 
00171     for (;cp!=0;)
00172     {
00173       cout << *(cp->data) <<  " ";
00174       cp = cp->next;
00175     }
00176     cout << endl;
00177   }
00178 
00179   cout << "Printing the default sorter" << endl;
00180   for ( ; pos!=defaultsorter.end(); ++pos )
00181     cout <<  *pos << " ";
00182   cout << endl;
00183   pos = defaultsorter.begin();
00184 #endif
00185 
00186   for (uint i=0; i < bucket<T,HFN>::fn.bucketsize; ++i)
00187   {
00188     cp = bucket<T,HFN>::vb[i];
00189     if (cp==0)
00190       continue;
00191 
00192     for (;cp!=0;)
00193     {
00194       mergeuptoelement(data2,pos,*(cp->data));
00195 
00196       *data2++ = *(cp->data);
00197       cp = cp->next;
00198     }
00199   }
00200 
00201   for ( ; pos!=defaultsorter.end(); ++pos )
00202     *data2++ = *pos;
00203 }

template<typename T , typename HFN , typename C >
void buckethybrid< T, HFN, C >::eval (  )  [inline]

Sort the data.

Reimplemented from bucket< T, HFN >.

Definition at line 207 of file buckethybrid.h.

References bucketlink< T >::data, bucketlink< T >::insertafterself(), buckethybrid< T, HFN, C >::maxchainlength, buckethybrid< T, HFN, C >::move(), and bucketlink< T >::next.

00208 {
00209   bucketlink<T> * cp;
00210   bucketlink<T> * b;
00211   uint k;
00212   // Insert data into the structure.
00213   for (uint i=0; i< bucket<T,HFN>::datasz; ++i )
00214   {
00215     b = & bucket<T,HFN>::link[i];
00216     b->data = & bucket<T,HFN>::data[i];
00217 
00218     k = bucket<T,HFN>::fn.index(*b->data); 
00219     assert( (k < bucket<T,HFN>::fn.bucketsize) );
00220     k = k % bucket<T,HFN>::fn.bucketsize;
00221 
00222 //cout << SHOW(i) << endl;
00223 
00224     cp = bucket<T,HFN>::vb[k];
00225     if (cp==0)
00226     {
00227       bucket<T,HFN>::vb[k] = b; 
00228       continue;
00229     }
00230 
00231     // If the insert is contant do it.
00232     if ( bucket<T,HFN>::fn(*(b->data),*(cp->data)) )
00233     {
00234       b->next = cp;
00235       bucket<T,HFN>::vb[k] = b;
00236     }
00237     else
00238     {
00239       //bucketlink<T>::insertafterselfcounter = 0;
00240       cp->insertafterself(b,bucket<T,HFN>::fn);
00241       if (bucketlink<T>::insertafterselfcounter > maxchainlength)
00242       {
00243         move(cp);
00244         bucket<T,HFN>::vb[k] = 0;
00245       }
00246     }
00247   }
00248 }

template<typename T , typename HFN , typename C >
void buckethybrid< T, HFN, C >::mergeuptoelement ( T *&  data2,
typename C::const_iterator &  i,
T const &  x 
) const [inline, protected]

Copies the default sorter to data2 upto x in value.

Definition at line 132 of file buckethybrid.h.

Referenced by buckethybrid< T, HFN, C >::copy().

00137 {
00138   for ( ;i!=defaultsorter.end() ; )
00139   {
00140     if (fn(*i,x))
00141     {
00142       *data2++ = *i;
00143       ++i;
00144     }
00145     else
00146       return;
00147   }
00148 }

template<typename T , typename HFN , typename C >
void buckethybrid< T, HFN, C >::move ( bucketlink< T > *  b  )  [inline, protected]

Move the bucket into the defaultsorter.

Definition at line 118 of file buckethybrid.h.

References bucketlink< T >::data, buckethybrid< T, HFN, C >::defaultsorter, and bucketlink< T >::next.

Referenced by buckethybrid< T, HFN, C >::eval().

00119 {
00120   for ( ; b!=0; )
00121   {
00122     assert(b!=0);
00123     defaultsorter.insert( * (b->data) );
00124     b = b->next;
00125   }
00126 //cout << SHOW(defaultsorter.size()) << endl;
00127 }


Member Data Documentation

template<typename T , typename HFN , typename C >
C buckethybrid< T, HFN, C >::defaultsorter

STL's multiset<T> has log(n) insertion and can handle multiple data entry.

Definition at line 76 of file buckethybrid.h.

Referenced by buckethybrid< T, HFN, C >::copy(), and buckethybrid< T, HFN, C >::move().

template<typename T , typename HFN , typename C >
uintc buckethybrid< T, HFN, C >::maxchainlength

The maximum number of elements inserted into a chain/list.

Keep small.

Definition at line 79 of file buckethybrid.h.

Referenced by buckethybrid< T, HFN, C >::eval().


The documentation for this class was generated from the following file:

Generated on Fri Mar 4 00:49:51 2011 for Chelton Evans Source by  doxygen 1.5.8