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,9 +1,9 @@
import std.stdio;
inout(T[]) maxSubseq(T)(inout T[] sequence) pure nothrow {
inout(T[]) maxSubseq(T)(inout T[] sequence) pure nothrow @nogc {
int maxSum, thisSum, i, start, end = -1;
foreach (j, x; sequence) {
foreach (immutable j, immutable x; sequence) {
thisSum += x;
if (thisSum < 0) {
i = j + 1;
@ -23,8 +23,8 @@ inout(T[]) maxSubseq(T)(inout T[] sequence) pure nothrow {
void main() {
const a1 = [-1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1];
writeln("Maximal subsequence: ", maxSubseq(a1));
writeln("Maximal subsequence: ", a1.maxSubseq);
const a2 = [-1, -2, -3, -5, -6, -2, -1, -4, -4, -2, -1];
writeln("Maximal subsequence: ", maxSubseq(a2));
writeln("Maximal subsequence: ", a2.maxSubseq);
}

View file

@ -3,35 +3,29 @@ import std.stdio, std.algorithm, std.range, std.typecons;
mixin template InitsTails(T) {
T[] data;
size_t pos;
@property bool empty() pure nothrow {
@property bool empty() pure nothrow @nogc {
return pos > data.length;
}
void popFront() pure nothrow { pos++; }
void popFront() pure nothrow @nogc { pos++; }
}
struct Inits(T) {
mixin InitsTails!T;
@property T[] front() pure nothrow { return data[0 .. pos]; }
@property T[] front() pure nothrow @nogc { return data[0 .. pos]; }
}
auto inits(T)(T[] seq) { return seq.Inits!T; }
auto inits(T)(T[] seq) pure nothrow @nogc { return seq.Inits!T; }
struct Tails(T) {
mixin InitsTails!T;
@property T[] front() pure nothrow { return data[pos .. $]; }
@property T[] front() pure nothrow @nogc { return data[pos .. $]; }
}
auto tails(T)(T[] seq) pure nothrow { return seq.Tails!T; }
auto tails(T)(T[] seq) pure nothrow @nogc { return seq.Tails!T; }
T[] maxSubseq(T)(T[] seq) pure nothrow {
//return seq.tails.map!inits.join.reduce!(max!sum);
return reduce!max(tuple(0, T[].init),
seq
.tails
.map!inits
.join
.map!q{ tuple(a.sum, a) }
)[1];
T[] maxSubseq(T)(T[] seq) pure nothrow /*@nogc*/ {
//return seq.tails.map!inits.joiner.reduce!(max!sum);
return seq.tails.map!inits.join.minPos!q{ a.sum > b.sum }[0];
}
void main() {