-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.go
60 lines (50 loc) · 1.51 KB
/
graph.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package go_directed_acyclic_graph
type graph struct {
nodes *nodeList
}
func newGraph() *graph {
return &graph{
nodes: newNodeList(),
}
}
// Copy returns a clone of the graph.
func (g *graph) Copy() *graph {
return &graph{
nodes: g.nodes.Copy(),
}
}
// Nodes returns the graph's nodes.
// The slice is mutable for performance reasons but should not be mutated.
func (g *graph) Nodes() []Node {
return g.nodes.Nodes()
}
// NodeCount returns the number of nodes.
func (g *graph) NodeCount() int {
return g.nodes.Count()
}
// AddNode inserts the specified node into the graph.
// A node can be any value, e.g. int, string, pointer to a struct, map etc.
// Duplicate nodes are ignored.
func (g *graph) AddNode(node Node) {
g.AddNodes(node)
}
// AddNodes inserts the specified nodes into the graph.
// A node can be any value, e.g. int, string, pointer to a struct, map etc.
// Duplicate nodes are ignored.
func (g *graph) AddNodes(nodes ...Node) {
g.nodes.Add(nodes...)
}
// RemoveNode removes the specified nodes from the graph.
// If the node does not exist within the graph the call will fail silently.
func (g *graph) RemoveNode(node Node) {
g.RemoveNodes(node)
}
// RemoveNodes removes the specified nodes from the graph.
// If a node does not exist within the graph the call will fail silently.
func (g *graph) RemoveNodes(nodes ...Node) {
g.nodes.Remove(nodes...)
}
// NodeExists determines whether the specified node exists within the graph.
func (g *graph) NodeExists(node Node) bool {
return g.nodes.Exists(node)
}