|
| 1 | +""" |
| 2 | + push(vec::StaticVector, item) |
| 3 | +
|
| 4 | +Return a new `StaticVector` with `item` inserted on the end of `vec`. |
| 5 | +
|
| 6 | +# Examples |
| 7 | +```jldoctest |
| 8 | +julia> push(@SVector[1, 2, 3], 4) |
| 9 | +4-element SArray{Tuple{4},Int64,1,4} with indices SOneTo(4): |
| 10 | + 1 |
| 11 | + 2 |
| 12 | + 3 |
| 13 | + 4 |
| 14 | +``` |
| 15 | +""" |
1 | 16 | @inline push(vec::StaticVector, x) = _push(Size(vec), vec, x)
|
2 | 17 | @generated function _push(::Size{s}, vec::StaticVector, x) where {s}
|
3 | 18 | newlen = s[1] + 1
|
|
8 | 23 | end
|
9 | 24 | end
|
10 | 25 |
|
| 26 | +""" |
| 27 | + pushfirst(vec::StaticVector, item) |
| 28 | +
|
| 29 | +Return a new `StaticVector` with `item` inserted at the beginning of `vec`. |
| 30 | +
|
| 31 | +# Examples |
| 32 | +```jldoctest |
| 33 | +julia> pushfirst(@SVector[1, 2, 3, 4], 5) |
| 34 | +5-element SArray{Tuple{5},Int64,1,5} with indices SOneTo(5): |
| 35 | + 5 |
| 36 | + 1 |
| 37 | + 2 |
| 38 | + 3 |
| 39 | + 4 |
| 40 | +``` |
| 41 | +""" |
11 | 42 | @inline pushfirst(vec::StaticVector, x) = _pushfirst(Size(vec), vec, x)
|
12 | 43 | @generated function _pushfirst(::Size{s}, vec::StaticVector, x) where {s}
|
13 | 44 | newlen = s[1] + 1
|
|
18 | 49 | end
|
19 | 50 | end
|
20 | 51 |
|
| 52 | +""" |
| 53 | + insert(vec::StaticVector, index::Integer, item) |
| 54 | +
|
| 55 | +Return a new vector with `item` inserted into `vec` at the given `index`. |
| 56 | +
|
| 57 | +# Examples |
| 58 | +```jldoctest |
| 59 | +julia> insert(@SVector[6, 5, 4, 2, 1], 4, 3) |
| 60 | +6-element SArray{Tuple{6},Int64,1,6} with indices SOneTo(6): |
| 61 | + 6 |
| 62 | + 5 |
| 63 | + 4 |
| 64 | + 3 |
| 65 | + 2 |
| 66 | + 1 |
| 67 | +``` |
| 68 | +""" |
21 | 69 | @propagate_inbounds insert(vec::StaticVector, index, x) = _insert(Size(vec), vec, index, x)
|
22 | 70 | @generated function _insert(::Size{s}, vec::StaticVector, index, x) where {s}
|
23 | 71 | newlen = s[1] + 1
|
|
33 | 81 | end
|
34 | 82 | end
|
35 | 83 |
|
| 84 | +""" |
| 85 | + pop(vec::StaticVector) |
| 86 | +
|
| 87 | +Return a new vector with the last item in `vec` removed. |
| 88 | +
|
| 89 | +# Examples |
| 90 | +```jldoctest |
| 91 | +julia> pop(@SVector[1,2,3]) |
| 92 | +2-element SArray{Tuple{2},Int64,1,2} with indices SOneTo(2): |
| 93 | + 1 |
| 94 | + 2 |
| 95 | +``` |
| 96 | +""" |
36 | 97 | @inline pop(vec::StaticVector) = _pop(Size(vec), vec)
|
37 | 98 | @generated function _pop(::Size{s}, vec::StaticVector) where {s}
|
38 | 99 | newlen = s[1] - 1
|
|
43 | 104 | end
|
44 | 105 | end
|
45 | 106 |
|
| 107 | +""" |
| 108 | + popfirst(vec::StaticVector) |
| 109 | +
|
| 110 | +Return a new vector with the first item in `vec` removed. |
| 111 | +
|
| 112 | +# Examples |
| 113 | +```jldoctest |
| 114 | +julia> popfirst(@SVector[1,2,3]) |
| 115 | +2-element SArray{Tuple{2},Int64,1,2} with indices SOneTo(2): |
| 116 | + 2 |
| 117 | + 3 |
| 118 | +``` |
| 119 | +""" |
46 | 120 | @inline popfirst(vec::StaticVector) = _popfirst(Size(vec), vec)
|
47 | 121 | @generated function _popfirst(::Size{s}, vec::StaticVector) where {s}
|
48 | 122 | newlen = s[1] - 1
|
|
53 | 127 | end
|
54 | 128 | end
|
55 | 129 |
|
| 130 | +""" |
| 131 | + deleteat(vec::StaticVector, index::Integer) |
| 132 | +
|
| 133 | +Return a new vector with the item at the given `index` removed. |
| 134 | +
|
| 135 | +# Examples |
| 136 | +```jldoctest |
| 137 | +julia> deleteat(@SVector[6, 5, 4, 3, 2, 1], 2) |
| 138 | +5-element SArray{Tuple{5},Int64,1,5} with indices SOneTo(5): |
| 139 | + 6 |
| 140 | + 4 |
| 141 | + 3 |
| 142 | + 2 |
| 143 | + 1 |
| 144 | +``` |
| 145 | +""" |
56 | 146 | @propagate_inbounds deleteat(vec::StaticVector, index) = _deleteat(Size(vec), vec, index)
|
57 | 147 | @generated function _deleteat(::Size{s}, vec::StaticVector, index) where {s}
|
58 | 148 | newlen = s[1] - 1
|
|
73 | 163 | # Immutable version of setindex!(). Seems similar in nature to the above, but
|
74 | 164 | # could also be justified to live in src/indexing.jl
|
75 | 165 | import Base.setindex
|
| 166 | +""" |
| 167 | + setindex(vec::StaticArray, x, index::Int) |
| 168 | +
|
| 169 | +Return a new array with the item at `index` replaced by `x`. |
| 170 | +
|
| 171 | +# Examples |
| 172 | +```jldoctest |
| 173 | +julia> setindex(@SVector[1,2,3], 4, 2) |
| 174 | +3-element SArray{Tuple{3},Int64,1,3} with indices SOneTo(3): |
| 175 | + 1 |
| 176 | + 4 |
| 177 | + 3 |
| 178 | +
|
| 179 | +julia> setindex(@SMatrix[2 4; 6 8], 1, 2) |
| 180 | +2×2 SArray{Tuple{2,2},Int64,2,4} with indices SOneTo(2)×SOneTo(2): |
| 181 | + 2 4 |
| 182 | + 1 8 |
| 183 | +``` |
| 184 | +""" |
76 | 185 | @propagate_inbounds setindex(a::StaticArray, x, index::Int) = _setindex(Length(a), a, convert(eltype(typeof(a)), x), index)
|
77 | 186 | @generated function _setindex(::Length{L}, a::StaticArray{<:Tuple,T}, x::T, index::Int) where {L, T}
|
78 | 187 | exprs = [:(ifelse($i == index, x, a[$i])) for i = 1:L]
|
|
0 commit comments