Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -1,18 +1,18 @@
|
|||
import std.stdio;
|
||||
|
||||
T[][] ncsub(T)(in T[] seq, in int s=0) pure nothrow {
|
||||
T[][] ncsub(T)(in T[] seq, in uint s=0) pure nothrow @safe {
|
||||
if (seq.length) {
|
||||
T[][] aux;
|
||||
foreach (ys; ncsub(seq[1..$], s + !(s % 2)))
|
||||
typeof(return) aux;
|
||||
foreach (ys; ncsub(seq[1 .. $], s + !(s % 2)))
|
||||
aux ~= seq[0] ~ ys;
|
||||
return aux ~ ncsub(seq[1..$], s + s % 2);
|
||||
return aux ~ ncsub(seq[1 .. $], s + s % 2);
|
||||
} else
|
||||
return new T[][](s >= 3, 0);
|
||||
return new typeof(return)(s >= 3, 0);
|
||||
}
|
||||
|
||||
void main() {
|
||||
writeln(ncsub([1, 2, 3]));
|
||||
writeln(ncsub([1, 2, 3, 4]));
|
||||
foreach (nc; ncsub([1, 2, 3, 4, 5]))
|
||||
writeln(nc);
|
||||
void main() @safe {
|
||||
import std.stdio;
|
||||
|
||||
[1, 2, 3].ncsub.writeln;
|
||||
[1, 2, 3, 4].ncsub.writeln;
|
||||
foreach (const nc; [1, 2, 3, 4, 5].ncsub)
|
||||
nc.writeln;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,29 +1,28 @@
|
|||
struct Ncsub(T) {
|
||||
T[] seq;
|
||||
|
||||
int opApply(int delegate(ref int[]) dg) const {
|
||||
immutable int n = seq.length;
|
||||
int opApply(int delegate(ref T[]) dg) const {
|
||||
immutable n = seq.length;
|
||||
int result;
|
||||
auto S = new int[n];
|
||||
auto S = new T[n];
|
||||
|
||||
FOR_I:
|
||||
foreach (i; 1 .. 1 << seq.length) {
|
||||
int len_S;
|
||||
OUTER: foreach (immutable i; 1 .. 1 << n) {
|
||||
uint lenS;
|
||||
bool nc = false;
|
||||
foreach (j; 0 .. seq.length + 1) {
|
||||
immutable int k = i >> j;
|
||||
foreach (immutable j; 0 .. n + 1) {
|
||||
immutable k = i >> j;
|
||||
if (k == 0) {
|
||||
if (nc) {
|
||||
auto auxS = S[0 .. len_S];
|
||||
auto auxS = S[0 .. lenS];
|
||||
result = dg(auxS);
|
||||
if (result)
|
||||
break FOR_I;
|
||||
break OUTER;
|
||||
}
|
||||
break;
|
||||
} else if (k % 2) {
|
||||
S[len_S] = seq[j];
|
||||
len_S++;
|
||||
} else if (len_S)
|
||||
S[lenS] = seq[j];
|
||||
lenS++;
|
||||
} else if (lenS)
|
||||
nc = true;
|
||||
}
|
||||
}
|
||||
|
|
@ -34,9 +33,10 @@ struct Ncsub(T) {
|
|||
|
||||
void main() {
|
||||
import std.array, std.range;
|
||||
//assert(iota(24).array().Ncsub!int().walkLength() == 16_776_915);
|
||||
auto r = array(iota(24));
|
||||
int counter;
|
||||
|
||||
//assert(24.iota.array.Ncsub!int.walkLength == 16_776_915);
|
||||
auto r = 24.iota.array;
|
||||
uint counter = 0;
|
||||
foreach (s; Ncsub!int(r))
|
||||
counter++;
|
||||
assert(counter == 16_776_915);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,34 @@
|
|||
import std.stdio, std.array, std.range, std.concurrency;
|
||||
|
||||
Generator!(T[]) ncsub(T)(in T[] seq) {
|
||||
return new typeof(return)({
|
||||
immutable n = seq.length;
|
||||
auto S = new T[n];
|
||||
|
||||
foreach (immutable i; 1 .. 1 << n) {
|
||||
uint lenS = 0;
|
||||
bool nc = false;
|
||||
foreach (immutable j; 0 .. n + 1) {
|
||||
immutable k = i >> j;
|
||||
if (k == 0) {
|
||||
if (nc)
|
||||
yield(S[0 .. lenS]);
|
||||
break;
|
||||
} else if (k % 2) {
|
||||
S[lenS] = seq[j];
|
||||
lenS++;
|
||||
} else if (lenS)
|
||||
nc = true;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void main() {
|
||||
assert(24.iota.array.ncsub.walkLength == 16_776_915);
|
||||
|
||||
[1, 2, 3].ncsub.writeln;
|
||||
[1, 2, 3, 4].ncsub.writeln;
|
||||
foreach (const nc; [1, 2, 3, 4, 5].ncsub)
|
||||
nc.writeln;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue