瀏覽代碼

Cleans up comments and names regarding Graph

Wade Simba Khadder 8 年之前
父節點
當前提交
5c6a9fbe03
共有 2 個檔案被更改,包括 84 行新增79 行删除
  1. 22
    19
      internal/graph/graph.go
  2. 62
    60
      internal/graph/graph_test.go

+ 22
- 19
internal/graph/graph.go 查看文件

144
 // NodeSet is a collection of node info structs.
144
 // NodeSet is a collection of node info structs.
145
 type NodeSet map[NodeInfo]bool
145
 type NodeSet map[NodeInfo]bool
146
 
146
 
147
-// NodePtrSet is a collection of Nodes. Trimming a graph or tree requires a set
148
-// of objects that uniquely identify which nodes to keep. In a graph, NodeInfo
149
-// works as a unique identifier; however, in a tree, multiple nodes may share
150
-// identical NodeInfos. *Node do uniquely identify nodes so we can use those
151
-// instead.Though *Node also uniquely identify nodes in a Graph, we currently
152
-// rebuild Graphs from scratch using only the NodeSet when trimming, so there
153
-// would not be the required context of the initial graph to allow for the use
154
-// of *Node to identify the nodes.
147
+// NodePtrSet is a collection of nodes. Trimming a graph or tree requires a set
148
+// of objects which uniquely identify the nodes to keep. In a graph, NodeInfo
149
+// works as a unique identifier; however, in a tree multiple nodes may share
150
+// identical NodeInfos. A *Node does uniquely identify a node so we can use that
151
+// instead. Though a *Node also uniquely identifies a node in a graph,
152
+// currently, during trimming, graphs are rebult from scratch using only the
153
+// NodeSet, so there would not be the required context of the initial graph to
154
+// allow for the use of *Node.
155
 type NodePtrSet map[*Node]bool
155
 type NodePtrSet map[*Node]bool
156
 
156
 
157
 // FindOrInsertNode takes the info for a node and either returns a matching node
157
 // FindOrInsertNode takes the info for a node and either returns a matching node
341
 	return selectNodesForGraph(nodes, o.DropNegative)
341
 	return selectNodesForGraph(nodes, o.DropNegative)
342
 }
342
 }
343
 
343
 
344
-// Trims a Graph that is in forest form to contain only the nodes in kept. This
345
-// will not work correctly in the case that a node has multiple parents.
344
+// TrimTree trims a Graph in forest form, keeping only the nodes in kept. This
345
+// will not work correctly if even a single node has multiple parents.
346
 func (g *Graph) TrimTree(kept NodePtrSet) {
346
 func (g *Graph) TrimTree(kept NodePtrSet) {
347
 	// Creates a new list of nodes
347
 	// Creates a new list of nodes
348
 	oldNodes := g.Nodes
348
 	oldNodes := g.Nodes
354
 			panic("TrimTree only works on trees")
354
 			panic("TrimTree only works on trees")
355
 		}
355
 		}
356
 
356
 
357
-		// If a node should be kept, add it to the next list of nodes
357
+		// If a node should be kept, add it to the new list of nodes
358
 		if _, ok := kept[cur]; ok {
358
 		if _, ok := kept[cur]; ok {
359
 			g.Nodes = append(g.Nodes, cur)
359
 			g.Nodes = append(g.Nodes, cur)
360
 			continue
360
 			continue
361
 		}
361
 		}
362
 
362
 
363
-		// If a node has no parents, delete all the in edges of the children to make them
364
-		// all roots of their own trees.
363
+		// If a node has no parents, then delete all of the in edges of its
364
+		// children to make them each roots of their own trees.
365
 		if len(cur.In) == 0 {
365
 		if len(cur.In) == 0 {
366
 			for _, outEdge := range cur.Out {
366
 			for _, outEdge := range cur.Out {
367
 				delete(outEdge.Dest.In, cur)
367
 				delete(outEdge.Dest.In, cur)
369
 			continue
369
 			continue
370
 		}
370
 		}
371
 
371
 
372
-		// Get the parent. Since cur.In may only be of size 0 or 1, parent will be
373
-		// equal to either nil or the only node in cur.In
372
+		// Get the parent. This works since at this point cur.In must contain only
373
+		// one element.
374
+		if len(cur.In) != 1 {
375
+			panic("Get parent assertion failed. cur.In expected to be of length 1.")
376
+		}
374
 		var parent *Node
377
 		var parent *Node
375
 		for _, edge := range cur.In {
378
 		for _, edge := range cur.In {
376
 			parent = edge.Src
379
 			parent = edge.Src
606
 	return makeNodeSet(g.Nodes, nodeCutoff)
609
 	return makeNodeSet(g.Nodes, nodeCutoff)
607
 }
610
 }
608
 
611
 
609
-// discardLowFrequencyNodePtrs returns a NodePtrSet of nodes at or over a
612
+// DiscardLowFrequencyNodePtrs returns a NodePtrSet of nodes at or over a
610
 // specific cum value cutoff.
613
 // specific cum value cutoff.
611
 func (g *Graph) DiscardLowFrequencyNodePtrs(nodeCutoff int64) NodePtrSet {
614
 func (g *Graph) DiscardLowFrequencyNodePtrs(nodeCutoff int64) NodePtrSet {
612
 	cutNodes := getNodesAboveCumCutoff(g.Nodes, nodeCutoff)
615
 	cutNodes := getNodesAboveCumCutoff(g.Nodes, nodeCutoff)
626
 	return kept
629
 	return kept
627
 }
630
 }
628
 
631
 
629
-// getNodesAboveCumCutoff returns all the nodes who have a Cum value greater
630
-// than or equal to cutoff
632
+// getNodesAboveCumCutoff returns all the nodes which have a Cum value greater
633
+// than or equal to cutoff.
631
 func getNodesAboveCumCutoff(nodes Nodes, nodeCutoff int64) Nodes {
634
 func getNodesAboveCumCutoff(nodes Nodes, nodeCutoff int64) Nodes {
632
 	cutoffNodes := make(Nodes, 0, len(nodes))
635
 	cutoffNodes := make(Nodes, 0, len(nodes))
633
 	for _, n := range nodes {
636
 	for _, n := range nodes {
705
 	return makeNodeSet(g.selectTopNodes(maxNodes, visualMode), 0)
708
 	return makeNodeSet(g.selectTopNodes(maxNodes, visualMode), 0)
706
 }
709
 }
707
 
710
 
708
-// selectTopNodes returns a slice of the top maxNodes nodes in a graph
711
+// selectTopNodes returns a slice of the top maxNodes nodes in a graph.
709
 func (g *Graph) selectTopNodes(maxNodes int, visualMode bool) Nodes {
712
 func (g *Graph) selectTopNodes(maxNodes int, visualMode bool) Nodes {
710
 	if maxNodes > 0 {
713
 	if maxNodes > 0 {
711
 		if visualMode {
714
 		if visualMode {

+ 62
- 60
internal/graph/graph_test.go 查看文件

44
 	return debug
44
 	return debug
45
 }
45
 }
46
 
46
 
47
-func expectedNodesDebugString(Expected []ExpectedNode) string {
47
+func expectedNodesDebugString(expected []ExpectedNode) string {
48
 	debug := ""
48
 	debug := ""
49
-	for i, node := range Expected {
49
+	for i, node := range expected {
50
 		debug += fmt.Sprintf("Node %d: %p\n", i, node.Node)
50
 		debug += fmt.Sprintf("Node %d: %p\n", i, node.Node)
51
 	}
51
 	}
52
 
52
 
53
-	for i, node := range Expected {
53
+	for i, node := range expected {
54
 		debug += "\n"
54
 		debug += "\n"
55
 		debug += fmt.Sprintf("===  Node %d: %p  ===\n", i, node.Node)
55
 		debug += fmt.Sprintf("===  Node %d: %p  ===\n", i, node.Node)
56
 		debug += edgeMapsDebugString(node.In, node.Out)
56
 		debug += edgeMapsDebugString(node.In, node.Out)
58
 	return debug
58
 	return debug
59
 }
59
 }
60
 
60
 
61
-// Checks if all the edges in this equal all the edges in that.
61
+// edgeMapsEqual checks if all the edges in this equal all the edges in that.
62
 func edgeMapsEqual(this, that EdgeMap) bool {
62
 func edgeMapsEqual(this, that EdgeMap) bool {
63
 	if len(this) != len(that) {
63
 	if len(this) != len(that) {
64
 		return false
64
 		return false
71
 	return true
71
 	return true
72
 }
72
 }
73
 
73
 
74
-// Check if node is equal to Expected
75
-func nodesEqual(node *Node, Expected ExpectedNode) bool {
76
-	return node == Expected.Node && edgeMapsEqual(node.In, Expected.In) &&
77
-		edgeMapsEqual(node.Out, Expected.Out)
74
+// nodesEqual checks if node is equal to expected.
75
+func nodesEqual(node *Node, expected ExpectedNode) bool {
76
+	return node == expected.Node && edgeMapsEqual(node.In, expected.In) &&
77
+		edgeMapsEqual(node.Out, expected.Out)
78
 }
78
 }
79
 
79
 
80
-// Check if the graph equals the one templated by Expected.
81
-func graphsEqual(graph *Graph, Expected []ExpectedNode) bool {
82
-	if len(graph.Nodes) != len(Expected) {
80
+// graphsEqual checks if graph is equivalent to the graph templated by expected.
81
+func graphsEqual(graph *Graph, expected []ExpectedNode) bool {
82
+	if len(graph.Nodes) != len(expected) {
83
 		return false
83
 		return false
84
 	}
84
 	}
85
-	ExpectedSet := make(map[*Node]ExpectedNode)
86
-	for i := range Expected {
87
-		ExpectedSet[Expected[i].Node] = Expected[i]
85
+	expectedSet := make(map[*Node]ExpectedNode)
86
+	for i := range expected {
87
+		expectedSet[expected[i].Node] = expected[i]
88
 	}
88
 	}
89
 
89
 
90
 	for _, node := range graph.Nodes {
90
 	for _, node := range graph.Nodes {
91
-		ExpectedNode, found := ExpectedSet[node]
92
-		if !found || !nodesEqual(node, ExpectedNode) {
91
+		expectedNode, found := expectedSet[node]
92
+		if !found || !nodesEqual(node, expectedNode) {
93
 			return false
93
 			return false
94
 		}
94
 		}
95
 	}
95
 	}
107
 	Keep     NodePtrSet
107
 	Keep     NodePtrSet
108
 }
108
 }
109
 
109
 
110
-// Makes the edge from parent to child residual
110
+// makeExpectedEdgeResidual makes the edge from parent to child residual.
111
 func makeExpectedEdgeResidual(parent, child ExpectedNode) {
111
 func makeExpectedEdgeResidual(parent, child ExpectedNode) {
112
 	parent.Out[child.Node].Residual = true
112
 	parent.Out[child.Node].Residual = true
113
 	child.In[parent.Node].Residual = true
113
 	child.In[parent.Node].Residual = true
121
 	edgeMap[node].Weight = weight
121
 	edgeMap[node].Weight = weight
122
 }
122
 }
123
 
123
 
124
-// Creates a directed edges from the parent to each of the children
124
+// createEdges creates directed edges from the parent to each of the children.
125
 func createEdges(parent *Node, children ...*Node) {
125
 func createEdges(parent *Node, children ...*Node) {
126
 	for _, child := range children {
126
 	for _, child := range children {
127
 		edge := &Edge{
127
 		edge := &Edge{
133
 	}
133
 	}
134
 }
134
 }
135
 
135
 
136
-// Creates a node without any edges
136
+// createEmptyNode creates a node without any edges.
137
 func createEmptyNode() *Node {
137
 func createEmptyNode() *Node {
138
 	return &Node{
138
 	return &Node{
139
 		In:  make(EdgeMap),
139
 		In:  make(EdgeMap),
141
 	}
141
 	}
142
 }
142
 }
143
 
143
 
144
-// Creates an array of ExpectedNodes from nodes.
144
+// createExpectedNodes creates a slice of ExpectedNodes from nodes.
145
 func createExpectedNodes(nodes ...*Node) ([]ExpectedNode, NodePtrSet) {
145
 func createExpectedNodes(nodes ...*Node) ([]ExpectedNode, NodePtrSet) {
146
-	Expected := make([]ExpectedNode, len(nodes))
147
-	Keep := make(NodePtrSet, len(nodes))
146
+	expected := make([]ExpectedNode, len(nodes))
147
+	keep := make(NodePtrSet, len(nodes))
148
 
148
 
149
 	for i, node := range nodes {
149
 	for i, node := range nodes {
150
-		Expected[i] = ExpectedNode{
150
+		expected[i] = ExpectedNode{
151
 			Node: node,
151
 			Node: node,
152
 			In:   make(EdgeMap),
152
 			In:   make(EdgeMap),
153
 			Out:  make(EdgeMap),
153
 			Out:  make(EdgeMap),
154
 		}
154
 		}
155
-		Keep[node] = true
155
+		keep[node] = true
156
 	}
156
 	}
157
 
157
 
158
-	return Expected, Keep
158
+	return expected, keep
159
 }
159
 }
160
 
160
 
161
-// Creates a directed edges from the parent to each of the children
161
+// createExpectedEdges creates directed edges from the parent to each of the
162
+// children.
162
 func createExpectedEdges(parent ExpectedNode, children ...ExpectedNode) {
163
 func createExpectedEdges(parent ExpectedNode, children ...ExpectedNode) {
163
 	for _, child := range children {
164
 	for _, child := range children {
164
 		edge := &Edge{
165
 		edge := &Edge{
170
 	}
171
 	}
171
 }
172
 }
172
 
173
 
173
-// The first test case looks like:
174
+// createTestCase1 creates a test case that initally looks like:
174
 //     0
175
 //     0
175
 //     |(5)
176
 //     |(5)
176
 //     1
177
 //     1
177
 // (3)/ \(4)
178
 // (3)/ \(4)
178
-//   2   3
179
+//   2   3.
179
 //
180
 //
180
-// After Keeping 0, 2, 3. We should see:
181
+// After keeping 0, 2, and 3, it expects the graph:
181
 //     0
182
 //     0
182
 // (3)/ \(4)
183
 // (3)/ \(4)
183
-//   2   3
184
+//   2   3.
184
 func createTestCase1() TrimTreeTestCase {
185
 func createTestCase1() TrimTreeTestCase {
185
 	// Create Initial graph
186
 	// Create Initial graph
186
 	graph := &Graph{make(Nodes, 4)}
187
 	graph := &Graph{make(Nodes, 4)}
197
 	setEdgeWeight(nodes[1].Out, nodes[3], 4)
198
 	setEdgeWeight(nodes[1].Out, nodes[3], 4)
198
 
199
 
199
 	// Create Expected graph
200
 	// Create Expected graph
200
-	Expected, Keep := createExpectedNodes(nodes[0], nodes[2], nodes[3])
201
-	createExpectedEdges(Expected[0], Expected[1], Expected[2])
202
-	makeEdgeInline(Expected[0].Out, Expected[1].Node)
203
-	makeExpectedEdgeResidual(Expected[0], Expected[1])
204
-	makeExpectedEdgeResidual(Expected[0], Expected[2])
205
-	setEdgeWeight(Expected[0].Out, Expected[1].Node, 3)
206
-	setEdgeWeight(Expected[0].Out, Expected[2].Node, 4)
201
+	expected, keep := createExpectedNodes(nodes[0], nodes[2], nodes[3])
202
+	createExpectedEdges(expected[0], expected[1], expected[2])
203
+	makeEdgeInline(expected[0].Out, expected[1].Node)
204
+	makeExpectedEdgeResidual(expected[0], expected[1])
205
+	makeExpectedEdgeResidual(expected[0], expected[2])
206
+	setEdgeWeight(expected[0].Out, expected[1].Node, 3)
207
+	setEdgeWeight(expected[0].Out, expected[2].Node, 4)
207
 	return TrimTreeTestCase{
208
 	return TrimTreeTestCase{
208
 		Initial:  graph,
209
 		Initial:  graph,
209
-		Expected: Expected,
210
-		Keep:     Keep,
210
+		Expected: expected,
211
+		Keep:     keep,
211
 	}
212
 	}
212
 }
213
 }
213
 
214
 
214
-// This test case looks like:
215
+// createTestCase2 creates a test case that initially looks like:
215
 //   3
216
 //   3
216
 //   | (12)
217
 //   | (12)
217
 //   1
218
 //   1
220
 //   | (15)
221
 //   | (15)
221
 //   0
222
 //   0
222
 //   | (10)
223
 //   | (10)
223
-//   4
224
+//   4.
224
 //
225
 //
225
-// After Keeping 3 and 4. We should see:
226
+// After keeping 3 and 4, it expects the graph:
226
 //   3
227
 //   3
227
 //   | (10)
228
 //   | (10)
228
-//   4
229
+//   4.
229
 func createTestCase2() TrimTreeTestCase {
230
 func createTestCase2() TrimTreeTestCase {
230
 	// Create Initial graph
231
 	// Create Initial graph
231
 	graph := &Graph{make(Nodes, 5)}
232
 	graph := &Graph{make(Nodes, 5)}
243
 	setEdgeWeight(nodes[0].Out, nodes[4], 10)
244
 	setEdgeWeight(nodes[0].Out, nodes[4], 10)
244
 
245
 
245
 	// Create Expected graph
246
 	// Create Expected graph
246
-	Expected, Keep := createExpectedNodes(nodes[3], nodes[4])
247
-	createExpectedEdges(Expected[0], Expected[1])
248
-	makeExpectedEdgeResidual(Expected[0], Expected[1])
249
-	setEdgeWeight(Expected[0].Out, Expected[1].Node, 10)
247
+	expected, keep := createExpectedNodes(nodes[3], nodes[4])
248
+	createExpectedEdges(expected[0], expected[1])
249
+	makeExpectedEdgeResidual(expected[0], expected[1])
250
+	setEdgeWeight(expected[0].Out, expected[1].Node, 10)
250
 	return TrimTreeTestCase{
251
 	return TrimTreeTestCase{
251
 		Initial:  graph,
252
 		Initial:  graph,
252
-		Expected: Expected,
253
-		Keep:     Keep,
253
+		Expected: expected,
254
+		Keep:     keep,
254
 	}
255
 	}
255
 }
256
 }
256
 
257
 
257
-// If we trim an empty graph it should still be empty afterwards
258
+// createTestCase3 creates an initally empty graph and expects an empty graph
259
+// after trimming.
258
 func createTestCase3() TrimTreeTestCase {
260
 func createTestCase3() TrimTreeTestCase {
259
 	graph := &Graph{make(Nodes, 0)}
261
 	graph := &Graph{make(Nodes, 0)}
260
-	Expected, Keep := createExpectedNodes()
262
+	expected, keep := createExpectedNodes()
261
 	return TrimTreeTestCase{
263
 	return TrimTreeTestCase{
262
 		Initial:  graph,
264
 		Initial:  graph,
263
-		Expected: Expected,
264
-		Keep:     Keep,
265
+		Expected: expected,
266
+		Keep:     keep,
265
 	}
267
 	}
266
 }
268
 }
267
 
269
 
268
-// This test case looks like:
269
-//   0
270
+// createTestCase4 creates a test case that initially looks like:
271
+//   0.
270
 //
272
 //
271
-// After Keeping 0. We should see:
272
-//   0
273
+// After keeping 0, it expects the graph:
274
+//   0.
273
 func createTestCase4() TrimTreeTestCase {
275
 func createTestCase4() TrimTreeTestCase {
274
 	graph := &Graph{make(Nodes, 1)}
276
 	graph := &Graph{make(Nodes, 1)}
275
 	nodes := graph.Nodes
277
 	nodes := graph.Nodes
276
 	for i := range nodes {
278
 	for i := range nodes {
277
 		nodes[i] = createEmptyNode()
279
 		nodes[i] = createEmptyNode()
278
 	}
280
 	}
279
-	Expected, Keep := createExpectedNodes(nodes[0])
281
+	expected, keep := createExpectedNodes(nodes[0])
280
 	return TrimTreeTestCase{
282
 	return TrimTreeTestCase{
281
 		Initial:  graph,
283
 		Initial:  graph,
282
-		Expected: Expected,
283
-		Keep:     Keep,
284
+		Expected: expected,
285
+		Keep:     keep,
284
 	}
286
 	}
285
 }
287
 }
286
 
288