Skip to content

Commit 7cd946e

Browse files
Fixup some of the linear indexing
1 parent 8240745 commit 7cd946e

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

src/vector_of_array.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ function VectorOfArray(vec::AbstractVector)
1313
end
1414

1515
VectorOfArray{T, N}(vec::AbstractVector{T}, dims::NTuple{N}) = VectorOfArray{eltype(T), N, typeof(vec)}(vec, dims)
16+
17+
Base.endof(S::VectorOfArray) = endof(S.data)
1618
Base.size(S::VectorOfArray) = (size(S.data[1])..., length(S.data))
1719

1820
@inline function Base.getindex{T, N}(S::VectorOfArray{T, N}, I::Vararg{Int, N})
@@ -47,3 +49,8 @@ end
4749
Base.start{T, N}(S::VectorOfArray{T, N}) = 1
4850
Base.next{T, N}(S::VectorOfArray{T, N}, state) = (S[state], state + 1)
4951
Base.done{T, N}(S::VectorOfArray{T, N}, state) = state >= length(S.data) + 1
52+
53+
# conversion tools
54+
function vecarr_to_arr(S::VectorOfArray)
55+
return cat(length(size(S)), S.data...)
56+
end

test/basic_indexing.jl

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using RecursiveArrayTools
21

32
# Example Problem
43
recs = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
@@ -14,10 +13,10 @@ testa = cat(2, recs...)
1413
testva = VectorOfArray(recs)
1514

1615
# ## Linear indexing
17-
testva[1] == testa[:, 1]
18-
testva[:] == recs
19-
testva[end] == testa[:, end]
20-
testva[2:end] == [recs[i] for i = 2:length(recs)]
16+
@test testva[1] == testa[:, 1]
17+
@test testva[:] == recs
18+
@test testva[end] == testa[:, end]
19+
@test testva[2:end] == [recs[i] for i = 2:length(recs)]
2120

2221
# ## (Int, Int)
2322
@test testa[5, 4] == testva[5, 4]
@@ -61,3 +60,8 @@ testva = VectorOfArray(recs)
6160
@test testa[1:1, 2:3, 1:2] == testva[1:1, 2:3, 1:2]
6261

6362
# ## Test ragged arrays work, or give errors as needed
63+
#TODO: I am not really sure what the behavior of this is, what does Mathematica do?
64+
recs = [[1, 2, 3], [3 5; 6 7], [8, 9, 10, 11]]
65+
testva = VectorOfArray(recs) #TODO: clearly this printed form is nonsense
66+
@test testva[:, 1] == recs[1]
67+
testva[1:2, 1:2]

test/interface_tests.jl

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ push!(testva2, [13, 14, 15])
1717
append!(testva, testva)
1818
@test testva[1:2, 5:6] == [1 4; 2 5]
1919

20-
# Test that adding a array of different dimension triggers the ragged flag
20+
# Test that adding a array of different dimension makes the array ragged
2121
push!(testva, [-1, -2, -3, -4])
2222
#testva #TODO: this screws up printing, try to make a fallback
23-
#testva.ragged
24-
@test_throws BoundsError testva[1:2, 5:6]
23+
@test testva[1:2, 5:6] == [1 4; 2 5] # we just let the indexing happen if it works
24+
testva[4, 9] # == testva.data[9][4]
25+
@test testva[4:5, 5:6]
2526
@test testva[9] == [-1, -2, -3, -4]
26-
testva[end]
27-
testva.dims #TODO: what to do about dims when ragged?
27+
@test testva[end] == [-1, -2, -3, -4]
2828

2929
# Currently we enforce the general shape, they can just be different lengths, ie we
3030
# can't do
@@ -37,3 +37,7 @@ recs = [rand(10, 7) for i = 1:8]
3737
testva = VectorOfArray(recs)
3838
testa = cat(3, recs...)
3939
@test vecarr_to_arr(testva) == testa
40+
41+
recs = [[1, 2, 3], [3 5; 6 7], [8, 9, 10, 11]]
42+
testva = VectorOfArray(recs)
43+
vecarr_to_arr(testva)

0 commit comments

Comments
 (0)