digitalmars.D.bugs - [Issue 12471] New: Struct of arrays in Phobos
- d-bugmail puremagic.com (72/72) Mar 25 2014 https://d.puremagic.com/issues/show_bug.cgi?id=12471
https://d.puremagic.com/issues/show_bug.cgi?id=12471 Summary: Struct of arrays in Phobos Product: D Version: D2 Platform: All OS/Version: All Status: NEW Severity: enhancement Priority: P2 Component: DMD AssignedTo: nobody puremagic.com ReportedBy: bearophile_hugs eml.cc 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. -- Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Mar 25 2014