mirror of
https://github.com/dlang/phobos.git
synced 2025-04-30 07:00:37 +03:00
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:
commit
b026e490c4
1 changed files with 24 additions and 1 deletions
|
@ -7926,7 +7926,13 @@ if (isRandomAccessRange!Range && hasLength!Range)
|
||||||
_indices = iota(size_t(r.length)).array;
|
_indices = iota(size_t(r.length)).array;
|
||||||
_empty = r.length == 0;
|
_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.
|
/// Returns: `true` if the range is empty, `false` otherwise.
|
||||||
@property bool empty() const pure nothrow @safe @nogc
|
@property bool empty() const pure nothrow @safe @nogc
|
||||||
{
|
{
|
||||||
|
@ -7967,6 +7973,11 @@ if (isRandomAccessRange!Range && hasLength!Range)
|
||||||
|
|
||||||
next(2);
|
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],
|
[1, 2, 0],
|
||||||
[2, 1, 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);
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue