|
1 |
| -struct CatVector{T, N, I<:Tuple{Vararg{AbstractVector{T}, N}}} <: AbstractVector{T} |
2 |
| - vecs::I |
| 1 | +struct CatVector{T, N, V<:AbstractVector{T}} <: AbstractVector{T} |
| 2 | + vecs::NTuple{N, V} |
3 | 3 | end
|
4 | 4 |
|
5 | 5 | @inline Base.size(vec::CatVector) = (mapreduce(length, +, vec.vecs; init=0),)
|
@@ -39,7 +39,7 @@ Base.@propagate_inbounds function Base.setindex!(vec::CatVector, val, i::Int)
|
39 | 39 | error()
|
40 | 40 | end
|
41 | 41 |
|
42 |
| -Base.@propagate_inbounds function Base.copyto!(dest::AbstractVector, src::CatVector) |
| 42 | +Base.@propagate_inbounds function Base.copyto!(dest::AbstractVector{T}, src::CatVector{T}) where {T} |
43 | 43 | @boundscheck length(dest) == length(src) || throw(DimensionMismatch())
|
44 | 44 | dest_indices = eachindex(dest)
|
45 | 45 | k = 1
|
|
55 | 55 |
|
56 | 56 | Base.similar(vec::CatVector) = CatVector(map(similar, vec.vecs))
|
57 | 57 | Base.similar(vec::CatVector, ::Type{T}) where {T} = CatVector(map(x -> similar(x, T), vec.vecs))
|
| 58 | + |
| 59 | +function check_cat_vectors_line_up(x::CatVector, ys::CatVector...) |
| 60 | + for j in eachindex(ys) |
| 61 | + y = ys[j] |
| 62 | + length(x.vecs) == length(y.vecs) || throw(ArgumentError("Subvectors must line up")) |
| 63 | + for i in eachindex(x.vecs) |
| 64 | + length(x.vecs[i]) == length(y.vecs[i]) || throw(ArgumentError("Subvectors must line up")) |
| 65 | + end |
| 66 | + end |
| 67 | +end |
| 68 | + |
| 69 | +@inline function Base.copyto!(dest::CatVector, src::CatVector) |
| 70 | + @boundscheck check_cat_vectors_line_up(dest, src) |
| 71 | + @inbounds for i in eachindex(dest.vecs) |
| 72 | + copyto!(dest.vecs[i], src.vecs[i]) |
| 73 | + end |
| 74 | + return dest |
| 75 | +end |
| 76 | + |
| 77 | +@inline function Base.map!(f::F, dest::CatVector, args::CatVector...) where F |
| 78 | + @boundscheck check_cat_vectors_line_up(dest, args...) |
| 79 | + @inbounds for i in eachindex(dest.vecs) |
| 80 | + map!(f, dest.vecs[i], map(arg -> arg.vecs[i], args)...) |
| 81 | + end |
| 82 | + return dest |
| 83 | +end |
| 84 | + |
0 commit comments