@@ -7,15 +7,15 @@ import (
7
7
"strings"
8
8
9
9
"github.com/gonum/graph"
10
- "github.com/gonum/graph/concrete"
11
10
"github.com/gonum/graph/encoding/dot"
11
+ "github.com/gonum/graph/simple"
12
12
13
13
"k8s.io/apimachinery/pkg/api/meta"
14
14
"k8s.io/apimachinery/pkg/util/sets"
15
15
)
16
16
17
17
type Node struct {
18
- concrete .Node
18
+ simple .Node
19
19
UniqueName
20
20
}
21
21
@@ -71,19 +71,19 @@ type MutableDirectedEdge interface {
71
71
}
72
72
73
73
type MutableUniqueGraph interface {
74
- graph.Mutable
74
+ graph.DirectedBuilder
75
75
MutableDirectedEdge
76
76
UniqueNodeInitializer
77
77
NodeFinder
78
78
}
79
79
80
80
type Edge struct {
81
- concrete .Edge
81
+ simple .Edge
82
82
kinds sets.String
83
83
}
84
84
85
- func NewEdge (from , to graph.Node , kinds ... string ) Edge {
86
- return Edge {concrete .Edge {F : from , T : to }, sets .NewString (kinds ... )}
85
+ func NewEdge (from , to graph.Node , weight float64 , kinds ... string ) Edge {
86
+ return Edge {Edge : simple .Edge {F : from , T : to , W : weight }, kinds : sets .NewString (kinds ... )}
87
87
}
88
88
89
89
func (e Edge ) Kinds () sets.String {
@@ -107,12 +107,12 @@ type GraphDescriber interface {
107
107
}
108
108
109
109
type Interface interface {
110
- graph.Directed
110
+ graph.DirectedBuilder
111
111
112
112
GraphDescriber
113
- MutableUniqueGraph
114
-
115
- Edges () []graph. Edge
113
+ MutableDirectedEdge
114
+ UniqueNodeInitializer
115
+ NodeFinder
116
116
}
117
117
118
118
type Namer interface {
@@ -134,38 +134,32 @@ func (namer) ResourceName(obj interface{}) string {
134
134
135
135
type Graph struct {
136
136
// the standard graph
137
- graph.Directed
137
+ graph.DirectedBuilder
138
138
// helper methods for switching on the kind and types of the node
139
139
GraphDescriber
140
140
141
141
// exposes the public interface for adding nodes
142
142
uniqueNamedGraph
143
143
// the internal graph object, which allows edges and nodes to be directly added
144
- internal * concrete .DirectedGraph
144
+ internal * simple .DirectedGraph
145
145
}
146
146
147
147
// Graph must implement MutableUniqueGraph
148
148
var _ MutableUniqueGraph = Graph {}
149
149
150
150
// New initializes a graph from input to output.
151
151
func New () Graph {
152
- g := concrete .NewDirectedGraph ()
152
+ g := simple .NewDirectedGraph (1.0 , 0.0 )
153
153
return Graph {
154
- Directed : g ,
155
- GraphDescriber : typedGraph {},
154
+ DirectedBuilder : g ,
155
+ GraphDescriber : typedGraph {},
156
156
157
157
uniqueNamedGraph : newUniqueNamedGraph (g ),
158
158
159
159
internal : g ,
160
160
}
161
161
}
162
162
163
- // Edges returns all the edges of the graph. Note that the returned set
164
- // will have no specific ordering.
165
- func (g Graph ) Edges () []graph.Edge {
166
- return g .internal .Edges ()
167
- }
168
-
169
163
func (g Graph ) String () string {
170
164
ret := ""
171
165
@@ -189,6 +183,18 @@ func (g Graph) String() string {
189
183
return ret
190
184
}
191
185
186
+ func (g Graph ) Edges () []graph.Edge {
187
+ return g .internal .Edges ()
188
+ }
189
+
190
+ func (g Graph ) RemoveEdge (e graph.Edge ) {
191
+ g .internal .RemoveEdge (e )
192
+ }
193
+
194
+ func (g Graph ) RemoveNode (node graph.Node ) {
195
+ g .internal .RemoveNode (node )
196
+ }
197
+
192
198
// ByID is a sorted group of nodes by ID
193
199
type ByID []graph.Node
194
200
@@ -198,23 +204,6 @@ func (m ByID) Less(i, j int) bool {
198
204
return m [i ].ID () < m [j ].ID ()
199
205
}
200
206
201
- // SyntheticNodes returns back the set of nodes that were created in response to edge requests, but did not exist
202
- func (g Graph ) SyntheticNodes () []graph.Node {
203
- ret := []graph.Node {}
204
-
205
- nodes := g .Nodes ()
206
- sort .Sort (ByID (nodes ))
207
- for _ , node := range nodes {
208
- if potentiallySyntheticNode , ok := node .(ExistenceChecker ); ok {
209
- if ! potentiallySyntheticNode .Found () {
210
- ret = append (ret , node )
211
- }
212
- }
213
- }
214
-
215
- return ret
216
- }
217
-
218
207
// NodesByKind returns all the nodes of the graph with the provided kinds
219
208
func (g Graph ) NodesByKind (nodeKinds ... string ) []graph.Node {
220
209
ret := []graph.Node {}
@@ -229,18 +218,6 @@ func (g Graph) NodesByKind(nodeKinds ...string) []graph.Node {
229
218
return ret
230
219
}
231
220
232
- // RootNodes returns all the roots of this graph.
233
- func (g Graph ) RootNodes () []graph.Node {
234
- roots := []graph.Node {}
235
- for _ , n := range g .Nodes () {
236
- if len (g .To (n )) != 0 {
237
- continue
238
- }
239
- roots = append (roots , n )
240
- }
241
- return roots
242
- }
243
-
244
221
// PredecessorEdges invokes fn with all of the predecessor edges of node that have the specified
245
222
// edge kind.
246
223
func (g Graph ) PredecessorEdges (node graph.Node , fn EdgeFunc , edgeKinds ... string ) {
@@ -365,21 +342,21 @@ func (g Graph) AddEdge(from, to graph.Node, edgeKind string) {
365
342
kinds .Insert (g .EdgeKinds (existingEdge ).List ()... )
366
343
}
367
344
368
- g .internal .SetEdge (NewEdge (from , to , kinds .List ()... ), 1.0 )
345
+ g .internal .SetEdge (NewEdge (from , to , 1.0 , kinds .List ()... ))
369
346
}
370
347
371
348
// addEdges adds the specified edges, filtered by the provided edge connection
372
349
// function.
373
350
func (g Graph ) addEdges (edges []graph.Edge , fn EdgeFunc ) {
374
351
for _ , e := range edges {
375
352
switch t := e .(type ) {
376
- case concrete.WeightedEdge :
377
- if fn (g , t .From (), t .To (), t .Edge .(Edge ).Kinds ()) {
378
- g .internal .SetEdge (t .Edge .(Edge ), t .Cost )
379
- }
380
353
case Edge :
381
354
if fn (g , t .From (), t .To (), t .Kinds ()) {
382
- g .internal .SetEdge (t , 1.0 )
355
+ g .internal .SetEdge (t )
356
+ }
357
+ case simple.Edge :
358
+ if fn (g , t .From (), t .To (), sets .NewString ()) {
359
+ g .internal .SetEdge (t )
383
360
}
384
361
default :
385
362
panic ("bad edge" )
@@ -484,20 +461,6 @@ func (g Graph) SubgraphWithNodes(nodes []graph.Node, fn EdgeFunc) Graph {
484
461
return out
485
462
}
486
463
487
- // ConnectedEdgeSubgraph creates a new graph that iterates through all edges in the graph
488
- // and includes all edges the provided function returns true for. Nodes not referenced by
489
- // an edge will be dropped unless the function adds them explicitly.
490
- func (g Graph ) ConnectedEdgeSubgraph (fn EdgeFunc ) Graph {
491
- out := New ()
492
- out .addEdges (g .internal .Edges (), fn )
493
- return out
494
- }
495
-
496
- // AllNodes includes all nodes in the graph
497
- func AllNodes (g Interface , node graph.Node ) bool {
498
- return true
499
- }
500
-
501
464
// ExistingDirectEdge returns true if both from and to already exist in the graph and the edge kind is
502
465
// not ReferencedByEdgeKind (the generic reverse edge kind). This will purge the graph of any
503
466
// edges created by AddReversedEdge.
@@ -528,26 +491,14 @@ func AddReversedEdge(g Interface, from, to graph.Node, edgeKinds sets.String) bo
528
491
return true
529
492
}
530
493
531
- // AddGraphEdgesTo returns an EdgeFunc that will add the selected edges to the passed
532
- // graph.
533
- func AddGraphEdgesTo (g Interface ) EdgeFunc {
534
- return func (_ Interface , from , to graph.Node , edgeKinds sets.String ) bool {
535
- for edgeKind := range edgeKinds {
536
- g .AddEdge (from , to , edgeKind )
537
- }
538
-
539
- return false
540
- }
541
- }
542
-
543
494
type uniqueNamedGraph struct {
544
- graph.Mutable
495
+ graph.Builder
545
496
names map [UniqueName ]graph.Node
546
497
}
547
498
548
- func newUniqueNamedGraph (g graph.Mutable ) uniqueNamedGraph {
499
+ func newUniqueNamedGraph (g graph.Builder ) uniqueNamedGraph {
549
500
return uniqueNamedGraph {
550
- Mutable : g ,
501
+ Builder : g ,
551
502
names : make (map [UniqueName ]graph.Node ),
552
503
}
553
504
}
@@ -557,7 +508,7 @@ func (g uniqueNamedGraph) FindOrCreate(name UniqueName, fn NodeInitializerFunc)
557
508
return node , true
558
509
}
559
510
id := g .NewNodeID ()
560
- node := fn (Node {concrete .Node (id ), name })
511
+ node := fn (Node {simple .Node (id ), name })
561
512
g .names [name ] = node
562
513
g .AddNode (node )
563
514
return node , false
@@ -610,27 +561,16 @@ func (g typedGraph) Kind(node graph.Node) string {
610
561
func (g typedGraph ) EdgeKinds (edge graph.Edge ) sets.String {
611
562
var e Edge
612
563
switch t := edge .(type ) {
613
- case concrete.WeightedEdge :
614
- e = t .Edge .(Edge )
615
564
case Edge :
616
565
e = t
566
+ case simple.Edge :
567
+ e = Edge {Edge : t }
617
568
default :
618
569
return sets .NewString (UnknownEdgeKind )
619
570
}
620
571
return e .Kinds ()
621
572
}
622
573
623
- type NodeSet map [int ]struct {}
624
-
625
- func (n NodeSet ) Has (id int ) bool {
626
- _ , ok := n [id ]
627
- return ok
628
- }
629
-
630
- func (n NodeSet ) Add (id int ) {
631
- n [id ] = struct {}{}
632
- }
633
-
634
574
func NodesByKind (g Interface , nodes []graph.Node , kinds ... string ) [][]graph.Node {
635
575
buckets := make (map [string ]int )
636
576
for i , kind := range kinds {
0 commit comments