You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
2.5 KiB
78 lines
2.5 KiB
//+----------------------------------------------------------------------------
|
|
//
|
|
// File: bsearch.cpp
|
|
//
|
|
// Module: CMPBK32.DLL
|
|
//
|
|
// Synopsis: Binary search implementation
|
|
//
|
|
// Copyright (c) 1997-1998 Microsoft Corporation
|
|
//
|
|
// Author: quintinb created header 08/17/99
|
|
//
|
|
//+----------------------------------------------------------------------------
|
|
#include "cmmaster.h"
|
|
|
|
/***
|
|
*char *bsearch() - do a binary search on an array
|
|
*
|
|
*Purpose:
|
|
* Does a binary search of a sorted array for a key.
|
|
*
|
|
*Entry:
|
|
* const char *key - key to search for
|
|
* const char *base - base of sorted array to search
|
|
* unsigned int num - number of elements in array
|
|
* unsigned int width - number of bytes per element
|
|
* int (*compare)() - pointer to function that compares two array
|
|
* elements, returning neg when #1 < #2, pos when #1 > #2, and
|
|
* 0 when they are equal. Function is passed pointers to two
|
|
* array elements.
|
|
*
|
|
*Exit:
|
|
* if key is found:
|
|
* returns pointer to occurrence of key in array
|
|
* if key is not found:
|
|
* returns NULL
|
|
*
|
|
*Exceptions:
|
|
*
|
|
*******************************************************************************/
|
|
|
|
void * __cdecl CmBSearch (
|
|
REG4 const void *key,
|
|
const void *base,
|
|
size_t num,
|
|
size_t width,
|
|
int (__cdecl *compare)(const void *, const void *)
|
|
)
|
|
{
|
|
REG1 char *lo = (char *)base;
|
|
REG2 char *hi = (char *)base + (num - 1) * width;
|
|
REG3 char *mid;
|
|
unsigned int half;
|
|
int result;
|
|
|
|
while (lo <= hi)
|
|
if (half = num / 2)
|
|
{
|
|
mid = lo + (num & 1 ? half : (half - 1)) * width;
|
|
if (!(result = (*compare)(key,mid)))
|
|
return(mid);
|
|
else if (result < 0)
|
|
{
|
|
hi = mid - width;
|
|
num = num & 1 ? half : half-1;
|
|
}
|
|
else {
|
|
lo = mid + width;
|
|
num = half;
|
|
}
|
|
}
|
|
else if (num)
|
|
return((*compare)(key,lo) ? NULL : lo);
|
|
else
|
|
break;
|
|
|
|
return(NULL);
|
|
}
|