@@ -3,11 +3,11 @@ struct CatVector{T, N, V<:AbstractVector{T}} <: AbstractVector{T}
3
3
end
4
4
5
5
@inline Base. size (vec:: CatVector ) = (mapreduce (length, + , vec. vecs; init= 0 ),)
6
- Base. eltype (vec:: CatVector ) = eltype (eltype (vec. vecs))
7
6
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.
9
9
Base. @propagate_inbounds function Base. getindex (vec:: CatVector , index:: Int )
10
- @boundscheck checkbounds ( vec, index)
10
+ @boundscheck index >= 1 || throw ( BoundsError ( vec, index) )
11
11
i = 1
12
12
j = index
13
13
@inbounds while true
@@ -20,11 +20,11 @@ Base.@propagate_inbounds function Base.getindex(vec::CatVector, index::Int)
20
20
i += 1
21
21
end
22
22
end
23
- error ( )
23
+ throw ( BoundsError (vec, index) )
24
24
end
25
25
26
26
Base. @propagate_inbounds function Base. setindex! (vec:: CatVector , val, index:: Int )
27
- @boundscheck checkbounds ( vec, index)
27
+ @boundscheck index >= 1 || throw ( BoundsError ( vec, index) )
28
28
i = 1
29
29
j = index
30
30
while true
@@ -38,7 +38,7 @@ Base.@propagate_inbounds function Base.setindex!(vec::CatVector, val, index::Int
38
38
i += 1
39
39
end
40
40
end
41
- error ( )
41
+ throw ( BoundsError (vec, index) )
42
42
end
43
43
44
44
Base. @propagate_inbounds function Base. copyto! (dest:: AbstractVector{T} , src:: CatVector{T} ) where {T}
58
58
Base. similar (vec:: CatVector ) = CatVector (map (similar, vec. vecs))
59
59
Base. similar (vec:: CatVector , :: Type{T} ) where {T} = CatVector (map (x -> similar (x, T), vec. vecs))
60
60
61
+ @noinline cat_vectors_line_up_error () = throw (ArgumentError (" Subvectors must line up" ))
62
+
61
63
@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 ( )
63
65
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 ( )
65
67
end
66
68
nothing
67
69
end
68
70
69
71
@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
71
76
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)
75
79
copyto! (dest. vecs[i], src. vecs[i])
76
80
end
77
81
return dest
0 commit comments