00001 #ifndef Impala_Core_Column_Sort_h
00002 #define Impala_Core_Column_Sort_h
00003
00004 #include <iostream>
00005 #include "Core/Column/ColumnTem.h"
00006 #include "Util/QuickSort.h"
00007
00008 namespace Impala
00009 {
00010 namespace Core
00011 {
00012 namespace Column
00013 {
00014
00015
00019 template <class C1, class C2>
00020 inline void
00021 SortAscending(C1* first, C2* second, int nrElem)
00022 {
00023 ILOG_VAR(Impala.Core.Column.SortAscending);
00024 if (first->Capacity() == 0 || second->Capacity() == 0)
00025 {
00026
00027 return;
00028 }
00029 if ((nrElem > first->Capacity()) || (nrElem > second->Capacity()))
00030 {
00031 ILOG_ERROR("Incompatible sizes");
00032 return;
00033 }
00034
00035
00036
00037 Util::QuickSortCo(second->GetData(), first->GetData(), 0, nrElem - 1);
00038 }
00039
00044 template <class C1, class C2>
00045 inline void
00046 SortAscendingTopN(C1* first, C2* second, C1* result, C2* resultSecond)
00047 {
00048 ILOG_VAR(Impala.Core.Column.SortAscendingTopN);
00049 if (first->Capacity() != second->Capacity())
00050 {
00051 ILOG_ERROR("Incompatible sizes");
00052 return;
00053 }
00054 if (result->Capacity() != resultSecond->Capacity())
00055 {
00056 ILOG_ERROR("Incompatible sizes");
00057 return;
00058 }
00059
00060 int nrElem = first->Capacity();
00061 int topN = result->Capacity();
00062 result->Set(0, first->Get(0));
00063 resultSecond->Set(0, second->Get(0));
00064 int curNr = 1;
00065 for (int i=1 ; i<nrElem ; i++)
00066 {
00067 int j=0;
00068 while ((j<curNr) && (second->Get(i) > resultSecond->Get(j)))
00069 {
00070 j++;
00071 }
00072 if (j == curNr)
00073 {
00074 if (curNr < topN)
00075 {
00076 result->Set(curNr, first->Get(i));
00077 resultSecond->Set(curNr, second->Get(i));
00078 curNr++;
00079 }
00080 }
00081 else
00082 {
00083 if (curNr < topN)
00084 {
00085 curNr++;
00086 }
00087 for (int k=curNr-1 ; k>j ; k--)
00088 {
00089 result->Set(k, result->Get(k-1));
00090 resultSecond->Set(k, resultSecond->Get(k-1));
00091 }
00092 result->Set(j, first->Get(i));
00093 resultSecond->Set(j, second->Get(i));
00094 }
00095 }
00096 }
00097
00101 template <class C1, class C2>
00102 inline void
00103 SortDescending(C1* first, C2* second, int nrElem)
00104 {
00105 ILOG_VAR(Impala.Core.Column.SortDescending);
00106 if ((nrElem > first->Capacity()) || (nrElem > second->Capacity()))
00107 {
00108 ILOG_ERROR("Incompatible sizes");
00109 return;
00110 }
00111
00112
00113
00114 Util::QuickSortDescCo(second->GetData(), first->GetData(), 0, nrElem - 1);
00115 }
00116
00121 template <class C1, class C2>
00122 inline void
00123 SortDescendingTopN(C1* first, C2* second, C1* result, C2* resultSecond)
00124 {
00125 ILOG_VAR(Impala.Core.Column.SortDescendingTopN);
00126 if (first->Capacity() != second->Capacity())
00127 {
00128 ILOG_ERROR("Incompatible sizes");
00129 return;
00130 }
00131 if (result->Capacity() != resultSecond->Capacity())
00132 {
00133 ILOG_ERROR("Incompatible sizes");
00134 return;
00135 }
00136
00137 int nrElem = first->Capacity();
00138 int topN = result->Capacity();
00139 result->Set(0, first->Get(0));
00140 resultSecond->Set(0, second->Get(0));
00141 int curNr = 1;
00142 for (int i=1 ; i<nrElem ; i++)
00143 {
00144 int j=0;
00145 while ((j<curNr) && (second->Get(i) < resultSecond->Get(j)))
00146 {
00147 j++;
00148 }
00149 if (j == curNr)
00150 {
00151 if (curNr < topN)
00152 {
00153 result->Set(curNr, first->Get(i));
00154 resultSecond->Set(curNr, second->Get(i));
00155 curNr++;
00156 }
00157 }
00158 else
00159 {
00160 if (curNr < topN)
00161 {
00162 curNr++;
00163 }
00164 for (int k=curNr-1 ; k>j ; k--)
00165 {
00166 result->Set(k, result->Get(k-1));
00167 resultSecond->Set(k, resultSecond->Get(k-1));
00168 }
00169 result->Set(j, first->Get(i));
00170 resultSecond->Set(j, second->Get(i));
00171 }
00172 }
00173 }
00174
00175 }
00176 }
00177 }
00178
00179 #endif