s3:docs: document ldap deref
[Samba/ekacnet.git] / lib / util / binsearch.h
blobac839900728354213c33ddb30f391200143a2bee
1 /*
2 Unix SMB/CIFS implementation.
4 a generic binary search macro
6 Copyright (C) Andrew Tridgell 2009
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #ifndef _BINSEARCH_H
23 #define _BINSEARCH_H
25 /* a binary array search, where the array is an array of pointers to structures,
26 and we want to find a match for 'target' on 'field' in those structures.
28 Inputs:
29 array: base pointer to an array of structures
30 arrray_size: number of elements in the array
31 field: the name of the field in the structure we are keying off
32 target: the field value we are looking for
33 comparison_fn: the comparison function
34 result: where the result of the search is put
36 if the element is found, then 'result' is set to point to the found array element. If not,
37 then 'result' is set to NULL.
39 The array is assumed to be sorted by the same comparison_fn as the
40 search (with, for example, qsort)
42 #define BINARY_ARRAY_SEARCH_P(array, array_size, field, target, comparison_fn, result) do { \
43 int32_t _b, _e; \
44 (result) = NULL; \
45 if (array_size) { for (_b = 0, _e = (array_size)-1; _b <= _e; ) { \
46 int32_t _i = (_b+_e)/2; \
47 int _r = comparison_fn(target, array[_i]->field); \
48 if (_r == 0) { (result) = array[_i]; break; } \
49 if (_r < 0) _e = _i - 1; else _b = _i + 1; \
50 }} } while (0)
53 like BINARY_ARRAY_SEARCH_P, but assumes that the array is an array
54 of structures, rather than pointers to structures
56 result points to the found structure, or NULL
58 #define BINARY_ARRAY_SEARCH(array, array_size, field, target, comparison_fn, result) do { \
59 int32_t _b, _e; \
60 (result) = NULL; \
61 if (array_size) { for (_b = 0, _e = (array_size)-1; _b <= _e; ) { \
62 int32_t _i = (_b+_e)/2; \
63 int _r = comparison_fn(target, array[_i].field); \
64 if (_r == 0) { (result) = &array[_i]; break; } \
65 if (_r < 0) _e = _i - 1; else _b = _i + 1; \
66 }} } while (0)
68 #endif