00001 #ifndef Impala_Core_Column_ApplyOrder_h
00002 #define Impala_Core_Column_ApplyOrder_h
00003
00004 #include "Util/ApplyOrder.h"
00005 #include "Core/Vector/VectorSet.h"
00006
00007 namespace Impala
00008 {
00009 namespace Core
00010 {
00011 namespace Column
00012 {
00013
00014
00017 template <class C>
00018 void
00019 ApplyOrder(C* data, ColumnTem<int>* order)
00020 {
00021 if(data->Size() == 0)
00022 {
00023
00024 return;
00025 }
00026 if(data->Size() < order->Size())
00027 {
00028 std::cout << "[Column::ApplyOrder] column to reorder shorter than the presented order" << std::endl;
00029 return;
00030 }
00031 Util::ApplyOrder(data->GetData(), order->GetData(), order->Size());
00032 }
00033
00034
00036 template <>
00037 void
00038 ApplyOrder(Vector::VectorSet<Array::Array2dScalarReal64>* data, ColumnTem<int>* orderCol)
00039 {
00040 typedef Vector::VectorTem<Array::Array2dScalarReal64::StorType> Type;
00041
00042 int length = orderCol->Size();
00043
00044 int* order = new int[length];
00045 memcpy(order, orderCol->GetData(), length*sizeof(int));
00046
00047 int i;
00048 for(i=0 ; i<length ; ++i)
00049 {
00050 if(order[i] != i)
00051 {
00052
00053 int indexFrom = i;
00054 int indexTo = order[i];
00055 order[i] = i;
00056 Type value = data->Get(indexFrom);
00057 while(indexFrom != indexTo)
00058 {
00059 Type temp = data->Get(indexTo);
00060 data->Set(indexTo, value);
00061 value = temp;
00062 indexFrom = indexTo;
00063 indexTo = order[indexFrom];
00064 order[indexFrom] = indexFrom;
00065 }
00066 }
00067 }
00068 }
00069
00070 }
00071 }
00072 }
00073
00074 #endif