util: added BINARY_ARRAY_SEARCH_V()
authorAndrew Tridgell <tridge@samba.org>
Wed, 29 Sep 2010 22:45:27 +0000 (15:45 -0700)
committerAndrew Tridgell <tridge@samba.org>
Wed, 29 Sep 2010 23:36:22 +0000 (16:36 -0700)
this is used to search an array of values

lib/util/binsearch.h

index ac839900728354213c33ddb30f391200143a2bee..f85d11694ec5685e0d6ed2b3b6fe5a209e6b3955 100644 (file)
                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