Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -1,6 +1,6 @@
import std.stdio, std.array;
T[] lcs(T)(in T[] a, in T[] b) pure nothrow {
T[] lcs(T)(in T[] a, in T[] b) pure nothrow @safe {
if (a.empty || b.empty) return null;
if (a[0] == b[0])
return a[0] ~ lcs(a[1 .. $], b[1 .. $]);

View file

@ -1,7 +1,6 @@
import std.stdio, std.algorithm, std.range, std.array, std.string,
std.typecons;
import std.stdio, std.algorithm, std.range, std.array, std.string, std.typecons;
uint[] lensLCS(R)(R xs, R ys) pure nothrow {
uint[] lensLCS(R)(R xs, R ys) pure nothrow @safe {
auto prev = new typeof(return)(1 + ys.length);
auto curr = new typeof(return)(1 + ys.length);
@ -9,9 +8,7 @@ uint[] lensLCS(R)(R xs, R ys) pure nothrow {
swap(curr, prev);
size_t i = 0;
foreach (immutable y; ys) {
curr[i + 1] = (x == y)
? prev[i] + 1
: max(curr[i], prev[i + 1]);
curr[i + 1] = (x == y) ? prev[i] + 1 : max(curr[i], prev[i + 1]);
i++;
}
}
@ -20,7 +17,7 @@ uint[] lensLCS(R)(R xs, R ys) pure nothrow {
}
void calculateLCS(T)(in T[] xs, in T[] ys, bool[] xs_in_lcs,
in size_t idx=0) pure nothrow {
in size_t idx=0) pure nothrow @safe {
immutable nx = xs.length;
immutable ny = ys.length;
@ -36,33 +33,27 @@ void calculateLCS(T)(in T[] xs, in T[] ys, bool[] xs_in_lcs,
const xe = xs[mid .. $];
immutable ll_b = lensLCS(xb, ys);
// retro is slow with dmd.
const ll_e = lensLCS(xe.retro, ys.retro);
const ll_e = lensLCS(xe.retro, ys.retro); // retro is slow with dmd.
//immutable k = iota(ny + 1)
// .reduce!(max!(j => ll_b[j] + ll_e[ny - j]));
immutable k = iota(ny + 1)
.minPos!((i,j)=> tuple(ll_b[i] + ll_e[ny-i]) >
tuple(ll_b[j] + ll_e[ny-j]))[0];
.minPos!((i, j) => tuple(ll_b[i] + ll_e[ny - i]) >
tuple(ll_b[j] + ll_e[ny - j]))[0];
calculateLCS(xb, ys[0 .. k], xs_in_lcs, idx);
calculateLCS(xe, ys[k .. $], xs_in_lcs, idx + mid);
}
}
const(T)[] lcs(T)(in T[] xs, in T[] ys) pure /*nothrow*/ {
const(T)[] lcs(T)(in T[] xs, in T[] ys) pure /*nothrow*/ @safe {
auto xs_in_lcs = new bool[xs.length];
calculateLCS(xs, ys, xs_in_lcs);
return zip(xs, xs_in_lcs) // Not nothrow.
.filter!q{ a[1] }
.map!q{ a[0] }
.array;
return zip(xs, xs_in_lcs).filter!q{ a[1] }.map!q{ a[0] }.array; // Not nothrow.
}
string lcsString(in string s1, in string s2) pure /*nothrow*/ {
//return lcs(s1.representation, s2.representation).assumeChars;
return cast(string)lcs(s1.representation, s2.representation);
string lcsString(in string s1, in string s2) pure /*nothrow*/ @safe {
return lcs(s1.representation, s2.representation).assumeUTF;
}
void main() {