Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend getproperty and setproperty! with swizzling for vectors #1221

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 59 additions & 14 deletions src/MVector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,67 @@
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)
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
# names from low-dimensional geometry and computer graphics:
# - (x, y, z, w) where w is a homogenous coordinate.
# - (r, g, b, a) where a is an alpha component.
# - (xy, rg, xyz, ...) to obtain a subset of the vector via swizzling.

@inline function swizzle(v::SVector, index, indices...)

Check warning on line 22 in src/MVector.jl

View check run for this annotation

Codecov / codecov/patch

src/MVector.jl#L22

Added line #L22 was not covered by tests
isempty(indices) && return v[index]
indices = (index, indices...)
SVector(ntuple(i -> v[indices[i]], length(indices)))
end
@inline function swizzle(v::MVector, index, indices...)

Check warning on line 27 in src/MVector.jl

View check run for this annotation

Codecov / codecov/patch

src/MVector.jl#L27

Added line #L27 was not covered by tests
isempty(indices) && return v[index]
indices = (index, indices...)
MVector(ntuple(i -> v[indices[i]], length(indices)))
end

@inline function swizzle!(v::MVector, value, indices...)

Check warning on line 33 in src/MVector.jl

View check run for this annotation

Codecov / codecov/patch

src/MVector.jl#L33

Added line #L33 was not covered by tests
for (i, index) in enumerate(indices)
setindex!(v, value[i], index)
end
value
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
let dimension_names = zip((:x, :y, :z, :w), (:r, :g, :b, :a))
getproperty_bodies = [Expr[], Expr[], Expr[], Expr[]]
setproperty_bodies = [Expr[], Expr[], Expr[], Expr[]]
for (i, (dx1, dr1)) in enumerate(dimension_names)
field1 = dx1
field2 = dr1
push!(getproperty_bodies[i], :((name === $(QuoteNode(field1)) || name === $(QuoteNode(field2))) && return swizzle(v, $i)))
push!(setproperty_bodies[i], :((name === $(QuoteNode(field1)) || name === $(QuoteNode(field2))) && return swizzle!(v, value, $i)))
for (j, (dx2, dr2)) in enumerate(dimension_names)
field1 = Symbol(dx1, dx2)
field2 = Symbol(dr1, dr2)
push!(getproperty_bodies[max(i, j)], :((name === $(QuoteNode(field1)) || name === $(QuoteNode(field2))) && return swizzle(v, $i, $j)))
j ≠ i && push!(setproperty_bodies[max(i, j)], :((name === $(QuoteNode(field1)) || name === $(QuoteNode(field2))) && return swizzle!(v, value, $i, $j)))
for (k, (dx3, dr3)) in enumerate(dimension_names)
field1 = Symbol(dx1, dx2, dx3)
field2 = Symbol(dr1, dr2, dr3)
push!(getproperty_bodies[max(i, j, k)], :((name === $(QuoteNode(field1)) || name === $(QuoteNode(field2))) && return swizzle(v, $i, $j, $k)))
(k ≠ j && k ≠ i) && push!(setproperty_bodies[max(i, j, k)], :((name === $(QuoteNode(field1)) || name === $(QuoteNode(field2))) && return swizzle!(v, value, $i, $j, $k)))
for (l, (dx4, dr4)) in enumerate(dimension_names)
field1 = Symbol(dx1, dx2, dx3, dx4)
field2 = Symbol(dr1, dr2, dr3, dr4)
push!(getproperty_bodies[max(i, j, k, l)], :((name === $(QuoteNode(field1)) || name === $(QuoteNode(field2))) && return swizzle(v, $i, $j, $k, $l)))
(l ≠ k && l ≠ j && l ≠ i) && push!(setproperty_bodies[max(i, j, k, l)], :((name === $(QuoteNode(field1)) || name === $(QuoteNode(field2))) && return swizzle!(v, value, $i, $j, $k, $l)))
end
end
end
end
for i in 1:4
@eval function Base.getproperty(v::Union{SVector{$i},MVector{$i}},
name::Symbol)
name === :data && return getfield(v, :data)
$(foldl(append!, getproperty_bodies[1:i])...)
getfield(v, name)
end
@eval function Base.setproperty!(v::MVector{$i}, name::Symbol, value)
name === :data && return setfield!(v, :data, value)
$(foldl(append!, setproperty_bodies[1:i])...)
setfield!(v, name, value)
end
end
end
46 changes: 33 additions & 13 deletions test/MVector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,30 +105,50 @@
@testset "Named field access - getproperty/setproperty!" begin
# getproperty
v4 = @MVector [10,20,30,40]
@test v4.x == 10
@test v4.y == 20
@test v4.z == 30
@test v4.w == 40
@test v4.x == v4.r == 10
@test v4.y == v4.g == 20
@test v4.z == v4.b == 30
@test v4.w == v4.a == 40
@test v4.xy == v4.rg == @MVector [v4.x, v4.y]
@test v4.wzx == v4.abr == @MVector [v4.w, v4.z, v4.x]
@test v4.xyx == @MVector [v4.x, v4.y, v4.x]
@test v4.xyzw == v4.rgba == v4
@test_throws ErrorException v4.xgb

v2 = @MVector [10,20]
@test v2.x == 10
@test v2.y == 20
@test v2.x == v2.r == 10
@test v2.y == v2.g == 20
@test_throws ErrorException v2.z
@test_throws ErrorException v2.w
@test_throws ErrorException v2.b
@test_throws ErrorException v2.a
@test v2.xy == v2.rg == v2
@test v2.yx == v2.gr == @MVector [v2.y, v2.x]
@test_throws ErrorException v2.ba
@test_throws ErrorException v2.rgb
@test_throws ErrorException v2.rgba

# setproperty!
@test (v4.x = 100) == 100
@test (v4.y = 200) == 200
@test (v4.z = 300) == 300
@test (v4.w = 400) == 400
@test (v4.x = 100) == (v4.r = 100) == 100
@test (v4.y = 200) == (v4.g = 200) == 200
@test (v4.z = 300) == (v4.b = 300) == 300
@test (v4.w = 400) == (v4.a = 400) == 400
@test v4[1] == 100
@test v4[2] == 200
@test v4[3] == 300
@test v4[4] == 400

@test (v2.x = 100) == 100
@test (v2.y = 200) == 200
@test (v4.xy = (1000, 2000)) == (v4.rg = (1000, 2000)) == (1000, 2000)
@test v4[1] == 1000
@test v4[2] == 2000
@test (v4.xywz = (10, 20, 40, 30)) == (v4.rgab = (10, 20, 40, 30)) == (10, 20, 40, 30)
@test v4 == @MVector [10, 20, 30, 40]
@test_throws ErrorException (v2.xyx = (100, 200, 100))

@test (v2.x = 100) == (v2.r = 100) == 100
@test (v2.y = 200) == (v2.g = 200) == 200
@test_throws ErrorException (v2.z = 200)
@test_throws ErrorException (v2.w = 200)
@test_throws ErrorException (v2.xz = (100, 300))
@test_throws ErrorException (v2.wx = (400, 100))
end
end
29 changes: 21 additions & 8 deletions test/SVector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,29 @@
end

@testset "Named field access - getproperty" begin
v4 = SA[10,20,30,40]
@test v4.x == 10
@test v4.y == 20
@test v4.z == 30
@test v4.w == 40
v2 = SA[10,20]
@test v2.x == 10
@test v2.y == 20
v4 = @SVector [10,20,30,40]
@test v4.x == v4.r == 10
@test v4.y == v4.g == 20
@test v4.z == v4.b == 30
@test v4.w == v4.a == 40
@test v4.xy === v4.rg === @SVector [v4.x, v4.y]
@test v4.wzx === v4.abr === @SVector [v4.w, v4.z, v4.x]
@test v4.xyx === @SVector [v4.x, v4.y, v4.x]
@test v4.xyzw == v4.rgba == v4
@test_throws ErrorException v4.xgb

v2 = @SVector [10,20]
@test v2.x == v2.r == 10
@test v2.y == v2.g == 20
@test_throws ErrorException v2.z
@test_throws ErrorException v2.w
@test_throws ErrorException v2.b
@test_throws ErrorException v2.a
@test v2.xy == v2.rg == v2
@test v2.yx === v2.gr === @SVector [v2.y, v2.x]
@test_throws ErrorException v2.ba
@test_throws ErrorException v2.rgb
@test_throws ErrorException v2.rgba
end

@testset "issue 1042" begin
Expand Down
Loading