Re: How to dynamically calculate an index?

2022-07-12 Thread anonymouse via Digitalmars-d-learn
On Tuesday, 12 July 2022 at 14:55:47 UTC, anonymouse wrote: I've tried using a foreach loop to achieve this but failed miserably. --anonymouse Wait, I think I've got it. This little snippet does the trick: int index; foreach(i, v; a) { int t = v; foreach(d;

Re: How to dynamically calculate an index?

2022-07-12 Thread anonymouse via Digitalmars-d-learn
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) {

How to dynamically calculate an index?

2022-07-12 Thread ananymouse via Digitalmars-d-learn
Given a 3D matrix of shape[3, 5, 7] that is implemented using a 1D array, I can calculate the index of any element (e.g. [1, 0, 6]) as follows: index = 1 * 5 * 7 + 0 * 7 + 6 In general, if I have a K x L x M matrix, I can access index[i][j][k] by calculating i*L*M + j*M + k to yield the