Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "RecursiveArrayTools"
uuid = "731186ca-8d62-57ce-b412-fbd966d074cd"
authors = ["Chris Rackauckas <accounts@chrisrackauckas.com>"]
version = "2.3.3"
version = "2.3.4"

[deps]
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
Expand Down
8 changes: 4 additions & 4 deletions src/array_partition.jl
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ end
@inline Base.firstindex(A::ArrayPartition) = 1
@inline Base.lastindex(A::ArrayPartition) = length(A)

@inline function Base.getindex(A::ArrayPartition, i::Int)
Base.@propagate_inbounds function Base.getindex(A::ArrayPartition, i::Int)
@boundscheck checkbounds(A, i)
@inbounds for j in 1:length(A.x)
i -= length(A.x[j])
Expand All @@ -157,7 +157,7 @@ end

Return the entry at index `j...` of the `i`th partition of `A`.
"""
@inline function Base.getindex(A::ArrayPartition, i::Int, j...)
Base.@propagate_inbounds function Base.getindex(A::ArrayPartition, i::Int, j...)
@boundscheck 0 < i <= length(A.x) || throw(BoundsError(A.x, i))
@inbounds b = A.x[i]
@boundscheck checkbounds(b, j...)
Expand All @@ -171,7 +171,7 @@ Return vector with all elements of array partition `A`.
"""
Base.getindex(A::ArrayPartition{T,S}, ::Colon) where {T,S} = T[a for a in Chain(A.x)]

@inline function Base.setindex!(A::ArrayPartition, v, i::Int)
Base.@propagate_inbounds function Base.setindex!(A::ArrayPartition, v, i::Int)
@boundscheck checkbounds(A, i)
@inbounds for j in 1:length(A.x)
i -= length(A.x[j])
Expand All @@ -187,7 +187,7 @@ end

Set the entry at index `j...` of the `i`th partition of `A` to `v`.
"""
@inline function Base.setindex!(A::ArrayPartition, v, i::Int, j...)
Base.@propagate_inbounds function Base.setindex!(A::ArrayPartition, v, i::Int, j...)
@boundscheck 0 < i <= length(A.x) || throw(BoundsError(A.x, i))
@inbounds b = A.x[i]
@boundscheck checkbounds(b, j...)
Expand Down
37 changes: 25 additions & 12 deletions src/vector_of_array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,38 @@ DiffEqArray(vec::AbstractVector{VT},ts::AbstractVector) where {T, N, VT<:Abstrac
@inline Base.IteratorSize(VA::AbstractVectorOfArray) = Base.HasLength()
# Linear indexing will be over the container elements, not the individual elements
# unlike an true AbstractArray
@inline Base.getindex(VA::AbstractVectorOfArray{T, N}, I::Int) where {T, N} = VA.u[I]
@inline Base.getindex(VA::AbstractVectorOfArray{T, N}, I::Colon) where {T, N} = VA.u[I]
@inline Base.getindex(VA::AbstractVectorOfArray{T, N}, I::AbstractArray{Int}) where {T, N} = VectorOfArray(VA.u[I])
@inline Base.getindex(VA::AbstractDiffEqArray{T, N}, I::AbstractArray{Int}) where {T, N} = DiffEqArray(VA.u[I],VA.t[I])
@inline Base.getindex(VA::AbstractVectorOfArray{T, N}, i::Int,::Colon) where {T, N} = [VA.u[j][i] for j in 1:length(VA)]
@inline Base.setindex!(VA::AbstractVectorOfArray{T, N}, v, I::Int) where {T, N} = VA.u[I] = v
@inline Base.setindex!(VA::AbstractVectorOfArray{T, N}, v, I::Colon) where {T, N} = VA.u[I] = v
@inline Base.setindex!(VA::AbstractVectorOfArray{T, N}, v, I::AbstractArray{Int}) where {T, N} = VA.u[I] = v
@inline function Base.setindex!(VA::AbstractVectorOfArray{T, N}, v, i::Int,::Colon) where {T, N}
Base.@propagate_inbounds Base.getindex(VA::AbstractVectorOfArray{T, N}, I::Int) where {T, N} = VA.u[I]
Base.@propagate_inbounds Base.getindex(VA::AbstractVectorOfArray{T, N}, I::Colon) where {T, N} = VA.u[I]
Base.@propagate_inbounds Base.getindex(VA::AbstractVectorOfArray{T, N}, I::AbstractArray{Int}) where {T, N} = VectorOfArray(VA.u[I])
Base.@propagate_inbounds Base.getindex(VA::AbstractDiffEqArray{T, N}, I::AbstractArray{Int}) where {T, N} = DiffEqArray(VA.u[I],VA.t[I])
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)]
Base.@propagate_inbounds function Base.getindex(VA::AbstractVectorOfArray{T,N}, ii::CartesianIndex) where {T, N}
ti = Tuple(ii)
i = first(ti)
jj = CartesianIndex(Base.tail(ti))
return VA.u[i][jj]
end
Base.@propagate_inbounds Base.setindex!(VA::AbstractVectorOfArray{T, N}, v, I::Int) where {T, N} = VA.u[I] = v
Base.@propagate_inbounds Base.setindex!(VA::AbstractVectorOfArray{T, N}, v, I::Colon) where {T, N} = VA.u[I] = v
Base.@propagate_inbounds Base.setindex!(VA::AbstractVectorOfArray{T, N}, v, I::AbstractArray{Int}) where {T, N} = VA.u[I] = v
Base.@propagate_inbounds function Base.setindex!(VA::AbstractVectorOfArray{T, N}, v, i::Int,::Colon) where {T, N}
for j in 1:length(VA)
VA.u[j][i] = v[j]
end
return v
end
Base.@propagate_inbounds function Base.setindex!(VA::AbstractVectorOfArray{T,N}, x, ii::CartesianIndex) where {T, N}
ti = Tuple(ii)
i = first(ti)
jj = CartesianIndex(Base.tail(ti))
return VA.u[i][jj] = x
end

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

# The iterator will be over the subarrays of the container, not the individual elements
# unlike an true AbstractArray
Expand Down
5 changes: 5 additions & 0 deletions test/basic_indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,8 @@ a = testva .+ rand(3,3)
recs = [rand(2,2) for i in 1:5]
testva = VectorOfArray(recs)
@test Array(testva) isa Array{Float64,3}

v = VectorOfArray([zeros(20), zeros(10,10), zeros(3,3,3)])
v[CartesianIndex((3, 2, 3, 2))] = 1
@test v[CartesianIndex((3, 2, 3, 2))] == 1
Copy link
Contributor

Choose a reason for hiding this comment

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

The testing here is insufficient. The abstract array contract is violated

julia> IndexStyle(v)
IndexCartesian()

julia> v[CartesianIndex(size(v))]
ERROR: BoundsError: attempt to access 3-element Array{Array{Float64,N} where N,1} at index [20]
Stacktrace:
 [1] getindex at ./array.jl:788 [inlined]
 [2] getindex(::VectorOfArray{Float64,2,Array{Array{Float64,N} where N,1}}, ::CartesianIndex{2}) at /Users/andreasnoack/.julia/packages/RecursiveArrayTools/Wx7AV/src/vector_of_array.jl:46
 [3] top-level scope at REPL[124]:1

Hasn't this change broken a lot of DiffEq tests?

julia> x = [rand(2) for i in 1:3]
3-element Array{Array{Float64,1},1}:
 [0.9852201256045343, 0.32847272010896966]
 [0.9368428797761215, 0.9223074651115168]
 [0.2577978464729409, 0.9482205432256972]

julia> xde = RecursiveArrayTools.DiffEqArray(x, [1.0, 2.0, 3.0])
t: 3-element Array{Float64,1}:
 1.0
 2.0
 3.0
u: 3-element Array{Array{Float64,1},1}:
 [0.9852201256045343, 0.32847272010896966]
 [0.9368428797761215, 0.9223074651115168]
 [0.2577978464729409, 0.9482205432256972]

julia> hcat(x...)
2×3 Array{Float64,2}:
 0.98522   0.936843  0.257798
 0.328473  0.922307  0.948221

julia> hcat(x...) - xde
2×3 Array{Float64,2}:
  0.0      0.60837  0.257798
 -0.60837  0.0      0.948221

and it gives bounds errors if bounds checking is enabled.

Also, the show method lies about the type

julia> v = VectorOfArray([zeros(20), zeros(10,10), zeros(3,3,3)])
3-element Array{Array{Float64,N} where N,1}:
...

Copy link
Member

Choose a reason for hiding this comment

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

It broke a ton of downstream tests. It'll get worked on today.

@test v.u[3][2, 3, 2] == 1