do not use pointer addr strings as map keys in set

When creating a Set of BasicEdges, the Hashcode function is used to determine
map keys for the underlying set data structure.

The string hex representation of the two vertices' pointers is unsafe to use
as a map key, since these addresses may change between the time they are added
to the set and the time the set is operated on.

Instead we modify the Hashcode function to maintain the references to the
underlying vertices so they cannot be garbage collected during the lifetime
of the Set.
This commit is contained in:
Katy Moe 2022-01-05 11:28:47 +00:00
parent a8bc7a0170
commit f8fdb6de3f
No known key found for this signature in database
GPG Key ID: 8C3780F6DCDDA885
1 changed files with 1 additions and 5 deletions

View File

@ -1,9 +1,5 @@
package dag package dag
import (
"fmt"
)
// Edge represents an edge in the graph, with a source and target vertex. // Edge represents an edge in the graph, with a source and target vertex.
type Edge interface { type Edge interface {
Source() Vertex Source() Vertex
@ -25,7 +21,7 @@ type basicEdge struct {
} }
func (e *basicEdge) Hashcode() interface{} { func (e *basicEdge) Hashcode() interface{} {
return fmt.Sprintf("%p-%p", e.S, e.T) return [...]interface{}{e.S, e.T}
} }
func (e *basicEdge) Source() Vertex { func (e *basicEdge) Source() Vertex {