bsearch

Searches a sorted array for the specified element or for the insert position of the element. The array is assumed to be pre-sorted in ascending order, the search will not work properly if it is not. If T is a class or struct, comparison is performed using T.opCmp(). Otherwise, elements of T are compared using ">" and ">=" or, if T is compatible to size_t (which includes ssize_t, the signed version of size_t), by calculating the difference.

Template params: T = type of array element

bool
bsearch
(
T
)
(
T[] array
,,
out size_t position
)
out (found) { if (found) { assert (position < array.length); } else { assert (position <= array.length); } }

Parameters

array T[]

array to search

match T

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 in bsearchCustom().

Meta