View source code
Display the source code in std/algorithm/searching.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
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.

std.algorithm.searching.count - multiple declarations

Function count

Counts matches of needle in haystack.

size_t count(alias pred, Range, E) (
  Range haystack,
  E needle
)
if (isInputRange!Range && !isInfinite!Range && is(typeof(binaryFun!pred(haystack.front, needle))));

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))));

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

NameDescription
pred The predicate to compare elements.
haystack The range to count.
needle The element or sub-range to count in haystack.

Returns

The number of matches in haystack.

Example

// count elements in range
int[] a = [ 1, 2, 4, 3, 2, 5, 3, 2, 4 ];
writeln(count(a, 2)); // 3
writeln(count!("a > b")(a, 2)); // 5

Example

import std.uni : toLower;
// count range in range
writeln(count("abcadfabf", "ab")); // 2
writeln(count("ababab", "abab")); // 1
writeln(count("ababab", "abx")); // 0
// fuzzy count range in range
writeln(count!((a, b) => toLower(a) == toLower(b))("AbcAdFaBf", "ab")); // 2

Function count

Counts all elements or elements satisfying a predicate in haystack.

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

size_t count(R) (
  R haystack
)
if (isInputRange!R && !isInfinite!R);

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

NameDescription
pred Optional predicate to find elements.
haystack The range to count.

Returns

The number of elements in haystack (for which pred returned true).

Example

// count elements in range
int[] a = [ 1, 2, 4, 3, 2, 5, 3, 2, 4 ];
writeln(count(a)); // 9
// count predicate in range
writeln(count!("a > 2")(a)); // 5

Authors

Andrei Alexandrescu

License

Boost License 1.0.