Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

seqUtils.txx

Go to the documentation of this file.
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         }//end if-else-if
00030       }//end while
00031       *ret_idx = nelem; 
00032       return false;
00033     }//end function
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       }//end while
00048       return itr;
00049     }//end function
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       }//end if 
00062       //binary search
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         }//end if-else-if
00087       }//end while
00088 
00089       //If binary search did not find an exact match, it would have
00090       //stopped one element after or one element before. 
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     }//end function
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     //Using the array [] is faster than the vector version
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     }//end for
00113     vecT.clear();
00114     vecT.insert(vecT.begin(), tmp.begin(), (tmp.begin() + tmpSize));
00115     tmp.clear();
00116   }//end function
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;     /* minimum value for m */
00123 
00124       /* declare variables */
00125       int *l, nmin, nmax, i, j, k, nmove, nx, mx;
00126 
00127       T c1,c2,flash,hold;
00128 
00129       /* allocate space for the l vector */
00130       l=(int*)calloc(m,sizeof(int));
00131 
00132       /***** CLASS FORMATION ****/
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; /* since the base of the "a" (data) array is 0 */
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         /**** PERMUTATION *****/
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         /**** Choice of RECURSION or STRAIGHT INSERTION *****/
00190 
00191         for (k=0;k<(m-1);k++)
00192           if ( (nx = l[k+1]-l[k]) > THRESHOLD )  /* then use recursion */
00193           {
00194             flashsort(&a[l[k]+1],nx,CLASS_SIZE,ctr);
00195             (*ctr)++;
00196           }
00197 
00198           else  /* use insertion sort */
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);   /* need to free the memory we grabbed for the l vector */
00208     }
00209 
00210 }//end namespace
00211 
00212 
00213  

Generated on Tue Mar 24 16:14:04 2009 for DENDRO by  doxygen 1.3.9.1