LowerBound (open)
Does a (case sensitive) binary search within a list
Syntax
LOADLIB "wh::util/algorithms.whlib";
RECORD FUNCTION LowerBound(VARIANT list, VARIANT element, FUNCTION PTR comparefunction)Parameters
VARIANT listArray to search in
VARIANT elementElement to search for
FUNCTION PTR comparefunctionOptional compare function, which must return whether the first parameter should be sorted before the second (signature: BOOLEAN FUNCTION func(VARIANT a, VARIANT b)
Return value
RECORDWhether the element was found, and the position of the element/position is should be inserted to preserve the ordering of list
foundWhether the element was found
positionPosition of the element (when found, otherwise the insert position)
Description
This function searches for an element within a sorted list. If the element is found, its position is returned, otherwise the insert position is returned (the place the element should be inserted to preserve the ordering of the list)
Examples
STRING ARRAY list := [ "b", "d", "f" ];
// Returns [ found := TRUE, position := 1 ]
RECORD res := LowerBound(list, "d");
// Returns [ found := FALSE, position := 3 ] (one past the end)
RECORD res := LowerBound(list, "g");
// Returns [ found := FALSE, position := 0 ]
RECORD res := LowerBound(list, "a");