From d24592ee76a1027bb136f98698319c3d941bc0b8 Mon Sep 17 00:00:00 2001 From: ceriel Date: Tue, 30 Aug 1988 12:47:45 +0000 Subject: [PATCH] Added bsearch --- lang/cem/libcc/gen/LIST | 1 + lang/cem/libcc/gen/bsearch.c | 51 ++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 lang/cem/libcc/gen/bsearch.c diff --git a/lang/cem/libcc/gen/LIST b/lang/cem/libcc/gen/LIST index cbb4adf9f..66d70b179 100644 --- a/lang/cem/libcc/gen/LIST +++ b/lang/cem/libcc/gen/LIST @@ -33,6 +33,7 @@ monitor.c perror.c procentry.c qsort.c +bsearch.c rand.c seekdir.c sleep.c diff --git a/lang/cem/libcc/gen/bsearch.c b/lang/cem/libcc/gen/bsearch.c new file mode 100644 index 000000000..e38bf0ceb --- /dev/null +++ b/lang/cem/libcc/gen/bsearch.c @@ -0,0 +1,51 @@ +/* bsearch(3) + * + * Author: Terrence Holm Aug. 1988 + * + * + * Performs a binary search for a given within a sorted + * table. The table contains entries of size + * and starts at . + * + * Entries are compared using keycmp( key, entry ), each argument + * is a (char *), the function returns an int < 0, = 0 or > 0 + * according to the order of the two arguments. + * + * Bsearch(3) returns a pointer to the matching entry, if found, + * otherwise NULL is returned. + */ + +#define NULL (char *) 0 + + +char *bsearch( key, base, count, width, keycmp ) + char *key; + char *base; + unsigned int count; + unsigned int width; + int (*keycmp)(); + + { + char *mid_point; + int cmp; + + while ( count > 0 ) + { + mid_point = base + width * (count >> 1); + + cmp = (*keycmp)( key, mid_point ); + + if ( cmp == 0 ) + return( mid_point ); + + if ( cmp < 0 ) + count >>= 1; + else + { + base = mid_point + width; + count = (count - 1) >> 1; + } + } + + return( NULL ); + }