Doug Zongker | 424296a | 2014-09-02 08:53:09 -0700 | [diff] [blame] | 1 | # Copyright (C) 2014 The Android Open Source Project |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 15 | from __future__ import print_function |
Tao Bao | fe97dbd | 2018-02-06 15:01:54 -0800 | [diff] [blame] | 16 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 17 | import heapq |
| 18 | import itertools |
| 19 | |
Tao Bao | fe97dbd | 2018-02-06 15:01:54 -0800 | [diff] [blame] | 20 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 21 | __all__ = ["RangeSet"] |
| 22 | |
Tao Bao | fe97dbd | 2018-02-06 15:01:54 -0800 | [diff] [blame] | 23 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 24 | class RangeSet(object): |
Tao Bao | fe97dbd | 2018-02-06 15:01:54 -0800 | [diff] [blame] | 25 | """A RangeSet represents a set of non-overlapping ranges on integers. |
| 26 | |
| 27 | Attributes: |
| 28 | monotonic: Whether the input has all its integers in increasing order. |
| 29 | extra: A dict that can be used by the caller, e.g. to store info that's |
| 30 | only meaningful to caller. |
| 31 | """ |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 32 | |
| 33 | def __init__(self, data=None): |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 34 | self.monotonic = False |
Tao Bao | c765cca | 2018-01-31 17:32:40 -0800 | [diff] [blame] | 35 | self._extra = {} |
Doug Zongker | 846cb3a | 2014-09-09 10:55:36 -0700 | [diff] [blame] | 36 | if isinstance(data, str): |
| 37 | self._parse_internal(data) |
| 38 | elif data: |
Tao Bao | e8f7561 | 2015-08-26 17:07:14 -0700 | [diff] [blame] | 39 | assert len(data) % 2 == 0 |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 40 | self.data = tuple(self._remove_pairs(data)) |
Tao Bao | e8f7561 | 2015-08-26 17:07:14 -0700 | [diff] [blame] | 41 | self.monotonic = all(x < y for x, y in zip(self.data, self.data[1:])) |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 42 | else: |
| 43 | self.data = () |
| 44 | |
| 45 | def __iter__(self): |
| 46 | for i in range(0, len(self.data), 2): |
| 47 | yield self.data[i:i+2] |
| 48 | |
| 49 | def __eq__(self, other): |
| 50 | return self.data == other.data |
Tao Bao | e8f7561 | 2015-08-26 17:07:14 -0700 | [diff] [blame] | 51 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 52 | def __ne__(self, other): |
| 53 | return self.data != other.data |
Tao Bao | e8f7561 | 2015-08-26 17:07:14 -0700 | [diff] [blame] | 54 | |
Tao Bao | d660c8d | 2019-06-19 10:17:21 -0700 | [diff] [blame] | 55 | def __bool__(self): |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 56 | return bool(self.data) |
| 57 | |
Tao Bao | d660c8d | 2019-06-19 10:17:21 -0700 | [diff] [blame] | 58 | # Python 2 uses __nonzero__, while Python 3 uses __bool__. |
| 59 | __nonzero__ = __bool__ |
| 60 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 61 | def __str__(self): |
| 62 | if not self.data: |
| 63 | return "empty" |
| 64 | else: |
| 65 | return self.to_string() |
| 66 | |
Doug Zongker | 846cb3a | 2014-09-09 10:55:36 -0700 | [diff] [blame] | 67 | def __repr__(self): |
| 68 | return '<RangeSet("' + self.to_string() + '")>' |
| 69 | |
Tao Bao | c765cca | 2018-01-31 17:32:40 -0800 | [diff] [blame] | 70 | @property |
| 71 | def extra(self): |
| 72 | return self._extra |
| 73 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 74 | @classmethod |
| 75 | def parse(cls, text): |
Tao Bao | fe97dbd | 2018-02-06 15:01:54 -0800 | [diff] [blame] | 76 | """Parses a text string into a RangeSet. |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 77 | |
Tao Bao | fe97dbd | 2018-02-06 15:01:54 -0800 | [diff] [blame] | 78 | The input text string consists of a space-separated list of blocks and |
| 79 | ranges, e.g. "10-20 30 35-40". Ranges are interpreted to include both their |
| 80 | ends (so the above example represents 18 individual blocks). Returns a |
| 81 | RangeSet object. |
| 82 | |
| 83 | If the input has all its blocks in increasing order, then the 'monotonic' |
| 84 | attribute of the returned RangeSet will be set to True. For example the |
| 85 | input "10-20 30" is monotonic, but the input "15-20 30 10-14" is not, even |
| 86 | though they represent the same set of blocks (and the two RangeSets will |
| 87 | compare equal with ==). |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 88 | """ |
Doug Zongker | 846cb3a | 2014-09-09 10:55:36 -0700 | [diff] [blame] | 89 | return cls(text) |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 90 | |
Tao Bao | 8179d68 | 2016-03-24 11:08:51 -0700 | [diff] [blame] | 91 | @classmethod |
| 92 | def parse_raw(cls, text): |
| 93 | """Parse a string generated by RangeSet.to_string_raw(). |
| 94 | |
| 95 | >>> RangeSet.parse_raw(RangeSet("0-9").to_string_raw()) |
| 96 | <RangeSet("0-9")> |
| 97 | """ |
| 98 | |
| 99 | raw = [int(i) for i in text.split(',')] |
| 100 | assert raw[0] == len(raw[1:]), "Invalid raw string." |
| 101 | |
| 102 | return cls(data=raw[1:]) |
| 103 | |
Doug Zongker | 846cb3a | 2014-09-09 10:55:36 -0700 | [diff] [blame] | 104 | def _parse_internal(self, text): |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 105 | data = [] |
| 106 | last = -1 |
| 107 | monotonic = True |
| 108 | for p in text.split(): |
| 109 | if "-" in p: |
Tao Bao | e8f7561 | 2015-08-26 17:07:14 -0700 | [diff] [blame] | 110 | s, e = (int(x) for x in p.split("-")) |
| 111 | data.append(s) |
| 112 | data.append(e+1) |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 113 | if last <= s <= e: |
| 114 | last = e |
| 115 | else: |
| 116 | monotonic = False |
| 117 | else: |
| 118 | s = int(p) |
| 119 | data.append(s) |
| 120 | data.append(s+1) |
| 121 | if last <= s: |
| 122 | last = s+1 |
| 123 | else: |
Tianjie Xu | cd1e16a | 2016-04-07 20:17:48 -0700 | [diff] [blame] | 124 | monotonic = False |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 125 | data.sort() |
Doug Zongker | 846cb3a | 2014-09-09 10:55:36 -0700 | [diff] [blame] | 126 | self.data = tuple(self._remove_pairs(data)) |
| 127 | self.monotonic = monotonic |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 128 | |
| 129 | @staticmethod |
| 130 | def _remove_pairs(source): |
Tao Bao | e8f7561 | 2015-08-26 17:07:14 -0700 | [diff] [blame] | 131 | """Remove consecutive duplicate items to simplify the result. |
| 132 | |
| 133 | [1, 2, 2, 5, 5, 10] will become [1, 10].""" |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 134 | last = None |
| 135 | for i in source: |
| 136 | if i == last: |
| 137 | last = None |
| 138 | else: |
| 139 | if last is not None: |
| 140 | yield last |
| 141 | last = i |
| 142 | if last is not None: |
| 143 | yield last |
| 144 | |
| 145 | def to_string(self): |
| 146 | out = [] |
| 147 | for i in range(0, len(self.data), 2): |
| 148 | s, e = self.data[i:i+2] |
| 149 | if e == s+1: |
| 150 | out.append(str(s)) |
| 151 | else: |
| 152 | out.append(str(s) + "-" + str(e-1)) |
| 153 | return " ".join(out) |
| 154 | |
| 155 | def to_string_raw(self): |
Tao Bao | e8f7561 | 2015-08-26 17:07:14 -0700 | [diff] [blame] | 156 | assert self.data |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 157 | return str(len(self.data)) + "," + ",".join(str(i) for i in self.data) |
| 158 | |
| 159 | def union(self, other): |
| 160 | """Return a new RangeSet representing the union of this RangeSet |
Doug Zongker | 846cb3a | 2014-09-09 10:55:36 -0700 | [diff] [blame] | 161 | with the argument. |
| 162 | |
| 163 | >>> RangeSet("10-19 30-34").union(RangeSet("18-29")) |
| 164 | <RangeSet("10-34")> |
| 165 | >>> RangeSet("10-19 30-34").union(RangeSet("22 32")) |
| 166 | <RangeSet("10-19 22 30-34")> |
| 167 | """ |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 168 | out = [] |
| 169 | z = 0 |
| 170 | for p, d in heapq.merge(zip(self.data, itertools.cycle((+1, -1))), |
| 171 | zip(other.data, itertools.cycle((+1, -1)))): |
| 172 | if (z == 0 and d == 1) or (z == 1 and d == -1): |
| 173 | out.append(p) |
| 174 | z += d |
| 175 | return RangeSet(data=out) |
| 176 | |
| 177 | def intersect(self, other): |
| 178 | """Return a new RangeSet representing the intersection of this |
Doug Zongker | 846cb3a | 2014-09-09 10:55:36 -0700 | [diff] [blame] | 179 | RangeSet with the argument. |
| 180 | |
| 181 | >>> RangeSet("10-19 30-34").intersect(RangeSet("18-32")) |
| 182 | <RangeSet("18-19 30-32")> |
| 183 | >>> RangeSet("10-19 30-34").intersect(RangeSet("22-28")) |
| 184 | <RangeSet("")> |
| 185 | """ |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 186 | out = [] |
| 187 | z = 0 |
| 188 | for p, d in heapq.merge(zip(self.data, itertools.cycle((+1, -1))), |
| 189 | zip(other.data, itertools.cycle((+1, -1)))): |
| 190 | if (z == 1 and d == 1) or (z == 2 and d == -1): |
| 191 | out.append(p) |
| 192 | z += d |
| 193 | return RangeSet(data=out) |
| 194 | |
| 195 | def subtract(self, other): |
| 196 | """Return a new RangeSet representing subtracting the argument |
Doug Zongker | 846cb3a | 2014-09-09 10:55:36 -0700 | [diff] [blame] | 197 | from this RangeSet. |
| 198 | |
| 199 | >>> RangeSet("10-19 30-34").subtract(RangeSet("18-32")) |
| 200 | <RangeSet("10-17 33-34")> |
| 201 | >>> RangeSet("10-19 30-34").subtract(RangeSet("22-28")) |
| 202 | <RangeSet("10-19 30-34")> |
| 203 | """ |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 204 | |
| 205 | out = [] |
| 206 | z = 0 |
| 207 | for p, d in heapq.merge(zip(self.data, itertools.cycle((+1, -1))), |
| 208 | zip(other.data, itertools.cycle((-1, +1)))): |
| 209 | if (z == 0 and d == 1) or (z == 1 and d == -1): |
| 210 | out.append(p) |
| 211 | z += d |
| 212 | return RangeSet(data=out) |
| 213 | |
| 214 | def overlaps(self, other): |
| 215 | """Returns true if the argument has a nonempty overlap with this |
Doug Zongker | 846cb3a | 2014-09-09 10:55:36 -0700 | [diff] [blame] | 216 | RangeSet. |
| 217 | |
| 218 | >>> RangeSet("10-19 30-34").overlaps(RangeSet("18-32")) |
| 219 | True |
| 220 | >>> RangeSet("10-19 30-34").overlaps(RangeSet("22-28")) |
| 221 | False |
| 222 | """ |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 223 | |
| 224 | # This is like intersect, but we can stop as soon as we discover the |
| 225 | # output is going to be nonempty. |
| 226 | z = 0 |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 227 | for _, d in heapq.merge(zip(self.data, itertools.cycle((+1, -1))), |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 228 | zip(other.data, itertools.cycle((+1, -1)))): |
| 229 | if (z == 1 and d == 1) or (z == 2 and d == -1): |
| 230 | return True |
| 231 | z += d |
| 232 | return False |
| 233 | |
| 234 | def size(self): |
| 235 | """Returns the total size of the RangeSet (ie, how many integers |
Doug Zongker | 846cb3a | 2014-09-09 10:55:36 -0700 | [diff] [blame] | 236 | are in the set). |
| 237 | |
| 238 | >>> RangeSet("10-19 30-34").size() |
| 239 | 15 |
| 240 | """ |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 241 | |
| 242 | total = 0 |
| 243 | for i, p in enumerate(self.data): |
| 244 | if i % 2: |
| 245 | total += p |
| 246 | else: |
| 247 | total -= p |
| 248 | return total |
Doug Zongker | 6233818 | 2014-09-08 08:29:55 -0700 | [diff] [blame] | 249 | |
| 250 | def map_within(self, other): |
| 251 | """'other' should be a subset of 'self'. Returns a RangeSet |
| 252 | representing what 'other' would get translated to if the integers |
| 253 | of 'self' were translated down to be contiguous starting at zero. |
| 254 | |
Doug Zongker | 846cb3a | 2014-09-09 10:55:36 -0700 | [diff] [blame] | 255 | >>> RangeSet("0-9").map_within(RangeSet("3-4")) |
| 256 | <RangeSet("3-4")> |
| 257 | >>> RangeSet("10-19").map_within(RangeSet("13-14")) |
| 258 | <RangeSet("3-4")> |
| 259 | >>> RangeSet("10-19 30-39").map_within(RangeSet("17-19 30-32")) |
| 260 | <RangeSet("7-12")> |
| 261 | >>> RangeSet("10-19 30-39").map_within(RangeSet("12-13 17-19 30-32")) |
| 262 | <RangeSet("2-3 7-12")> |
Doug Zongker | 6233818 | 2014-09-08 08:29:55 -0700 | [diff] [blame] | 263 | """ |
| 264 | |
| 265 | out = [] |
| 266 | offset = 0 |
| 267 | start = None |
| 268 | for p, d in heapq.merge(zip(self.data, itertools.cycle((-5, +5))), |
| 269 | zip(other.data, itertools.cycle((-1, +1)))): |
| 270 | if d == -5: |
| 271 | start = p |
| 272 | elif d == +5: |
| 273 | offset += p-start |
| 274 | start = None |
| 275 | else: |
| 276 | out.append(offset + p - start) |
| 277 | return RangeSet(data=out) |
| 278 | |
Tao Bao | e9b6191 | 2015-07-09 17:37:49 -0700 | [diff] [blame] | 279 | def extend(self, n): |
| 280 | """Extend the RangeSet by 'n' blocks. |
| 281 | |
| 282 | The lower bound is guaranteed to be non-negative. |
| 283 | |
| 284 | >>> RangeSet("0-9").extend(1) |
| 285 | <RangeSet("0-10")> |
| 286 | >>> RangeSet("10-19").extend(15) |
| 287 | <RangeSet("0-34")> |
| 288 | >>> RangeSet("10-19 30-39").extend(4) |
| 289 | <RangeSet("6-23 26-43")> |
| 290 | >>> RangeSet("10-19 30-39").extend(10) |
| 291 | <RangeSet("0-49")> |
| 292 | """ |
| 293 | out = self |
| 294 | for i in range(0, len(self.data), 2): |
| 295 | s, e = self.data[i:i+2] |
| 296 | s1 = max(0, s - n) |
| 297 | e1 = e + n |
| 298 | out = out.union(RangeSet(str(s1) + "-" + str(e1-1))) |
| 299 | return out |
| 300 | |
Tao Bao | 9a5caf2 | 2015-08-25 15:10:10 -0700 | [diff] [blame] | 301 | def first(self, n): |
| 302 | """Return the RangeSet that contains at most the first 'n' integers. |
| 303 | |
| 304 | >>> RangeSet("0-9").first(1) |
| 305 | <RangeSet("0")> |
| 306 | >>> RangeSet("10-19").first(5) |
| 307 | <RangeSet("10-14")> |
| 308 | >>> RangeSet("10-19").first(15) |
| 309 | <RangeSet("10-19")> |
| 310 | >>> RangeSet("10-19 30-39").first(3) |
| 311 | <RangeSet("10-12")> |
| 312 | >>> RangeSet("10-19 30-39").first(15) |
| 313 | <RangeSet("10-19 30-34")> |
| 314 | >>> RangeSet("10-19 30-39").first(30) |
| 315 | <RangeSet("10-19 30-39")> |
| 316 | >>> RangeSet("0-9").first(0) |
| 317 | <RangeSet("")> |
| 318 | """ |
| 319 | |
| 320 | if self.size() <= n: |
| 321 | return self |
| 322 | |
| 323 | out = [] |
| 324 | for s, e in self: |
| 325 | if e - s >= n: |
| 326 | out += (s, s+n) |
| 327 | break |
| 328 | else: |
| 329 | out += (s, e) |
| 330 | n -= e - s |
| 331 | return RangeSet(data=out) |
| 332 | |
Tao Bao | 08c8583 | 2016-09-19 22:26:30 -0700 | [diff] [blame] | 333 | def next_item(self): |
| 334 | """Return the next integer represented by the RangeSet. |
| 335 | |
| 336 | >>> list(RangeSet("0-9").next_item()) |
| 337 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 338 | >>> list(RangeSet("10-19 3-5").next_item()) |
| 339 | [3, 4, 5, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] |
Tao Bao | c0dcbd0 | 2017-11-02 12:19:36 -0700 | [diff] [blame] | 340 | >>> list(RangeSet("10-19 3 5 7").next_item()) |
Tao Bao | 08c8583 | 2016-09-19 22:26:30 -0700 | [diff] [blame] | 341 | [3, 5, 7, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] |
| 342 | """ |
| 343 | for s, e in self: |
| 344 | for element in range(s, e): |
| 345 | yield element |
| 346 | |
Doug Zongker | 6233818 | 2014-09-08 08:29:55 -0700 | [diff] [blame] | 347 | |
| 348 | if __name__ == "__main__": |
| 349 | import doctest |
| 350 | doctest.testmod() |