C++ / Why Iterators Got It All Wrong

jmh530 via Digitalmars-d digitalmars-d at puremagic.com
Thu Sep 7 05:27:19 PDT 2017


On Thursday, 7 September 2017 at 12:04:12 UTC, jmh530 wrote:
>
> I think what's missing from the documentation is a clear 
> explanation of how the strides determine how the iterator 
> moves. Even something like below (assuming I have the math 
> right) would be an improvement, though I'm sure there is a 
> clearer way to express the concept.
>
> auto x = iota(3, 4).universal;
> assert(x.strides == [4, 1]);
> assert(x[2, 3] == 6); //(2 - 1) * 4 + (3 - 1) * 1
>
> auto y = x.transposed;
> assert(y.strides == [1, 4]);
> assert(y[2, 3] == 9); //(2 - 1) * 1 + (3 - 1) * 4

Hmm, maybe I can express this better as something like

auto data = iota(12);

auto x = data.sliced(2, 3).universal;
assert(x.strides == [4, 1]);
assert(x[2, 3] == data[(2 - 1) * 4 + (3 - 1) * 1]);

auto y = x.transposed;
assert(y.strides == [1, 4]);
assert(y[2, 3] == data[(2 - 1) * 1 + (3 - 1) * 4]);


More information about the Digitalmars-d mailing list