util/binsearch: macro for greater than or equal search
authorDouglas Bagnall <douglas.bagnall@catalyst.net.nz>
Fri, 29 Jan 2016 04:53:20 +0000 (17:53 +1300)
committerAndrew Bartlett <abartlet@samba.org>
Wed, 9 Mar 2016 09:32:16 +0000 (10:32 +0100)
commit5235f1facf551672d202e7400f1ba3bd85a8f7dc
tree39a73e340436dc1ef3f6d910c6687ec887499896
parent42fe66fa732d824d62db4b35739fdbdc93bbb312
util/binsearch: macro for greater than or equal search

Sometimes you want to find the place where an item would be in a
sorted list, whether or not it is actually there.

The BINARY_ARRAY_SEARCH_GTE macro takes an extra 'next' pointer
argument over the other binsearch macros. This will end up pointing to
the next element in the case where there is not an exact match, or
NULL when there is. That is, searching the list

     { 2, 3, 4, 4, 9}

with a standard integer compare should give the following results:

search term    *result    *next
         1      -          2
         3      3          -
         4      4 [1]      -
         7      -          9
         9      9          -
        10      -          - [2]

Notes
[1] There are two fours, but you will always get the first one.
[2] The both NULL case means the search term is beyond the last list
item.

You can safely use the same pointer for both 'result' and 'next', if
you don't care to distinguish between the 'greater-than' and 'equals'
cases.

There is a torture test for this.

Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Garming Sam <garming@catalyst.net.nz>
lib/util/binsearch.h
lib/util/tests/binsearch.c [new file with mode: 0644]
source4/torture/local/local.c
source4/torture/local/wscript_build