00001
00008 #include <cmath>
00009
00010 namespace seq {
00011
00012 template <typename T>
00013 bool BinarySearch(const T* arr, unsigned int nelem, const T & key, unsigned int *ret_idx) {
00014 if(!nelem) {*ret_idx = nelem; return false;}
00015 unsigned int left = 0;
00016 unsigned int right = (nelem -1);
00017 while (left <= right) {
00018 unsigned int mid =
00019 (unsigned int)( left + (unsigned int)(floor((double)(right-left)/2.0)) );
00020
00021 if (key > arr[mid]) {
00022 left = mid+1;
00023 } else if (key < arr[mid]) {
00024 if(mid>0) { right = mid-1; }
00025 else { right = 0; break;}
00026 } else {
00027 *ret_idx = mid;
00028 return true;
00029 }
00030 }
00031 *ret_idx = nelem;
00032 return false;
00033 }
00034
00035 template <typename T>
00036 int UpperBound (unsigned int p,const T * splitt,unsigned int itr, const T & elem)
00037 {
00038 if (itr >= p) {
00039 return p;
00040 }
00041 while (itr < p){
00042 if (elem <= splitt[itr]) {
00043 return itr;
00044 } else {
00045 itr = itr + 1;
00046 }
00047 }
00048 return itr;
00049 }
00050
00051 template <typename T>
00052 bool maxLowerBound(const std::vector<T>& arr, const T & key, unsigned int &ret_idx,
00053 unsigned int* leftIdx, unsigned int* rightIdx ) {
00054 unsigned int nelem = static_cast<unsigned int>(arr.size());
00055 ret_idx = 0;
00056 if(!nelem) { return false;}
00057 if(arr[0] > key) { return false; }
00058 if(arr[nelem-1] < key) {
00059 ret_idx = (nelem-1);
00060 return true;
00061 }
00062
00063 unsigned int left = 0;
00064 unsigned int right = (nelem -1);
00065 unsigned int mid = 0;
00066 if(leftIdx) {
00067 left = (*leftIdx);
00068 }
00069 if(rightIdx) {
00070 right = (*rightIdx);
00071 }
00072 while (left <= right) {
00073 mid = (unsigned int)( left + (unsigned int)(floor((double)(right-left)/2.0)) );
00074 if (key > arr[mid]) {
00075 left = mid + (1u);
00076 } else if (key < arr[mid]){
00077 if(mid>0) {
00078 right = mid-1;
00079 }else {
00080 right=0;
00081 break;
00082 }
00083 } else {
00084 ret_idx = mid;
00085 return true;
00086 }
00087 }
00088
00089
00090
00091
00092 if( (arr[mid] > key) && (mid > 0) ){ mid--; }
00093 if(arr[mid] <= key ) { ret_idx = mid; return true; }
00094 else { ret_idx = 0; return false;}
00095 }
00096
00097 template<typename T> void makeVectorUnique(std::vector<T>& vecT, bool isSorted) {
00098 if(vecT.size() < 2) { return;}
00099 if(!isSorted) { sort(vecT.begin(),vecT.end()); }
00100 std::vector<T> tmp(vecT.size());
00101
00102 T* tmpPtr = (&(*(tmp.begin())));
00103 T* vecTptr = (&(*(vecT.begin())));
00104 tmpPtr[0] = vecTptr[0];
00105 unsigned int tmpSize=1;
00106 unsigned int vecTsz = static_cast<unsigned int>(vecT.size());
00107 for(unsigned int i = 1; i < vecTsz; i++) {
00108 if(tmpPtr[tmpSize-1] != vecTptr[i]) {
00109 tmpPtr[tmpSize] = vecTptr[i];
00110 tmpSize++;
00111 }
00112 }
00113 vecT.clear();
00114 vecT.insert(vecT.begin(), tmp.begin(), (tmp.begin() + tmpSize));
00115 tmp.clear();
00116 }
00117
00118 template <typename T>
00119 void flashsort(T* a, int n, int m, int *ctr)
00120 {
00121 const int THRESHOLD = 75;
00122 const int CLASS_SIZE = 75;
00123
00124
00125 int *l, nmin, nmax, i, j, k, nmove, nx, mx;
00126
00127 T c1,c2,flash,hold;
00128
00129
00130 l=(int*)calloc(m,sizeof(int));
00131
00132
00133
00134 nmin=nmax=0;
00135 for (i=0 ; i<n ; i++)
00136 if (a[i] < a[nmin]) nmin = i;
00137 else if (a[i] > a[nmax]) nmax = i;
00138
00139 if ( (a[nmax]==a[nmin]) && (ctr==0) )
00140 {
00141 printf("All the numbers are identical, the list is sorted\n");
00142 return;
00143 }
00144
00145 c1=(m-1.0)/(a[nmax]-a[nmin]) ;
00146 c2=a[nmin];
00147
00148 l[0]=-1;
00149 for (k=1; k<m ; k++) l[k]=0;
00150
00151 for (i=0; i<n ; i++)
00152 {
00153 k=floor(c1*(a[i]-c2) );
00154 l[k]+=1;
00155 }
00156
00157 for (k=1; k<m ; k++) l[k]+=l[k-1];
00158
00159 hold=a[nmax];
00160 a[nmax]=a[0];
00161 a[0]=hold;
00162
00163
00164 nmove=0;
00165 j=0;
00166 k=m-1;
00167
00168 while(nmove<n)
00169 {
00170 while (j > l[k] )
00171 {
00172 j++;
00173 k=floor(c1*(a[j]-c2) ) ;
00174 }
00175
00176 flash=a[ j ] ;
00177
00178 while ( j <= l[k] )
00179 {
00180 k=floor(c1*(flash-c2));
00181 hold=a[ l[k] ];
00182 a[ l[k] ] = flash;
00183 l[k]--;
00184 flash=hold;
00185 nmove++;
00186 }
00187 }
00188
00189
00190
00191 for (k=0;k<(m-1);k++)
00192 if ( (nx = l[k+1]-l[k]) > THRESHOLD )
00193 {
00194 flashsort(&a[l[k]+1],nx,CLASS_SIZE,ctr);
00195 (*ctr)++;
00196 }
00197
00198 else
00199 for (i=l[k+1]-1; i > l[k] ; i--)
00200 if (a[i] > a[i+1])
00201 {
00202 hold=a[i];
00203 j=i;
00204 while (hold > a[j+1] ) a[j++]=a[j+1] ;
00205 a[j]=hold;
00206 }
00207 free(l);
00208 }
00209
00210 }
00211
00212
00213