aboutsummaryrefslogtreecommitdiffstats
path: root/src/libposixplz/bsearchr.c
blob: 87b664f4ab72b10e62bd879a9139fe032202a9af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/* ISC license. */

#include <skalibs/functypes.h>
#include <skalibs/posixplz.h>

void *bsearchr (void const *key, void const *base, size_t n, size_t width, cmp_func_ref cmp, void *aux)
{
  while (n)
  {
    void *cur = (char *)base + width * (n >> 1) ;
    int h = (*cmp)(key, cur, aux) ;
    if (h < 0) n >>= 1 ;
    else if (h > 0) { base = (char *)cur + width ; n = (n-1) >> 1 ; }
    else return cur ;
  }
  return 0 ;
}