Files Classes Functions Hierarchy
00001 #ifndef SNAKESORT_H 00002 #define SNAKESORT_H 00003 00004 #include <cmath> 00005 using namespace std; 00006 00007 #include <functionalobjectoperators.h> 00008 #include <typedefs.h> 00009 00010 00025 class snakesort 00026 { 00027 public: 00028 00030 template< typename T, typename CMP > 00031 static void d1 00032 ( 00033 T* vi, 00034 uintc N, 00035 uintc width, 00036 CMP compare, 00037 uintc parity=0 00038 ); 00039 00041 template< typename T, typename CMP > 00042 static void d2 00043 ( 00044 T* vi, 00045 uintc N, 00046 CMP compare 00047 ); 00048 00051 template< typename T, typename CMP > 00052 static void d3 00053 ( 00054 T* vi, 00055 uintc N, 00056 CMP compare 00057 ); 00058 00059 }; 00060 00061 00062 00063 //--------------------------------------------------------- 00064 // Implementation 00065 00066 00068 template< typename T, typename CMP > 00069 void snakesort::d1 00070 ( 00071 T* vi, 00072 uintc N, 00073 uintc width, 00074 CMP compare, 00075 uintc parity 00076 ) 00077 { 00078 uint k=parity; 00079 for (uint i=0; i<N; i+=width, ++k) 00080 { 00081 if (i+width<=N) 00082 { 00083 if (k%2) 00084 sort(vi+i,vi+i+width,mynot2(compare)); 00085 else 00086 sort(vi+i,vi+i+width,compare); 00087 } 00088 else 00089 { 00090 if (k%2) 00091 sort(vi+i,vi+N,mynot2(compare)); 00092 else 00093 sort(vi+i,vi+N,compare); 00094 } 00095 } 00096 } 00097 00098 template< typename T, typename CMP > 00099 void snakesort::d2 00100 ( 00101 T* vi, 00102 uintc N, 00103 CMP compare 00104 ) 00105 { 00106 uint width=(int)sqrt(N); 00107 sort(vi,vi+N,mycomparexobject<CMP>()); 00108 d1(vi,N,width,mycompareyobject<CMP>()); 00109 } 00110 00111 template< typename T, typename CMP > 00112 void snakesort::d3 00113 ( 00114 T* vi, 00115 uintc N, 00116 CMP compare 00117 ) 00118 { 00119 uint width=(int)pow(N,2.0/3.0); 00120 uint width2=(int)pow(N,1.0/3.0); 00121 sort(vi,vi+N,mycomparexobject<CMP>()); 00122 d1(vi,N,width,mycompareyobject<CMP>()); 00123 uint parity=0; 00124 mycomparezobject<CMP> compare2; 00125 for (uint i=0; i<N; i+=width) 00126 { 00127 if (i+width<N) 00128 d1(vi+i,width,width2,compare2,parity); 00129 else 00130 d1(vi+i,N-i,width2,compare2,parity); 00131 ++parity; 00132 } 00133 } 00134 00135 00136 #endif 00137 00138
1.5.8