Skip to content

Commit d80455d

Browse files
authored
Improved StaticIndexing (issue JuliaArrays#878) (JuliaArrays#879)
* Improved StaticIndexing (issue JuliaArrays#878) * improve coverage * fix test on 32-bit systems * add subtyping * fix * disambiguation * implement size * don't `<:AbstractVector` for now * remove disambiguation
1 parent 217e6f1 commit d80455d

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "StaticArrays"
22
uuid = "90137ffa-7385-5640-81b9-e52037218182"
3-
version = "1.6.4"
3+
version = "1.6.5"
44

55
[deps]
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

src/indexing.jl

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,40 @@ function Base.to_indices(A, I::Tuple{Vararg{Union{Integer, CartesianIndex, Stati
220220
return map(StaticIndexing, inds)
221221
end
222222

223+
# Overloading getindex, size, iterate, lastindex and to_index is necessary to support
224+
# external operations that want to use to_indices on a StaticArray (see issue #878)
225+
226+
function getindex(ind::StaticIndexing, i::Int)
227+
return ind.ind[i]
228+
end
229+
230+
function Base.size(ind::StaticIndexing)
231+
return size(ind.ind)
232+
end
233+
234+
function Base.length(ind::StaticIndexing)
235+
return length(ind.ind)
236+
end
237+
238+
function Base.iterate(ind::StaticIndexing)
239+
return iterate(ind.ind)
240+
end
241+
function Base.iterate(ind::StaticIndexing, state)
242+
return iterate(ind.ind, state)
243+
end
244+
245+
function Base.firstindex(ind::StaticIndexing)
246+
return firstindex(ind.ind)
247+
end
248+
249+
function Base.lastindex(ind::StaticIndexing)
250+
return lastindex(ind.ind)
251+
end
252+
253+
function Base.to_index(ind::StaticIndexing)
254+
return Base.to_index(ind.ind)
255+
end
256+
223257
# getindex
224258

225259
@propagate_inbounds function getindex(a::StaticArray, inds::Union{Int, StaticArray{<:Tuple, Int}, SOneTo, Colon}...)

test/indexing.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,4 +237,20 @@ using StaticArrays, Test
237237
@test eltype(Bvv) == Int
238238
@test Bvv[:] == [B[1,2,3,4], B[1,1,3,4]]
239239
end
240+
241+
@testset "Supporting external code calling to_indices on StaticArray (issue #878)" begin
242+
a = @SArray randn(2, 3, 4)
243+
ind = to_indices(a, (CartesianIndex(1, 2), SA[2, 3]))
244+
@test ind[1] === StaticArrays.StaticIndexing(1)
245+
@test ind[3][2] === 3
246+
@test (@inferred Base.to_index(ind[1])) === 1
247+
@test (@inferred Base.to_index(ind[2])) === 2
248+
@test (@inferred Base.to_index(ind[3])) === SA[2, 3]
249+
@test (ind[3]...,) === (2, 3)
250+
@test (@inferred length(ind[3])) === 2
251+
@test length(ind) === 3
252+
@test firstindex(ind[3]) === 1
253+
@test lastindex(ind[3]) === 2
254+
@test size(ind[3]) === (2,)
255+
end
240256
end

0 commit comments

Comments
 (0)