aboutsummaryrefslogtreecommitdiffstats
path: root/src/libposixplz
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2025-09-08 15:49:40 +0000
committerLaurent Bercot <ska-skaware@skarnet.org>2025-09-08 15:49:40 +0000
commitf04b8eea689980cb25233317c84775dfed3c4aa6 (patch)
treec4df0d1ccb51146052be55d50010795c1b932ead /src/libposixplz
parent62cc58a2589b136176d4bb49ba19cab7fa76f8f8 (diff)
downloadskalibs-f04b8eea689980cb25233317c84775dfed3c4aa6.tar.gz
Add bsearchr
Diffstat (limited to 'src/libposixplz')
-rw-r--r--src/libposixplz/bsearchr.c17
1 files changed, 17 insertions, 0 deletions
diff --git a/src/libposixplz/bsearchr.c b/src/libposixplz/bsearchr.c
new file mode 100644
index 0000000..87b664f
--- /dev/null
+++ b/src/libposixplz/bsearchr.c
@@ -0,0 +1,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 ;
+}