Merge pull request #8580 from jamesragray/b18975

Make permutations into forward range.

Signed-off-by: Razvan Nitu <RazvanN7@users.noreply.github.com>
Signed-off-by: H. S. Teoh <quickfur@users.noreply.github.com>
Merged-on-behalf-of: Nicholas Wilson <thewilsonator@users.noreply.github.com>
This commit is contained in:
The Dlang Bot 2022-09-27 01:05:31 +02:00 committed by GitHub
commit b026e490c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -7926,7 +7926,13 @@ if (isRandomAccessRange!Range && hasLength!Range)
_indices = iota(size_t(r.length)).array;
_empty = r.length == 0;
}
private this(size_t[] indices, size_t[] state, Range r, bool empty_)
{
_indices = indices;
_state = state;
_r = r;
_empty = empty_;
}
/// Returns: `true` if the range is empty, `false` otherwise.
@property bool empty() const pure nothrow @safe @nogc
{
@ -7967,6 +7973,11 @@ if (isRandomAccessRange!Range && hasLength!Range)
next(2);
}
/// Returns: an independent copy of the permutations range.
auto save()
{
return typeof(this)(_indices.dup, _state.dup, _r.save, _empty);
}
}
///
@ -7982,3 +7993,15 @@ if (isRandomAccessRange!Range && hasLength!Range)
[1, 2, 0],
[2, 1, 0]]));
}
@safe unittest
{
import std.algorithm.comparison : equal;
import std.range : ElementType;
import std.array : array;
auto p = [1, 2, 3].permutations;
auto x = p.save.front;
p.popFront;
auto y = p.front;
assert(x != y);
}