1
1
# 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}
3
3
u:: A # A <: AbstractVector{<: AbstractArray{T, N - 1}}
4
4
end
5
5
# 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}
7
7
u:: A # A <: AbstractVector{<: AbstractArray{T, N - 1}}
8
8
t:: B
9
9
end
10
10
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)
12
12
# Assume that the first element is representative all all other elements
13
13
VectorOfArray (vec:: AbstractVector ) = VectorOfArray (vec, (size (vec[1 ])... , length (vec)))
14
14
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)
16
16
# Assume that the first element is representative all all other elements
17
17
DiffEqArray (vec:: AbstractVector ,ts:: AbstractVector ) = DiffEqArray (vec, ts, (size (vec[1 ])... , length (vec)))
18
18
@@ -24,38 +24,38 @@ DiffEqArray(vec::AbstractVector,ts::AbstractVector) = DiffEqArray(vec, ts, (size
24
24
@inline Base. iteratorsize (VA:: AbstractVectorOfArray ) = Base. HasLength ()
25
25
# Linear indexing will be over the container elements, not the individual elements
26
26
# 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}
35
35
for j in 1 : length (VA)
36
36
VA. u[j][i] = v[j]
37
37
end
38
38
end
39
39
40
40
# Interface for the two dimensional indexing, a more standard AbstractArray interface
41
41
@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
45
45
46
46
# The iterator will be over the subarrays of the container, not the individual elements
47
47
# 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
51
51
tuples (VA:: DiffEqArray ) = tuple .(VA. t,VA. u)
52
52
53
53
# Growing the array simply adds to the container vector
54
54
Base. 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)
57
57
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}
59
59
for item in copy (new_item)
60
60
push! (VA, item)
61
61
end
0 commit comments