data-oriented struct abstraction ?

via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat May 30 07:23:36 PDT 2015


Something like this?

     struct S {
        static int[] a_;
        static float[] b_;
        const size_t idx;
        @property ref int   a() { return a_[idx]; }
        @property ref float b() { return b_[idx]; }
        void opAssign(const S other) {
            a_[idx] = a_[other.idx];
            b_[idx] = b_[other.idx];
        }
        // opSliceAssign() ...
     }

     // ... initialize S.a_ and S.b_ here ...
     auto s = S(200); // 200th entry
     writefln("a = %s, b = %s", s.a, s.b);

You can then simply iterate over S.a_ directly. With some UDA and 
mixin template magic, the accessors can be generated 
automatically.

Of course, depending on what you're actually trying to do with 
it, other abstractions may be more useful to support your typical 
access patterns.


More information about the Digitalmars-d-learn mailing list