proj home

Files   Classes   Functions   Hierarchy  

d2sortvec.h

Go to the documentation of this file.
00001 #ifndef D2SORTVEC_H
00002 #define D2SORTVEC_H
00003 
00004 #include <cassert>
00005 #include <algorithm>
00006 #include <cmath>
00007 using namespace std;
00008 
00009 #include <functionalobjectoperators.h>
00010 #include <typedefs.h>
00011 
00012 /*
00013 \brief Sort a 2D vector.
00014 
00015 */
00016 template< typename T, typename CX, typename CY >
00017 class d2sortvec
00018 {
00019   CX cmpx;
00020   CY cmpy;
00021 
00023   void setwidth2D();
00024     
00026   void setwidth3D();
00027 public:
00028 
00030   T * vi;
00032   uint N;
00033 
00035   uint width;
00038   uint parity;
00039 
00040   d2sortvec(T * _vi, uintc _N, uintc _parity=0);
00041 
00042   d2sortvec
00043   (
00044     T * _vi, 
00045     uintc _N, 
00046     CX const & _cmpx, 
00047     CY const & _cmpy, 
00048     uintc _parity=0
00049   );
00050 
00052   void eval();
00053 
00056   void eval(uintc n2);
00057 
00058 };
00059 
00060 template< typename T, typename CX, typename CY >
00061 void d2sortvecfunction
00062 (
00063   T* vi,
00064   uintc N,
00065   CX const & cx,
00066   CY const & cy
00067 )
00068 {
00069   
00070   d2sortvec<T,CX,CY> s(vi,N,cx,cy);
00071   //s.eval();
00072 }
00073     
00074   
00075 
00076 
00077 //----------------------------------------------------------
00078 //  Implementation
00079 
00080 
00081 
00082 template< typename T, typename CX, typename CY >
00083 d2sortvec<T,CX,CY>::d2sortvec
00084 (
00085   T * _vi, 
00086   uintc _N, 
00087   uintc _parity
00088 )
00089   : vi(_vi), N(_N), parity(_parity) 
00090 {
00091   setwidth2D();
00092 }
00093 
00094 template< typename T, typename CX, typename CY >
00095 d2sortvec<T,CX,CY>::d2sortvec
00096 (
00097   T * _vi, 
00098   uintc _N, 
00099   CX const & _cmpx, 
00100   CY const & _cmpy, 
00101   uintc _parity
00102 )
00103   : cmpx(_cmpx), cmpy(_cmpy), vi(_vi), N(_N), 
00104       parity(_parity) 
00105 {
00106   setwidth2D();
00107 }
00108 
00109 template< typename T, typename CX, typename CY >
00110 void d2sortvec<T,CX,CY>::setwidth2D()
00111 {
00112   width = (int)(sqrt(N)); 
00113 }
00114 
00115 template< typename T, typename CX, typename CY >
00116 void d2sortvec<T,CX,CY>::setwidth3D()
00117 {
00118   width = (int)pow(N,2.0/3.0);
00119 }
00120 
00121 
00122 template< typename T, typename CX, typename CY >
00123 void d2sortvec<T,CX,CY>::eval()
00124 {
00125   //assert(width!=0);
00126   assert(N!=0);
00127 
00128   sort(&vi[0],&vi[0]+N,cmpx);
00129 
00130   uint k=parity;
00131   for (uint i=0; i<N; i+=width, ++k)
00132   {
00133     if (i+width<=N)
00134     {
00135       if (k%2)
00136         sort(vi+i,vi+i+width,mynot2(cmpy));
00137       else
00138         sort(vi+i,vi+i+width,cmpy);
00139     }
00140     else
00141     {
00142       if (k%2)
00143         sort(vi+i,vi+N,mynot2(cmpy));
00144       else
00145         sort(vi+i,vi+N,cmpy);
00146     }
00147   }
00148 }
00149 
00150 
00151 
00152 
00153 
00154 #endif
00155 
00156 

Generated on Fri Mar 4 00:49:31 2011 for Chelton Evans Source by  doxygen 1.5.8