How to dynamically calculate an index?
anonymouse
anony at mouse.com
Tue Jul 12 14:55:47 UTC 2022
On Tuesday, 12 July 2022 at 14:17:36 UTC, ananymouse wrote:
>
> Been racking my brain on this for hours. Please point me in the
> right direction.
>
> Thanks,
> --anonymouse
My current implementation:
``` d
// Allow for indexing to read a value, e.g a[0,0,0]
T opIndex(A...)(A a) {
static if(a.length == 3) {
int index = a[0] * dims[1] * dims[2] + a[2];
return data[index];
}
else static if(a.length == 2) {
return data[a[0] * dims[1] + a[1];
}
else static if (a.length == 1) {
return data[0]
}
assert(0);
}
```
This does not scale well. Therefore, I'd like to change the first
if statement to work with any array.length > 2 and dynamically
generate the code. I've tried using a foreach loop to achieve
this but failed miserably.
--anonymouse
More information about the Digitalmars-d-learn
mailing list