BINSEARCHX

BINSEARCHX performs a binary search of an array for a specified key value by using a specified compare function and returns a size_t value.

Read syntax diagramSkip visual syntax diagram
>>-BINSEARCHX(x,p,f-+-----------+-)----------------------------><
                    '-,n-+----+-'     
                         '-,m-'       

x
An expression that specifies the target array that would be searched within. x must be a one-dimensional array and the elements of x must be in ascending order. If x is an array of NONVARYING BIT, it must be aligned.
p
An expression that specifies the address of the key value to be searched for.
f
An expression that specifies the function that will be invoked to perform all the required comparisons.
n
An expression that specifies the index of the first array element to be examined. It defaults to LBOUND(x).
m
An expression that specifies the number of to-be-examined array elements. The counting starts with the nth and defaults to HBOUND(x) – n + 1.
The function f must have the OPTLINK linkage and it is passed 2 POINTER BYVALUE arguments: The function f must have the attributes RETURNS( BYVALUE FIXED BINARY(31) ), and it must return one of the values -1, 0 or +1:

The value returned by the BINSEARCHX built-in function itself is the relative index of the key value in this array. If the key value y is not found in the array, the returned size_t value is zero.

The relative index is the index if the array has a lower bound of 1. Therefore, the true index would be calculated as: the returned value + LBOUND(x) – 1. For example:





Published: 23 December 2018