jpayne@68
|
1 """
|
jpayne@68
|
2 Given a list of integers, made up of (hopefully) a small number of long runs
|
jpayne@68
|
3 of consecutive integers, compute a representation of the form
|
jpayne@68
|
4 ((start1, end1), (start2, end2) ...). Then answer the question "was x present
|
jpayne@68
|
5 in the original list?" in time O(log(# runs)).
|
jpayne@68
|
6 """
|
jpayne@68
|
7
|
jpayne@68
|
8 import bisect
|
jpayne@68
|
9 from typing import List, Tuple
|
jpayne@68
|
10
|
jpayne@68
|
11
|
jpayne@68
|
12 def intranges_from_list(list_: List[int]) -> Tuple[int, ...]:
|
jpayne@68
|
13 """Represent a list of integers as a sequence of ranges:
|
jpayne@68
|
14 ((start_0, end_0), (start_1, end_1), ...), such that the original
|
jpayne@68
|
15 integers are exactly those x such that start_i <= x < end_i for some i.
|
jpayne@68
|
16
|
jpayne@68
|
17 Ranges are encoded as single integers (start << 32 | end), not as tuples.
|
jpayne@68
|
18 """
|
jpayne@68
|
19
|
jpayne@68
|
20 sorted_list = sorted(list_)
|
jpayne@68
|
21 ranges = []
|
jpayne@68
|
22 last_write = -1
|
jpayne@68
|
23 for i in range(len(sorted_list)):
|
jpayne@68
|
24 if i + 1 < len(sorted_list):
|
jpayne@68
|
25 if sorted_list[i] == sorted_list[i + 1] - 1:
|
jpayne@68
|
26 continue
|
jpayne@68
|
27 current_range = sorted_list[last_write + 1 : i + 1]
|
jpayne@68
|
28 ranges.append(_encode_range(current_range[0], current_range[-1] + 1))
|
jpayne@68
|
29 last_write = i
|
jpayne@68
|
30
|
jpayne@68
|
31 return tuple(ranges)
|
jpayne@68
|
32
|
jpayne@68
|
33
|
jpayne@68
|
34 def _encode_range(start: int, end: int) -> int:
|
jpayne@68
|
35 return (start << 32) | end
|
jpayne@68
|
36
|
jpayne@68
|
37
|
jpayne@68
|
38 def _decode_range(r: int) -> Tuple[int, int]:
|
jpayne@68
|
39 return (r >> 32), (r & ((1 << 32) - 1))
|
jpayne@68
|
40
|
jpayne@68
|
41
|
jpayne@68
|
42 def intranges_contain(int_: int, ranges: Tuple[int, ...]) -> bool:
|
jpayne@68
|
43 """Determine if `int_` falls into one of the ranges in `ranges`."""
|
jpayne@68
|
44 tuple_ = _encode_range(int_, 0)
|
jpayne@68
|
45 pos = bisect.bisect_left(ranges, tuple_)
|
jpayne@68
|
46 # we could be immediately ahead of a tuple (start, end)
|
jpayne@68
|
47 # with start < int_ <= end
|
jpayne@68
|
48 if pos > 0:
|
jpayne@68
|
49 left, right = _decode_range(ranges[pos - 1])
|
jpayne@68
|
50 if left <= int_ < right:
|
jpayne@68
|
51 return True
|
jpayne@68
|
52 # or we could be immediately behind a tuple (int_, end)
|
jpayne@68
|
53 if pos < len(ranges):
|
jpayne@68
|
54 left, _ = _decode_range(ranges[pos])
|
jpayne@68
|
55 if left == int_:
|
jpayne@68
|
56 return True
|
jpayne@68
|
57 return False
|