Function std.range.primitives.popBackExactly
Eagerly advances
itself (not a copy) exactly r
times (by
calling n
r.popFront
).
takes popFrontExactly
by r
ref
,
so it mutates the original range. Completes in Ο(1
) steps for ranges
that support slicing, and have either length or are infinite.
Completes in Ο(
) time for all other ranges.
n
Prototype
void popBackExactly(Range)( Range r, size_t n ) if (isBidirectionalRange!Range);
Note
Unlike popFrontN
,
will assume that the
range holds at least popFrontExactly
elements. This makes n
faster than popFrontExactly
, but it also means that if popFrontN
range
does
not contain at least
elements, it will attempt to call n
on an popFront
empty
range, which is undefined behavior. So, only use
when it is guaranteed that popFrontExactly
range
holds at least
elements.
n
will behave the same but instead removes elements from
the popBackExactly
back
of the (bidirectional) range instead of the front
.
Example
import std.algorithm : filterBidirectional, equal; auto a = [1, 2, 3]; a.popFrontExactly(1); assert(a == [2, 3]); a.popBackExactly(1); assert(a == [2]); string s = "日本語"; s.popFrontExactly(1); assert(s == "本語"); s.popBackExactly(1); assert(s == "本"); auto bd = filterBidirectional!"true"([1, 2, 3]); bd.popFrontExactly(1); assert(bd.equal([2, 3])); bd.popBackExactly(1); assert(bd.equal([2]));
Authors
Andrei Alexandrescu, David Simcha, and Jonathan M Davis. Credit for some of the ideas in building this module goes to Leonardo Maffi.