Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../lib/python2..../site-pac.../idna
File: intranges.py
"""
[0] Fix | Delete
Given a list of integers, made up of (hopefully) a small number of long runs
[1] Fix | Delete
of consecutive integers, compute a representation of the form
[2] Fix | Delete
((start1, end1), (start2, end2) ...). Then answer the question "was x present
[3] Fix | Delete
in the original list?" in time O(log(# runs)).
[4] Fix | Delete
"""
[5] Fix | Delete
[6] Fix | Delete
import bisect
[7] Fix | Delete
[8] Fix | Delete
def intranges_from_list(list_):
[9] Fix | Delete
"""Represent a list of integers as a sequence of ranges:
[10] Fix | Delete
((start_0, end_0), (start_1, end_1), ...), such that the original
[11] Fix | Delete
integers are exactly those x such that start_i <= x < end_i for some i.
[12] Fix | Delete
[13] Fix | Delete
Ranges are encoded as single integers (start << 32 | end), not as tuples.
[14] Fix | Delete
"""
[15] Fix | Delete
[16] Fix | Delete
sorted_list = sorted(list_)
[17] Fix | Delete
ranges = []
[18] Fix | Delete
last_write = -1
[19] Fix | Delete
for i in range(len(sorted_list)):
[20] Fix | Delete
if i+1 < len(sorted_list):
[21] Fix | Delete
if sorted_list[i] == sorted_list[i+1]-1:
[22] Fix | Delete
continue
[23] Fix | Delete
current_range = sorted_list[last_write+1:i+1]
[24] Fix | Delete
ranges.append(_encode_range(current_range[0], current_range[-1] + 1))
[25] Fix | Delete
last_write = i
[26] Fix | Delete
[27] Fix | Delete
return tuple(ranges)
[28] Fix | Delete
[29] Fix | Delete
def _encode_range(start, end):
[30] Fix | Delete
return (start << 32) | end
[31] Fix | Delete
[32] Fix | Delete
def _decode_range(r):
[33] Fix | Delete
return (r >> 32), (r & ((1 << 32) - 1))
[34] Fix | Delete
[35] Fix | Delete
[36] Fix | Delete
def intranges_contain(int_, ranges):
[37] Fix | Delete
"""Determine if `int_` falls into one of the ranges in `ranges`."""
[38] Fix | Delete
tuple_ = _encode_range(int_, 0)
[39] Fix | Delete
pos = bisect.bisect_left(ranges, tuple_)
[40] Fix | Delete
# we could be immediately ahead of a tuple (start, end)
[41] Fix | Delete
# with start < int_ <= end
[42] Fix | Delete
if pos > 0:
[43] Fix | Delete
left, right = _decode_range(ranges[pos-1])
[44] Fix | Delete
if left <= int_ < right:
[45] Fix | Delete
return True
[46] Fix | Delete
# or we could be immediately behind a tuple (int_, end)
[47] Fix | Delete
if pos < len(ranges):
[48] Fix | Delete
left, _ = _decode_range(ranges[pos])
[49] Fix | Delete
if left == int_:
[50] Fix | Delete
return True
[51] Fix | Delete
return False
[52] Fix | Delete
[53] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function