Created
September 29, 2013 17:29
-
-
Save matklad/6754574 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import std.stdio; | |
import std.conv; | |
class Vector(T, int S) { | |
static immutable size = S; | |
T[S] elemets; | |
public this(){ | |
for(int i = 0; i < S; i++) | |
elemets[i] = i; | |
} | |
public void print(){ | |
writeln(to!string(elemets)); | |
} | |
auto concat(otherT)(otherT other) { | |
//ugly a bit, but cool | |
static immutable int new_size = otherT.size + S; | |
auto ret = new Vector!(T, new_size); | |
for(int i = 0; i < S; i++) | |
ret.elemets[i] = this.elemets[i]; | |
for(int i = 0; i < otherT.size; i++) | |
ret.elemets[i + S] = other.elemets[i]; | |
return ret; | |
} | |
Vector!(T, S) sum(otherT)(otherT other) { | |
//cool! | |
static assert(S == otherT.size); | |
auto ret = new Vector!(T, S); | |
for(int i = 0; i < S; i++) | |
ret.elemets[i] = this.elemets[i] + other.elemets[i]; | |
return ret; | |
} | |
auto takeN(int n)() { | |
//cool!! | |
static assert (n <= S); | |
auto ret = new Vector!(T, n); | |
for(int i = 0; i < n; i++) | |
ret.elemets[i] = this.elemets[i]; | |
return ret; | |
} | |
} | |
void main() { | |
auto a = new Vector!(int, 2); | |
auto b = new Vector!(int, 3); | |
auto c = new Vector!(int, 5); | |
auto d = a.concat(b); | |
a.print(); | |
b.print(); | |
c.print(); | |
d.print(); | |
version(none) { | |
//won't compile | |
auto sab = a.sum(b); | |
} | |
auto scd = c.sum(d); | |
scd.print(); | |
version(none){ | |
//won't compile | |
auto at4 = a.takeN!(4); | |
} | |
auto dt4 = d.takeN!(4); | |
dt4.print(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment