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

Indexing SOneTo with IdentityUnitRange preserves indices #922

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

jishnub
Copy link
Member

@jishnub jishnub commented Jun 14, 2021

On master:

julia> SOneTo(3)[Base.IdentityUnitRange(2:3)]
2:3

After this PR:

julia> SOneTo(3)[Base.IdentityUnitRange(2:3)]
Base.IdentityUnitRange(2:3)

This makes it consistent with julia's vector indexing, eg. for Base.OneTo (JuliaLang/julia#40997)

@jishnub
Copy link
Member Author

jishnub commented Jun 14, 2021

After 47ce9a0:

julia> sm = @SMatrix [1 3; 2 4]
2×2 SMatrix{2, 2, Int64, 4} with indices SOneTo(2)×SOneTo(2):
 1  3
 2  4

julia> sm[SOneTo(length(sm))]
4-element SVector{4, Int64} with indices SOneTo(4):
 1
 2
 3
 4

julia> view(sm, map(Base.IdentityUnitRange, axes(sm))...) |> axes
(SOneTo(2), SOneTo(2))

@jishnub
Copy link
Member Author

jishnub commented Jun 14, 2021

After c53a671 vector indexing may reshape a StaticArray if necessary

julia> sm = @SMatrix [1 3; 2 4]
2×2 SMatrix{2, 2, Int64, 4} with indices SOneTo(2)×SOneTo(2):
 1  3
 2  4

julia> sm[:, SOneTo(1), 1, SOneTo(1)]
2×1×1 SArray{Tuple{2, 1, 1}, Int64, 3, 2} with indices SOneTo(2)×SOneTo(1)×SOneTo(1):
[:, :, 1] =
 1
 2

One side-effect is that the error message for out-of-bounds indexing becomes a bit confusing as we don't perform the bounds checking at the top level:

julia> sm[:, SOneTo(1), 1, SOneTo(2)]
ERROR: BoundsError: attempt to access 2×2×1×1 SArray{Tuple{2, 2, 1, 1}, Int64, 4, 4} with indices SOneTo(2)×SOneTo(2)×SOneTo(1)×SOneTo(1) at index [1, 1, 1, 2]

However this may be changed.

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

Successfully merging this pull request may close these issues.

None yet

1 participant