Skip to content

Commit 2ce01ec

Browse files
Move to abstract type. Clean up tests.
1 parent 23172f3 commit 2ce01ec

File tree

2 files changed

+25
-27
lines changed

2 files changed

+25
-27
lines changed

src/vector_of_array.jl

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,44 @@
1-
# Based on code from M. Bauman Stackexchange answer + Gitter discussion
1+
abstract AbstractVectorOfArray{T, N} <: AbstractArray{T, N}
22

3-
type VectorOfArray{T, N, A} <: AbstractArray{T, N}
3+
# Based on code from M. Bauman Stackexchange answer + Gitter discussion
4+
type VectorOfArray{T, N, A} <: AbstractVectorOfArray{T, N}
45
data::A # A <: AbstractVector{<: AbstractArray{T, N - 1}}
56
end
67

78
VectorOfArray{T, N}(vec::AbstractVector{T}, dims::NTuple{N}) = VectorOfArray{eltype(T), N, typeof(vec)}(vec)
89
# Assume that the first element is representative all all other elements
910
VectorOfArray(vec::AbstractVector) = VectorOfArray(vec, (size(vec[1])..., length(vec)))
1011

11-
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
15-
16-
@inline function Base.getindex{T, N}(VA::VectorOfArray{T, N}, I::Vararg{Int, N})
17-
VA.data[I[end]][Base.front(I)...]
18-
end
12+
# Interface for the linear indexing. This is just a view of the underlying nested structure
13+
@inline Base.endof(VA::AbstractVectorOfArray) = endof(VA.data)
14+
@inline Base.length(VA::AbstractVectorOfArray) = length(VA.data)
1915
# Linear indexing will be over the container elements, not the individual elements
2016
# unlike an true AbstractArray
21-
@inline Base.getindex{T, N}(VA::VectorOfArray{T, N}, I::Int) = VA.data[I]
22-
@inline Base.getindex{T, N}(VA::VectorOfArray{T, N}, I::Colon) = VA.data[I]
23-
@inline Base.getindex{T, N}(VA::VectorOfArray{T, N}, I::AbstractArray{Int}) = VA.data[I]
17+
@inline Base.getindex{T, N}(VA::AbstractVectorOfArray{T, N}, I::Int) = VA.data[I]
18+
@inline Base.getindex{T, N}(VA::AbstractVectorOfArray{T, N}, I::Colon) = VA.data[I]
19+
@inline Base.getindex{T, N}(VA::AbstractVectorOfArray{T, N}, I::AbstractArray{Int}) = VA.data[I]
2420

25-
Base.copy(VA::VectorOfArray) = VectorOfArray(copy(VA.data), size(VA))
21+
# Interface for the two dimensional indexing, a more standard AbstractArray interface
22+
@inline Base.size(VA::AbstractVectorOfArray) = (size(VA.data[1])..., length(VA.data))
23+
@inline Base.getindex{T, N}(VA::AbstractVectorOfArray{T, N}, I::Vararg{Int, N}) = VA.data[I[end]][Base.front(I)...]
2624

27-
Base.sizehint!{T, N}(VA::VectorOfArray{T, N}, i) = sizehint!(VA.data, i)
25+
# The iterator will be over the subarrays of the container, not the individual elements
26+
# unlike an true AbstractArray
27+
Base.start{T, N}(VA::AbstractVectorOfArray{T, N}) = 1
28+
Base.next{T, N}(VA::AbstractVectorOfArray{T, N}, state) = (VA[state], state + 1)
29+
Base.done{T, N}(VA::AbstractVectorOfArray{T, N}, state) = state >= length(VA.data) + 1
2830

29-
Base.push!{T, N}(VA::VectorOfArray{T, N}, new_item::AbstractVector) = push!(VA.data, new_item)
31+
# Growing the array simply adds to the container vector
32+
Base.copy(VA::AbstractVectorOfArray) = typeof(VA)(copy(VA.data))
33+
Base.sizehint!{T, N}(VA::AbstractVectorOfArray{T, N}, i) = sizehint!(VA.data, i)
34+
Base.push!{T, N}(VA::AbstractVectorOfArray{T, N}, new_item::AbstractVector) = push!(VA.data, new_item)
3035

31-
function Base.append!{T, N}(VA::VectorOfArray{T, N}, new_item::VectorOfArray{T, N})
36+
function Base.append!{T, N}(VA::AbstractVectorOfArray{T, N}, new_item::AbstractVectorOfArray{T, N})
3237
for item in copy(new_item)
3338
push!(VA, item)
3439
end
3540
return VA
3641
end
3742

38-
39-
# The iterator will be over the subarrays of the container, not the individual elements
40-
# unlike an true AbstractArray
41-
Base.start{T, N}(VA::VectorOfArray{T, N}) = 1
42-
Base.next{T, N}(VA::VectorOfArray{T, N}, state) = (VA[state], state + 1)
43-
Base.done{T, N}(VA::VectorOfArray{T, N}, state) = state >= length(VA.data) + 1
44-
4543
# conversion tools
46-
vecarr_to_arr(VA::VectorOfArray) = cat(length(size(VA)), VA.data...)
44+
vecarr_to_arr(VA::AbstractVectorOfArray) = cat(length(size(VA)), VA.data...)

test/interface_tests.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ push!(testva, [-1, -2, -3, -4])
2222
#testva #TODO: this screws up printing, try to make a fallback
2323
@test testva[1:2, 5:6] == [1 4; 2 5] # we just let the indexing happen if it works
2424
testva[4, 9] # == testva.data[9][4]
25-
@test testva[4:5, 5:6]
25+
@test_throws BoundsError testva[4:5, 5:6]
2626
@test testva[9] == [-1, -2, -3, -4]
2727
@test testva[end] == [-1, -2, -3, -4]
2828

@@ -40,4 +40,4 @@ testa = cat(3, recs...)
4040

4141
recs = [[1, 2, 3], [3 5; 6 7], [8, 9, 10, 11]]
4242
testva = VectorOfArray(recs)
43-
vecarr_to_arr(testva)
43+
@test_throws DimensionMismatch vecarr_to_arr(testva)

0 commit comments

Comments
 (0)