std.algorithm.searching.find
- multiple declarations
- Function find
- Function find
- Function find
- Function find
- Function find
Function find
Finds
in needle
efficiently using the
Boyer-Moore method.
haystack
Prototype
Range1 find(Range1, alias pred, Range2)( Range1 haystack, BoyerMooreFinder!(pred,Range2) needle );
Parameters
Name | Description |
---|---|
haystack | A random-access range with length and slicing. |
needle | A BoyerMooreFinder . |
Returns
advanced such that haystack
is a prefix of it (if no
such position exists, returns needle
advanced to termination).
haystack
Example
int[] a = [ -1, 0, 1, 2, 3, 4, 5 ]; int[] b = [ 1, 2, 3 ]; assert(find(a, boyerMooreFinder(b)) == [ 1, 2, 3, 4, 5 ]); assert(find(b, boyerMooreFinder(a)).empty);
Function find
Finds the first occurrence of a forward range in another forward range.
Performs Ο(walkLength(
) comparisons in the
worst case. There are specializations that improve performance by taking
advantage of bidirectional or random access in the given ranges (where
possible), depending on the statistics of the two ranges' content.
haystack
) * walkLength(needle
)
Prototype
R1 find(alias pred, R1, R2)( R1 haystack, R2 needle ) if (isForwardRange!R1 && isForwardRange!R2 && is(typeof(binaryFun!pred(haystack.front, needle.front)) : bool) && !isRandomAccessRange!R1);
Parameters
Name | Description |
---|---|
pred | The predicate to use for comparing respective elements from the haystack
and the needle . Defaults to simple equality "a == b" .
|
haystack | The forward range searched in. |
needle | The forward range searched for. |
Returns
advanced such that haystack
is a prefix of it (if no
such position exists, returns needle
advanced to termination).
haystack
Example
import std.container : SList; assert(find("hello, world", "World").empty); assert(find("hello, world", "wo") == "world"); assert([1, 2, 3, 4].find(SList!int(2, 3)[]) == [2, 3, 4]); alias C = Tuple!(int, "x", int, "y"); auto a = [C(1,0), C(2,0), C(3,1), C(4,0)]; assert(a.find!"a.x == b"([2, 3]) == [C(2,0), C(3,1), C(4,0)]); assert(a[1 .. $].find!"a.x == b"([2, 3]) == [C(2,0), C(3,1), C(4,0)]);
Function find
Advances the input range
by calling haystack
haystack.popFront
until
either pred(haystack.front)
, or haystack.empty
. Performs Ο(haystack.length
) evaluations of pred
.
To find the last element of a bidirectional
satisfying
haystack
pred
, call
. See std.range.retro.
find
!(pred)(retro(haystack
))
Prototype
InputRange find(alias pred, InputRange)( InputRange haystack ) if (isInputRange!InputRange);
Parameters
Name | Description |
---|---|
pred | The predicate for determining if a given element is the one being searched for. |
haystack | The input range to search in. |
Returns
advanced such that the front element is the one searched for;
that is, haystack
until
binaryFun!pred(haystack.front, needle)
is true
. If no
such position exists, returns an empty
.
haystack
See Also
Example
auto arr = [ 1, 2, 3, 4, 1 ]; assert(find!("a > 2")(arr) == [ 3, 4, 1 ]); // with predicate alias bool pred(int x) { return x + 1 > 1.5; } assert(find!(pred)(arr) == arr);
Function find
Finds an individual element in an input range. Elements of
are compared with haystack
by using predicate needle
pred
. Performs Ο(walkLength(
) evaluations of haystack
)pred
.
To find the last occurrence of
in needle
, call haystack
. See std.range.retro.
find
(retro(haystack
), needle
)
Prototype
InputRange find(alias pred, InputRange, Element)( InputRange haystack, Element needle ) if (isInputRange!InputRange && is(typeof(binaryFun!pred(haystack.front, needle)) : bool));
Parameters
Name | Description |
---|---|
pred | The predicate for comparing each element with the needle , defaulting to
"a == b" .
The negated predicate "a != b" can be used to search instead for the first
element not matching the needle .
|
haystack | The input range searched in. |
needle | The element searched for. |
Constraints
isInputRange!InputRange && is(typeof(binaryFun!pred(haystack.front,
needle
)
: bool))
Returns
advanced such that the front element is the one searched for;
that is, haystack
until
binaryFun!pred(haystack.front,
is needle
)true
. If no
such position exists, returns an empty
.
haystack
See Also
Example
import std.algorithm.comparison : equal; import std.container : SList; assert(find("hello, world", ',') == ", world"); assert(find([1, 2, 3, 5], 4) == []); assert(equal(find(SList!int(1, 2, 3, 4, 5)[], 4), SList!int(4, 5)[])); assert(find!"a > b"([1, 2, 3, 5], 2) == [3, 5]); auto a = [ 1, 2, 3 ]; assert(find(a, 5).empty); // not found assert(!find(a, 2).empty); // found // Case-insensitive find of a string string[] s = [ "Hello", "world", "!" ]; assert(!find!("toLower(a) == b")(s, "hello").empty);
Function find
Finds two or more
into a needles
. The predicate haystack
pred
is used throughout to compare elements. By default, elements are
compared for equality.
Prototype
Tuple!(Range,size_t) find(alias pred, Range, Ranges...)( Range haystack, Ranges needles ) if (Ranges.length > 1 && is(typeof(startsWith!pred(haystack, needles))));
Parameters
Name | Description |
---|---|
pred | The predicate to use for comparing elements. |
haystack | The target of the search. Must be an input range.
If any of is a range with elements comparable to
elements in , then must be a forward range
such that the search can backtrack.
|
needles | One or more items to search for. Each of must
be either comparable to one element in , or be itself a
forward range with elements comparable with elements in
. |
Returns
A tuple containing
positioned to match one of the
haystack
needles
and also the 1-based index of the matching element in
(0 if none of needles
matched, 1 if needles
matched, 2 if needles
[0]
matched...). The first needle to be found
will be the one that matches. If multiple needles
[1]needles
are found at the
same spot in the range, then the shortest one is the one which matches
(if multiple needles
of the same length are found at the same spot (e.g
"a"
and 'a'
), then the left-most of them in the argument list
matches).
The relationship between
and haystack
simply means
that one can e.g. search for individual needles
int
s or arrays of int
s in an array of int
s. In addition, if elements are
individually comparable, searches of heterogeneous types are allowed
as well: a double[]
can be searched for an int
or a short[]
, and conversely a long
can be searched for a float
or a double[]
. This makes for efficient searches without the need
to coerce one side of the comparison into the other's side type.
The complexity of the search is Ο(haystack.length *
max(needles.length)
). (For needles
that are individual items, length
is considered to be 1.) The strategy used in searching several
subranges at once maximizes cache usage by moving in
as
few times as possible.
haystack
Example
int[] a = [ 1, 4, 2, 3 ]; assert(find(a, 4) == [ 4, 2, 3 ]); assert(find(a, [ 1, 4 ]) == [ 1, 4, 2, 3 ]); assert(find(a, [ 1, 3 ], 4) == tuple([ 4, 2, 3 ], 2)); // Mixed types allowed if comparable assert(find(a, 5, [ 1.2, 3.5 ], 2.0) == tuple([ 2, 3 ], 3));