Created
October 14, 2013 21:13
-
-
Save Jaffe-/6982317 to your computer and use it in GitHub Desktop.
Parallel array test
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; | |
struct Test { | |
int felt1; | |
string felt2; | |
double felt3; | |
} | |
template make_fields(int SIZE, fields ...) { | |
immutable string base = typeof(fields)[0].stringof ~ "[" ~ SIZE.stringof ~ "] " ~ fields[0].stringof ~ "; "; | |
static if (fields.length == 1) | |
immutable string make_fields = base; | |
else { | |
immutable string make_fields = base ~ make_fields!(SIZE, fields[1 .. $]); | |
} | |
} | |
struct ParallelArray(S, int SIZE) { | |
mixin(make_fields!(SIZE, S.tupleof)); | |
S opIndex(size_t i) { | |
S temp; | |
foreach(j, f; this.tupleof) { | |
temp.tupleof[j] = f[i]; | |
} | |
return temp; | |
} | |
S opIndexAssign(S val, size_t i) { | |
foreach(j, ref f; this.tupleof) { | |
f[i] = val.tupleof[j]; | |
} | |
return val; | |
} | |
/* Used for foreach iteration */ | |
int opApply(int delegate(ref S) dg) { | |
int result = 0; | |
for (int i = 0; i < SIZE; i++) { | |
S temp = this[i]; | |
result = dg(temp); | |
if (result) | |
break; | |
} | |
return result; | |
} | |
} | |
int main() | |
{ | |
ParallelArray!(Test, 5) test; | |
test[0] = Test(1, "hei", 3.1); | |
test[4] = Test(2, "hallo", 4.2); | |
foreach (s; test) { | |
writefln("%d, %s, %f", s.felt1, s.felt2, s.felt3); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment