Phobos' std.algorithm
module includes search functions that can work on any ranges. It automatically specializes based on type information. Searching a sorted range is faster than an unsorted range.
Searching has a number of different scenarios, each with different methods:
canFind
.find
function. It returns the remainder of the range, with the located item at the front.haystack.find(boyerMooreFinder(needle))
. This uses the Boyer-Moore algorithm which may give better performance.countUntil
. It returns a numeric index into the range, just like the indexOf
function for strings.find
function can take a predicate to customize the search operation.assumeSorted
on it before passing it to the search functions. The assumeSorted
function has no runtime cost; it only adds information to the type that is used for compile-time specialization.The search functions in Phobos make use of the ranges' available features to choose good-fit algorithms. Pass them efficiently implemented ranges with accurate capabilities to get best performance.
The find
function returns the remainder of the data because this is the most general behavior; it doesn't need random access, like returning an index, and doesn't require an additional function if you are implementing a function to split a range on a given condition. The find
function can work with a basic input range, serving as a foundation to implement whatever you need on top of it, and it will transparently optimize to use more range features if available.
3.139.97.202