Files Classes Functions Hierarchy
00001 #ifndef INDEXTABLE_H 00002 #define INDEXTABLE_H 00003 00004 #include <cassert> 00005 #include <vector> 00006 00007 using namespace std; 00008 00009 #include <typedefs.h> 00010 00023 template< class Compare, class T > 00024 void sortIndexTable 00025 ( 00026 vector<uint> & out, 00027 Compare c, 00028 vector<T> const & v 00029 ); 00030 00031 00054 template< class IterOut, class Compare, class IterIn > 00055 void sortIndexTable 00056 ( 00057 IterOut out, 00058 Compare c, 00059 IterIn first, 00060 IterIn last 00061 ); 00062 00066 template< typename T > 00067 void vectorshuffle 00068 ( 00069 vector<T> & v1, 00070 vector<uint> & index 00071 ); 00072 00073 //--------------------------------------------------------- 00074 // Implementation 00075 00076 00077 template< class T, class U, class Compare > 00078 struct comparepairfirst 00079 { 00080 bool operator() 00081 ( 00082 pair<T,U> const & a, 00083 pair<T,U> const & b 00084 ) const 00085 { return Compare()(*a.first,*b.first); } 00086 }; 00087 00088 template< class IterOut, class Compare, class IterIn > 00089 void sortIndexTable 00090 ( 00091 IterOut out, 00092 Compare c, 00093 IterIn first, 00094 IterIn last 00095 ) 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 } 00110 00111 00112 template< class Compare, class T > 00113 void sortIndexTable 00114 ( 00115 vector<uint> & out, 00116 Compare c, 00117 vector<T> const & v 00118 ) 00119 { 00120 out.resize(v.size()); 00121 sortIndexTable(out.begin(),c,v.begin(),v.end()); 00122 } 00123 00124 00125 template< typename T > 00126 void vectorshuffle 00127 ( 00128 vector<T> & v1, 00129 vector<uint> & index 00130 ) 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 } 00142 00143 00144 00145 00146 #endif
1.5.8