Skip to content

Commit

Permalink
Add hash functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Liozou committed Feb 21, 2022
1 parent 7da621d commit 9ee0c53
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "PeriodicGraphs"
uuid = "18c5b727-b240-4874-878a-f2e242435bab"
authors = ["Lionel Zoubritzky [email protected]"]
version = "0.4.0"
version = "0.5.0"

[deps]
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
Expand Down
12 changes: 11 additions & 1 deletion src/PeriodicGraphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export PeriodicVertex, PeriodicEdge, PeriodicGraph,
dimensionality, change_dimension

import Base: (==), isless, convert, show, showerror, eltype, iterate, zero,
length, in, ndims, print, cmp
length, in, ndims, print, cmp, hash
import Base.Order: Forward, Lt

"""
Expand Down Expand Up @@ -54,6 +54,7 @@ function cmp(x::PeriodicVertex{N}, y::PeriodicVertex{N}) where N
end
isless(x::PeriodicVertex{N}, y::PeriodicVertex{N}) where {N} = cmp(x, y) < 0
==(x::PeriodicVertex{N}, y::PeriodicVertex{M}) where {N,M} = N == M && iszero(cmp(x, y))
hash(x::PeriodicVertex, h::UInt) = hash(x.ofs, hash(x.v, h))

const PeriodicVertex1D = PeriodicVertex{1}
const PeriodicVertex2D = PeriodicVertex{2}
Expand Down Expand Up @@ -203,6 +204,7 @@ function cmp(x::PeriodicEdge{N}, y::PeriodicEdge{N}) where N
end
isless(x::PeriodicEdge{N}, y::PeriodicEdge{N}) where {N} = cmp(x, y) < 0
==(x::PeriodicEdge{N}, y::PeriodicEdge{M}) where {N,M} = N == M && iszero(cmp(x, y))
hash(x::PeriodicEdge, h::UInt) = hash(x.dst, hash(x.src, h))


"""
Expand Down Expand Up @@ -402,6 +404,14 @@ function ==(g1::PeriodicGraph{N}, g2::PeriodicGraph{M}) where {N,M}
return N == M && nv(g1) == nv(g2) && edges(g1) == edges(g2)
end

function hash(g::PeriodicGraph, h::UInt)
ret = h
for e in edges(g)
ret = hash(e, ret)
end
return ret
end

ndims(::PeriodicVertex{N}) where {N} = N
ndims(::PeriodicEdge{N}) where {N} = N
ndims(::PeriodicGraph{N}) where {N} = N
Expand Down
3 changes: 3 additions & 0 deletions src/precompile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ function _precompile_()
@enforce Base.precompile(Tuple{typeof(==),PeriodicVertex{i},PeriodicVertex{i}})
@enforce Base.precompile(Tuple{typeof(<),PeriodicVertex{i},PeriodicVertex{i}})
@enforce Base.precompile(Tuple{typeof(isless),PeriodicVertex{i},PeriodicVertex{i}})
@enforce Base.precompile(Tuple{typeof(hash),PeriodicVertex{i},UInt})
@enforce Base.precompile(Tuple{typeof(==),PeriodicEdge{i},PeriodicEdge{i}})
@enforce Base.precompile(Tuple{typeof(<),PeriodicEdge{i},PeriodicEdge{i}})
@enforce Base.precompile(Tuple{typeof(hash),PeriodicEdge{i},UInt})
@enforce Base.precompile(Tuple{typeof(==),PeriodicGraph{i},PeriodicGraph{i}})
@enforce Base.precompile(Tuple{typeof(hash),PeriodicGraph{i},UInt})

@enforce Base.precompile(Tuple{typeof(Graphs._neighborhood),PeriodicGraph{i},Int,Int,Graphs.DefaultDistance,Function})
@enforce Base.precompile(Tuple{typeof(Graphs.add_edge!),PeriodicGraph{i},PeriodicEdge{i}})
Expand Down

2 comments on commit 9ee0c53

@Liozou
Copy link
Owner Author

@Liozou Liozou commented on 9ee0c53 Feb 21, 2022

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/55130

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.5.0 -m "<description of version>" 9ee0c5314cde35b99a587e5f0b6067b2d60c5d5c
git push origin v0.5.0

Please sign in to comment.