|
6 | 6 | Base.eltype(vec::CatVector) = eltype(eltype(vec.vecs))
|
7 | 7 |
|
8 | 8 | # Note: getindex and setindex are pretty naive.
|
9 |
| -Base.@propagate_inbounds function Base.getindex(vec::CatVector, i::Int) |
10 |
| - @boundscheck checkbounds(vec, i) |
11 |
| - I = 1 |
| 9 | +Base.@propagate_inbounds function Base.getindex(vec::CatVector, index::Int) |
| 10 | + @boundscheck checkbounds(vec, index) |
| 11 | + i = 1 |
| 12 | + j = index |
12 | 13 | @inbounds while true
|
13 |
| - subvec = vec.vecs[I] |
| 14 | + subvec = vec.vecs[i] |
14 | 15 | l = length(subvec)
|
15 |
| - if i <= l |
16 |
| - return subvec[eachindex(subvec)[i]] |
| 16 | + if j <= l |
| 17 | + return subvec[eachindex(subvec)[j]] |
17 | 18 | else
|
18 |
| - i -= l |
19 |
| - I += 1 |
| 19 | + j -= l |
| 20 | + i += 1 |
20 | 21 | end
|
21 | 22 | end
|
22 | 23 | error()
|
23 | 24 | end
|
24 | 25 |
|
25 |
| -Base.@propagate_inbounds function Base.setindex!(vec::CatVector, val, i::Int) |
26 |
| - @boundscheck checkbounds(vec, i) |
27 |
| - I = 1 |
| 26 | +Base.@propagate_inbounds function Base.setindex!(vec::CatVector, val, index::Int) |
| 27 | + @boundscheck checkbounds(vec, index) |
| 28 | + i = 1 |
| 29 | + j = index |
28 | 30 | while true
|
29 |
| - subvec = vec.vecs[I] |
| 31 | + subvec = vec.vecs[i] |
30 | 32 | l = length(subvec)
|
31 |
| - if i <= l |
32 |
| - subvec[eachindex(subvec)[i]] = val |
| 33 | + if j <= l |
| 34 | + subvec[eachindex(subvec)[j]] = val |
33 | 35 | return val
|
34 | 36 | else
|
35 |
| - i -= l |
36 |
| - I += 1 |
| 37 | + j -= l |
| 38 | + i += 1 |
37 | 39 | end
|
38 | 40 | end
|
39 | 41 | error()
|
|
83 | 85 | return dest
|
84 | 86 | end
|
85 | 87 |
|
86 |
| -Base.@propagate_inbounds catvec_broadcast_getindex(vec::CatVector, i::Int, j::Int, k::Int) = vec.vecs[i][j] |
87 |
| -Base.@propagate_inbounds catvec_broadcast_getindex(x, i::Int, j::Int, k::Int) = Broadcast._broadcast_getindex(x, i) |
| 88 | +Base.@propagate_inbounds catvec_broadcast_vec(x::CatVector, k::Int) = x.vecs[k] |
| 89 | +Base.@propagate_inbounds catvec_broadcast_vec(x::Number, k::Int) = x |
88 | 90 |
|
89 | 91 | @inline function Base.copyto!(dest::CatVector, bc::Broadcast.Broadcasted{Nothing})
|
90 | 92 | flat = Broadcast.flatten(bc)
|
91 |
| - index = 1 |
92 |
| - dest_vecs = dest.vecs |
93 |
| - @boundscheck check_cat_vectors_line_up(dest, bc.args...) |
94 |
| - @inbounds for i in eachindex(dest_vecs) |
95 |
| - vec = dest_vecs[i] |
96 |
| - for j in eachindex(vec) |
97 |
| - k = axes(flat)[1][index] |
98 |
| - let f = flat.f, args = flat.args, i = i, j = j, k = k |
99 |
| - vec[j] = Broadcast._broadcast_getindex_evalf(f, map(arg -> catvec_broadcast_getindex(arg, i, j, k), args)...) |
100 |
| - end |
101 |
| - index += 1 |
| 93 | + @boundscheck check_cat_vectors_line_up(dest, flat.args...) |
| 94 | + @inbounds for i in eachindex(dest.vecs) |
| 95 | + let i = i, f = flat.f, args = flat.args |
| 96 | + dest′ = catvec_broadcast_vec(dest, i) |
| 97 | + args′ = map(arg -> catvec_broadcast_vec(arg, i), args) |
| 98 | + axes′ = (eachindex(dest′),) |
| 99 | + copyto!(dest′, Broadcast.Broadcasted{Nothing}(f, args′, axes′)) |
102 | 100 | end
|
103 | 101 | end
|
104 | 102 | return dest
|
|
0 commit comments