RecordUpperBound (open)
Does a (case sensitive) binary search within a sorted record array, returns the position of the first element that is greater than the searched-for element
Syntax
LOADLIB "wh::util/algorithms.whlib";
INTEGER FUNCTION RecordUpperBound(RECORD ARRAY list, RECORD element, STRING ARRAY cellnames)
Parameters
RECORD ARRAY list
Record array to search in
RECORD element
Record with values to search for
STRING ARRAY cellnames
Names of cells to search for (the list must be ordered on these cellnames, in the same order they are passed to this function)
Return value
INTEGER
Position of the first element that is greater than the searched-for element.
Description
This function searches for an record with specific values within a record array that is sorted on specific cells. The position of the first element that is greater than the searched-for element is returned, or one past the last element if no such element exists.
Examples
// List (sorted on 'a', then 'b')
RECORD ARRAY list :=
[ [ a := 1, b := 10, text := "value 1" ]
, [ a := 3, b := 1, text := "second value" ]
, [ a := 3, b := 1, text := "second value again" ]
, [ a := 3, b := 3, text := "value 3" ]
, [ a := 5, b := 7, text := "last value" ]
];
// Returns [ found := TRUE, position := 3 ]
INTEGER res := RecordUpperBound(list, [ a := 3, b := 1 ], [ "A", "B" ]);
// Returns [ found := FALSE, position := 5 ] (one past the end)
INTEGER res := RecordUpperBound(list, [ a := 5, b := 8 ], [ "A", "B" ]);
// Returns [ found := FALSE, position := 0 ]
INTEGER res := RecordUpperBound(list, [ a := 1, b := 8 ], [ "A", "B" ]);