Home || Architecture || Video Search || Visual Search || Scripts || Applications || Important Messages || OGL || Src

Sort.h

Go to the documentation of this file.
00001 #ifndef Impala_Core_Table_Sort_h
00002 #define Impala_Core_Table_Sort_h
00003 
00004 #include "Core/Column/Sort.h"
00005 #include "Core/Column/ApplyOrder.h"
00006 #include "Core/Table/TableTem.h"
00007 #include "Util/QuickSort.h"
00008 
00009 namespace Impala
00010 {
00011 namespace Core
00012 {
00013 namespace Table
00014 {
00015 
00016 typedef Column::ColumnTem<Int32> ColumnInt32;
00017 
00018 // todo: general TableSort (first generate permutation, then permute all columns
00019 //inline void
00020 //SortAscending(TableIdsWithWeight* t)
00021 //{
00022 //    Column::SortAscending(t->GetColumn1(), t->GetColumn2());
00023 //}
00024 
00025 template<class Col1T, class Col2T, class Col3T, class Col4T, class Col5T,
00026          class Col6T, class Col7T, class Col8T, class Col9T>
00027 void
00028 Sort(TableTem<Col1T, Col2T, Col3T, Col4T, Col5T, Col6T, Col7T, Col8T, Col9T>* t, int sortColumn, bool ascending)
00029 {
00030     switch(sortColumn)
00031     {
00032         case 1: SortOnColumn1(t, ascending); break;
00033         case 2: SortOnColumn2(t, ascending); break;
00034         case 3: SortOnColumn3(t, ascending); break;
00035         case 4: SortOnColumn4(t, ascending); break;
00036         case 5: SortOnColumn5(t, ascending); break;
00037         case 6: SortOnColumn6(t, ascending); break;
00038         case 7: SortOnColumn7(t, ascending); break;
00039         case 8: SortOnColumn8(t, ascending); break;
00040         case 9: SortOnColumn9(t, ascending); break;
00041     }
00042 }
00043 
00044 template<class Col1T, class Col2T, class Col3T, class Col4T, class Col5T,
00045          class Col6T, class Col7T, class Col8T, class Col9T>
00046 void SortOnColumn1(TableTem<Col1T, Col2T, Col3T, Col4T, Col5T, Col6T, Col7T, Col8T, Col9T>* t, bool ascending)
00047 {
00048     int i;
00049     ColumnInt32 rank(t->Size());
00050     for(i=0 ; i<t->Size() ; i++)
00051         rank.Set(i, i);
00052 
00053     if(ascending)
00054         Util::QuickSortCo(t->GetColumn1()->GetData(), rank.GetData(), 0, t->Size()-1);
00055     else
00056         Util::QuickSortDescCo(t->GetColumn1()->GetData(), rank.GetData(), 0, t->Size()-1);
00057 
00058     /* compute order from the rank, so fill an array with numbers
00059        at the positions they are found in the array 'rank'.
00060        this is the 'inverse of the ordering' so to say... */
00061     ColumnInt32 order(t->Size());
00062     for(i=0 ; i<t->Size() ; i++)
00063         order.Set(rank.Get(i), i);
00064 
00065     ApplyOrder(t->GetColumn2(), &order);
00066     ApplyOrder(t->GetColumn3(), &order);
00067     ApplyOrder(t->GetColumn4(), &order);
00068     ApplyOrder(t->GetColumn5(), &order);
00069     ApplyOrder(t->GetColumn6(), &order);
00070     ApplyOrder(t->GetColumn7(), &order);
00071     ApplyOrder(t->GetColumn8(), &order);
00072     ApplyOrder(t->GetColumn9(), &order);
00073 }
00074 
00075 template<class Col1T, class Col2T, class Col3T, class Col4T, class Col5T,
00076          class Col6T, class Col7T, class Col8T, class Col9T>
00077 void SortOnColumn2(TableTem<Col1T, Col2T, Col3T, Col4T, Col5T, Col6T, Col7T, Col8T, Col9T>* t, bool ascending)
00078 {
00079     int i;
00080     ColumnInt32 rank(t->Size());
00081     for(i=0 ; i<t->Size() ; i++)
00082         rank.Set(i, i);
00083 
00084     if(ascending)
00085         Util::QuickSortCo(t->GetColumn2()->GetData(), rank.GetData(), 0, t->Size()-1);
00086     else
00087         Util::QuickSortDescCo(t->GetColumn2()->GetData(), rank.GetData(), 0, t->Size()-1);
00088 
00089     /* compute order from the rank, so fill an array with numbers
00090        at the positions they are found in the array 'rank'.
00091        this is the 'inverse of the ordering' so to say... */
00092     ColumnInt32 order(t->Size());
00093     for(i=0 ; i<t->Size() ; i++)
00094         order.Set(rank.Get(i), i);
00095 
00096     ApplyOrder(t->GetColumn1(), &order);
00097     ApplyOrder(t->GetColumn3(), &order);
00098     ApplyOrder(t->GetColumn4(), &order);
00099     ApplyOrder(t->GetColumn5(), &order);
00100     ApplyOrder(t->GetColumn6(), &order);
00101     ApplyOrder(t->GetColumn7(), &order);
00102     ApplyOrder(t->GetColumn8(), &order);
00103     ApplyOrder(t->GetColumn9(), &order);
00104 }
00105 
00106 template<class Col1T, class Col2T, class Col3T, class Col4T, class Col5T,
00107          class Col6T, class Col7T, class Col8T, class Col9T>
00108 void SortOnColumn3(TableTem<Col1T, Col2T, Col3T, Col4T, Col5T, Col6T, Col7T, Col8T, Col9T>* t, bool ascending)
00109 {
00110     int i;
00111     ColumnInt32 rank(t->Size());
00112     for(i=0 ; i<t->Size() ; i++)
00113         rank.Set(i, i);
00114 
00115     if(ascending)
00116         Util::QuickSortCo(t->GetColumn3()->GetData(), rank.GetData(), 0, t->Size()-1);
00117     else
00118         Util::QuickSortDescCo(t->GetColumn3()->GetData(), rank.GetData(), 0, t->Size()-1);
00119 
00120     /* compute order from the rank, so fill an array with numbers
00121        at the positions they are found in the array 'rank'.
00122        this is the 'inverse of the ordering' so to say... */
00123     ColumnInt32 order(t->Size());
00124     for(i=0 ; i<t->Size() ; i++)
00125         order.Set(rank.Get(i), i);
00126 
00127     ApplyOrder(t->GetColumn1(), &order);
00128     ApplyOrder(t->GetColumn2(), &order);
00129     ApplyOrder(t->GetColumn4(), &order);
00130     ApplyOrder(t->GetColumn5(), &order);
00131     ApplyOrder(t->GetColumn6(), &order);
00132     ApplyOrder(t->GetColumn7(), &order);
00133     ApplyOrder(t->GetColumn8(), &order);
00134     ApplyOrder(t->GetColumn9(), &order);
00135 }
00136 
00137 template<class Col1T, class Col2T, class Col3T, class Col4T, class Col5T,
00138          class Col6T, class Col7T, class Col8T, class Col9T>
00139 void SortOnColumn4(TableTem<Col1T, Col2T, Col3T, Col4T, Col5T, Col6T, Col7T, Col8T, Col9T>* t, bool ascending)
00140 {
00141     int i;
00142     ColumnInt32 rank(t->Size());
00143     for(i=0 ; i<t->Size() ; i++)
00144         rank.Set(i, i);
00145 
00146     if(ascending)
00147         Util::QuickSortCo(t->GetColumn4()->GetData(), rank.GetData(), 0, t->Size()-1);
00148     else
00149         Util::QuickSortDescCo(t->GetColumn4()->GetData(), rank.GetData(), 0, t->Size()-1);
00150 
00151     /* compute order from the rank, so fill an array with numbers
00152        at the positions they are found in the array 'rank'.
00153        this is the 'inverse of the ordering' so to say... */
00154     ColumnInt32 order(t->Size());
00155     for(i=0 ; i<t->Size() ; i++)
00156         order.Set(rank.Get(i), i);
00157 
00158     ApplyOrder(t->GetColumn1(), &order);
00159     ApplyOrder(t->GetColumn2(), &order);
00160     ApplyOrder(t->GetColumn3(), &order);
00161     ApplyOrder(t->GetColumn5(), &order);
00162     ApplyOrder(t->GetColumn6(), &order);
00163     ApplyOrder(t->GetColumn7(), &order);
00164     ApplyOrder(t->GetColumn8(), &order);
00165     ApplyOrder(t->GetColumn9(), &order);
00166 }
00167 
00168 template<class Col1T, class Col2T, class Col3T, class Col4T, class Col5T,
00169          class Col6T, class Col7T, class Col8T, class Col9T>
00170 void SortOnColumn5(TableTem<Col1T, Col2T, Col3T, Col4T, Col5T, Col6T, Col7T, Col8T, Col9T>* t, bool ascending)
00171 {
00172     int i;
00173     ColumnInt32 rank(t->Size());
00174     for(i=0 ; i<t->Size() ; i++)
00175         rank.Set(i, i);
00176 
00177     if(ascending)
00178         Util::QuickSortCo(t->GetColumn5()->GetData(), rank.GetData(), 0, t->Size()-1);
00179     else
00180         Util::QuickSortDescCo(t->GetColumn5()->GetData(), rank.GetData(), 0, t->Size()-1);
00181 
00182     /* compute order from the rank, so fill an array with numbers
00183        at the positions they are found in the array 'rank'.
00184        this is the 'inverse of the ordering' so to say... */
00185     ColumnInt32 order(t->Size());
00186     for(i=0 ; i<t->Size() ; i++)
00187         order.Set(rank.Get(i), i);
00188 
00189     ApplyOrder(t->GetColumn1(), &order);
00190     ApplyOrder(t->GetColumn2(), &order);
00191     ApplyOrder(t->GetColumn3(), &order);
00192     ApplyOrder(t->GetColumn4(), &order);
00193     ApplyOrder(t->GetColumn6(), &order);
00194     ApplyOrder(t->GetColumn7(), &order);
00195     ApplyOrder(t->GetColumn8(), &order);
00196     ApplyOrder(t->GetColumn9(), &order);
00197 }
00198 
00199 template<class Col1T, class Col2T, class Col3T, class Col4T, class Col5T,
00200          class Col6T, class Col7T, class Col8T, class Col9T>
00201 void SortOnColumn6(TableTem<Col1T, Col2T, Col3T, Col4T, Col5T, Col6T, Col7T, Col8T, Col9T>* t, bool ascending)
00202 {
00203     int i;
00204     ColumnInt32 rank(t->Size());
00205     for(i=0 ; i<t->Size() ; i++)
00206         rank.Set(i, i);
00207 
00208     if(ascending)
00209         Util::QuickSortCo(t->GetColumn6()->GetData(), rank.GetData(), 0, t->Size()-1);
00210     else
00211         Util::QuickSortDescCo(t->GetColumn6()->GetData(), rank.GetData(), 0, t->Size()-1);
00212 
00213     /* compute order from the rank, so fill an array with numbers
00214        at the positions they are found in the array 'rank'.
00215        this is the 'inverse of the ordering' so to say... */
00216     ColumnInt32 order(t->Size());
00217     for(i=0 ; i<t->Size() ; i++)
00218         order.Set(rank.Get(i), i);
00219 
00220     ApplyOrder(t->GetColumn1(), &order);
00221     ApplyOrder(t->GetColumn2(), &order);
00222     ApplyOrder(t->GetColumn3(), &order);
00223     ApplyOrder(t->GetColumn4(), &order);
00224     ApplyOrder(t->GetColumn5(), &order);
00225     ApplyOrder(t->GetColumn7(), &order);
00226     ApplyOrder(t->GetColumn8(), &order);
00227     ApplyOrder(t->GetColumn9(), &order);
00228 }
00229 
00230 template<class Col1T, class Col2T, class Col3T, class Col4T, class Col5T,
00231          class Col6T, class Col7T, class Col8T, class Col9T>
00232 void SortOnColumn7(TableTem<Col1T, Col2T, Col3T, Col4T, Col5T, Col6T, Col7T, Col8T, Col9T>* t, bool ascending)
00233 {
00234     int i;
00235     ColumnInt32 rank(t->Size());
00236     for(i=0 ; i<t->Size() ; i++)
00237         rank.Set(i, i);
00238 
00239     if(ascending)
00240         Util::QuickSortCo(t->GetColumn7()->GetData(), rank.GetData(), 0, t->Size()-1);
00241     else
00242         Util::QuickSortDescCo(t->GetColumn7()->GetData(), rank.GetData(), 0, t->Size()-1);
00243 
00244     /* compute order from the rank, so fill an array with numbers
00245        at the positions they are found in the array 'rank'.
00246        this is the 'inverse of the ordering' so to say... */
00247     ColumnInt32 order(t->Size());
00248     for(i=0 ; i<t->Size() ; i++)
00249         order.Set(rank.Get(i), i);
00250 
00251     ApplyOrder(t->GetColumn1(), &order);
00252     ApplyOrder(t->GetColumn2(), &order);
00253     ApplyOrder(t->GetColumn3(), &order);
00254     ApplyOrder(t->GetColumn4(), &order);
00255     ApplyOrder(t->GetColumn5(), &order);
00256     ApplyOrder(t->GetColumn6(), &order);
00257     ApplyOrder(t->GetColumn8(), &order);
00258     ApplyOrder(t->GetColumn9(), &order);
00259 }
00260 
00261 template<class Col1T, class Col2T, class Col3T, class Col4T, class Col5T,
00262          class Col6T, class Col7T, class Col8T, class Col9T>
00263 void SortOnColumn8(TableTem<Col1T, Col2T, Col3T, Col4T, Col5T, Col6T, Col7T, Col8T, Col9T>* t, bool ascending)
00264 {
00265     int i;
00266     ColumnInt32 rank(t->Size());
00267     for(i=0 ; i<t->Size() ; i++)
00268         rank.Set(i, i);
00269 
00270     if(ascending)
00271         Util::QuickSortCo(t->GetColumn8()->GetData(), rank.GetData(), 0, t->Size()-1);
00272     else
00273         Util::QuickSortDescCo(t->GetColumn8()->GetData(), rank.GetData(), 0, t->Size()-1);
00274 
00275     /* compute order from the rank, so fill an array with numbers
00276        at the positions they are found in the array 'rank'.
00277        this is the 'inverse of the ordering' so to say... */
00278     ColumnInt32 order(t->Size());
00279     for(i=0 ; i<t->Size() ; i++)
00280         order.Set(rank.Get(i), i);
00281 
00282     ApplyOrder(t->GetColumn1(), &order);
00283     ApplyOrder(t->GetColumn2(), &order);
00284     ApplyOrder(t->GetColumn3(), &order);
00285     ApplyOrder(t->GetColumn4(), &order);
00286     ApplyOrder(t->GetColumn5(), &order);
00287     ApplyOrder(t->GetColumn6(), &order);
00288     ApplyOrder(t->GetColumn7(), &order);
00289     ApplyOrder(t->GetColumn9(), &order);
00290 }
00291 
00292 template<class Col1T, class Col2T, class Col3T, class Col4T, class Col5T,
00293          class Col6T, class Col7T, class Col8T, class Col9T>
00294 void SortOnColumn9(TableTem<Col1T, Col2T, Col3T, Col4T, Col5T, Col6T, Col7T, Col8T, Col9T>* t, bool ascending)
00295 {
00296     int i;
00297     ColumnInt32 rank(t->Size());
00298     for(i=0 ; i<t->Size() ; i++)
00299         rank.Set(i, i);
00300 
00301     if(ascending)
00302         Util::QuickSortCo(t->GetColumn9()->GetData(), rank.GetData(), 0, t->Size()-1);
00303     else
00304         Util::QuickSortDescCo(t->GetColumn9()->GetData(), rank.GetData(), 0, t->Size()-1);
00305 
00306     /* compute order from the rank, so fill an array with numbers
00307        at the positions they are found in the array 'rank'.
00308        this is the 'inverse of the ordering' so to say... */
00309     ColumnInt32 order(t->Size());
00310     for(i=0 ; i<t->Size() ; i++)
00311         order.Set(rank.Get(i), i);
00312 
00313     ApplyOrder(t->GetColumn1(), &order);
00314     ApplyOrder(t->GetColumn2(), &order);
00315     ApplyOrder(t->GetColumn3(), &order);
00316     ApplyOrder(t->GetColumn4(), &order);
00317     ApplyOrder(t->GetColumn5(), &order);
00318     ApplyOrder(t->GetColumn6(), &order);
00319     ApplyOrder(t->GetColumn7(), &order);
00320     ApplyOrder(t->GetColumn8(), &order);
00321 }
00322 
00323 } // namespace Table
00324 } // namespace Core
00325 } // namespace Impala
00326 
00327 #endif

Generated on Fri Mar 19 09:30:54 2010 for ImpalaSrc by  doxygen 1.5.1