Skip to content

Commit e46a068

Browse files
committed
Minor tweaks.
1 parent 5ad06ac commit e46a068

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

src/custom_collections/CatVector.jl

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ struct CatVector{T, N, V<:AbstractVector{T}} <: AbstractVector{T}
33
end
44

55
@inline Base.size(vec::CatVector) = (mapreduce(length, +, vec.vecs; init=0),)
6-
Base.eltype(vec::CatVector) = eltype(eltype(vec.vecs))
76

8-
# Note: getindex and setindex are pretty naive.
7+
# Note: getindex and setindex are pretty naive. Consider precomputing map from
8+
# index to vector upon CatVector construction.
99
Base.@propagate_inbounds function Base.getindex(vec::CatVector, index::Int)
10-
@boundscheck checkbounds(vec, index)
10+
@boundscheck index >= 1 || throw(BoundsError(vec, index))
1111
i = 1
1212
j = index
1313
@inbounds while true
@@ -20,11 +20,11 @@ Base.@propagate_inbounds function Base.getindex(vec::CatVector, index::Int)
2020
i += 1
2121
end
2222
end
23-
error()
23+
throw(BoundsError(vec, index))
2424
end
2525

2626
Base.@propagate_inbounds function Base.setindex!(vec::CatVector, val, index::Int)
27-
@boundscheck checkbounds(vec, index)
27+
@boundscheck index >= 1 || throw(BoundsError(vec, index))
2828
i = 1
2929
j = index
3030
while true
@@ -38,7 +38,7 @@ Base.@propagate_inbounds function Base.setindex!(vec::CatVector, val, index::Int
3838
i += 1
3939
end
4040
end
41-
error()
41+
throw(BoundsError(vec, index))
4242
end
4343

4444
Base.@propagate_inbounds function Base.copyto!(dest::AbstractVector{T}, src::CatVector{T}) where {T}
@@ -58,20 +58,24 @@ end
5858
Base.similar(vec::CatVector) = CatVector(map(similar, vec.vecs))
5959
Base.similar(vec::CatVector, ::Type{T}) where {T} = CatVector(map(x -> similar(x, T), vec.vecs))
6060

61+
@noinline cat_vectors_line_up_error() = throw(ArgumentError("Subvectors must line up"))
62+
6163
@inline function check_cat_vectors_line_up(x::CatVector, y::CatVector)
62-
length(x.vecs) == length(y.vecs) || throw(ArgumentError("Subvectors must line up"))
64+
length(x.vecs) == length(y.vecs) || cat_vectors_line_up_error()
6365
for i in eachindex(x.vecs)
64-
length(x.vecs[i]) == length(y.vecs[i]) || throw(ArgumentError("Subvectors must line up"))
66+
length(x.vecs[i]) == length(y.vecs[i]) || cat_vectors_line_up_error()
6567
end
6668
nothing
6769
end
6870

6971
@inline check_cat_vectors_line_up(x::CatVector, y) = nothing
70-
@inline check_cat_vectors_line_up(x::CatVector, y, tail...) = (check_cat_vectors_line_up(x, y); check_cat_vectors_line_up(x, tail...))
72+
@inline function check_cat_vectors_line_up(x::CatVector, y, tail...)
73+
check_cat_vectors_line_up(x, y)
74+
check_cat_vectors_line_up(x, tail...)
75+
end
7176

72-
@inline function Base.copyto!(dest::CatVector, src::CatVector)
73-
@boundscheck check_cat_vectors_line_up(dest, src)
74-
@inbounds for i in eachindex(dest.vecs)
77+
@propagate_inbounds function Base.copyto!(dest::CatVector, src::CatVector)
78+
for i in eachindex(dest.vecs)
7579
copyto!(dest.vecs[i], src.vecs[i])
7680
end
7781
return dest

0 commit comments

Comments
 (0)