count

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 O(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.

  1. size_t count(Range haystack, E needle)
  2. size_t count(R1 haystack, R2 needle)
    size_t
    count
    (
    alias pred = "a == b"
    R1
    R2
    )
    if (
    is(typeof(binaryFun!pred(haystack.front, needle.front)))
    )
  3. size_t count(R haystack)
  4. size_t count(R haystack)

Parameters

pred

The predicate to compare elements.

haystack R1

The range to count.

needle R2

The element or sub-range to count in haystack.

Return Value

Type: size_t

The number of matches in haystack.

Examples

// 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);
import std.uni : toLower;
// 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) => toLower(a) == toLower(b))("AbcAdFaBf", "ab") == 2);

Meta