Skip to content

Commit

Permalink
Improved StaticIndexing (issue #878) (#879)
Browse files Browse the repository at this point in the history
* Improved StaticIndexing (issue #878)

* improve coverage

* fix test on 32-bit systems

* add subtyping

* fix

* disambiguation

* implement size

* don't `<:AbstractVector` for now

* remove disambiguation
  • Loading branch information
mateuszbaran authored Sep 28, 2023
1 parent 217e6f1 commit d80455d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "StaticArrays"
uuid = "90137ffa-7385-5640-81b9-e52037218182"
version = "1.6.4"
version = "1.6.5"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
34 changes: 34 additions & 0 deletions src/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,40 @@ function Base.to_indices(A, I::Tuple{Vararg{Union{Integer, CartesianIndex, Stati
return map(StaticIndexing, inds)
end

# Overloading getindex, size, iterate, lastindex and to_index is necessary to support
# external operations that want to use to_indices on a StaticArray (see issue #878)

function getindex(ind::StaticIndexing, i::Int)
return ind.ind[i]
end

function Base.size(ind::StaticIndexing)
return size(ind.ind)
end

function Base.length(ind::StaticIndexing)
return length(ind.ind)
end

function Base.iterate(ind::StaticIndexing)
return iterate(ind.ind)
end
function Base.iterate(ind::StaticIndexing, state)
return iterate(ind.ind, state)
end

function Base.firstindex(ind::StaticIndexing)
return firstindex(ind.ind)
end

function Base.lastindex(ind::StaticIndexing)
return lastindex(ind.ind)
end

function Base.to_index(ind::StaticIndexing)
return Base.to_index(ind.ind)
end

# getindex

@propagate_inbounds function getindex(a::StaticArray, inds::Union{Int, StaticArray{<:Tuple, Int}, SOneTo, Colon}...)
Expand Down
16 changes: 16 additions & 0 deletions test/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,20 @@ using StaticArrays, Test
@test eltype(Bvv) == Int
@test Bvv[:] == [B[1,2,3,4], B[1,1,3,4]]
end

@testset "Supporting external code calling to_indices on StaticArray (issue #878)" begin
a = @SArray randn(2, 3, 4)
ind = to_indices(a, (CartesianIndex(1, 2), SA[2, 3]))
@test ind[1] === StaticArrays.StaticIndexing(1)
@test ind[3][2] === 3
@test (@inferred Base.to_index(ind[1])) === 1
@test (@inferred Base.to_index(ind[2])) === 2
@test (@inferred Base.to_index(ind[3])) === SA[2, 3]
@test (ind[3]...,) === (2, 3)
@test (@inferred length(ind[3])) === 2
@test length(ind) === 3
@test firstindex(ind[3]) === 1
@test lastindex(ind[3]) === 2
@test size(ind[3]) === (2,)
end
end

2 comments on commit d80455d

@mateuszbaran
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/92406

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.6.5 -m "<description of version>" d80455d2e1aba57a6cf65cc8d787dc6474ec3980
git push origin v1.6.5

Please sign in to comment.