unsigned mdb_id2l_search( ID2L ids, ID id ) { /* * binary search of id in ids * if found, returns position of id * if not found, returns first position greater than id */ unsigned base = 0; unsigned cursor = 1; int val = 0; unsigned n = ids[0].mid; while( 0 < n ) { unsigned pivot = n >> 1; cursor = base + pivot + 1; val = IDL_CMP( id, ids[cursor].mid ); if( val < 0 ) { n = pivot; } else if ( val > 0 ) { base = cursor; n -= pivot + 1; } else { return cursor; } } if( val > 0 ) { ++cursor; } return cursor; }
unsigned bdb_idl_search( ID *ids, ID id ) { #define IDL_BINARY_SEARCH 1 #ifdef IDL_BINARY_SEARCH /* * binary search of id in ids * if found, returns position of id * if not found, returns first postion greater than id */ unsigned base = 0; unsigned cursor = 0; int val = 0; unsigned n = ids[0]; #if IDL_DEBUG > 0 idl_check( ids ); #endif while( 0 < n ) { int pivot = n >> 1; cursor = base + pivot; val = IDL_CMP( id, ids[cursor + 1] ); if( val < 0 ) { n = pivot; } else if ( val > 0 ) { base = cursor + 1; n -= pivot + 1; } else { return cursor + 1; } } if( val > 0 ) { return cursor + 2; } else { return cursor + 1; } #else /* (reverse) linear search */ int i; #if IDL_DEBUG > 0 idl_check( ids ); #endif for( i=ids[0]; i; i-- ) { if( id > ids[i] ) { break; } } return i+1; #endif }