www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How can this best be done?

reply "Johan Fjeldtvedt" <jaffe1 gmail.com> writes:
This post concerns the following code:

https://gist.github.com/Jaffe-/6983847

It is an attempt to abstract away the details of storing a 
collection of struct elements as so-called parallel arrays 
(http://en.wikipedia.org/wiki/Parallel_array) instead of storing 
them sequentially.

The ParallelArray template takes a struct, S, and a number of 
elements, SIZE, and creates a struct where each field is an array 
of size SIZE, for each field of S. The struct overloads opIndex, 
opIndexAssign and opApply, so that it can be used just like an 
ordinary array of structs.

It's pretty neat and works fine, but I feel it's somewhat hacky. 
Is it okay to use .tupleof the way I do here? It does work, but I 
can't say I understand exactly how. Also, the recursive template 
bugs me. Can the code which declares the struct members be 
generated in some better way? I've tried writing a CTFE-able 
function to build the string instead of doing it recursively in 
the template, but I haven't been able to do it yet.
Oct 14 2013
parent "bearophile" <bearophileHUGS lycos.com> writes:
Johan Fjeldtvedt:

 https://gist.github.com/Jaffe-/6983847
I think something like that should go in Phobos. But I'd like some improvements: - Perhaps you can use a syntax similar to the tuples one: ParallelStaticArray!(int,"field1", string,"field2", double,"field3", 5) test1; auto test2 = ParallelDynArray!(int,"field1", string,"field2", double,"field3")(5); A version is meant to have fixed size and the other is more like a dynamic array. - I think it's useful to have a syntax to "group" fields, so they are represented as parallel arrays of smaller structs instead of just parallel arrays. This is useful if you have to read/write fields in a coherent way. - I think this data structure should have a basic capability to analyse its usage patterns (this feature is off on default and can be switched on with a template argument). This is useful to tune the grouping of fields. - Iteration is a bit tricky. Probably it's a good idea to iterate only on one group at a time. Bye, bearophile
Oct 14 2013