Skip to content

Commit d03a5e6

Browse files
authored
Views of SArray may use getindex to avoid the SubArray wrapper (#908)
* views of SArray use indexing * move fn defn to abstractarray.jl * wrap elements in Scalar * restrict index types * allow indexing with SArrays * views for CartesianIndex, inferred in tests * bump version to v1.2.0
1 parent 8b90b9c commit d03a5e6

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

Project.toml

+1-1
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.1.3"
3+
version = "1.2.0"
44

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

src/abstractarray.jl

+10
Original file line numberDiff line numberDiff line change
@@ -295,3 +295,13 @@ if VERSION >= v"1.6.0-DEV.1334"
295295
return similar_type(typeof(a), Size(newlen))(Base.rest(Tuple(a), i + 1))
296296
end
297297
end
298+
299+
# SArrays may avoid the SubArray wrapper and consequently an additional level of indirection
300+
# The output may use the broadcasting machinery defined for StaticArrays (see issue #892)
301+
# wrap elements in Scalar to be consistent with 0D views
302+
_maybewrapscalar(S::SArray{<:Any,T}, r::T) where {T} = Scalar{T}(r)
303+
_maybewrapscalar(S, r) = r
304+
function Base.view(S::SArray, I::Union{Colon, Integer, SOneTo, StaticArray{<:Tuple, Int}, CartesianIndex}...)
305+
V = getindex(S, I...)
306+
_maybewrapscalar(S, V)
307+
end

test/SArray.jl

+10
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,16 @@
139139
@test_throws Exception m[1] = 1
140140

141141
@test Base.dataids(m) === ()
142+
143+
@test (@inferred view(m, :, :)) === m
144+
@test (@inferred view(m, :, 1)) === @SArray [11, 12]
145+
@test (@inferred view(m, SVector{2,Int}(1,2), 1)) === @SArray [11, 12]
146+
@test (@inferred view(m, SMatrix{2,2,Int}(1,2,3,4))) === m
147+
@test (@inferred view(m, SOneTo(2), 1)) === @SArray [11, 12]
148+
@test (@inferred view(m, 1, 1)) === Scalar(m[1, 1])
149+
@test (@inferred view(m, CartesianIndex(1, 1))) === Scalar(m[1, 1])
150+
@test (@inferred view(m, CartesianIndex(1, 1, 1))) === Scalar(m[1, 1])
151+
@test (@inferred view(m, 1, 1, CartesianIndex(1))) === Scalar(m[1, 1])
142152
end
143153

144154
@testset "promotion" begin

test/SVector.jl

+8
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@
8282
@test length(v) === 3
8383

8484
@test_throws Exception v[1] = 1
85+
86+
@test (@inferred view(v, :)) === v
87+
@test (@inferred view(v, 1)) === Scalar(v[1])
88+
@test (@inferred view(v, CartesianIndex(1))) === Scalar(v[1])
89+
@test (@inferred view(v, CartesianIndex(1,1))) === Scalar(v[1])
90+
@test (@inferred view(v, 1, CartesianIndex(1))) === Scalar(v[1])
91+
@test (@inferred view(v, SVector{2,Int}(1,2))) === @SArray [11, 12]
92+
@test (@inferred view(v, SOneTo(2))) === @SArray [11, 12]
8593
end
8694

8795
@testset "CartesianIndex" begin

0 commit comments

Comments
 (0)