Debian Codegraph @

build/union.go

raw symbols SCIP
1 package build
2
3 // Union returns the graph union g1 ∪ g2, which
4 // consists of the union of the two vertex sets
5 // and the union of the two edge sets of g1 and g2.
6 // The edges of the new graph will have zero cost.
7 func (g1 *Virtual) Union(g2 *Virtual) *Virtual {
8 return g1.union(g2, false)
9 }
10
11 // If cost is true keep costs and use g1's cost for edges that belong to both g1 and g2.
12 func (g1 *Virtual) union(g2 *Virtual, cost bool) *Virtual {
13 switch {
14 case g1.order == 0:
15 if cost {
16 return g2
17 }
18 return g2.AddCost(0)
19 case g2.order == 0:
20 if cost {
21 return g1
22 }
23 return g1.AddCost(0)
24 }
25
26 newCost := zero
27 if cost {
28 newCost = func(v, w int) int64 {
29 if g2.edge(v, w) && !g1.edge(v, w) {
30 return g2.cost(v, w)
31 }
32 return g1.cost(v, w)
33 }
34 }
35 var res *Virtual
36 switch {
37 case g1.order == g2.order:
38 res = generic(g1.order, newCost, func(v, w int) bool {
39 return g1.edge(v, w) || g2.edge(v, w)
40 })
41 case g1.order < g2.order:
42 res = generic(g2.order, newCost, func(v, w int) bool {
43 return v < g1.order && w < g1.order && g1.edge(v, w) || g2.edge(v, w)
44 })
45 default:
46 res = generic(g1.order, newCost, func(v, w int) bool {
47 return v < g2.order && w < g2.order && g2.edge(v, w) || g1.edge(v, w)
48 })
49 }
50
51 res.visit = func(v int, a int, do func(w int, c int64) bool) (aborted bool) {
52 next := 0
53 if v < g1.order && g1.visit(v, a, func(w int, c int64) (skip bool) {
54 // First all neighbors from g2 that are less than w...
55 if next != w && v < g2.order && a <= w {
56 if more := false; g2.visit(v, max(next, a), func(w0 int, c0 int64) (skip bool) {
57 if w0 >= w {
58 more, skip = true, true
59 return
60 }
61 if cost {
62 return do(w0, c0)
63 }
64 return do(w0, 0)
65 }) && !more {
66 return true
67 }
68 }
69 // ...then w.
70 switch {
71 case cost && do(w, c):
72 return true
73 case !cost && do(w, 0):
74 return true
75 }
76 next = w + 1
77 return
78 }) {
79 return true
80 }
81 // When done with g1, produce any leftovers from g2.
82 return v < g2.order && g2.visit(v, max(next, a), func(w int, c int64) (skip bool) {
83 if cost {
84 return do(w, c)
85 }
86 return do(w, 0)
87 })
88 }
89 return res
90 }