opentofu/internal/dag/edge.go
Katy Moe f8fdb6de3f
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.
2022-01-05 11:28:47 +00:00

34 lines
664 B
Go

package dag
// Edge represents an edge in the graph, with a source and target vertex.
type Edge interface {
Source() Vertex
Target() Vertex
Hashable
}
// BasicEdge returns an Edge implementation that simply tracks the source
// and target given as-is.
func BasicEdge(source, target Vertex) Edge {
return &basicEdge{S: source, T: target}
}
// basicEdge is a basic implementation of Edge that has the source and
// target vertex.
type basicEdge struct {
S, T Vertex
}
func (e *basicEdge) Hashcode() interface{} {
return [...]interface{}{e.S, e.T}
}
func (e *basicEdge) Source() Vertex {
return e.S
}
func (e *basicEdge) Target() Vertex {
return e.T
}