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 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

RECORD

Whether the element was found, and the position of the element/position is should be inserted to preserve the ordering of list

found

Whether the element was found

position

Position 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");