UpperBound (open)

Does a (case sensitive) binary search within a list, returns of the position of the element that is greater than the searched-for element.

Syntax

LOADLIB "wh::util/algorithms.whlib";

INTEGER FUNCTION UpperBound(VARIANT list, VARIANT element, FUNCTION PTR comparefunction)

Parameters

VARIANT list

Array to search in

VARIANT element

Element to search for

FUNCTION PTR comparefunction

Optional 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

INTEGER

Position of the first element that is greater than the searched-for element, or the end of the list if no such element can be found.

Description

This function searches for an element within a sorted list. The position of the first element that is greater than the search-for element is returned; if no such element exists the length of the list is returned (so an insert at the returned position preserves the ordering of the list)

Examples


STRING ARRAY list := [ "b", "d", "d", "f" ];

// Returns 3
INTEGER res := LowerBound(list, "d");

// Returns 4 (one past the end)
INTEGER res := LowerBound(list, "g");

// Returns 0
INTEGER res := LowerBound(list, "a");