Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow dynamic indexing of array members of a record #702

Open
bernhardmgruber opened this issue Feb 11, 2023 · 0 comments
Open

Allow dynamic indexing of array members of a record #702

bernhardmgruber opened this issue Feb 11, 2023 · 0 comments

Comments

@bernhardmgruber
Copy link
Member

Given the following record dimension:

using Vec = llama::Record<
    llama::Field<tag::X, double>,
    llama::Field<tag::Y, double>
>;
using Particle = llama::Record<
    llama::Field<tag::Pos, Vec>,
    llama::Field<tag::Mass, float>,
    llama::Field<tag::Flags, bool[4]>
>;

Some users expect to be able to index the flags array. E.g. with a 1-dimensional view:

bool b2 = view[42](tag::Flags{})(2); // or [2]

However, this is a compilation error, since LLAMA does not allow dynamic indexing here. Arrays of static size inside a record are just syntactic sure for tuples with same-type elements. This e.g. allows to split off the second bool into a separate blob and producing the corresponding expressions for the memory mapping function at compile time. The right way to index is with a compile-time constant:

bool b2 = view[42](tag::Flags{})(llama::RecordCoord<2>{}); // or (2_RC)

We could make this work however, by translating runtime indices into compile time ones. A dynamic index would thus effectively call into a dispatch selecting the right instantiation of a compile-time access.

bool b2 = view[42](tag::Flags{})(i); // would switch on i and call operator() with the right RecordCoord.

This would additionally allow dynamic indexing into records with fields having the same data type. E.g. using a view of the Vec record above:

double x1 = view[42](tag::X{}); // current, static indexing
double x2 = view[42]llama::RecordCoord<0>{}); // current, static indexing
double x3 = view[42](0); // NEW: dynamic indexing, dispatch internally

This additional dispatching could easily be created using boost::mp11::mp_with_index, but definitely introduces overhead, that the compiler may optimize out again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant