Browse Source

Cleans up comments/naming regarding TrimTree

Wade Simba Khadder 9 years ago
parent
commit
c577814d78
2 changed files with 24 additions and 15 deletions
  1. 21
    12
      internal/graph/graph.go
  2. 3
    3
      internal/graph/graph_test.go

+ 21
- 12
internal/graph/graph.go View File

141
 // report entries with the same info.
141
 // report entries with the same info.
142
 type NodeMap map[NodeInfo]*Node
142
 type NodeMap map[NodeInfo]*Node
143
 
143
 
144
-// NodeSet maps 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
-type nodePtrSet map[*Node]bool
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.
155
+type NodePtrSet map[*Node]bool
148
 
156
 
149
 // 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
150
 // from the node map if one exists, or adds one to the map if one does not.
158
 // from the node map if one exists, or adds one to the map if one does not.
335
 
343
 
336
 // Trims a Graph that is in forest form to contain only the nodes in kept. This
344
 // Trims a Graph that is in forest form to contain only the nodes in kept. This
337
 // will not work correctly in the case that a node has multiple parents.
345
 // will not work correctly in the case that a node has multiple parents.
338
-func (g *Graph) TrimTree(kept nodePtrSet) {
346
+func (g *Graph) TrimTree(kept NodePtrSet) {
339
 	// Creates a new list of nodes
347
 	// Creates a new list of nodes
340
 	oldNodes := g.Nodes
348
 	oldNodes := g.Nodes
341
 	g.Nodes = make(Nodes, 0, len(kept))
349
 	g.Nodes = make(Nodes, 0, len(kept))
600
 
608
 
601
 // discardLowFrequencyNodePtrs returns a NodePtrSet of nodes at or over a
609
 // discardLowFrequencyNodePtrs returns a NodePtrSet of nodes at or over a
602
 // specific cum value cutoff.
610
 // specific cum value cutoff.
603
-func (g *Graph) DiscardLowFrequencyNodePtrs(nodeCutoff int64) nodePtrSet {
604
-	cutNodes := getNodesWithCumCutoff(g.Nodes, nodeCutoff)
605
-	kept := make(nodePtrSet, len(cutNodes))
611
+func (g *Graph) DiscardLowFrequencyNodePtrs(nodeCutoff int64) NodePtrSet {
612
+	cutNodes := getNodesAboveCumCutoff(g.Nodes, nodeCutoff)
613
+	kept := make(NodePtrSet, len(cutNodes))
606
 	for _, n := range cutNodes {
614
 	for _, n := range cutNodes {
607
 		kept[n] = true
615
 		kept[n] = true
608
 	}
616
 	}
610
 }
618
 }
611
 
619
 
612
 func makeNodeSet(nodes Nodes, nodeCutoff int64) NodeSet {
620
 func makeNodeSet(nodes Nodes, nodeCutoff int64) NodeSet {
613
-	cutNodes := getNodesWithCumCutoff(nodes, nodeCutoff)
621
+	cutNodes := getNodesAboveCumCutoff(nodes, nodeCutoff)
614
 	kept := make(NodeSet, len(cutNodes))
622
 	kept := make(NodeSet, len(cutNodes))
615
 	for _, n := range cutNodes {
623
 	for _, n := range cutNodes {
616
 		kept[n.Info] = true
624
 		kept[n.Info] = true
618
 	return kept
626
 	return kept
619
 }
627
 }
620
 
628
 
621
-// Returns all the nodes who have a Cum value greater than or equal to cutoff
622
-func getNodesWithCumCutoff(nodes Nodes, nodeCutoff int64) Nodes {
629
+// getNodesAboveCumCutoff returns all the nodes who have a Cum value greater
630
+// than or equal to cutoff
631
+func getNodesAboveCumCutoff(nodes Nodes, nodeCutoff int64) Nodes {
623
 	cutoffNodes := make(Nodes, 0, len(nodes))
632
 	cutoffNodes := make(Nodes, 0, len(nodes))
624
 	for _, n := range nodes {
633
 	for _, n := range nodes {
625
 		if abs64(n.Cum) < nodeCutoff {
634
 		if abs64(n.Cum) < nodeCutoff {
682
 	}
691
 	}
683
 }
692
 }
684
 
693
 
685
-// selectTopNodePtrs returns a set of the top maxNodes *Node in a graph.
686
-func (g *Graph) SelectTopNodePtrs(maxNodes int, visualMode bool) nodePtrSet {
687
-	set := make(nodePtrSet)
694
+// SelectTopNodePtrs returns a set of the top maxNodes *Node in a graph.
695
+func (g *Graph) SelectTopNodePtrs(maxNodes int, visualMode bool) NodePtrSet {
696
+	set := make(NodePtrSet)
688
 	for _, node := range g.selectTopNodes(maxNodes, visualMode) {
697
 	for _, node := range g.selectTopNodes(maxNodes, visualMode) {
689
 		set[node] = true
698
 		set[node] = true
690
 	}
699
 	}

+ 3
- 3
internal/graph/graph_test.go View File

109
 type TrimTreeTestCase struct {
109
 type TrimTreeTestCase struct {
110
 	Initial  *Graph
110
 	Initial  *Graph
111
 	Expected []ExpectedNode
111
 	Expected []ExpectedNode
112
-	Keep     nodePtrSet
112
+	Keep     NodePtrSet
113
 }
113
 }
114
 
114
 
115
 // Makes the edge from parent to child residual
115
 // Makes the edge from parent to child residual
143
 }
143
 }
144
 
144
 
145
 // Creates an array of ExpectedNodes from nodes.
145
 // Creates an array of ExpectedNodes from nodes.
146
-func createExpectedNodes(nodes ...*Node) ([]ExpectedNode, nodePtrSet) {
146
+func createExpectedNodes(nodes ...*Node) ([]ExpectedNode, NodePtrSet) {
147
 	Expected := make([]ExpectedNode, len(nodes))
147
 	Expected := make([]ExpectedNode, len(nodes))
148
-	Keep := make(nodePtrSet, len(nodes))
148
+	Keep := make(NodePtrSet, len(nodes))
149
 
149
 
150
 	for i, node := range nodes {
150
 	for i, node := range nodes {
151
 		Expected[i] = ExpectedNode{
151
 		Expected[i] = ExpectedNode{