proj home

Files   Classes   Functions   Hierarchy  

indextable.h File Reference

#include <cassert>
#include <vector>
#include <typedefs.h>

Include dependency graph for indextable.h:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  comparepairfirst< T, U, Compare >

Functions

template<class Compare , class T >
void sortIndexTable (vector< uint > &out, Compare c, vector< T > const &v)
 Sort a vector without altering the original vector. An array of indexes are sorted.
template<class IterOut , class Compare , class IterIn >
void sortIndexTable (IterOut out, Compare c, IterIn first, IterIn last)
 Indexed containers.
template<typename T >
void vectorshuffle (vector< T > &v1, vector< uint > &index)
 Randomly shuffles vector v1 with the index preserved.


Function Documentation

template<class IterOut , class Compare , class IterIn >
void sortIndexTable ( IterOut  out,
Compare  c,
IterIn  first,
IterIn  last 
) [inline]

Indexed containers.

I modified the code from an online C++ coding review. Herb Sutter http://www.gotw.ca/gotw/073.htm

Changes: added a general Compare template parameter. Modified the variables order to my workspaces convention which has the output variables at the start of the function signature. Renamed the class variables and added an explicit vector interface which although in the example adds one more line of code I believe makes the function interface more clear. Chelton Evans 2007-02-06.

Example
  uintc aimax=10;
  int ai[10]={15,12,13,14,18,11,10,17,16,19};
  vector<int> aidxtbl(aimax);
  sortIndexTable(aidxtbl.begin(),less<int>(),ai,ai+aimax);

Definition at line 90 of file indextable.h.

00096 {
00097   vector<pair<IterIn,int> > s(last-first);
00098   uintc imax(s.size());
00099   for (uint i=0; i<imax; ++i)
00100     s[i]=make_pair(first+i,i);
00101   sort
00102   (
00103     s.begin(),
00104     s.end(),
00105     comparepairfirst<IterIn,int,Compare>()
00106   );
00107   for (uint i=0; i<imax; ++i,++out)
00108     *out=s[i].second;
00109 }

template<class Compare , class T >
void sortIndexTable ( vector< uint > &  out,
Compare  c,
vector< T > const &  v 
) [inline]

Sort a vector without altering the original vector. An array of indexes are sorted.

Example
  int ai[10]={15,12,13,14,18,11,10,17,16,19};
  uintc aimax=10;
  vector<int> ai2(ai,ai+aimax);  // data
  vector<uint> aidxtbl2; // index array
  sortIndexTable(aidxtbl2,greater<int>(),ai2);

Definition at line 114 of file indextable.h.

Referenced by indextabletest01().

00119 {
00120   out.resize(v.size());
00121   sortIndexTable(out.begin(),c,v.begin(),v.end());
00122 }

template<typename T >
void vectorshuffle ( vector< T > &  v1,
vector< uint > &  index 
) [inline]

Randomly shuffles vector v1 with the index preserved.

ie the original element at the i'th index can be found at v1[index[i]].

Definition at line 127 of file indextable.h.

Referenced by quickhull2Drandomized< PT, D >::quickhull2Drandomized().

00131 {
00132   uintc n(v1.size());
00133   index.resize(n);
00134   for (uint i=0; i<n; ++i)
00135     index[i]=i;
00136   random_shuffle(index.begin(),index.end());
00137 
00138   vector<T> v2(v1.begin(),v1.end());
00139   for (uint i=0; i<n; ++i)
00140     v1[i] = v2[ index[i] ];
00141 }


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