Code failing unknown reason out of memory, also recursive types
Mr.Bingo
Bingo at Namo.com
Mon Jun 25 14:36:31 UTC 2018
import std.stdio;
union Vector(T, size_t N = size_t.max)
{
import std.range, std.typecons, std.meta, std.algorithm,
std.conv, std.math;
static if (N == size_t.max) // For size_t.max sets N to be
infinite/dynamic;
{
mixin("Tuple!("~"T,".repeat(N).join()~") data;");
@property size_t Length() { return rect.length; }
@property double norm(size_t n = 2)()
{
return (iota(0,data.length).map!(a =>
data[a].pow(n))).pow(1/cast(double)n);
}
}
else
{
mixin("Tuple!("~"T,".repeat(N).join()~") data;");
@property size_t Length() { return N; }
@property double norm(size_t n = 2)()
{
mixin("return ("~(iota(0,N).map!(a =>
"data["~to!string(a)~"].pow(n)").join("+"))~").pow(1/cast(double)n);");
}
}
auto opDispatch(string s, Args...)(Args v)
if (s.length > 1 && s[0] == 'x')
{
static if (N == size_t.max)
if (data.length < to!int(s[1..$]))
for(int i = 0; i < to!int(s[1..$]) - data.length; i++) data
~= 0;
static if (Args.length == 0)
mixin(`return data[`~s[1..$]~`];`);
else static if (Args.length == 1)
mixin(`data[`~s[1..$]~`] = v[0]; `);
}
alias data this;
}
void main()
{
import std.math, std.variant;
Vector!(Algebraic!(Vector!int, int)) v;
//v.x1 = 3;
//v.x2 = 4;
//v.x3 = 5;
//writeln(v.x3);
//writeln(v.norm);
}
Trying to create a vector of vectors where any entry can be
another vector of vectors or an int.
More information about the Digitalmars-d-learn
mailing list