data-oriented struct abstraction ?
short2cave via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat May 30 08:06:15 PDT 2015
On Saturday, 30 May 2015 at 14:23:37 UTC, Marc Schütz wrote:
> 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.
Yes, kind of. The template should be able to generate the
accessors using the fields of the struct passed as template
argument. But also a special accessor wich returns all the items
accessors, in a structured way:
---
struct Model
{
uint a, b;
}
template Structured(T)
{
alias Structured = ...;
}
---
allowing things like:
---
Structured!Model sm;
sm[0].a = 0;
sm[0].b = 0;
auto item = sm[0];
sm.a = 0;
sm.pushBack;
auto item = sm.back;
sm.length += 1,
---
etc.
More information about the Digitalmars-d-learn
mailing list