jpayne@68: """ jpayne@68: Given a list of integers, made up of (hopefully) a small number of long runs jpayne@68: of consecutive integers, compute a representation of the form jpayne@68: ((start1, end1), (start2, end2) ...). Then answer the question "was x present jpayne@68: in the original list?" in time O(log(# runs)). jpayne@68: """ jpayne@68: jpayne@68: import bisect jpayne@68: from typing import List, Tuple jpayne@68: jpayne@68: jpayne@68: def intranges_from_list(list_: List[int]) -> Tuple[int, ...]: jpayne@68: """Represent a list of integers as a sequence of ranges: jpayne@68: ((start_0, end_0), (start_1, end_1), ...), such that the original jpayne@68: integers are exactly those x such that start_i <= x < end_i for some i. jpayne@68: jpayne@68: Ranges are encoded as single integers (start << 32 | end), not as tuples. jpayne@68: """ jpayne@68: jpayne@68: sorted_list = sorted(list_) jpayne@68: ranges = [] jpayne@68: last_write = -1 jpayne@68: for i in range(len(sorted_list)): jpayne@68: if i + 1 < len(sorted_list): jpayne@68: if sorted_list[i] == sorted_list[i + 1] - 1: jpayne@68: continue jpayne@68: current_range = sorted_list[last_write + 1 : i + 1] jpayne@68: ranges.append(_encode_range(current_range[0], current_range[-1] + 1)) jpayne@68: last_write = i jpayne@68: jpayne@68: return tuple(ranges) jpayne@68: jpayne@68: jpayne@68: def _encode_range(start: int, end: int) -> int: jpayne@68: return (start << 32) | end jpayne@68: jpayne@68: jpayne@68: def _decode_range(r: int) -> Tuple[int, int]: jpayne@68: return (r >> 32), (r & ((1 << 32) - 1)) jpayne@68: jpayne@68: jpayne@68: def intranges_contain(int_: int, ranges: Tuple[int, ...]) -> bool: jpayne@68: """Determine if `int_` falls into one of the ranges in `ranges`.""" jpayne@68: tuple_ = _encode_range(int_, 0) jpayne@68: pos = bisect.bisect_left(ranges, tuple_) jpayne@68: # we could be immediately ahead of a tuple (start, end) jpayne@68: # with start < int_ <= end jpayne@68: if pos > 0: jpayne@68: left, right = _decode_range(ranges[pos - 1]) jpayne@68: if left <= int_ < right: jpayne@68: return True jpayne@68: # or we could be immediately behind a tuple (int_, end) jpayne@68: if pos < len(ranges): jpayne@68: left, _ = _decode_range(ranges[pos]) jpayne@68: if left == int_: jpayne@68: return True jpayne@68: return False