Debian Codegraph @

build/edgeset.go

raw symbols SCIP
1 package build
2
3 // EdgeSet describes a set of edges; an edge (v, w), v ≠ w, belongs to the set
4 // if Keep(v, w) is true and (v, w) belongs to either From × To or To × From.
5 // The zero value of an edge set is the universe, the set containing all edges.
6 type EdgeSet struct {
7 From, To VertexSet
8 Keep FilterFunc
9 Cost CostFunc
10 }
11
12 // AllEdges returns the universe, the set containing all edges.
13 // The edge cost is zero.
14 func AllEdges() EdgeSet {
15 return EdgeSet{}
16 }
17
18 // NoEdges returns a set that includes no edges.
19 func NoEdges() EdgeSet {
20 return EdgeSet{
21 From: Range(0, 0),
22 To: Range(0, 0),
23 Keep: neverEdge,
24 }
25 }
26
27 // Edge returns a set consisting of a single edge {v, w}, v ≠ w, of zero cost.
28 func Edge(v, w int) EdgeSet {
29 if v < 0 || w < 0 || v == w {
30 return NoEdges()
31 }
32 return EdgeSet{
33 From: Vertex(v),
34 To: Vertex(w),
35 }
36 }
37
38 // Contains tells if the set contains the edge {v, w}.
39 func (e EdgeSet) Contains(v, w int) bool {
40 switch {
41 case e.Keep != nil && !e.Keep(v, w):
42 return false
43 case e.From.Contains(v) && e.To.Contains(w):
44 return true
45 case e.To.Contains(v) && e.From.Contains(w):
46 return true
47 default:
48 return false
49 }
50 }
51
52 // Add returns a graph containing all edges in g plus all edges in e.
53 // Any edges belonging to both g and e will retain their cost from g.
54 func (g *Virtual) Add(e EdgeSet) *Virtual {
55 return g.union(newEdges(g.Order(), e), true)
56 }
57
58 // Delete returns a graph containing all edges in g except those also found in e.
59 func (g *Virtual) Delete(e EdgeSet) *Virtual {
60 return g.Keep(func(v, w int) bool {
61 return !e.Contains(v, w)
62 })
63 }
64
65 // newEdges returns a virtual graph with n vertices and all edges
66 // belonging to the edge set.
67 func newEdges(n int, e EdgeSet) *Virtual {
68 switch {
69 case n < 0:
70 return nil
71 case n == 0:
72 return null
73 case n == 1:
74 return singleton()
75 }
76
77 var noCost bool
78 if e.Cost == nil {
79 noCost = true
80 e.Cost = zero
81 }
82 var noFilter bool
83 if e.Keep == nil {
84 noFilter = true
85 e.Keep = alwaysEdge
86 }
87
88 from := e.From.And(Range(0, n))
89 to := e.To.And(Range(0, n))
90 if from.size() == 0 || to.size() == 0 {
91 return Empty(n)
92 }
93 res := generic(n, e.Cost, func(v, w int) (edge bool) {
94 return e.Contains(v, w)
95 })
96
97 intersect := from.And(to)
98 union := from.Or(to)
99 if noFilter {
100 res.degree = func(v int) (deg int) {
101 switch {
102 case intersect.Contains(v):
103 return union.size() - 1
104 case from.Contains(v):
105 return to.size()
106 case to.Contains(v):
107 return from.size()
108 default:
109 return
110 }
111 }
112 }
113
114 visit := func(v int, a int, do func(w int, c int64) bool) (aborted bool) {
115 switch {
116 case intersect.Contains(v):
117 for _, in := range union.And(Range(a, n)).set {
118 for w := in.a; w < in.b; w++ {
119 if v != w && e.Keep(v, w) && do(w, e.Cost(v, w)) {
120 return true
121 }
122 }
123 }
124 return
125 case from.Contains(v):
126 for _, in := range to.And(Range(a, n)).set {
127 for w := in.a; w < in.b; w++ {
128 if e.Keep(v, w) && do(w, e.Cost(v, w)) {
129 return true
130 }
131 }
132 }
133 return
134 case to.Contains(v):
135 for _, in := range from.And(Range(a, n)).set {
136 for w := in.a; w < in.b; w++ {
137 if e.Keep(v, w) && do(w, e.Cost(v, w)) {
138 return true
139 }
140 }
141 }
142 return
143 default:
144 return
145 }
146 }
147
148 visit0 := func(v int, a int, do func(w int, c int64) bool) (aborted bool) {
149 switch {
150 case intersect.Contains(v):
151 for _, in := range union.And(Range(a, n)).set {
152 for w := in.a; w < in.b; w++ {
153 if v != w && e.Keep(v, w) && do(w, 0) {
154 return true
155 }
156 }
157 }
158 return
159 case from.Contains(v):
160 for _, in := range to.And(Range(a, n)).set {
161 for w := in.a; w < in.b; w++ {
162 if e.Keep(v, w) && do(w, 0) {
163 return true
164 }
165 }
166 }
167 return
168 case to.Contains(v):
169 for _, in := range from.And(Range(a, n)).set {
170 for w := in.a; w < in.b; w++ {
171 if e.Keep(v, w) && do(w, 0) {
172 return true
173 }
174 }
175 }
176 return
177 default:
178 return
179 }
180 }
181
182 if noCost {
183 res.visit = visit0
184 } else {
185 res.visit = visit
186 }
187 return res
188 }