Debian Codegraph @

build/vertexset.go

raw symbols SCIP
1 package build
2
3 import "sort"
4
5 // VertexSet represents a set of vertices in a graph.
6 // The zero value of a VertexSet is the universe;
7 // the set containing all vertices.
8 type VertexSet struct {
9 // A set is an immutable sorted list of non-empty disjoint intervals.
10 // The zero value VertexSet{nil} represents the universe.
11 set []interval
12 }
13
14 const (
15 maxInt = int(^uint(0) >> 1)
16 minInt = -maxInt - 1
17 )
18
19 // An interval represents the numbers [a, b).
20 type interval struct {
21 a, b int
22 index int // index of a in the whole set
23 }
24
25 // update updates the index values.
26 func (s VertexSet) update() {
27 prev := 0
28 for i, in := range s.set {
29 s.set[i].index = prev
30 prev += in.b - in.a
31 }
32 }
33
34 // empty returns the empty set.
35 func empty() VertexSet {
36 return VertexSet{[]interval{}}
37 }
38
39 // Range returns a set containing all vertices v, a ≤ v < b.
40 func Range(a, b int) VertexSet {
41 if a >= b {
42 return empty()
43 }
44 return VertexSet{[]interval{{a, b, 0}}}
45 }
46
47 // Vertex returns a set containing the single vertex v.
48 func Vertex(v int) VertexSet {
49 return VertexSet{[]interval{{v, v + 1, 0}}}
50 }
51
52 // size returns the number of elements in this set, or -1 for the universe.
53 func (s VertexSet) size() (size int) {
54 switch {
55 case s.set == nil:
56 return -1
57 case len(s.set) == 0:
58 return 0
59 }
60 in := s.set[len(s.set)-1]
61 return in.index + in.b - in.a
62 }
63
64 // get returns the i:th element in the set, or -1 if not available.
65 func (s VertexSet) get(i int) int {
66 if i < 0 || i >= s.size() {
67 return -1
68 }
69 // The smallest index j such that i < set[j].index.
70 j := sort.Search(len(s.set), func(j int) bool {
71 return i < s.set[j].index
72 })
73 in := s.set[j-1]
74 return in.a + i - in.index
75 }
76
77 // rank returns the position of n in the set, or -1 if not available.
78 func (s VertexSet) rank(n int) int {
79 if len(s.set) == 0 || n < s.set[0].a {
80 return -1
81 }
82 // The smallest index i such that n < set[i].a
83 i := sort.Search(len(s.set), func(i int) bool {
84 return n < s.set[i].a
85 })
86 in := s.set[i-1]
87 if n >= in.b {
88 return -1
89 }
90 return in.index + n - in.a
91 }
92
93 // Contains tells if v is a member of the set.
94 func (s VertexSet) Contains(v int) bool {
95 switch {
96 case s.set == nil:
97 return true
98 case len(s.set) == 0 || v < s.set[0].a:
99 return false
100 }
101 // The smallest index i such that v < set[i].a
102 i := sort.Search(len(s.set), func(i int) bool {
103 return v < s.set[i].a
104 })
105 if v >= s.set[i-1].b {
106 return false
107 }
108 return true
109 }
110
111 // AndNot returns the set of all vertices belonging to s1 but not to s2.
112 func (s1 VertexSet) AndNot(s2 VertexSet) VertexSet {
113 return s1.And(s2.complement())
114 }
115
116 // complement returns the set of all vertices not belonging to s.
117 func (s VertexSet) complement() VertexSet {
118 switch {
119 case s.set == nil:
120 return empty()
121 case len(s.set) == 0:
122 return VertexSet{}
123 }
124 t := empty()
125 prev := minInt
126 for _, in := range s.set {
127 if prev != in.a {
128 t.set = append(t.set, interval{prev, in.a, 0})
129 }
130 prev = in.b
131 }
132 if prev < maxInt {
133 t.set = append(t.set, interval{prev, maxInt, 0})
134 }
135 t.update()
136 return t
137 }
138
139 // And returns the set of all vertices belonging to both s1 and s2.
140 func (s1 VertexSet) And(s2 VertexSet) VertexSet {
141 switch {
142 case s1.set == nil:
143 return s2
144 case s2.set == nil:
145 return s1
146 }
147
148 type point struct {
149 x int
150 a bool // Tells if x is a of [a, b).
151 }
152 points := make([]point, 0, 2*(len(s1.set)+len(s2.set)))
153 for _, in := range s1.set {
154 points = append(points, point{in.a, true})
155 points = append(points, point{in.b, false})
156 }
157 for _, in := range s2.set {
158 points = append(points, point{in.a, true})
159 points = append(points, point{in.b, false})
160 }
161 sort.Slice(points, func(i, j int) bool {
162 if points[i].x == points[j].x {
163 return !points[i].a
164 }
165 return points[i].x < points[j].x
166 })
167
168 s := empty()
169 start, count := 0, 0
170 for _, p := range points {
171 switch count {
172 case 0:
173 count++
174 case 1:
175 if p.a {
176 start = p.x
177 count++
178 } else {
179 count--
180 }
181 case 2:
182 s.set = append(s.set, interval{start, p.x, 0})
183 count--
184 }
185 }
186 s.update()
187 return s
188 }
189
190 // Or returns the set of all vertices belonging to either s1 or s2.
191 func (s1 VertexSet) Or(s2 VertexSet) VertexSet {
192 if s1.set == nil || s2.set == nil {
193 return VertexSet{nil}
194 }
195
196 type point struct {
197 x int
198 a bool // Tells if x is a of [a, b).
199 }
200 points := make([]point, 0, 2*(len(s1.set)+len(s2.set)))
201 for _, in := range s1.set {
202 points = append(points, point{in.a, true})
203 points = append(points, point{in.b, false})
204 }
205 for _, in := range s2.set {
206 points = append(points, point{in.a, true})
207 points = append(points, point{in.b, false})
208 }
209 sort.Slice(points, func(i, j int) bool {
210 if points[i].x == points[j].x {
211 return points[i].a
212 }
213 return points[i].x < points[j].x
214 })
215
216 s := empty()
217 start, count := 0, 0
218 for _, p := range points {
219 switch count {
220 case 0:
221 start = p.x
222 count++
223 case 1:
224 if p.a {
225 count++
226 } else {
227 s.set = append(s.set, interval{start, p.x, 0})
228 count--
229 }
230 case 2:
231 count--
232 }
233 }
234 s.update()
235 return s
236 }