Debian Codegraph @

build/match.go

raw symbols SCIP
1 package build
2
3 // Match connects g1 to g2 by matching vertices in g1 with vertices in g2.
4 // Only vertices belonging to the bridge are included,
5 // and the vertices are matched in numerical order.
6 // The vertices of g2 are renumbered before the matching:
7 // vertex v ∊ g2 becomes v + g1.Order() in the new graph.
8 func (g1 *Virtual) Match(g2 *Virtual, bridge EdgeSet) *Virtual {
9 n := g1.order + g2.order
10 t := g1.order // transpose
11 switch {
12 case n == 0:
13 return null
14 case g1.order == 0:
15 return g2
16 case g2.order == 0:
17 return g1
18 }
19
20 if bridge.Cost == nil {
21 bridge.Cost = zero
22 }
23 matchCost := func(v, w int) int64 {
24 switch {
25 case v < t && w < t:
26 return g1.cost(v, w)
27 case v >= t && w >= t:
28 return g2.cost(v-t, w-t)
29 default:
30 return bridge.Cost(v, w)
31 }
32 }
33
34 if bridge.Keep == nil {
35 bridge.Keep = alwaysEdge
36 }
37 s1 := bridge.From.And(Range(0, t))
38 s2 := bridge.To.And(Range(t, g2.order+t))
39
40 res := generic(n, matchCost, func(v, w int) (edge bool) {
41 switch {
42 case v < t && w < t:
43 return g1.edge(v, w)
44 case v >= t && w >= t:
45 return g2.edge(v-t, w-t)
46 case !bridge.Keep(v, w):
47 return false
48 default:
49 s1v := s1.rank(v)
50 s1w := s1.rank(w)
51 s2v := s2.rank(v)
52 s2w := s2.rank(w)
53 return s1v != -1 && s1v == s2w || s1w != -1 && s1w == s2v
54 }
55 })
56
57 res.visit = func(v int, a int, do func(w int, c int64) bool) (aborted bool) {
58 if v < t {
59 if g1.visit(v, a, do) {
60 return true
61 }
62 if w := s2.get(s1.rank(v)); w >= a && bridge.Keep(v, w) {
63 if do(w, bridge.Cost(v, w)) {
64 return true
65 }
66 }
67 return
68 }
69 if w := s1.get(s2.rank(v)); w >= a && bridge.Keep(v, w) {
70 if do(w, bridge.Cost(v, w)) {
71 return true
72 }
73 }
74 return g2.visit(v-t, max(0, a-t), func(w int, c int64) bool {
75 return do(w+t, c)
76 })
77 }
78 return res
79 }