Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,23 @@
|
|||
import std.stdio, std.traits, std.algorithm;
|
||||
|
||||
auto sma(T, int period)() pure nothrow @safe {
|
||||
T[period] data = 0;
|
||||
T sum = 0;
|
||||
int index, nFilled;
|
||||
|
||||
return (in T v) nothrow @safe @nogc {
|
||||
sum += -data[index] + v;
|
||||
data[index] = v;
|
||||
index = (index + 1) % period;
|
||||
nFilled = min(period, nFilled + 1);
|
||||
return CommonType!(T, float)(sum) / nFilled;
|
||||
};
|
||||
}
|
||||
|
||||
void main() {
|
||||
immutable s3 = sma!(int, 3);
|
||||
immutable s5 = sma!(double, 5);
|
||||
|
||||
foreach (immutable e; [1, 2, 3, 4, 5, 5, 4, 3, 2, 1])
|
||||
writefln("Added %d, sma(3) = %f, sma(5) = %f", e, s3(e), s5(e));
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
import std.stdio, std.traits, std.algorithm;
|
||||
|
||||
struct SMA(T, int period) {
|
||||
T[period] data = 0;
|
||||
T sum = 0;
|
||||
int index, nFilled;
|
||||
|
||||
auto opCall(in T v) pure nothrow @safe @nogc {
|
||||
sum += -data[index] + v;
|
||||
data[index] = v;
|
||||
index = (index + 1) % period;
|
||||
nFilled = min(period, nFilled + 1);
|
||||
return CommonType!(T, float)(sum) / nFilled;
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
SMA!(int, 3) s3;
|
||||
SMA!(double, 5) s5;
|
||||
|
||||
foreach (immutable e; [1, 2, 3, 4, 5, 5, 4, 3, 2, 1])
|
||||
writefln("Added %d, sma(3) = %f, sma(5) = %f", e, s3(e), s5(e));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue