View source code
Display the source code in std/algorithm/searching.d from which this page was generated on
github.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.
Requires a signed-in GitHub account. This works well for small changes.
If you'd like to make larger changes you may want to consider using
local clone.
Page wiki
View or edit the community-maintained wiki page associated with this page.
Function std.algorithm.searching.count
The first version counts the number of elements x
in r
for
which pred(x, value)
is true
. pred
defaults to
equality. Performs Ο(haystack.length
) evaluations of pred
.
The second version returns the number of times
occurs in
needle
. Throws an exception if haystack
needle.empty
, as the count
of the empty range in any
range would be infinite. Overlapped counts
are not considered, for example
is count
("aaa", "aa")1
, not
2
.
The third version counts the elements for which pred(x)
is true
. Performs Ο(haystack.length
) evaluations of pred
.
Prototypes
size_t count(alias pred, Range, E)( Range haystack, E needle ) if (isInputRange!Range && !isInfinite!Range && is(typeof(binaryFun!pred(haystack.front, needle)) : bool)); size_t count(alias pred, R1, R2)( R1 haystack, R2 needle ) if (isForwardRange!R1 && !isInfinite!R1 && isForwardRange!R2 && is(typeof(binaryFun!pred(haystack.front, needle.front)) : bool)); size_t count(alias pred, R)( R haystack ) if (isInputRange!R && !isInfinite!R && is(typeof(unaryFun!pred(haystack.front)) : bool));
Note
Regardless of the overload,
will not accept
infinite ranges for count
.
haystack
Example
import std.uni : toLower; // count elements in range int[] a = [ 1, 2, 4, 3, 2, 5, 3, 2, 4 ]; assert(count(a, 2) == 3); assert(count!("a > b")(a, 2) == 5); // count range in range assert(count("abcadfabf", "ab") == 2); assert(count("ababab", "abab") == 1); assert(count("ababab", "abx") == 0); // fuzzy count range in range assert(count!((a, b) => std.uni.toLower(a) == std.uni.toLower(b))("AbcAdFaBf", "ab") == 2); // count predicate in range assert(count!("a > 1")(a) == 8);