@@ -32,7 +32,7 @@ GC'ed when removed.
3232defaultval (:: Type{T} ) where {T <: Number } = zero (T)
3333defaultval (:: Type{Any} ) = nothing
3434
35- function Base. getindex (x:: Backing , i:: Int )
35+ Base . @propagate_inbounds function Base. getindex (x:: Backing , i:: Int )
3636 @boundscheck 1 <= i <= x. len
3737 if i == 1
3838 x. x1
@@ -43,7 +43,7 @@ function Base.getindex(x::Backing, i::Int)
4343 end
4444end
4545
46- function Base. setindex! (x:: Backing , v, i:: Int )
46+ Base . @propagate_inbounds function Base. setindex! (x:: Backing , v, i:: Int )
4747 @boundscheck 1 <= i <= x. len
4848 if i == 1
4949 setfield! (x, :x1 , v)
@@ -54,20 +54,65 @@ function Base.setindex!(x::Backing, v, i::Int)
5454 end
5555end
5656
57- function Base. push! (x:: Backing , v)
58- x. len < 3 || throw ( ArgumentError ( " `Backing` is full " ))
57+ Base . @propagate_inbounds function Base. push! (x:: Backing , v)
58+ @boundscheck x. len < 3
5959 x. len += 1
6060 x[x. len] = v
6161end
6262
63- function Base. pop! (x:: Backing{T} ) where {T}
64- x. len > 0 || throw ( ArgumentError ( " Array is empty " ))
63+ Base . @propagate_inbounds function Base. pop! (x:: Backing{T} ) where {T}
64+ @boundscheck x. len > 0
6565 v = x[x. len]
6666 x[x. len] = defaultval (T)
6767 x. len -= 1
6868 v
6969end
7070
71+ function Base. any (f:: Function , x:: Backing )
72+ if x. len == 0
73+ false
74+ elseif x. len == 1
75+ f (x. x1)
76+ elseif x. len == 2
77+ f (x. x1) || f (x. x2)
78+ elseif x. len == 3
79+ f (x. x1) || f (x. x2) || f (x. x3)
80+ end
81+ end
82+
83+ function Base. all (f:: Function , x:: Backing )
84+ if x. len == 0
85+ true
86+ elseif x. len == 1
87+ f (x. x1)
88+ elseif x. len == 2
89+ f (x. x1) && f (x. x2)
90+ elseif x. len == 3
91+ f (x. x1) && f (x. x2) && f (x. x3)
92+ end
93+ end
94+
95+ function Base. map (f, x:: Backing{T} ) where {T}
96+ if x. len == 0
97+ # StaticArrays does this, so we are only as bad as they are
98+ Backing {Core.Compiler.return_type(f, Tuple{T})} ()
99+ elseif x. len == 1
100+ x1 = f (x. x1)
101+ Backing {typeof(x1)} (x1)
102+ elseif x. len == 2
103+ x1 = f (x. x1)
104+ x2 = f (x. x2)
105+ Backing {Base.promote_typejoin(typeof(x1), typeof(x2))} (x1, x2)
106+ elseif x. len == 3
107+ x1 = f (x. x1)
108+ x2 = f (x. x2)
109+ x3 = f (x. x3)
110+ _T = Base. promote_typejoin (typeof (x1), typeof (x2))
111+ _T = Base. promote_typejoin (_T, typeof (x3))
112+ Backing {_T} (x1, x2, x3)
113+ end
114+ end
115+
71116"""
72117 $(TYPEDSIGNATURES)
73118
@@ -94,6 +139,10 @@ mutable struct SmallVec{T, V <: AbstractVector{T}} <: AbstractVector{T}
94139 end
95140 end
96141
142+ function SmallVec {T, V} (x:: Backing{T} ) where {T, V}
143+ new {T, V} (x)
144+ end
145+
97146 function SmallVec {T, V} () where {T, V}
98147 new {T, V} (Backing {T} ())
99148 end
@@ -113,21 +162,24 @@ Base.convert(::Type{SmallVec{T, V}}, x::SmallVec{T, V}) where {T, V} = x
113162
114163Base. size (x:: SmallVec ) = size (x. data)
115164Base. isempty (x:: SmallVec ) = isempty (x. data)
116- Base. getindex (x:: SmallVec , i:: Int ) = x. data[i]
117- Base. setindex! (x:: SmallVec , v, i:: Int ) = setindex! (x. data, v, i)
165+ Base. @propagate_inbounds Base . getindex (x:: SmallVec , i:: Int ) = x. data[i]
166+ Base. @propagate_inbounds Base . setindex! (x:: SmallVec , v, i:: Int ) = setindex! (x. data, v, i)
118167
119- function Base. push! (x:: SmallVec{T, V} , v) where {T, V}
168+ Base . @propagate_inbounds function Base. push! (x:: SmallVec{T, V} , v) where {T, V}
120169 buf = x. data
121170 buf isa Backing{T} || return push! (buf:: V , v)
122171 isfull (buf) || return push! (buf:: Backing{T} , v)
123172 x. data = V (buf)
124173 return push! (x. data:: V , v)
125174end
126175
127- Base. pop! (x:: SmallVec ) = pop! (x. data)
176+ Base. @propagate_inbounds Base . pop! (x:: SmallVec ) = pop! (x. data)
128177
129178function Base. sizehint! (x:: SmallVec{T, V} , n; kwargs... ) where {T, V}
130179 x. data isa Backing && return x
131180 sizehint! (x. data, n; kwargs... )
132181 x
133182end
183+
184+ Base. any (f:: Function , x:: SmallVec ) = any (f, x. data)
185+ Base. all (f:: Function , x:: SmallVec ) = all (f, x. data)
0 commit comments