The OpenD Programming Language

SearchPolicy

Policy used with the searching primitives lowerBound, upperBound, and equalRange of SortedRange below.

Values

ValueMeaning
linear

Searches in a linear fashion.

trot

Searches with a step that is grows linearly (1, 2, 3,...) leading to a quadratic search schedule (indexes tried are 0, 1, 3, 6, 10, 15, 21, 28,...) Once the search overshoots its target, the remaining interval is searched using binary search. The search is completed in O(sqrt(n)) time. Use it when you are reasonably confident that the value is around the beginning of the range.

gallop

Performs a galloping search algorithm, i.e. searches with a step that doubles every time, (1, 2, 4, 8, ...) leading to an exponential search schedule (indexes tried are 0, 1, 3, 7, 15, 31, 63,...) Once the search overshoots its target, the remaining interval is searched using binary search. A value is found in O(log(n)) time.

binarySearch

Searches using a classic interval halving policy. The search starts in the middle of the range, and each search step cuts the range in half. This policy finds a value in O(log(n)) time but is less cache friendly than gallop for large ranges. The binarySearch policy is used as the last step of trot, gallop, trotBackwards, and gallopBackwards strategies.

trotBackwards

Similar to trot but starts backwards. Use it when confident that the value is around the end of the range.

gallopBackwards

Similar to gallop but starts backwards. Use it when confident that the value is around the end of the range.

Examples

import std.algorithm.comparison : equal;

auto a = assumeSorted([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
auto p1 = a.upperBound!(SearchPolicy.binarySearch)(3);
assert(p1.equal([4, 5, 6, 7, 8, 9]));

auto p2 = a.lowerBound!(SearchPolicy.gallop)(4);
assert(p2.equal([0, 1, 2, 3]));

Meta