00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef GUMA_SORTING_H
00021 #define GUMA_SORTING_H
00022
00023 namespace guma {
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 typedef struct bbt_node
00036 {
00037 void *key;
00038
00039 struct
00040 {
00041 int b : 2;
00042 int l : 2;
00043 }F;
00044
00045 struct bbt_node *left;
00046 struct bbt_node *right;
00047 struct bbt_node *parent;
00048 }
00049 BBTNode;
00050
00051
00052
00053
00054 GULAPI void InitBBT( BBTNode *head );
00055
00056
00057
00058
00059
00060
00061
00062 GULAPI int SearchElementInBBT( BBTNode *head, void *key,
00063 int compare( void *, void * ),
00064 BBTNode **element );
00065
00066
00067
00068 GULAPI BBTNode *SearchSuccessorInBBT( BBTNode *element, BBTNode *head );
00069
00070
00071
00072
00073 GULAPI BBTNode *SearchPredecessorInBBT( BBTNode *element, BBTNode *head );
00074
00075
00076
00077
00078
00079
00080 GULAPI void InsertElementIntoBBT( BBTNode *element,
00081 BBTNode *where, int side, BBTNode *head );
00082
00083
00084
00085
00086
00087 GULAPI void DeleteElementFromBBT( BBTNode *element, BBTNode *head );
00088
00089
00090
00091
00092 GULAPI void DumpBBTTree( BBTNode *node, int level,
00093 void dumpkey_func( void * ) );
00094 GULAPI void DumpBBTNode( BBTNode *node, int level );
00095
00096
00097
00098
00099
00100 GULAPI void HeapSort( size_t nelem, size_t elsize, void *elptr,
00101 int (*usrfunc)( void *, void *, void *), void *usrdata );
00102 GULAPI void PtrHeapSort( size_t nelem, void **elptr,
00103 int (*usrfunc)( void *, void *, void *), void *usrdata );
00104
00105
00106
00107
00108 template< class T >
00109 inline void _heapsort( int nA, gul::Ptr<T>& A )
00110 {
00111 gul::Ptr<T>& ra = A;
00112 T rra;
00113 int n = nA, l, ir, i, j;
00114
00115 if( n < 2 ) return;
00116 l = (n >> 1) + 1;
00117 ir = n;
00118
00119 for(;;)
00120 {
00121 if( l > 1 )
00122 {
00123 rra = ra[l-2];
00124 l--;
00125 }
00126 else
00127 {
00128 rra = ra[ir-1];
00129 ra[ir-1] = ra[0];
00130
00131 if( --ir == 1 )
00132 {
00133 ra[0] = rra;
00134 break;
00135 }
00136 }
00137 i = l;
00138 j = l+l;
00139
00140 while( j <= ir )
00141 {
00142 if( (j < ir) && (ra[j-1] < ra[j]) )
00143 j++;
00144
00145 if( rra < ra[j-1] )
00146 {
00147 ra[i-1] = ra[j-1];
00148 i = j;
00149 j <<= 1;
00150 }
00151 else
00152 j = ir + 1;
00153 }
00154
00155 ra[i-1] = rra;
00156 }
00157 }
00158
00159 GULAPI void heapsort( int nA, gul::Ptr<void *>& A );
00160 GULAPI void heapsort( int nA, gul::Ptr<int>& A );
00161
00162
00163
00164
00165
00166
00167 template< class T >
00168 inline int _BinarySearch( T x, int nA, const gul::Ptr<T>& A )
00169 {
00170 int mid,right,left;
00171
00172 if( nA == 0 ) return -1;
00173
00174 if( A[0] >= x )
00175 {
00176 if( A[0] > x ) return -1;
00177 return 0;
00178 }
00179 if( A[nA-1] <= x )
00180 {
00181 if( A[nA-1] < x ) return -1;
00182 return nA-1;
00183 }
00184
00185 left = 0;
00186 right = nA;
00187 mid = (left+right)/2;
00188
00189 while( (mid!=left) && (mid!=right) )
00190 {
00191 if( A[mid] == x )
00192 break;
00193 else if( A[mid] > x )
00194 {
00195 right = mid;
00196 mid = (left+right)>>1;
00197 }
00198 else
00199 {
00200 left = mid;
00201 mid = (left+right)>>1;
00202 }
00203 }
00204 if( A[mid] != x ) return -1;
00205
00206 return mid;
00207 }
00208
00209
00210 GULAPI int BinarySearch( int x, int nA, const gul::Ptr<int>& A );
00211 GULAPI int BinarySearch( void *x, int nA, const gul::Ptr<void *>& A );
00212
00213
00214
00215
00216
00217
00218 template< class T >
00219 inline int _BinarySearch( T x, int nA, const gul::Ptr<T>& A,
00220 int *rleft, int *rright )
00221 {
00222 int mid,right,left;
00223
00224 if( nA == 0 )
00225 {
00226 *rleft = -1;
00227 *rright = -1;
00228 return -1;
00229 }
00230
00231 if( A[0] >= x )
00232 {
00233 *rright = 0;
00234
00235 if( A[0] > x )
00236 {
00237 *rleft = -1;
00238 return -1;
00239 }
00240 else
00241 {
00242 *rleft = 0;
00243 return 0;
00244 }
00245 }
00246
00247 if( A[nA-1] <= x )
00248 {
00249 *rleft = nA-1;
00250
00251 if( A[nA-1] < x )
00252 {
00253 *rright = -1;
00254 return -1;
00255 }
00256 else
00257 {
00258 *rright = nA-1;
00259 return nA-1;
00260 }
00261 }
00262
00263 left = 0;
00264 right = nA;
00265 mid = (left+right)/2;
00266
00267 while( (mid!=left) && (mid!=right) )
00268 {
00269 if( A[mid] == x )
00270 break;
00271 else if( A[mid] > x )
00272 {
00273 right = mid;
00274 mid = (left+right)>>1;
00275 }
00276 else
00277 {
00278 left = mid;
00279 mid = (left+right)>>1;
00280 }
00281 }
00282 if( A[mid] != x )
00283 {
00284 *rleft = left;
00285 *rright = right;
00286 return -1;
00287 }
00288 else
00289 {
00290 *rleft = mid;
00291 *rright = mid;
00292 }
00293
00294 return mid;
00295 }
00296
00297
00298
00299 }
00300
00301 #endif