Comment #0 by bearophile_hugs — 2014-03-25T15:36:07Z
I suggest to add to Phobos a struct constructor that could be named "StructOfArrays".
Given a POD struct like:
struct Foo {
int a, b, c;
}
You can use StructOfArrays to define:
StructOfArrays!Foo sfoo1;
A more complex usage example, using a first draft of API (an alternative usage syntax is with mixins):
StructOfArrays!(Foo, "a", "b c") sfoo2;
void main() {
sfoo2.a ~= 10;
sfoo2.b ~= 20;
sfoo2 ~= Foo(1, 2, 3);
}
The basic StructOfArrays!Foo usage is similar to defining a struct like this, that contains dynamic arrays for each field of Foo:
struct __SOA {
int[] soa_a;
int[] soa_b;
int[] soa_c;
// Several iteration and access methods here.
}
While the grouping one:
StructOfArrays!(Foo, "a", "b c")
Means:
struct __SOA {
int[] soa_a;
Tuple!(int, int)[] soa_b_c;
// Several iteration and access methods here.
}
The automatically defined "iteration methods" allow to iterate on fields, assign/append a struct (like in the "sfoo2 ~= Foo(1, 2, 3);" example), and so on.
The template verifies all fields are specified in the strings. Duplicated fields could be accepted for performance reasons, but a first implementation doesn't need this feature.
An additional syntax could be used to ignore some fields from the struct:
struct Bar {
int a, b, c, d;
}
StructOfArrays!(Bar, "a", "b d") sbar1;
The assignment to the c field is of course statically forbidden. But assigning a whole Bar could be accepted:
sbar1 ~= Bar(1, 2, 3, 4); // The value "3" is ignored and not copied.
More info on the performance advantages of using a struct of arrays instead of an array of structs in some cases:
http://channel9.msdn.com/Events/Build/2013/4-329
See for doing something similar in C++:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3814.html
The ideas presented here are just a first draft. Probably something better could be invented.
The struct suggested here is not meant to cover all use cases, it's a simple mean to implement an efficient and simple data structure. More specialized data structures are better implemented manually.
Comment #1 by robert.schadek — 2024-12-01T16:20:41Z