Skip to content

Commit a716a84

Browse files
matbesancongithub-actions[bot]Krastanov
authored
implementing a simple graph hash to correct for a buggy hash / == inconsistency (#485)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Stefan Krastanov <github.acc@krastanov.org>
1 parent 1d94b7a commit a716a84

4 files changed

Lines changed: 21 additions & 1 deletion

File tree

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "Graphs"
22
uuid = "86223c79-3864-5bf0-83f7-82e725a168b6"
3-
version = "1.13.3"
3+
version = "1.13.4"
44

55
[deps]
66
ArnoldiMethod = "ec485272-7323-5ecc-a04f-4719b315124d"

src/SimpleGraphs/simpledigraph.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,15 @@ function ==(g::SimpleDiGraph, h::SimpleDiGraph)
435435
badj(g) == badj(h)
436436
end
437437

438+
function Base.hash(g::SimpleDiGraph, h::UInt)
439+
r = hash(typeof(g), h)
440+
r = hash(nv(g), r)
441+
r = hash(ne(g), r)
442+
r = hash(fadj(g), r)
443+
r = hash(badj(g), r)
444+
return r
445+
end
446+
438447
is_directed(::Type{<:SimpleDiGraph}) = true
439448

440449
function has_edge(g::SimpleDiGraph{T}, s, d) where {T}

src/SimpleGraphs/simplegraph.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,14 @@ function ==(g::SimpleGraph, h::SimpleGraph)
430430
return vertices(g) == vertices(h) && ne(g) == ne(h) && fadj(g) == fadj(h)
431431
end
432432

433+
function Base.hash(g::SimpleGraph, h::UInt)
434+
r = hash(typeof(g), h)
435+
r = hash(nv(g), r)
436+
r = hash(ne(g), r)
437+
r = hash(fadj(g), r)
438+
return r
439+
end
440+
433441
"""
434442
is_directed(g)
435443

test/simplegraphs/simplegraphs.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ using Graphs.Test
103103

104104
@test @inferred(edgetype(g)) == SimpleGraphEdge{T}
105105
@test @inferred(copy(g)) == g
106+
@test @inferred(hash(g)) == hash(copy(g)) == hash(deepcopy(g))
106107
@test @inferred(!is_directed(g))
107108

108109
e = first(edges(g))
@@ -124,6 +125,8 @@ using Graphs.Test
124125
@test @inferred(!has_edge(g, 20, 3))
125126
@test @inferred(!has_edge(g, 2, 30))
126127

128+
@test @inferred(copy(g)) == g
129+
@test @inferred(hash(copy(g))) == hash(g)
127130
gc = copy(g)
128131
@test @inferred(add_edge!(gc, 4 => 1)) && gc == cycle_digraph(4)
129132
@test @inferred(has_edge(gc, 4 => 1)) && has_edge(gc, 0x04 => 0x01)

0 commit comments

Comments
 (0)