countsAtMost

Check if r counts to at most maxCount elements.

@"complexity", "O(maxCount)"
bool
countsAtMost
(
R
)
(
R r
,
size_t maxCount
)
if (
isInputRange!R
)

Examples

static void test(R)(R x) if (isInputRange!R)
{
    import std.algorithm : count;
    const n = x.count;

    // below
    foreach (const i; 0 .. n)
    {
        assert(x.countsAtLeast(i));
        assert(!x.countsExactly(i));
        assert(!x.countsAtMost(i));
    }

    // at
    assert(x.countsAtLeast(n));
    assert(x.countsExactly(n));
    assert(x.countsAtMost(n));

    // above
    foreach (const i; n + 1 .. n + 10)
    {
        assert(!x.countsAtLeast(i));
        assert(!x.countsExactly(i));
        assert(x.countsAtMost(i));
    }
}

import std.algorithm : filter;
import std.range : iota;
import std.array : array;

test(3.iota.filter!(x => true));
test(3.iota.array);

Meta