Skip to content

Commit 3710526

Browse files
Merge pull request #494 from ChrisRackauckas-Claude/fix-heterogeneous-view-issue-453
Fix view of VectorOfArray with heterogeneous array sizes
2 parents 9193003 + 63c3de8 commit 3710526

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/vector_of_array.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,15 @@ function Base.view(A::AbstractVectorOfArray{T, N, <:AbstractVector{T}},
675675
end
676676
function Base.view(A::AbstractVectorOfArray, I::Vararg{Any, M}) where {M}
677677
@inline
678+
# Special handling for heterogeneous arrays when viewing a single column
679+
# The issue is that to_indices uses axes, which is based on the first element's size
680+
# For heterogeneous arrays, we need to use the actual size of the specific column
681+
if length(I) == 2 && I[1] == Colon() && I[2] isa Int
682+
@boundscheck checkbounds(A.u, I[2])
683+
# Use the actual size of the specific column instead of relying on axes/to_indices
684+
J = (Base.OneTo(length(A.u[I[2]])), I[2])
685+
return SubArray(A, J)
686+
end
678687
J = map(i -> Base.unalias(A, i), to_indices(A, I))
679688
@boundscheck checkbounds(A, J...)
680689
SubArray(A, J)

test/basic_indexing.jl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,31 @@ diffeq = DiffEqArray(recs, t)
145145
@test diffeq[:, 1] == recs[1]
146146
@test diffeq[1:2, 1:2] == [1 3; 2 5]
147147

148+
# Test views of heterogeneous arrays (issue #453)
149+
f = VectorOfArray([[1.0], [2.0, 3.0]])
150+
@test length(view(f, :, 1)) == 1
151+
@test length(view(f, :, 2)) == 2
152+
@test view(f, :, 1) == [1.0]
153+
@test view(f, :, 2) == [2.0, 3.0]
154+
@test collect(view(f, :, 1)) == f[:, 1]
155+
@test collect(view(f, :, 2)) == f[:, 2]
156+
157+
f2 = VectorOfArray([[1.0, 2.0], [3.0]])
158+
@test length(view(f2, :, 1)) == 2
159+
@test length(view(f2, :, 2)) == 1
160+
@test view(f2, :, 1) == [1.0, 2.0]
161+
@test view(f2, :, 2) == [3.0]
162+
@test collect(view(f2, :, 1)) == f2[:, 1]
163+
@test collect(view(f2, :, 2)) == f2[:, 2]
164+
165+
# Test that views can be modified
166+
f3 = VectorOfArray([[1.0, 2.0], [3.0, 4.0, 5.0]])
167+
v = view(f3, :, 2)
168+
@test length(v) == 3
169+
v[1] = 10.0
170+
@test f3[1, 2] == 10.0
171+
@test f3.u[2][1] == 10.0
172+
148173
t = 1:5
149174
recs = [rand(2, 2) for i in 1:5]
150175
testva = VectorOfArray(recs)

0 commit comments

Comments
 (0)