|
1 | | -abstract AbstractVectorOfArray{T, N} <: AbstractArray{T, N} |
2 | | - |
3 | 1 | # Based on code from M. Bauman Stackexchange answer + Gitter discussion |
4 | | -type VectorOfArray{T, N, A} <: AbstractVectorOfArray{T, N} |
| 2 | + |
| 3 | +type VectorOfArray{T, N, A} <: AbstractArray{T, N} |
5 | 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 | 6 | dims::NTuple{N, Int} |
7 | | - ragged::Bool |
8 | 7 | end |
9 | 8 |
|
10 | 9 | function VectorOfArray(vec::AbstractVector) |
11 | | - # Only allow a vector of equally shaped subtypes, this will not work for ragged arrays |
12 | | - ragged = !all(size(vec[1]) == size(v) for v in vec) |
13 | | - VectorOfArray(vec, (size(vec[1])..., length(vec)), ragged) |
| 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))) |
14 | 13 | end |
15 | 14 |
|
16 | | -VectorOfArray{T, N}(vec::AbstractVector{T}, dims::NTuple{N}, ragged::Bool) = VectorOfArray{eltype(T), N, typeof(vec)}(vec, dims, ragged) |
17 | | -@inline function Base.size(S::AbstractVectorOfArray) |
18 | | - if S.ragged |
19 | | - return size(S.data) |
20 | | - else |
21 | | - return S.dims |
22 | | - end |
23 | | -end |
| 15 | +VectorOfArray{T, N}(vec::AbstractVector{T}, dims::NTuple{N}) = VectorOfArray{eltype(T), N, typeof(vec)}(vec, dims) |
| 16 | +Base.size(S::VectorOfArray) = (size(S.data[1])..., length(S.data)) |
24 | 17 |
|
25 | | -@inline function Base.getindex{T, N}(S::AbstractVectorOfArray{T, N}, I::Vararg{Int, N}) |
26 | | - @boundscheck checkbounds(S, I...) |
27 | | - S.ragged && throw(BoundsError("A ragged VectorOfArray does not support Cartesian indexing")) |
28 | | - S.data[I[end]][Base.front(I)...] |
| 18 | +@inline function Base.getindex{T, N}(S::VectorOfArray{T, N}, I::Vararg{Int, N}) |
| 19 | + @boundscheck checkbounds(S, I...) # is this needed? |
| 20 | + S.data[I[end]][Base.front(I)...] |
29 | 21 | end |
30 | 22 | # Linear indexing will be over the container elements, not the individual elements |
31 | 23 | # unlike an true AbstractArray |
32 | | -@inline Base.getindex{T, N}(S::AbstractVectorOfArray{T, N}, I::Union{Int, Colon, AbstractArray{Int}}) = S.data[I] |
| 24 | +@inline Base.getindex{T, N}(S::VectorOfArray{T, N}, I::Int) = S.data[I] |
| 25 | +@inline Base.getindex{T, N}(S::VectorOfArray{T, N}, I::AbstractArray{Int}) = S.data[I] |
33 | 26 |
|
34 | | -Base.copy(S::AbstractVectorOfArray) = VectorOfArray(copy(S.data), S.dims, S.ragged) |
| 27 | +Base.copy(S::VectorOfArray) = VectorOfArray(copy(S.data), S.dims) |
35 | 28 |
|
36 | | -Base.sizehint!{T, N}(S::AbstractVectorOfArray{T, N}, i) = sizehint!(S.data, i) |
| 29 | +Base.sizehint!{T, N}(S::VectorOfArray{T, N}, i) = sizehint!(S.data, i) |
37 | 30 |
|
38 | | -function Base.push!{T, N}(S::AbstractVectorOfArray{T, N}, new_item::AbstractArray) |
39 | | - if S.dims[1:(end - 1)] == size(new_item) |
40 | | - S.dims = (S.dims[1:(end - 1)]..., S.dims[end] + 1) |
41 | | - else |
42 | | - S.ragged = true |
43 | | - # just revert down to a vector of elements, make the dims data meaningless |
44 | | - #TODO: this is stupid ugly |
45 | | - S.dims = tuple(-ones(Int, N)...) |
46 | | - end |
47 | | - push!(S.data, copy(new_item)) |
| 31 | +function Base.push!{T, N}(S::VectorOfArray{T, N}, new_item::AbstractVector) |
| 32 | + #@assert S.dims[1:(end - 1)] == size(new_item) |
| 33 | + #S.dims = (S.dims[1:(end - 1)]..., S.dims[end] + 1) |
| 34 | + push!(S.data, new_item) |
48 | 35 | end |
49 | | -function Base.append!{T, N}(S::AbstractVectorOfArray{T, N}, new_item::AbstractVectorOfArray{T, N}) |
| 36 | + |
| 37 | +function Base.append!{T, N}(S::VectorOfArray{T, N}, new_item::VectorOfArray{T, N}) |
50 | 38 | for item in copy(new_item) |
51 | | - push!(S, item) |
| 39 | + push!(S, item) |
52 | 40 | end |
53 | 41 | return S |
54 | 42 | end |
55 | 43 |
|
| 44 | + |
56 | 45 | # The iterator will be over the subarrays of the container, not the individual elements |
57 | 46 | # unlike an true AbstractArray |
58 | | -Base.start{T, N}(S::AbstractVectorOfArray{T, N}) = 1 |
59 | | -Base.next{T, N}(S::AbstractVectorOfArray{T, N}, state) = (S[state], state + 1) |
60 | | -Base.done{T, N}(S::AbstractVectorOfArray{T, N}, state) = state >= length(S.data) + 1 |
61 | | - |
62 | | -# convert to a regular, sense array |
63 | | -function vecarr_to_arr(va::AbstractVectorOfArray) |
64 | | - va[[Colon() for d in 1:length(va.dims)]...] |
65 | | -end |
| 47 | +Base.start{T, N}(S::VectorOfArray{T, N}) = 1 |
| 48 | +Base.next{T, N}(S::VectorOfArray{T, N}, state) = (S[state], state + 1) |
| 49 | +Base.done{T, N}(S::VectorOfArray{T, N}, state) = state >= length(S.data) + 1 |
0 commit comments