-
Notifications
You must be signed in to change notification settings - Fork 145
/
deque.jl
84 lines (74 loc) · 3.09 KB
/
deque.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
@generated function push(vec::StaticVector, x)
newtype = similar_type(vec, Size(length(vec) + 1))
exprs = vcat([:(vec[$i]) for i = 1:length(vec)], :x)
return quote
$(Expr(:meta, :inline))
@inbounds return $(Expr(:call, newtype, Expr(:tuple, exprs...)))
end
end
@generated function unshift(vec::StaticVector, x)
newtype = similar_type(vec, Size(length(vec) + 1))
exprs = vcat(:x, [:(vec[$i]) for i = 1:length(vec)])
return quote
$(Expr(:meta, :inline))
@inbounds return $(Expr(:call, newtype, Expr(:tuple, exprs...)))
end
end
@generated function insert(vec::StaticVector, index, x)
newtype = similar_type(vec, Size(length(vec) + 1))
exprs = [(i == 1 ? :(ifelse($i < index, vec[$i], x)) :
i == length(vec)+1 ? :(ifelse($i == index, x, vec[$i-1])) :
:(ifelse($i < index, vec[$i], ifelse($i == index, x, vec[$i-1])))) for i = 1:length(vec) + 1]
return quote
$(Expr(:meta, :inline))
@boundscheck if (index < 1 || index > $(length(vec)+1))
throw(BoundsError(vec, index))
end
@inbounds return $(Expr(:call, newtype, Expr(:tuple, exprs...)))
end
end
@generated function pop(vec::StaticVector)
newtype = similar_type(vec, Size(length(vec) - 1))
exprs = [:(vec[$i]) for i = 1:length(vec)-1]
return quote
$(Expr(:meta, :inline))
@inbounds return $(Expr(:call, newtype, Expr(:tuple, exprs...)))
end
end
@generated function shift(vec::StaticVector)
newtype = similar_type(vec, Size(length(vec) - 1))
exprs = [:(vec[$i]) for i = 2:length(vec)]
return quote
$(Expr(:meta, :inline))
@inbounds return $(Expr(:call, newtype, Expr(:tuple, exprs...)))
end
end
@generated function deleteat(vec::StaticVector, index)
newtype = similar_type(vec, Size(length(vec) - 1))
exprs = [:(ifelse($i < index, vec[$i], vec[$i+1])) for i = 1:length(vec) - 1]
return quote
$(Expr(:meta, :inline))
@boundscheck if (index < 1 || index > $(length(vec)+1))
throw(BoundsError(vec, index))
end
@inbounds return $(Expr(:call, newtype, Expr(:tuple, exprs...)))
end
end
# TODO consider prepend, append (can use vcat, but eltype might change), and
# maybe splice (a bit hard to get statically sized without a "static" range)
# Immutable version of setindex!(). Seems similar in nature to the above, but
# could also be justified to live in src/indexing.jl
@generated function setindex{T}(a::StaticArray{T}, x::T, index::Int)
newtype = a
exprs = [:(ifelse($i == index, x, a[$i])) for i = 1:length(a)]
return quote
$(Expr(:meta, :inline))
@boundscheck if (index < 1 || index > $(length(a)))
throw(BoundsError(a, index))
end
@inbounds return $(Expr(:call, newtype, Expr(:tuple, exprs...)))
end
end
@propagate_inbounds setindex(a::StaticArray, x, index::Int) = setindex(a, convert(eltype(typeof(a)), x), index)
# TODO proper multidimension boundscheck
@propagate_inbounds setindex(a::StaticArray, x, inds::Int...) = setindex(a, x, sub2ind(size(typeof(a)), inds...))