Skip to content

Commit 0a478b4

Browse files
author
Michal Minář
committed
post-bump(gonum/graph): use the new interface
Signed-off-by: Michal Minář <[email protected]>
1 parent 83c095a commit 0a478b4

File tree

10 files changed

+168
-137
lines changed

10 files changed

+168
-137
lines changed

pkg/oc/cli/describe/chaindescriber_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"testing"
66

77
"github.com/gonum/graph"
8-
"github.com/gonum/graph/concrete"
8+
"github.com/gonum/graph/simple"
99

1010
"k8s.io/apimachinery/pkg/runtime"
1111
"k8s.io/apimachinery/pkg/util/sets"
@@ -275,15 +275,15 @@ func filterByScheme(scheme *runtime.Scheme, in ...runtime.Object) []runtime.Obje
275275
}
276276

277277
func TestDepthFirst(t *testing.T) {
278-
g := concrete.NewDirectedGraph()
279-
280-
a := concrete.Node(g.NewNodeID())
281-
b := concrete.Node(g.NewNodeID())
278+
g := simple.NewDirectedGraph(1.0, 0.0)
282279

280+
a := simple.Node(g.NewNodeID())
283281
g.AddNode(a)
282+
b := simple.Node(g.NewNodeID())
284283
g.AddNode(b)
285-
g.SetEdge(concrete.Edge{F: a, T: b}, 1)
286-
g.SetEdge(concrete.Edge{F: b, T: a}, 1)
284+
285+
g.SetEdge(simple.Edge{F: a, T: b})
286+
g.SetEdge(simple.Edge{F: b, T: a})
287287

288288
count := 0
289289

pkg/oc/graph/appsgraph/edge_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestNamespaceEdgeMatching(t *testing.T) {
4141
fn("other", g)
4242
AddAllDeploymentConfigsDeploymentEdges(g)
4343

44-
if len(g.Edges()) != 4 {
44+
if len(g.Edges()) != 6 {
4545
t.Fatal(g)
4646
}
4747
for _, edge := range g.Edges() {

pkg/oc/graph/genericgraph/graph.go

Lines changed: 39 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import (
77
"strings"
88

99
"github.com/gonum/graph"
10-
"github.com/gonum/graph/concrete"
1110
"github.com/gonum/graph/encoding/dot"
11+
"github.com/gonum/graph/simple"
1212

1313
"k8s.io/apimachinery/pkg/api/meta"
1414
"k8s.io/apimachinery/pkg/util/sets"
1515
)
1616

1717
type Node struct {
18-
concrete.Node
18+
simple.Node
1919
UniqueName
2020
}
2121

@@ -71,19 +71,19 @@ type MutableDirectedEdge interface {
7171
}
7272

7373
type MutableUniqueGraph interface {
74-
graph.Mutable
74+
graph.DirectedBuilder
7575
MutableDirectedEdge
7676
UniqueNodeInitializer
7777
NodeFinder
7878
}
7979

8080
type Edge struct {
81-
concrete.Edge
81+
simple.Edge
8282
kinds sets.String
8383
}
8484

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...)}
8787
}
8888

8989
func (e Edge) Kinds() sets.String {
@@ -107,12 +107,12 @@ type GraphDescriber interface {
107107
}
108108

109109
type Interface interface {
110-
graph.Directed
110+
graph.DirectedBuilder
111111

112112
GraphDescriber
113-
MutableUniqueGraph
114-
115-
Edges() []graph.Edge
113+
MutableDirectedEdge
114+
UniqueNodeInitializer
115+
NodeFinder
116116
}
117117

118118
type Namer interface {
@@ -134,38 +134,32 @@ func (namer) ResourceName(obj interface{}) string {
134134

135135
type Graph struct {
136136
// the standard graph
137-
graph.Directed
137+
graph.DirectedBuilder
138138
// helper methods for switching on the kind and types of the node
139139
GraphDescriber
140140

141141
// exposes the public interface for adding nodes
142142
uniqueNamedGraph
143143
// the internal graph object, which allows edges and nodes to be directly added
144-
internal *concrete.DirectedGraph
144+
internal *simple.DirectedGraph
145145
}
146146

147147
// Graph must implement MutableUniqueGraph
148148
var _ MutableUniqueGraph = Graph{}
149149

150150
// New initializes a graph from input to output.
151151
func New() Graph {
152-
g := concrete.NewDirectedGraph()
152+
g := simple.NewDirectedGraph(1.0, 0.0)
153153
return Graph{
154-
Directed: g,
155-
GraphDescriber: typedGraph{},
154+
DirectedBuilder: g,
155+
GraphDescriber: typedGraph{},
156156

157157
uniqueNamedGraph: newUniqueNamedGraph(g),
158158

159159
internal: g,
160160
}
161161
}
162162

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-
169163
func (g Graph) String() string {
170164
ret := ""
171165

@@ -189,6 +183,18 @@ func (g Graph) String() string {
189183
return ret
190184
}
191185

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+
192198
// ByID is a sorted group of nodes by ID
193199
type ByID []graph.Node
194200

@@ -198,23 +204,6 @@ func (m ByID) Less(i, j int) bool {
198204
return m[i].ID() < m[j].ID()
199205
}
200206

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-
218207
// NodesByKind returns all the nodes of the graph with the provided kinds
219208
func (g Graph) NodesByKind(nodeKinds ...string) []graph.Node {
220209
ret := []graph.Node{}
@@ -229,18 +218,6 @@ func (g Graph) NodesByKind(nodeKinds ...string) []graph.Node {
229218
return ret
230219
}
231220

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-
244221
// PredecessorEdges invokes fn with all of the predecessor edges of node that have the specified
245222
// edge kind.
246223
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) {
365342
kinds.Insert(g.EdgeKinds(existingEdge).List()...)
366343
}
367344

368-
g.internal.SetEdge(NewEdge(from, to, kinds.List()...), 1.0)
345+
g.internal.SetEdge(NewEdge(from, to, 1.0, kinds.List()...))
369346
}
370347

371348
// addEdges adds the specified edges, filtered by the provided edge connection
372349
// function.
373350
func (g Graph) addEdges(edges []graph.Edge, fn EdgeFunc) {
374351
for _, e := range edges {
375352
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-
}
380353
case Edge:
381354
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)
383360
}
384361
default:
385362
panic("bad edge")
@@ -484,20 +461,6 @@ func (g Graph) SubgraphWithNodes(nodes []graph.Node, fn EdgeFunc) Graph {
484461
return out
485462
}
486463

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-
501464
// ExistingDirectEdge returns true if both from and to already exist in the graph and the edge kind is
502465
// not ReferencedByEdgeKind (the generic reverse edge kind). This will purge the graph of any
503466
// edges created by AddReversedEdge.
@@ -528,26 +491,14 @@ func AddReversedEdge(g Interface, from, to graph.Node, edgeKinds sets.String) bo
528491
return true
529492
}
530493

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-
543494
type uniqueNamedGraph struct {
544-
graph.Mutable
495+
graph.Builder
545496
names map[UniqueName]graph.Node
546497
}
547498

548-
func newUniqueNamedGraph(g graph.Mutable) uniqueNamedGraph {
499+
func newUniqueNamedGraph(g graph.Builder) uniqueNamedGraph {
549500
return uniqueNamedGraph{
550-
Mutable: g,
501+
Builder: g,
551502
names: make(map[UniqueName]graph.Node),
552503
}
553504
}
@@ -557,7 +508,7 @@ func (g uniqueNamedGraph) FindOrCreate(name UniqueName, fn NodeInitializerFunc)
557508
return node, true
558509
}
559510
id := g.NewNodeID()
560-
node := fn(Node{concrete.Node(id), name})
511+
node := fn(Node{simple.Node(id), name})
561512
g.names[name] = node
562513
g.AddNode(node)
563514
return node, false
@@ -610,27 +561,16 @@ func (g typedGraph) Kind(node graph.Node) string {
610561
func (g typedGraph) EdgeKinds(edge graph.Edge) sets.String {
611562
var e Edge
612563
switch t := edge.(type) {
613-
case concrete.WeightedEdge:
614-
e = t.Edge.(Edge)
615564
case Edge:
616565
e = t
566+
case simple.Edge:
567+
e = Edge{Edge: t}
617568
default:
618569
return sets.NewString(UnknownEdgeKind)
619570
}
620571
return e.Kinds()
621572
}
622573

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-
634574
func NodesByKind(g Interface, nodes []graph.Node, kinds ...string) [][]graph.Node {
635575
buckets := make(map[string]int)
636576
for i, kind := range kinds {

pkg/oc/graph/genericgraph/graphview/veneering_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"time"
66

77
"github.com/gonum/graph"
8-
"github.com/gonum/graph/concrete"
8+
"github.com/gonum/graph/simple"
99

1010
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1111
kapi "k8s.io/kubernetes/pkg/apis/core"
@@ -368,7 +368,7 @@ func TestGraph(t *testing.T) {
368368
}
369369
}
370370

371-
edge := g.Edge(concrete.Node(bcTestNode.ID()), concrete.Node(istID))
371+
edge := g.Edge(simple.Node(bcTestNode.ID()), simple.Node(istID))
372372
if edge == nil {
373373
t.Fatalf("failed to find edge between %d and %d", bcTestNode.ID(), istID)
374374
}
@@ -379,7 +379,7 @@ func TestGraph(t *testing.T) {
379379
t.Fatalf("expected one edge")
380380
}
381381

382-
if e := g.Edge(concrete.Node(bcTestNode.ID()), concrete.Node(istID)); e == nil {
382+
if e := g.Edge(simple.Node(bcTestNode.ID()), simple.Node(istID)); e == nil {
383383
t.Errorf("expected edge for %d-%d", bcTestNode.ID(), istID)
384384
}
385385

0 commit comments

Comments
 (0)