-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbisect.py
More file actions
136 lines (109 loc) · 3.87 KB
/
Copy pathbisect.py
File metadata and controls
136 lines (109 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
@namespace("bisect")
from Promethium import List
# A small, opt-in subset of Python's bisect module. Like heapq.py, this
# follows PromethiumBaseLibrary's own sorted()/min()/max() precedent of
# hand-overloading for int/float/str rather than generic T, since
# unconstrained generic T has no `<` operator in Promethium.
#
# `hi` has no direct equivalent of Python's `hi=None` default (the parameter
# is a plain `int`, and Promethium has no `Optional[int]`/nullable-int
# default here), so `-1` is used as the "unspecified, search to the end of
# the list" sentinel instead — matching this project's existing pattern of
# picking a concrete sentinel where CPython uses `None` (see `DefaultDict`'s
# `get`/`pop` defaults).
def bisect_left(a: List[int], x: int, lo: int = 0, hi: int = -1) -> int:
high: int = hi
if high < 0:
high = len(a)
low: int = lo
while low < high:
mid: int = (low + high) >> 1
if a.__getitem__(mid) < x:
low = mid + 1
else:
high = mid
return low
def bisect_left(a: List[float], x: float, lo: int = 0, hi: int = -1) -> int:
high: int = hi
if high < 0:
high = len(a)
low: int = lo
while low < high:
mid: int = (low + high) >> 1
if a.__getitem__(mid) < x:
low = mid + 1
else:
high = mid
return low
def bisect_left(a: List[str], x: str, lo: int = 0, hi: int = -1) -> int:
high: int = hi
if high < 0:
high = len(a)
low: int = lo
while low < high:
mid: int = (low + high) >> 1
if a.__getitem__(mid) < x:
low = mid + 1
else:
high = mid
return low
def bisect_right(a: List[int], x: int, lo: int = 0, hi: int = -1) -> int:
high: int = hi
if high < 0:
high = len(a)
low: int = lo
while low < high:
mid: int = (low + high) >> 1
if x < a.__getitem__(mid):
high = mid
else:
low = mid + 1
return low
def bisect_right(a: List[float], x: float, lo: int = 0, hi: int = -1) -> int:
high: int = hi
if high < 0:
high = len(a)
low: int = lo
while low < high:
mid: int = (low + high) >> 1
if x < a.__getitem__(mid):
high = mid
else:
low = mid + 1
return low
def bisect_right(a: List[str], x: str, lo: int = 0, hi: int = -1) -> int:
high: int = hi
if high < 0:
high = len(a)
low: int = lo
while low < high:
mid: int = (low + high) >> 1
if x < a.__getitem__(mid):
high = mid
else:
low = mid + 1
return low
def bisect(a: List[int], x: int, lo: int = 0, hi: int = -1) -> int:
return bisect_right(a, x, lo, hi)
def bisect(a: List[float], x: float, lo: int = 0, hi: int = -1) -> int:
return bisect_right(a, x, lo, hi)
def bisect(a: List[str], x: str, lo: int = 0, hi: int = -1) -> int:
return bisect_right(a, x, lo, hi)
def insort_left(a: List[int], x: int, lo: int = 0, hi: int = -1):
a.insert(bisect_left(a, x, lo, hi), x)
def insort_left(a: List[float], x: float, lo: int = 0, hi: int = -1):
a.insert(bisect_left(a, x, lo, hi), x)
def insort_left(a: List[str], x: str, lo: int = 0, hi: int = -1):
a.insert(bisect_left(a, x, lo, hi), x)
def insort_right(a: List[int], x: int, lo: int = 0, hi: int = -1):
a.insert(bisect_right(a, x, lo, hi), x)
def insort_right(a: List[float], x: float, lo: int = 0, hi: int = -1):
a.insert(bisect_right(a, x, lo, hi), x)
def insort_right(a: List[str], x: str, lo: int = 0, hi: int = -1):
a.insert(bisect_right(a, x, lo, hi), x)
def insort(a: List[int], x: int, lo: int = 0, hi: int = -1):
insort_right(a, x, lo, hi)
def insort(a: List[float], x: float, lo: int = 0, hi: int = -1):
insort_right(a, x, lo, hi)
def insort(a: List[str], x: str, lo: int = 0, hi: int = -1):
insort_right(a, x, lo, hi)