From 0abf844cd9c22623e22f462c91f380f16524c7e8 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Fri, 5 Apr 2024 14:09:53 +0200 Subject: Add __MLIB_DEFINE_BSEARCH_CONTAINS() --- include/__bsearch.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'include') diff --git a/include/__bsearch.h b/include/__bsearch.h index d0dbf81..36dc6f3 100644 --- a/include/__bsearch.h +++ b/include/__bsearch.h @@ -21,6 +21,25 @@ return DEFAULT; \ } +#define __MLIB_DEFINE_BSEARCH_CONTAINS(TABLE) \ + static bool mlib_lookup_contains(rune ch) \ + { \ + ptrdiff_t i, lo, hi; \ + lo = 0; \ + hi = lengthof(TABLE) - 1; \ + i = (lo + hi) / 2; \ + do { \ + if (ch < TABLE[i].lo) \ + hi = i - 1; \ + else if (ch > TABLE[i].hi) \ + lo = i + 1; \ + else \ + return true; \ + i = (lo + hi) / 2; \ + } while (lo <= hi); \ + return false; \ + } + // [[gnu::always_inline]] static TYPE // lookup(rune ch) // { -- cgit v1.2.3