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

Add Base.hash(::Quaternion, ::UInt) #136

Merged
merged 4 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
11 changes: 11 additions & 0 deletions src/Quaternion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,17 @@ function Base.isequal(q::Quaternion, w::Quaternion)
isequal(q.s, w.s) & isequal(q.v1, w.v1) & isequal(q.v2, w.v2) & isequal(q.v3, w.v3)
end

if UInt === UInt64
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this be better?

Suggested change
if UInt === UInt64
@static if UInt === UInt64

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The @static macro brings better performance if it is used inside a function, but this case is not, so it seems there would not be better performance here.

julia> f() = @static Sys.iswindows() ? 1 : 2
f (generic function with 1 method)

julia> @code_lowered f()
CodeInfo(
1return 2
)

julia> g() = Sys.iswindows() ? 1 : 2
g (generic function with 1 method)

julia> @code_lowered g()
CodeInfo(
1%1 = Base.Sys.iswindows
│   %2 = (%1)()
└──      goto #3 if not %2
2return 1
3return 2
)

const h_imags = 0xfa29df508725c1bf, 0x5e6c4b26a400626d, 0x8dfd375bac9f9403
else
const h_imags = 0x62802719, 0x5a9b072e, 0x209019b1
end
const hash_0_imags = hash.(0, h_imags)

function Base.hash(z::Quaternion, h::UInt)
hash(real(z), h ⊻ xor(xor.(hash.(imag_part(z), h_imags), hash_0_imags)...))
end

"""
extend_analytic(f, q::Quaternion)

Expand Down
10 changes: 10 additions & 0 deletions test/Quaternion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ end
@test !isequal(Quaternion(NaN, 0.0, Inf, -Inf), Quaternion(NaN, -0.0, Inf, -Inf))
end

@testset "hash" begin
# https://github.com/JuliaGeometry/Quaternions.jl/issues/135
@test hash(0) == hash(quat(0)) == hash(0.0) == hash(quat(0.0))
@test hash(1) == hash(quat(1)) == hash(1.0) == hash(quat(1.0))
@test hash(quat(-0.0)) ≠ hash(quat(+0.0))
@test allunique([hash(quat(1,0,0,0)), hash(quat(0,1,0,0)), hash(quat(0,0,1,0)), hash(quat(0,0,0,1))])
@test hash(57, UInt(42)) == hash(quat(57), UInt(42))
@test length(unique(Quaternion[quat(2), quat(big"2")])) == 1
end

@testset "convert" begin
@test convert(Quaternion{Float64}, 1) === Quaternion(1.0)
@test convert(Quaternion{Float64}, Quaternion(1, 2, 3, 4)) ===
Expand Down
Loading