Skip to content

Commit 6866437

Browse files
Merge pull request #99 from SciML/myb/cart
Support CartesianIndices for VectorOfArray
2 parents b82c855 + a8e88ff commit 6866437

File tree

4 files changed

+35
-17
lines changed

4 files changed

+35
-17
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "RecursiveArrayTools"
22
uuid = "731186ca-8d62-57ce-b412-fbd966d074cd"
33
authors = ["Chris Rackauckas <[email protected]>"]
4-
version = "2.3.3"
4+
version = "2.3.4"
55

66
[deps]
77
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"

src/array_partition.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ end
142142
@inline Base.firstindex(A::ArrayPartition) = 1
143143
@inline Base.lastindex(A::ArrayPartition) = length(A)
144144

145-
@inline function Base.getindex(A::ArrayPartition, i::Int)
145+
Base.@propagate_inbounds function Base.getindex(A::ArrayPartition, i::Int)
146146
@boundscheck checkbounds(A, i)
147147
@inbounds for j in 1:length(A.x)
148148
i -= length(A.x[j])
@@ -157,7 +157,7 @@ end
157157
158158
Return the entry at index `j...` of the `i`th partition of `A`.
159159
"""
160-
@inline function Base.getindex(A::ArrayPartition, i::Int, j...)
160+
Base.@propagate_inbounds function Base.getindex(A::ArrayPartition, i::Int, j...)
161161
@boundscheck 0 < i <= length(A.x) || throw(BoundsError(A.x, i))
162162
@inbounds b = A.x[i]
163163
@boundscheck checkbounds(b, j...)
@@ -171,7 +171,7 @@ Return vector with all elements of array partition `A`.
171171
"""
172172
Base.getindex(A::ArrayPartition{T,S}, ::Colon) where {T,S} = T[a for a in Chain(A.x)]
173173

174-
@inline function Base.setindex!(A::ArrayPartition, v, i::Int)
174+
Base.@propagate_inbounds function Base.setindex!(A::ArrayPartition, v, i::Int)
175175
@boundscheck checkbounds(A, i)
176176
@inbounds for j in 1:length(A.x)
177177
i -= length(A.x[j])
@@ -187,7 +187,7 @@ end
187187
188188
Set the entry at index `j...` of the `i`th partition of `A` to `v`.
189189
"""
190-
@inline function Base.setindex!(A::ArrayPartition, v, i::Int, j...)
190+
Base.@propagate_inbounds function Base.setindex!(A::ArrayPartition, v, i::Int, j...)
191191
@boundscheck 0 < i <= length(A.x) || throw(BoundsError(A.x, i))
192192
@inbounds b = A.x[i]
193193
@boundscheck checkbounds(b, j...)

src/vector_of_array.jl

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,38 @@ DiffEqArray(vec::AbstractVector{VT},ts::AbstractVector) where {T, N, VT<:Abstrac
3434
@inline Base.IteratorSize(VA::AbstractVectorOfArray) = Base.HasLength()
3535
# Linear indexing will be over the container elements, not the individual elements
3636
# unlike an true AbstractArray
37-
@inline Base.getindex(VA::AbstractVectorOfArray{T, N}, I::Int) where {T, N} = VA.u[I]
38-
@inline Base.getindex(VA::AbstractVectorOfArray{T, N}, I::Colon) where {T, N} = VA.u[I]
39-
@inline Base.getindex(VA::AbstractVectorOfArray{T, N}, I::AbstractArray{Int}) where {T, N} = VectorOfArray(VA.u[I])
40-
@inline Base.getindex(VA::AbstractDiffEqArray{T, N}, I::AbstractArray{Int}) where {T, N} = DiffEqArray(VA.u[I],VA.t[I])
41-
@inline Base.getindex(VA::AbstractVectorOfArray{T, N}, i::Int,::Colon) where {T, N} = [VA.u[j][i] for j in 1:length(VA)]
42-
@inline Base.setindex!(VA::AbstractVectorOfArray{T, N}, v, I::Int) where {T, N} = VA.u[I] = v
43-
@inline Base.setindex!(VA::AbstractVectorOfArray{T, N}, v, I::Colon) where {T, N} = VA.u[I] = v
44-
@inline Base.setindex!(VA::AbstractVectorOfArray{T, N}, v, I::AbstractArray{Int}) where {T, N} = VA.u[I] = v
45-
@inline function Base.setindex!(VA::AbstractVectorOfArray{T, N}, v, i::Int,::Colon) where {T, N}
37+
Base.@propagate_inbounds Base.getindex(VA::AbstractVectorOfArray{T, N}, I::Int) where {T, N} = VA.u[I]
38+
Base.@propagate_inbounds Base.getindex(VA::AbstractVectorOfArray{T, N}, I::Colon) where {T, N} = VA.u[I]
39+
Base.@propagate_inbounds Base.getindex(VA::AbstractVectorOfArray{T, N}, I::AbstractArray{Int}) where {T, N} = VectorOfArray(VA.u[I])
40+
Base.@propagate_inbounds Base.getindex(VA::AbstractDiffEqArray{T, N}, I::AbstractArray{Int}) where {T, N} = DiffEqArray(VA.u[I],VA.t[I])
41+
Base.@propagate_inbounds Base.getindex(VA::AbstractVectorOfArray{T, N}, i::Int,::Colon) where {T, N} = [VA.u[j][i] for j in 1:length(VA)]
42+
Base.@propagate_inbounds function Base.getindex(VA::AbstractVectorOfArray{T,N}, ii::CartesianIndex) where {T, N}
43+
ti = Tuple(ii)
44+
i = first(ti)
45+
jj = CartesianIndex(Base.tail(ti))
46+
return VA.u[i][jj]
47+
end
48+
Base.@propagate_inbounds Base.setindex!(VA::AbstractVectorOfArray{T, N}, v, I::Int) where {T, N} = VA.u[I] = v
49+
Base.@propagate_inbounds Base.setindex!(VA::AbstractVectorOfArray{T, N}, v, I::Colon) where {T, N} = VA.u[I] = v
50+
Base.@propagate_inbounds Base.setindex!(VA::AbstractVectorOfArray{T, N}, v, I::AbstractArray{Int}) where {T, N} = VA.u[I] = v
51+
Base.@propagate_inbounds function Base.setindex!(VA::AbstractVectorOfArray{T, N}, v, i::Int,::Colon) where {T, N}
4652
for j in 1:length(VA)
4753
VA.u[j][i] = v[j]
4854
end
55+
return v
56+
end
57+
Base.@propagate_inbounds function Base.setindex!(VA::AbstractVectorOfArray{T,N}, x, ii::CartesianIndex) where {T, N}
58+
ti = Tuple(ii)
59+
i = first(ti)
60+
jj = CartesianIndex(Base.tail(ti))
61+
return VA.u[i][jj] = x
4962
end
5063

5164
# Interface for the two dimensional indexing, a more standard AbstractArray interface
5265
@inline Base.size(VA::AbstractVectorOfArray) = (size(VA.u[1])..., length(VA.u))
53-
@inline Base.getindex(VA::AbstractVectorOfArray{T, N}, I::Int...) where {T, N} = VA.u[I[end]][Base.front(I)...]
54-
@inline Base.getindex(VA::AbstractVectorOfArray{T, N}, ::Colon, I::Int) where {T, N} = VA.u[I]
55-
@inline Base.setindex!(VA::AbstractVectorOfArray{T, N}, v, I::Int...) where {T, N} = VA.u[I[end]][Base.front(I)...] = v
66+
Base.@propagate_inbounds Base.getindex(VA::AbstractVectorOfArray{T, N}, I::Int...) where {T, N} = VA.u[I[end]][Base.front(I)...]
67+
Base.@propagate_inbounds Base.getindex(VA::AbstractVectorOfArray{T, N}, ::Colon, I::Int) where {T, N} = VA.u[I]
68+
Base.@propagate_inbounds Base.setindex!(VA::AbstractVectorOfArray{T, N}, v, I::Int...) where {T, N} = VA.u[I[end]][Base.front(I)...] = v
5669

5770
# The iterator will be over the subarrays of the container, not the individual elements
5871
# unlike an true AbstractArray

test/basic_indexing.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,8 @@ a = testva .+ rand(3,3)
8080
recs = [rand(2,2) for i in 1:5]
8181
testva = VectorOfArray(recs)
8282
@test Array(testva) isa Array{Float64,3}
83+
84+
v = VectorOfArray([zeros(20), zeros(10,10), zeros(3,3,3)])
85+
v[CartesianIndex((3, 2, 3, 2))] = 1
86+
@test v[CartesianIndex((3, 2, 3, 2))] == 1
87+
@test v.u[3][2, 3, 2] == 1

0 commit comments

Comments
 (0)