RecordLowerBound (open)

Does a (case sensitive) binary search within a sorted record array

Syntax

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

RECORD FUNCTION RecordLowerBound(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

RECORD

Whether the element was found, and the position of the element/positionm 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 record with specific values within a record array that is sorted on specific cells. 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 record array)

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 := 3,  text := "value 3" ]
    , [ a := 5, b := 7,  text := "last value" ]
    ];

// Returns [ found := TRUE, position := 1 ]
RECORD res := RecordLowerBound(list, [ a := 3, b := 1 ], [ "A", "B" ]);

// Returns [ found := FALSE, position := 4 ] (one past the end)
RECORD res := RecordLowerBound(list, [ a := 5, b := 8 ], [ "A", "B" ]);

// Returns [ found := FALSE, position := 0 ]
RECORD res := RecordLowerBound(list, [ a := 1, b := 8 ], [ "A", "B" ]);