Skip to content

Commit 6f24562

Browse files
Clean up the code, and remove dims
1 parent 7cd946e commit 6f24562

File tree

1 file changed

+23
-32
lines changed

1 file changed

+23
-32
lines changed

src/vector_of_array.jl

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,47 @@
1-
# Based on code from M. Bauman Stackexchange answer + Gitter discussion
1+
# Based on code from M. Bauman VAtackexchange answer + Gitter discussion
22

33
type VectorOfArray{T, N, A} <: AbstractArray{T, N}
44
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}
75
end
86

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)))
1410

15-
VectorOfArray{T, N}(vec::AbstractVector{T}, dims::NTuple{N}) = VectorOfArray{eltype(T), N, typeof(vec)}(vec, dims)
1611

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
1915

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)...]
2319
end
2420
# Linear indexing will be over the container elements, not the individual elements
2521
# 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]
2825

29-
Base.copy(S::VectorOfArray) = VectorOfArray(copy(S.data), S.dims)
26+
Base.copy(VA::VectorOfArray) = VectorOfArray(copy(VA.data), size(VA))
3027

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)
3229

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)
3831

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})
4033
for item in copy(new_item)
41-
push!(S, item)
34+
push!(VA, item)
4235
end
43-
return S
36+
return VA
4437
end
4538

4639

4740
# The iterator will be over the subarrays of the container, not the individual elements
4841
# 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
5245

5346
# 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

Comments
 (0)