11# Based on code from M. Bauman Stackexchange answer + Gitter discussion
2- type VectorOfArray{T, N, A} <: AbstractVectorOfArray{T, N}
2+ mutable struct VectorOfArray{T, N, A} <: AbstractVectorOfArray{T, N}
33 u:: A # A <: AbstractVector{<: AbstractArray{T, N - 1}}
44end
55# VectorOfArray with an added series for time
6- type DiffEqArray{T, N, A, B} <: AbstractDiffEqArray{T, N}
6+ mutable struct DiffEqArray{T, N, A, B} <: AbstractDiffEqArray{T, N}
77 u:: A # A <: AbstractVector{<: AbstractArray{T, N - 1}}
88 t:: B
99end
1010
11- VectorOfArray {T, N} (vec:: AbstractVector{T} , dims:: NTuple{N} ) = VectorOfArray {eltype(T), N, typeof(vec)} (vec)
11+ VectorOfArray (vec:: AbstractVector{T} , dims:: NTuple{N} ) where {T, N} = VectorOfArray {eltype(T), N, typeof(vec)} (vec)
1212# Assume that the first element is representative all all other elements
1313VectorOfArray (vec:: AbstractVector ) = VectorOfArray (vec, (size (vec[1 ])... , length (vec)))
1414
15- DiffEqArray {T, N} (vec:: AbstractVector{T} , ts, dims:: NTuple{N} ) = DiffEqArray {eltype(T), N, typeof(vec), typeof(ts)} (vec, ts)
15+ DiffEqArray (vec:: AbstractVector{T} , ts, dims:: NTuple{N} ) where {T, N} = DiffEqArray {eltype(T), N, typeof(vec), typeof(ts)} (vec, ts)
1616# Assume that the first element is representative all all other elements
1717DiffEqArray (vec:: AbstractVector ,ts:: AbstractVector ) = DiffEqArray (vec, ts, (size (vec[1 ])... , length (vec)))
1818
@@ -24,38 +24,38 @@ DiffEqArray(vec::AbstractVector,ts::AbstractVector) = DiffEqArray(vec, ts, (size
2424@inline Base. iteratorsize (VA:: AbstractVectorOfArray ) = Base. HasLength ()
2525# Linear indexing will be over the container elements, not the individual elements
2626# unlike an true AbstractArray
27- @inline Base. getindex {T, N} (VA:: AbstractVectorOfArray{T, N} , I:: Int ) = VA. u[I]
28- @inline Base. getindex {T, N} (VA:: AbstractVectorOfArray{T, N} , I:: Colon ) = VA. u[I]
29- @inline Base. getindex {T, N} (VA:: AbstractVectorOfArray{T, N} , I:: AbstractArray{Int} ) = VA. u[I]
30- @inline Base. getindex {T, N} (VA:: AbstractVectorOfArray{T, N} , i:: Int ,:: Colon ) = [VA. u[j][i] for j in 1 : length (VA)]
31- @inline Base. setindex! {T, N} (VA:: AbstractVectorOfArray{T, N} , v, I:: Int ) = VA. u[I] = v
32- @inline Base. setindex! {T, N} (VA:: AbstractVectorOfArray{T, N} , v, I:: Colon ) = VA. u[I] = v
33- @inline Base. setindex! {T, N} (VA:: AbstractVectorOfArray{T, N} , v, I:: AbstractArray{Int} ) = VA. u[I] = v
34- @inline function Base. setindex! {T, N} (VA:: AbstractVectorOfArray{T, N} , v, i:: Int ,:: Colon )
27+ @inline Base. getindex (VA:: AbstractVectorOfArray{T, N} , I:: Int ) where {T, N} = VA. u[I]
28+ @inline Base. getindex (VA:: AbstractVectorOfArray{T, N} , I:: Colon ) where {T, N} = VA. u[I]
29+ @inline Base. getindex (VA:: AbstractVectorOfArray{T, N} , I:: AbstractArray{Int} ) where {T, N} = VA. u[I]
30+ @inline Base. getindex (VA:: AbstractVectorOfArray{T, N} , i:: Int ,:: Colon ) where {T, N} = [VA. u[j][i] for j in 1 : length (VA)]
31+ @inline Base. setindex! (VA:: AbstractVectorOfArray{T, N} , v, I:: Int ) where {T, N} = VA. u[I] = v
32+ @inline Base. setindex! (VA:: AbstractVectorOfArray{T, N} , v, I:: Colon ) where {T, N} = VA. u[I] = v
33+ @inline Base. setindex! (VA:: AbstractVectorOfArray{T, N} , v, I:: AbstractArray{Int} ) where {T, N} = VA. u[I] = v
34+ @inline function Base. setindex! (VA:: AbstractVectorOfArray{T, N} , v, i:: Int ,:: Colon ) where {T, N}
3535 for j in 1 : length (VA)
3636 VA. u[j][i] = v[j]
3737 end
3838end
3939
4040# Interface for the two dimensional indexing, a more standard AbstractArray interface
4141@inline Base. size (VA:: AbstractVectorOfArray ) = (size (VA. u[1 ])... , length (VA. u))
42- @inline Base. getindex {T, N} (VA:: AbstractVectorOfArray{T, N} , I:: Int... ) = VA. u[I[end ]][Base. front (I)... ]
43- @inline Base. getindex {T, N} (VA:: AbstractVectorOfArray{T, N} , :: Colon , I:: Int ) = VA. u[I]
44- @inline Base. setindex! {T, N} (VA:: AbstractVectorOfArray{T, N} , v, I:: Int... ) = VA. u[I[end ]][Base. front (I)... ] = v
42+ @inline Base. getindex (VA:: AbstractVectorOfArray{T, N} , I:: Int... ) where {T, N} = VA. u[I[end ]][Base. front (I)... ]
43+ @inline Base. getindex (VA:: AbstractVectorOfArray{T, N} , :: Colon , I:: Int ) where {T, N} = VA. u[I]
44+ @inline Base. setindex! (VA:: AbstractVectorOfArray{T, N} , v, I:: Int... ) where {T, N} = VA. u[I[end ]][Base. front (I)... ] = v
4545
4646# The iterator will be over the subarrays of the container, not the individual elements
4747# unlike an true AbstractArray
48- Base. start {T, N} (VA:: AbstractVectorOfArray{T, N} ) = 1
49- Base. next {T, N} (VA:: AbstractVectorOfArray{T, N} , state) = (VA[state], state + 1 )
50- Base. done {T, N} (VA:: AbstractVectorOfArray{T, N} , state) = state >= length (VA. u) + 1
48+ Base. start (VA:: AbstractVectorOfArray{T, N} ) where {T, N} = 1
49+ Base. next (VA:: AbstractVectorOfArray{T, N} , state) where {T, N} = (VA[state], state + 1 )
50+ Base. done (VA:: AbstractVectorOfArray{T, N} , state) where {T, N} = state >= length (VA. u) + 1
5151tuples (VA:: DiffEqArray ) = tuple .(VA. t,VA. u)
5252
5353# Growing the array simply adds to the container vector
5454Base. copy (VA:: AbstractVectorOfArray ) = typeof (VA)(copy (VA. u))
55- Base. sizehint! {T, N} (VA:: AbstractVectorOfArray{T, N} , i) = sizehint! (VA. u, i)
56- Base. push! {T, N} (VA:: AbstractVectorOfArray{T, N} , new_item:: AbstractVector ) = push! (VA. u, new_item)
55+ Base. sizehint! (VA:: AbstractVectorOfArray{T, N} , i) where {T, N} = sizehint! (VA. u, i)
56+ Base. push! (VA:: AbstractVectorOfArray{T, N} , new_item:: AbstractVector ) where {T, N} = push! (VA. u, new_item)
5757
58- function Base. append! {T, N} (VA:: AbstractVectorOfArray{T, N} , new_item:: AbstractVectorOfArray{T, N} )
58+ function Base. append! (VA:: AbstractVectorOfArray{T, N} , new_item:: AbstractVectorOfArray{T, N} ) where {T, N}
5959 for item in copy (new_item)
6060 push! (VA, item)
6161 end
0 commit comments