|
1 | | -# Based on code from M. Bauman Stackexchange answer + Gitter discussion |
| 1 | +# Based on code from M. Bauman VAtackexchange answer + Gitter discussion |
2 | 2 |
|
3 | 3 | type VectorOfArray{T, N, A} <: AbstractArray{T, N} |
4 | 4 | data::A # A <: AbstractVector{<: AbstractArray{T, N - 1}} |
5 | | - #TODO: I don't really use dims, how do I get rid of it, and still have everything work? |
6 | | - dims::NTuple{N, Int} |
7 | 5 | end |
8 | 6 |
|
9 | | -function VectorOfArray(vec::AbstractVector) |
10 | | - # Only allow a vector of equally shaped subtypes, this will not work for ragged arrays |
11 | | - #@assert all(size(vec[1]) == size(v) for v in vec) |
12 | | - VectorOfArray(vec, (size(vec[1])..., length(vec))) |
13 | | -end |
| 7 | +VectorOfArray{T, N}(vec::AbstractVector{T}, dims::NTuple{N}) = VectorOfArray{eltype(T), N, typeof(vec)}(vec) |
| 8 | +# Assume that the first element is representative all all other elements |
| 9 | +VectorOfArray(vec::AbstractVector) = VectorOfArray(vec, (size(vec[1])..., length(vec))) |
14 | 10 |
|
15 | | -VectorOfArray{T, N}(vec::AbstractVector{T}, dims::NTuple{N}) = VectorOfArray{eltype(T), N, typeof(vec)}(vec, dims) |
16 | 11 |
|
17 | | -Base.endof(S::VectorOfArray) = endof(S.data) |
18 | | -Base.size(S::VectorOfArray) = (size(S.data[1])..., length(S.data)) |
| 12 | +Base.endof(VA::VectorOfArray) = endof(VA.data) |
| 13 | +Base.size(VA::VectorOfArray) = (size(VA.data[1])..., length(VA.data)) |
| 14 | +#TODO: should we redefine length to be over the VA.data? Currently it is the number of total elements |
19 | 15 |
|
20 | | -@inline function Base.getindex{T, N}(S::VectorOfArray{T, N}, I::Vararg{Int, N}) |
21 | | - @boundscheck checkbounds(S, I...) # is this needed? |
22 | | - S.data[I[end]][Base.front(I)...] |
| 16 | +@inline function Base.getindex{T, N}(VA::VectorOfArray{T, N}, I::Vararg{Int, N}) |
| 17 | + @boundscheck checkbounds(VA, I...) # is this needed? |
| 18 | + VA.data[I[end]][Base.front(I)...] |
23 | 19 | end |
24 | 20 | # Linear indexing will be over the container elements, not the individual elements |
25 | 21 | # unlike an true AbstractArray |
26 | | -@inline Base.getindex{T, N}(S::VectorOfArray{T, N}, I::Int) = S.data[I] |
27 | | -@inline Base.getindex{T, N}(S::VectorOfArray{T, N}, I::AbstractArray{Int}) = S.data[I] |
| 22 | +@inline Base.getindex{T, N}(VA::VectorOfArray{T, N}, I::Int) = VA.data[I] |
| 23 | +@inline Base.getindex{T, N}(VA::VectorOfArray{T, N}, I::Colon) = VA.data[I] |
| 24 | +@inline Base.getindex{T, N}(VA::VectorOfArray{T, N}, I::AbstractArray{Int}) = VA.data[I] |
28 | 25 |
|
29 | | -Base.copy(S::VectorOfArray) = VectorOfArray(copy(S.data), S.dims) |
| 26 | +Base.copy(VA::VectorOfArray) = VectorOfArray(copy(VA.data), size(VA)) |
30 | 27 |
|
31 | | -Base.sizehint!{T, N}(S::VectorOfArray{T, N}, i) = sizehint!(S.data, i) |
| 28 | +Base.sizehint!{T, N}(VA::VectorOfArray{T, N}, i) = sizehint!(VA.data, i) |
32 | 29 |
|
33 | | -function Base.push!{T, N}(S::VectorOfArray{T, N}, new_item::AbstractVector) |
34 | | - #@assert S.dims[1:(end - 1)] == size(new_item) |
35 | | - #S.dims = (S.dims[1:(end - 1)]..., S.dims[end] + 1) |
36 | | - push!(S.data, new_item) |
37 | | -end |
| 30 | +Base.push!{T, N}(VA::VectorOfArray{T, N}, new_item::AbstractVector) = push!(VA.data, new_item) |
38 | 31 |
|
39 | | -function Base.append!{T, N}(S::VectorOfArray{T, N}, new_item::VectorOfArray{T, N}) |
| 32 | +function Base.append!{T, N}(VA::VectorOfArray{T, N}, new_item::VectorOfArray{T, N}) |
40 | 33 | for item in copy(new_item) |
41 | | - push!(S, item) |
| 34 | + push!(VA, item) |
42 | 35 | end |
43 | | - return S |
| 36 | + return VA |
44 | 37 | end |
45 | 38 |
|
46 | 39 |
|
47 | 40 | # The iterator will be over the subarrays of the container, not the individual elements |
48 | 41 | # unlike an true AbstractArray |
49 | | -Base.start{T, N}(S::VectorOfArray{T, N}) = 1 |
50 | | -Base.next{T, N}(S::VectorOfArray{T, N}, state) = (S[state], state + 1) |
51 | | -Base.done{T, N}(S::VectorOfArray{T, N}, state) = state >= length(S.data) + 1 |
| 42 | +Base.start{T, N}(VA::VectorOfArray{T, N}) = 1 |
| 43 | +Base.next{T, N}(VA::VectorOfArray{T, N}, state) = (VA[state], state + 1) |
| 44 | +Base.done{T, N}(VA::VectorOfArray{T, N}, state) = state >= length(VA.data) + 1 |
52 | 45 |
|
53 | 46 | # conversion tools |
54 | | -function vecarr_to_arr(S::VectorOfArray) |
55 | | - return cat(length(size(S)), S.data...) |
56 | | -end |
| 47 | +vecarr_to_arr(VA::VectorOfArray) = cat(length(size(VA)), VA.data...) |
0 commit comments