From 3b52b6249b94e104077ead134c35402fa1439493 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Wed, 29 Sep 2010 15:45:27 -0700 Subject: util: added BINARY_ARRAY_SEARCH_V() this is used to search an array of values --- lib/util/binsearch.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'lib/util') 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 -- cgit