C++ / Why Iterators Got It All Wrong

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


On Thursday, 7 September 2017 at 09:40:40 UTC, Ilya Yaroshenko 
wrote:
>
> For example, lets takes `transposed` function. It does not 
> transpose the date. Instead, it swap dimensions.
> Assume you have a canonical matrix with _lengths = [3, 4]. So 
> its strides are [4]. Now we want to swap dimensions, but to do 
> it we need to swap both lengths and strides. So first we need 
> to convert a slice to universal, so it will have both strides 
> we want to swap: [4, 1]. Transposed slice will have _lengths = 
> [4, 3] and _strides = [1, 4].
>
> Best Regards,
> Ilya

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


More information about the Digitalmars-d mailing list