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

/ray/src/lib/tl/binsearch.h

Go to the documentation of this file.
00001 /*
00002  * lib/tl/binsearch.h 
00003  * 
00004  * Binary search templates. 
00005  * 
00006  * Copyright (c) 2004 by Wolfgang Wieser ] wwieser (a) gmx <*> de [ 
00007  * 
00008  * This file may be distributed and/or modified under the terms of the 
00009  * GNU General Public License version 2 as published by the Free Software 
00010  * Foundation. (See COPYING.GPL for details.)
00011  * 
00012  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00013  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00014  * 
00015  */
00016 
00017 #ifndef _TemplateLibrary_BINARYSEARCH_H_
00018 #define _TemplateLibrary_BINARYSEARCH_H_ 1
00019 
00026 #include <lib/sconfig.h>    /* MUST be first */
00027 
00028 #include <lib/tl/defop.h>
00029 
00030 
00046 template<typename T,typename K>ssize_t BinarySearch(const T *arr,size_t nelem,
00047     const K &key)
00048 {
00049     if(!nelem)  return(-1);
00050     size_t a=0,b=nelem-1;
00051     while(b-a>1)
00052     {  size_t m=(a+b)/2;  (arr[m]<key) ? a=m : b=m;  }
00053     if(arr[a]==key)  return(a);
00054     if(b!=a && arr[b]==key)  return(b);
00055     return(-1);
00056 }
00057 
00058 
00066 template<typename T,typename K,typename _OP>ssize_t TLBinarySearch(
00067     const T *_arr,size_t nelem,
00068     const K &key,
00069     const _OP &op=TLDefaultOperators_CDT<T>())
00070 {
00071     if(!nelem)  return(-1);
00072     size_t a=0,b=nelem-1;
00073     while(b-a>1)
00074     {  size_t m=(a+b)/2;  op.lt(op.idx(_arr,m),key) ? a=m : b=m;  }
00075     if(op.eq(op.idx(_arr,a),key))  return(a);
00076     if(b!=a && op.eq(op.idx(_arr,b),key))  return(b);
00077     return(-1);
00078 }
00079 
00080 
00105 template<typename T,typename K>bool BinarySearch(const T *arr,size_t nelem,
00106     const K &key,size_t *ret_idx)
00107 {
00108     if(nelem<=1) {  if(!nelem)  {  *ret_idx=0;  return(0);  }
00109                     if(key>arr[0])  {  *ret_idx=1;  return(0);  }
00110                     *ret_idx=0;  return(key==arr[0]);  }
00111     size_t a=0,b=--nelem;  // NOTE: nelem decreased by 1!
00112     while(b-a>1)  {  size_t m=(a+b)/2;  (arr[m]<key) ? a=m : b=m;  }
00113     if(!a && key<arr[0])  {  *ret_idx=0;  return(0);  }
00114     if(b==nelem && key>arr[nelem])  {  *ret_idx=nelem+1;  return(0);  }
00115     if(key==arr[a])  {  *ret_idx=a;  return(1);  }
00116     *ret_idx=b;  return(b && key==arr[b]);
00117 }
00118 
00119 
00127 template<typename T,typename K,typename _OP>bool TLBinarySearch(
00128     const T *_arr,size_t nelem,
00129     const K &key,size_t *ret_idx,
00130     const _OP &op=TLDefaultOperators_CDT<T>())
00131 {
00132     if(nelem<=1)
00133     {
00134         if(!nelem)  {  *ret_idx=0;  return(0);  }
00135         if(op.lt(op.idx(_arr,0),key))  { *ret_idx=1;  return(0);  }
00136         *ret_idx=0;  return(op.eq(op.idx(_arr,0),key));
00137     }
00138     size_t a=0,b=--nelem;  // NOTE: nelem decreased by 1!
00139     while(b-a>1)
00140     {  size_t m=(a+b)/2; op.lt(op.idx(_arr,m),key) ? a=m : b=m;  }
00141     if(!a && op.gt(op.idx(_arr,0),key))  {  *ret_idx=0;  return(0);  }
00142     if(b==nelem && op.lt(op.idx(_arr,nelem),key))
00143         { *ret_idx=nelem+1; return(0); }
00144     if(op.eq(op.idx(_arr,a),key))  {  *ret_idx=a;  return(1);  }
00145     *ret_idx=b;  return(b && op.eq(op.idx(_arr,b),key));
00146 }
00147 
00148 #endif  /* _TemplateLibrary_BINARYSEARCH_H_ */

Generated on Sat Feb 19 22:33:46 2005 for Ray by doxygen 1.3.5