-
Notifications
You must be signed in to change notification settings - Fork 152
/
Copy pathMVector.jl
37 lines (33 loc) · 1.23 KB
/
MVector.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
#####################
## MVector methods ##
#####################
"""
@MVector [a, b, c, d]
@MVector [i for i in 1:2]
@MVector ones(2)
A convenience macro to construct `MVector`.
See [`@SArray`](@ref) for detailed features.
"""
macro MVector(ex)
static_vector_gen(MVector, ex, __module__)
end
# Named field access for the first four elements, using the conventional field
# names from low-dimensional geometry (x,y,z) and computer graphics (w).
let dimension_names = QuoteNode.([:x, :y, :z, :w])
body = :(getfield(v, name))
for (i,dim_name) in enumerate(dimension_names)
@eval @inline Base.propertynames(v::Union{SVector{$i},MVector{$i}}) = ($(first(dimension_names, i)...),)
body = :(name === $(dimension_names[i]) ? getfield(v, :data)[$i] : $body)
@eval @inline function Base.getproperty(v::Union{SVector{$i},MVector{$i}},
name::Symbol)
$body
end
end
body = :(setfield!(v, name, e))
for (i,dim_name) in enumerate(dimension_names)
body = :(name === $dim_name ? @inbounds(v[$i] = e) : $body)
@eval @inline function Base.setproperty!(v::MVector{$i}, name::Symbol, e)
$body
end
end
end