mirror of https://github.com/hashicorp/terraform
parent
87f4c3aae1
commit
2a910585a2
@ -0,0 +1,27 @@
|
||||
package dag
|
||||
|
||||
// Edge represents an edge in the graph, with a source and target vertex.
|
||||
type Edge interface {
|
||||
Source() Vertex
|
||||
Target() Vertex
|
||||
}
|
||||
|
||||
// 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) Source() Vertex {
|
||||
return e.S
|
||||
}
|
||||
|
||||
func (e *basicEdge) Target() Vertex {
|
||||
return e.T
|
||||
}
|
||||
@ -0,0 +1,45 @@
|
||||
package dag
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGraph_empty(t *testing.T) {
|
||||
var g Graph
|
||||
g.Add(1)
|
||||
g.Add(2)
|
||||
g.Add(3)
|
||||
|
||||
actual := strings.TrimSpace(g.String())
|
||||
expected := strings.TrimSpace(testGraphEmptyStr)
|
||||
if actual != expected {
|
||||
t.Fatalf("bad: %s", actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGraph_basic(t *testing.T) {
|
||||
var g Graph
|
||||
g.Add(1)
|
||||
g.Add(2)
|
||||
g.Add(3)
|
||||
g.Connect(BasicEdge(1, 3))
|
||||
|
||||
actual := strings.TrimSpace(g.String())
|
||||
expected := strings.TrimSpace(testGraphBasicStr)
|
||||
if actual != expected {
|
||||
t.Fatalf("bad: %s", actual)
|
||||
}
|
||||
}
|
||||
|
||||
const testGraphBasicStr = `
|
||||
1
|
||||
3
|
||||
2
|
||||
3
|
||||
`
|
||||
const testGraphEmptyStr = `
|
||||
1
|
||||
2
|
||||
3
|
||||
`
|
||||
@ -0,0 +1,58 @@
|
||||
package dag
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
// set is an internal Set data structure that is based on simply using
|
||||
// pointers as the hash key into a map.
|
||||
type set struct {
|
||||
m map[interface{}]struct{}
|
||||
once sync.Once
|
||||
}
|
||||
|
||||
// Add adds an item to the set
|
||||
func (s *set) Add(v interface{}) {
|
||||
s.once.Do(s.init)
|
||||
s.m[v] = struct{}{}
|
||||
}
|
||||
|
||||
// Delete removes an item from the set.
|
||||
func (s *set) Delete(v interface{}) {
|
||||
s.once.Do(s.init)
|
||||
delete(s.m, v)
|
||||
}
|
||||
|
||||
// Include returns true/false of whether a value is in the set.
|
||||
func (s *set) Include(v interface{}) bool {
|
||||
s.once.Do(s.init)
|
||||
_, ok := s.m[v]
|
||||
return ok
|
||||
}
|
||||
|
||||
// Len is the number of items in the set.
|
||||
func (s *set) Len() int {
|
||||
if s == nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
return len(s.m)
|
||||
}
|
||||
|
||||
// List returns the list of set elements.
|
||||
func (s *set) List() []interface{} {
|
||||
if s == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
r := make([]interface{}, 0, len(s.m))
|
||||
for k, _ := range s.m {
|
||||
r = append(r, k)
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
func (s *set) init() {
|
||||
s.m = make(map[interface{}]struct{})
|
||||
}
|
||||
Loading…
Reference in new issue