浏览代码

Refactors TrimTree

Wade Simba Khadder 8 年前
父节点
当前提交
7b6f12434c
共有 1 个文件被更改,包括 22 次插入21 次删除
  1. 22
    21
      internal/graph/graph.go

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

21
 	"os"
21
 	"os"
22
 	"path/filepath"
22
 	"path/filepath"
23
 	"sort"
23
 	"sort"
24
+	"errors"
24
 	"strconv"
25
 	"strconv"
25
 	"strings"
26
 	"strings"
26
 
27
 
345
 	for _, cur := range oldNodes {
346
 	for _, cur := range oldNodes {
346
 		// A node may not have multiple parents
347
 		// A node may not have multiple parents
347
 		if len(cur.In) > 1 {
348
 		if len(cur.In) > 1 {
348
-			fmt.Fprintf(os.Stderr, "ERROR: TrimTree only works on trees.\n")
349
+			panic(errors.New("TrimTree only works on trees.\n"))
349
 		}
350
 		}
350
 
351
 
351
 		// If a node should be kept, add it to the next list of nodes
352
 		// If a node should be kept, add it to the next list of nodes
354
 			continue
355
 			continue
355
 		}
356
 		}
356
 
357
 
358
+		// If a node has no parents, delete all the in edges of the children to make them
359
+		// all roots of their own trees.
360
+		if len(cur.In) == 0 {
361
+			for _, outEdge := range cur.Out {
362
+				delete(outEdge.Dest.In, cur)
363
+			}
364
+		}
365
+
357
 		// Get the parent. Since cur.In may only be of size 0 or 1, parent will be
366
 		// Get the parent. Since cur.In may only be of size 0 or 1, parent will be
358
 		// equal to either nil or the only node in cur.In
367
 		// equal to either nil or the only node in cur.In
359
 		var parent *Node
368
 		var parent *Node
361
 			parent = edge.Src
370
 			parent = edge.Src
362
 		}
371
 		}
363
 
372
 
364
-		if parent != nil {
365
-			// Remove the edge from the parent to this node
366
-			delete(parent.Out, cur)
373
+		// Remove the edge from the parent to this node
374
+		delete(parent.Out, cur)
367
 
375
 
368
-			// Reconfigure every edge from the current node to now begin at the parent.
369
-			for _, outEdge := range cur.Out {
370
-				child := outEdge.Dest
376
+		// Reconfigure every edge from the current node to now begin at the parent.
377
+		for _, outEdge := range cur.Out {
378
+			child := outEdge.Dest
371
 
379
 
372
-				delete(child.In, cur)
373
-				child.In[parent] = outEdge
374
-				parent.Out[child] = outEdge
380
+			delete(child.In, cur)
381
+			child.In[parent] = outEdge
382
+			parent.Out[child] = outEdge
375
 
383
 
376
-				outEdge.Src = parent
377
-				outEdge.Residual = true
378
-				// Any reconfigured edge can no longer be Inline.
379
-				outEdge.Inline = false
380
-			}
381
-		} else {
382
-			// If a node has no parents, delete all the in edges of the children to make them
383
-			// all roots of their own trees.
384
-			for _, outEdge := range cur.Out {
385
-				delete(outEdge.Dest.In, cur)
386
-			}
384
+			outEdge.Src = parent
385
+			outEdge.Residual = true
386
+			// Any reconfigured edge can no longer be Inline.
387
+			outEdge.Inline = false
387
 		}
388
 		}
388
 	}
389
 	}
389
 	g.RemoveRedundantEdges()
390
 	g.RemoveRedundantEdges()