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

ApplyOrder.h

Go to the documentation of this file.
00001 #ifndef Impala_Util_ApplyOrder_h
00002 #define Impala_Util_ApplyOrder_h
00003 
00004 #include "memory.h"
00005 
00006 namespace Impala
00007 {
00008 namespace Util
00009 {
00010 
00011 
00016 template <class Type>
00017 void
00018 ApplyOrder(Type* data, const int* constOrder, int length)
00019 {
00020     // order is copied to keep callers code simpler
00021     int* order = new int[length];
00022     memcpy(order, constOrder, length*sizeof(int));
00023     
00024     int i;
00025     for(i=0 ; i<length ; ++i)
00026     {
00027         if(order[i] != i)
00028         {
00029             // keep swapping until an element is put at the correct position (this will happen!)
00030             int indexFrom = i;
00031             int indexTo = order[i];
00032             order[i] = i;
00033             Type value = data[indexFrom];
00034             while(indexFrom != indexTo)
00035             {
00036                 Type temp = data[indexTo];
00037                 data[indexTo] = value;
00038                 value = temp;
00039                 indexFrom = indexTo;
00040                 indexTo = order[indexFrom];
00041                 order[indexFrom] = indexFrom;
00042             }
00043         }
00044     }
00045 }
00046 
00047 } // namespace Util
00048 } // namespace Impala
00049 
00050 #endif

Generated on Fri Mar 19 09:31:03 2010 for ImpalaSrc by  doxygen 1.5.1