walkLength

This is a best-effort implementation of length for any kind of range.

If hasLength!Range, simply returns range.length without checking upTo (when specified).

Otherwise, walks the range through its length and returns the number of elements seen. Performes O(n) evaluations of range.empty and range.popFront(), where n is the effective length of range.

The upTo parameter is useful to "cut the losses" in case the interest is in seeing whether the range has at least some number of elements. If the parameter upTo is specified, stops if upTo steps have been taken and returns upTo.

Infinite ranges are compatible, provided the parameter upTo is specified, in which case the implementation simply returns upTo.

  1. auto walkLength(Range range)
  2. auto walkLength(Range range, size_t upTo)
    walkLength
    (
    Range
    )
    (
    Range range
    ,
    const size_t upTo
    )
    if ()
  3. import std.traits;
  4. enum bool isInputRange(R, E);

Examples

import std.range : iota;

assert(10.iota.walkLength == 10);
// iota has a length function, and therefore the
// doesn't have to be walked, and the upTo
// parameter is ignored
assert(10.iota.walkLength(5) == 10);

Meta