Debian Codegraph @

api/v1/lib/ranges.go

raw symbols SCIP
1 package mesos
2
3 import (
4 "sort"
5 )
6
7 // Ranges represents a list of Ranges.
8 type Ranges []Value_Range
9
10 // NewRanges returns squashed Ranges from the given numbers.
11 func NewRanges(ns ...uint64) Ranges {
12 xs := append(uint64s{}, ns...)
13 sort.Sort(xs)
14 rs := make(Ranges, len(xs))
15 for i := range xs {
16 rs[i].Begin, rs[i].End = xs[i], xs[i]
17 }
18 return rs.Squash()
19 }
20
21 // NewPortRanges returns Ranges from the "ports" resource in the
22 // given *Offer. If that resource isn't provided, nil will be returned.
23 //
24 // The returned Ranges are sorted and have all overlapping ranges merged from
25 // left to right. e.g. [[0, 5], [4, 3], [10, 7]] -> [[0, 5], [7, 10]]
26 func NewPortRanges(o *Offer) Ranges {
27 if o == nil {
28 return Ranges{}
29 }
30
31 var (
32 r Resource
33 found bool
34 )
35 for i := range o.Resources {
36 if o.Resources[i].GetName() == "ports" {
37 r = o.Resources[i]
38 found = true
39 break
40 }
41 }
42
43 if !found {
44 return Ranges{}
45 }
46
47 offered := r.GetRanges().GetRange()
48 rs := make(Ranges, len(offered))
49 for i, r := range offered {
50 if lo, hi := r.GetBegin(), r.GetEnd(); lo <= hi {
51 rs[i].Begin, rs[i].End = lo, hi
52 } else {
53 rs[i].Begin, rs[i].End = hi, lo
54 }
55 }
56 return rs.Sort().Squash()
57 }
58
59 // These three methods implement sort.Interface
60 func (rs Ranges) Len() int { return len(rs) }
61 func (rs Ranges) Swap(i, j int) { rs[i], rs[j] = rs[j], rs[i] }
62 func (rs Ranges) Less(i, j int) bool {
63 return rs[i].Begin < rs[j].Begin || (rs[i].Begin == rs[j].Begin && rs[i].End < rs[j].End)
64 }
65
66 // Size returns the sum of the Size of all Ranges.
67 func (rs Ranges) Size() uint64 {
68 var sz uint64
69 for i := range rs {
70 sz += 1 + (rs[i].End - rs[i].Begin)
71 }
72 return sz
73 }
74
75 // Sort sorts the receiving Ranges and returns the result; convenience
76 func (rs Ranges) Sort() Ranges {
77 sort.Sort(rs)
78 return rs
79 }
80
81 // Squash merges overlapping and continuous Ranges. It assumes they're pre-sorted.
82 func (rs Ranges) Squash() Ranges {
83 if len(rs) < 2 {
84 return rs
85 }
86 squashed := Ranges{rs[0]}
87 for i := 1; i < len(rs); i++ {
88 switch max := squashed[len(squashed)-1].End; {
89 case 1+max < rs[i].Begin: // no overlap nor continuity: push
90 squashed = append(squashed, rs[i])
91 case max <= rs[i].End: // overlap or continuity: squash
92 squashed[len(squashed)-1].End = rs[i].End
93 }
94 }
95 return squashed
96 }
97
98 // Search performs a binary search for n returning the index of the Range it was
99 // found at or -1 if not found.
100 func (rs Ranges) Search(n uint64) int {
101 for lo, hi := 0, len(rs)-1; lo <= hi; {
102 switch m := lo + (hi-lo)/2; {
103 case n < rs[m].Begin:
104 hi = m - 1
105 case n > rs[m].End:
106 lo = m + 1
107 default:
108 return m
109 }
110 }
111 return -1
112 }
113
114 // Partition partitions Ranges around n. It returns the partitioned Ranges
115 // and a boolean indicating if n was found.
116 func (rs Ranges) Partition(n uint64) (Ranges, bool) {
117 i := rs.Search(n)
118 if i < 0 {
119 return rs, false
120 }
121
122 pn := make(Ranges, 0, len(rs)+1)
123 switch pn = append(pn, rs[:i]...); {
124 case rs[i].Begin == rs[i].End: // delete
125 case rs[i].Begin == n: // increment lower bound
126 pn = append(pn, Value_Range{rs[i].Begin + 1, rs[i].End})
127 case rs[i].End == n: // decrement upper bound
128 pn = append(pn, Value_Range{rs[i].Begin, rs[i].End - 1})
129 default: // split
130 pn = append(pn, Value_Range{rs[i].Begin, n - 1}, Value_Range{n + 1, rs[i].End})
131 }
132 return append(pn, rs[i+1:]...), true
133 }
134
135 // Remove removes a range from already coalesced ranges.
136 // The algorithms constructs a new vector of ranges which is then
137 // Squash'ed into a Ranges instance.
138 func (rs Ranges) Remove(removal Value_Range) Ranges {
139 ranges := make([]Value_Range, 0, len(rs))
140 for _, r := range rs {
141 // skip if the entire range is subsumed by removal
142 if r.Begin >= removal.Begin && r.End <= removal.End {
143 continue
144 }
145 // divide if the range subsumes the removal
146 if r.Begin < removal.Begin && r.End > removal.End {
147 ranges = append(ranges,
148 Value_Range{r.Begin, removal.Begin - 1},
149 Value_Range{removal.End + 1, r.End},
150 )
151 continue
152 }
153 // add the full range if there's no intersection
154 if r.End < removal.Begin || r.Begin > removal.End {
155 ranges = append(ranges, r)
156 continue
157 }
158 // trim if the range does intersect
159 if r.End > removal.End {
160 ranges = append(ranges, Value_Range{removal.End + 1, r.End})
161 } else {
162 if r.Begin >= removal.Begin {
163 // should never happen
164 panic("r.Begin >= removal.Begin")
165 }
166 ranges = append(ranges, Value_Range{r.Begin, removal.Begin - 1})
167 }
168 }
169 return Ranges(ranges).Squash()
170 }
171
172 // Compare assumes that both Ranges are already in sort-order.
173 // Returns 0 if rs and right are equivalent, -1 if rs is a subset of right, or else 1
174 func (rs Ranges) Compare(right Ranges) int {
175 x, y, result := rs.equiv(right)
176 if result {
177 return 0
178 }
179 for _, a := range x {
180 // make sure that this range is a subset of a range in y
181 matched := false
182 for _, b := range y {
183 if a.Begin >= b.Begin && a.End <= b.End {
184 matched = true
185 break
186 }
187 }
188 if !matched {
189 return 1
190 }
191 }
192 return -1
193 }
194
195 // Equivalent assumes that both Ranges are already in sort-order.
196 func (rs Ranges) Equivalent(right Ranges) (result bool) {
197 _, _, result = rs.equiv(right)
198 return
199 }
200
201 // Equivalent assumes that both Ranges are already in sort-order.
202 func (rs Ranges) equiv(right Ranges) (_, _ Ranges, _ bool) {
203 // we need to squash rs and right but don't want to change the originals
204 switch len(rs) {
205 case 0:
206 case 1:
207 rs = Ranges{rs[0]}
208 default:
209 rs = Ranges(append([]Value_Range{rs[0], rs[1]}, rs[2:]...)).Sort().Squash()
210 }
211 switch len(right) {
212 case 0:
213 case 1:
214 right = Ranges{right[0]}
215 default:
216 right = Ranges(append([]Value_Range{right[0], right[1]}, right[2:]...)).Sort().Squash()
217 }
218 return rs, right, (&Value_Ranges{Range: rs}).Equal(&Value_Ranges{Range: right})
219 }
220
221 func (rs Ranges) Clone() Ranges {
222 if len(rs) == 0 {
223 return nil
224 }
225 x := make(Ranges, len(rs))
226 copy(x, rs)
227 return x
228 }
229
230 // Min returns the minimum number in Ranges. It will panic on empty Ranges.
231 func (rs Ranges) Min() uint64 { return rs[0].Begin }
232
233 // Max returns the maximum number in Ranges. It will panic on empty Ranges.
234 func (rs Ranges) Max() uint64 { return rs[len(rs)-1].End }
235
236 // resource returns a *Resource with the given name and Ranges.
237 func (rs Ranges) resource(name string) Resource {
238 vr := make([]Value_Range, len(rs))
239 copy(vr, rs)
240 return Resource{
241 Name: name,
242 Type: RANGES.Enum(),
243 Ranges: &Value_Ranges{Range: vr},
244 }
245 }
246
247 // uint64s is an utility used to sort a slice of uint64s
248 type uint64s []uint64
249
250 // These three methods implement sort.Interface
251 func (ns uint64s) Len() int { return len(ns) }
252 func (ns uint64s) Less(i, j int) bool { return ns[i] < ns[j] }
253 func (ns uint64s) Swap(i, j int) { ns[i], ns[j] = ns[j], ns[i] }