diff options
-rw-r--r-- | lib/util/binsearch.h | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/lib/util/binsearch.h b/lib/util/binsearch.h index ac83990072..f85d11694e 100644 --- a/lib/util/binsearch.h +++ b/lib/util/binsearch.h @@ -65,4 +65,20 @@ if (_r < 0) _e = _i - 1; else _b = _i + 1; \ }} } while (0) +/* + like BINARY_ARRAY_SEARCH_P, but assumes that the array is an array + of elements, rather than pointers to structures + + result points to the found structure, or NULL + */ +#define BINARY_ARRAY_SEARCH_V(array, array_size, target, comparison_fn, result) do { \ + int32_t _b, _e; \ + (result) = NULL; \ + if (array_size) { for (_b = 0, _e = (array_size)-1; _b <= _e; ) { \ + int32_t _i = (_b+_e)/2; \ + int _r = comparison_fn(target, array[_i]); \ + if (_r == 0) { (result) = &array[_i]; break; } \ + if (_r < 0) _e = _i - 1; else _b = _i + 1; \ + }} } while (0) + #endif |