bsearchCustom

Searches a sorted array for an element or an insert position for an element. The array is assumed to be pre-sorted according to cmp.

bool
bsearchCustom
(,
scope ssize_t delegate
(
size_t i
)
cmp
,
out size_t position
)
out (found) { if (found) { assert (position < array_length); } else { assert (position <= array_length); } }

Parameters

array_length size_t

length of array to search

cmp ssize_t delegate
(
size_t i
)

comparison callback delegate, should return * a positive value if the array element at index i compares greater than the element to search for, * a negative value if the array element at index i compares less than the element to search for, * 0 if if the array element at index i compares equal to the element to search for.

position size_t

out value, value depends on whether the element was found:

1. If found, the position at which element was found is output.

2. If not found, the position at which the element could be inserted is output, as follows:

* A value of 0 means that the element is smaller than all elements in the array, and would need to be inserted at the beginning of the array, and all other elements shifted to the right. * A value of array.length means that the element is larger than all elements in the array, and would need to be appended to the end of the array. * A value of > 0 and < array.length means that the element would need to be inserted at the specified position, and all elements of index >= the specified position shifted to the right.

Return Value

Type: bool

true if the element was found in the array

In: array_length must be at most ssize_t.max (int.max if size_t is uint or long.max if size_t is ulong). TODO: Remove this restriction by rephrasing the implementation so that min/max cannot be less than 0.

Meta