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

cosmetics #8

Closed
wants to merge 3 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
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ version = "0.3.1"
Downloads = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
SHA = "ea8e919c-243c-51af-8825-aaa63cd721ce"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
nauty_jll = "55c6dc9b-343a-50ca-8ff2-b71adb3733d5"

Expand Down
1 change: 1 addition & 0 deletions src/NautyGraphs.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module NautyGraphs

import nauty_jll
using SHA
const libnauty = nauty_jll.libnautyTL
const WORDSIZE = 64
const WordType = Culong
Expand Down
10 changes: 10 additions & 0 deletions src/bitutils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@
return nidx
end

function _concatbytes(bytes::AbstractVector{<:UInt8})
@assert length(bytes) == sizeof(HashType)
w = HashType(0)
for b in bytes
w |= b
w <<= 8
end
return w

Check warning on line 125 in src/bitutils.jl

View check run for this annotation

Codecov / codecov/patch

src/bitutils.jl#L118-L125

Added lines #L118 - L125 were not covered by tests
end

function _to_matrixidx(idx::Integer, m::Integer)
return 1 + (idx - 1) ÷ m, mod1(idx, m)
end
Expand Down
1 change: 1 addition & 0 deletions src/densenautygraphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ mutable struct DenseNautyGraph{D} <: AbstractNautyGraph
end

function DenseNautyGraph{D}(n::Integer, vertex_labels::Union{Vector{<:Integer},Nothing}=nothing) where {D}
!isnothing(vertex_labels) && @assert n == length(vertex_labels)
m = ceil(Cint, n / WORDSIZE)
graphset = zeros(WordType, Int(n * m))
labels = _initialize_vertexlabels(n, vertex_labels)
Expand Down
24 changes: 15 additions & 9 deletions src/nauty.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,16 @@ function nauty(g::DenseNautyGraph, canonical_form=true; ignore_vertex_labels=fal
return grpsize, canong, canon_perm
end

function _nautyhash(g::AbstractNautyGraph, h::UInt=zero(UInt))
function _nautyhash(g::DenseNautyGraph)
grpsize, canong, canon_perm = nauty(g, true)
hashval = hash(view(g.labels, canon_perm), hash(canong, h))

# Base.hash skips elements in arrays of length >= 8192
# Use SHA in these cases
canong_hash = length(canong) >= 8192 ? hash_sha(canong) : hash(canong)
labels_hash = @views length(g.labels) >= 8192 ? hash_sha(g.labels[canon_perm]) : hash(g.labels[canon_perm])

hashval = hash(labels_hash, canong_hash)
g.hashval = hashval
return grpsize, canong, canon_perm, hashval
end

Expand All @@ -128,10 +135,8 @@ Reorder g's vertices to be in canonical order. Returns the permutation used to c
"""
function canonize!(g::AbstractNautyGraph)
grpsize, canong, canon_perm, hashval = _nautyhash(g)

g.graphset .= canong
g.labels .= g.labels[canon_perm]
g.hashval = hashval
copyto!(g.graphset, canong)
permute!(g.labels, canon_perm)
return canon_perm, grpsize
end

Expand All @@ -150,16 +155,17 @@ end


"""
ghash(g::AbstractNautyGraph, h::UInt=zero(UInt))
ghash(g::AbstractNautyGraph)

Hash the canonical version of g, so that (up to hash collisions) `ghash(g1) == ghash(g2)` implies `is_isomorphic(g1, g2) == true`.
Hashes are computed using `Base.hash` for small graphs (nv < 8192), or using the first 64 bits of `sha256` for larger graphs.
"""
function ghash(g::AbstractNautyGraph, h::UInt=zero(UInt))
function ghash(g::AbstractNautyGraph)
if !isnothing(g.hashval)
return g.hashval
end

_, _, _, hashval = _nautyhash(g, h)
_, _, _, hashval = _nautyhash(g)
g.hashval = hashval
return g.hashval
end
Expand Down
6 changes: 6 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,10 @@
end

return g.graphset[set_idx] != word_old
end

function hash_sha(x)
io = IOBuffer()
write(io, x)
return _concatbytes(sha256(take!(io))[1:8])

Check warning on line 92 in src/utils.jl

View check run for this annotation

Codecov / codecov/patch

src/utils.jl#L89-L92

Added lines #L89 - L92 were not covered by tests
end
Loading