Permutations: Difference between revisions

Content added Content deleted
(Added missing in lazy D version)
(Better lazy D entry)
Line 855: Line 855:
<lang d>import std.algorithm, std.exception;
<lang d>import std.algorithm, std.exception;


struct Permutations(T) {
struct Permutations(bool doCopy=true, T) {
private immutable size_t num;
private immutable size_t num;
private T[] items;
private T[] items;
Line 871: Line 871:
return result;
return result;
}
}



this.num = items.length;
this.num = items.length;
Line 878: Line 877:
}
}


@property T[] front() /*const*/ pure nothrow {
@property T[] front() /*const*/ pure /*nothrow*/ {
return items;
static if (doCopy)
return items.dup; // not nothrow
else
return items;
}
}


Line 908: Line 910:
}
}


Permutations!T permutations(T)(/*in*/ T[] items) pure nothrow {
Permutations!(doCopy,T) permutations(bool doCopy=true, T)
(/*in*/ T[] items)
return Permutations!T(items);
pure nothrow {
return Permutations!(doCopy, T)(items);
} unittest {
} unittest {
import std.bigint;
import std.bigint;
Line 919: Line 923:
void main() {
void main() {
import std.stdio;
import std.stdio;
foreach (p; permutations([1, 2, 3]))
foreach (p; permutations!false([1, 2, 3]))
writeln(p);
writeln(p);
}
}