#ifndef _LUBOUND_H
#define _LUBOUND_H

#ifdef __cplusplus
extern "C" {
#endif

#include <stddef.h>

static __attribute__((unused)) void *lbound(const void *key,const void *base,size_t nelems,size_t size,int (*compar)(const void *,const void *)) // {{{
{
  size_t left=0,right=nelems;
  while (left<right) {
    size_t mid=left+(right-left)/2;
    const void *elem=(void *)(((const char *)base)+(mid*size));

    int res=(*compar)(elem,key);
    if (res<0) {
      left=mid+1;
    } else {
      right=mid;
    } 
  }
  return (void *)(((const char *)base)+(left*size));
}
// }}}

static __attribute__((unused)) void *ubound(const void *key,const void *base,size_t nelems,size_t size,int (*compar)(const void *,const void *)) // {{{
{
  size_t left=0,right=nelems;
  while (left<right) {
    size_t mid=left+(right-left)/2;
    const void *elem=(void *)(((const char *)base)+(mid*size));

    int res=(*compar)(elem,key);
    if (res<=0) {
      left=mid+1;
    } else {
      right=mid;
    } 
  }
  return (void *)(((const char *)base)+(left*size));
}
// }}}

#ifdef __cplusplus
}
#endif

#endif
