modify idl_search to use binary search algorithm

This commit is contained in:
Kurt Zeilenga 2000-09-21 01:56:10 +00:00
parent 4830b23b9d
commit 101ec9c57f

View File

@ -20,19 +20,42 @@
#define BDB_IS_ALLIDS(ids) ((ids)[0] == NOID) #define BDB_IS_ALLIDS(ids) ((ids)[0] == NOID)
#define IDL_CMP(x,y) ( x < y ? -1 : ( x > y ? 1 : 0 ) )
static int idl_search( ID *ids, ID id ) static int idl_search( ID *ids, ID id )
{ {
/* we should replace this a binary search as ids is sorted */ /*
int i; * binary search of id in ids
int n = (int) ids[0]; * if found, returns position of id
* if not found, returns first postion greater than id
*/
int base = 0;
int cursor;
int val;
int n = ids[0];
for( i = 1; i <= n; i++ ) { while( 0 < n ) {
if( id <= ids[i] ) { int pivot = n >> 1;
return i; 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;
} }
} }
return 0; if( val < 0 ) {
return cursor + 1;
} else {
return cursor + 2;
}
} }
static int idl_insert( ID *ids, ID id ) static int idl_insert( ID *ids, ID id )