count

fnsize_t count(alias pred = "a == b", Range, E)(Range haystack, E needle) if (isInputRange!Range && !isInfinite!Range && is(typeof(binaryFun!pred(haystack.front, needle))))

Counts matches of needle in haystack.

The first overload counts each element e in haystack for which pred(e, needle) is true. pred defaults to equality. Performs haystack.length evaluations of pred.

The second overload counts the number of times needle was matched in haystack. pred compares elements in each range. Throws an exception if needle.empty is true, as the _count of the empty range in any range would be infinite. Overlapped counts are not considered, for example count("aaa", "aa") is 1, not 2.

Note

Regardless of the overload, count will not accept

infinite ranges for haystack.

Parameters

predThe predicate to compare elements.
haystackThe range to _count.
needleThe element or sub-range to _count in haystack.

Returns

The number of matches in haystack.
fnsize_t count(alias pred = "a == b", R1, R2)(R1 haystack, R2 needle) if (isForwardRange!R1 && !isInfinite!R1 && isForwardRange!R2 && is(typeof(binaryFun!pred(haystack.front, needle.front))))

Ditto

fnsize_t count(alias pred, R)(R haystack) if (isInputRange!R && !isInfinite!R && is(typeof(unaryFun!pred(haystack.front))))

Counts all elements or elements satisfying a predicate in haystack.

The first overload counts each element e in haystack for which pred(e) is true. Performs haystack.length evaluations of pred.

The second overload counts the number of elements in a range. If the given range has the length property, that is returned right away, otherwise performs haystack.length to walk the range.

Parameters

predOptional predicate to find elements.
haystackThe range to _count.

Returns

The number of elements in haystack (for which pred returned true).
fnsize_t count(R)(R haystack) if (isInputRange!R && !isInfinite!R)

Ditto