Skip to content

Instantly share code, notes, and snippets.

@Jaffe-
Created October 14, 2013 23:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jaffe-/6983847 to your computer and use it in GitHub Desktop.
Save Jaffe-/6983847 to your computer and use it in GitHub Desktop.
Parallel arrays implementation
import std.stdio;
struct Test {
int field1;
string field2;
double field3;
}
template make_fields(size_t SIZE, fields ...) {
immutable string field = typeof(fields)[0].stringof ~ "[" ~ SIZE.stringof ~ "] " ~ fields[0].stringof ~ "; ";
static if (fields.length == 1)
immutable string make_fields = field;
else {
immutable string make_fields = field ~ make_fields!(SIZE, fields[1 .. $]);
}
}
struct ParallelArray(S, size_t 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.field1, s.field2, s.field3);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment