annotate idna/intranges.py @ 15:0a3943480712

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