X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=obt%2Fbsearch.h;h=9613c51b36dda4f86cddabfc19bec7a4033cc9a6;hb=823997f0011b0c948ebd9d3a3b155884a336ed2f;hp=ca5ae5b27a05158b93a328cbf1473965c2aff217;hpb=262591ec1adc1badfcb36e9e6876dd81f2df9492;p=chaz%2Fopenbox diff --git a/obt/bsearch.h b/obt/bsearch.h index ca5ae5b2..9613c51b 100644 --- a/obt/bsearch.h +++ b/obt/bsearch.h @@ -23,9 +23,12 @@ G_BEGIN_DECLS +/*! Setup to do a binary search on an array holding elements of type @t */ #define BSEARCH_SETUP(t) \ register t l_BSEARCH, r_BSEARCH, out_BSEARCH; +/*! Search an array @ar holding elements of type @t, starting at index @start, + with @size elements, looking for value @val. */ #define BSEARCH(t, ar, start, size, val) \ { \ l_BSEARCH = (start); \ @@ -37,14 +40,18 @@ G_BEGIN_DECLS if ((val) == (ar)[out_BSEARCH]) { \ break; \ } \ - else if ((val) < (ar)[out_BSEARCH]) \ + else if ((val) < (ar)[out_BSEARCH] && out_BSEARCH > 0) { \ r_BSEARCH = out_BSEARCH-1; /* search to the left side */ \ + } \ else \ l_BSEARCH = out_BSEARCH+1; /* search to the left side */ \ } \ } +/*! Returns true if the element last searched for was found in the array */ #define BSEARCH_FOUND() (l_BSEARCH <= r_BSEARCH) +/*! Returns the position in the array at which the element last searched for + was found. */ #define BSEARCH_AT() (out_BSEARCH)