Skip to content

Commit 2724123

Browse files
committed
views for CartesianIndex, inferred in tests
1 parent 673adf4 commit 2724123

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

src/abstractarray.jl

+4-1
Original file line numberDiff line numberDiff line change
@@ -301,4 +301,7 @@ end
301301
# wrap elements in Scalar to be consistent with 0D views
302302
_maybewrapscalar(S::SArray{<:Any,T}, r::T) where {T} = Scalar{T}(r)
303303
_maybewrapscalar(S, r) = r
304-
Base.view(S::SArray, I::Union{Colon, Integer, SOneTo, StaticArray{<:Tuple, Int}}...) = _maybewrapscalar(S, getindex(S, I...))
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

+9-6
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,15 @@
140140

141141
@test Base.dataids(m) === ()
142142

143-
@test (@view m[:, :]) === m
144-
@test (@view m[:, 1]) === @SArray [11, 12]
145-
@test (@view m[SVector{2,Int}(1,2), 1]) === @SArray [11, 12]
146-
@test (@view m[SMatrix{2,2,Int}(1,2,3,4)]) === m
147-
@test (@view m[SOneTo(2), 1]) === @SArray [11, 12]
148-
@test (@view m[1, 1])[] === m[1, 1]
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])
149152
end
150153

151154
@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)