summaryrefslogtreecommitdiffstats
path: root/contrib/tools/python3/src/Lib/bisect.py
diff options
context:
space:
mode:
authorshadchin <[email protected]>2022-04-18 12:39:32 +0300
committershadchin <[email protected]>2022-04-18 12:39:32 +0300
commitd4be68e361f4258cf0848fc70018dfe37a2acc24 (patch)
tree153e294cd97ac8b5d7a989612704a0c1f58e8ad4 /contrib/tools/python3/src/Lib/bisect.py
parent260c02f5ccf242d9d9b8a873afaf6588c00237d6 (diff)
IGNIETFERRO-1816 Update Python 3 from 3.9.12 to 3.10.4
ref:9f96be6d02ee8044fdd6f124b799b270c20ce641
Diffstat (limited to 'contrib/tools/python3/src/Lib/bisect.py')
-rw-r--r--contrib/tools/python3/src/Lib/bisect.py70
1 files changed, 50 insertions, 20 deletions
diff --git a/contrib/tools/python3/src/Lib/bisect.py b/contrib/tools/python3/src/Lib/bisect.py
index 8f3f6a3fe35..d37da74f7b4 100644
--- a/contrib/tools/python3/src/Lib/bisect.py
+++ b/contrib/tools/python3/src/Lib/bisect.py
@@ -1,6 +1,7 @@
"""Bisection algorithms."""
-def insort_right(a, x, lo=0, hi=None):
+
+def insort_right(a, x, lo=0, hi=None, *, key=None):
"""Insert item x in list a, and keep it sorted assuming a is sorted.
If x is already in a, insert it to the right of the rightmost x.
@@ -8,15 +9,18 @@ def insort_right(a, x, lo=0, hi=None):
Optional args lo (default 0) and hi (default len(a)) bound the
slice of a to be searched.
"""
-
- lo = bisect_right(a, x, lo, hi)
+ if key is None:
+ lo = bisect_right(a, x, lo, hi)
+ else:
+ lo = bisect_right(a, key(x), lo, hi, key=key)
a.insert(lo, x)
-def bisect_right(a, x, lo=0, hi=None):
+
+def bisect_right(a, x, lo=0, hi=None, *, key=None):
"""Return the index where to insert item x in list a, assuming a is sorted.
The return value i is such that all e in a[:i] have e <= x, and all e in
- a[i:] have e > x. So if x already appears in the list, a.insert(x) will
+ a[i:] have e > x. So if x already appears in the list, a.insert(i, x) will
insert just after the rightmost x already there.
Optional args lo (default 0) and hi (default len(a)) bound the
@@ -27,14 +31,26 @@ def bisect_right(a, x, lo=0, hi=None):
raise ValueError('lo must be non-negative')
if hi is None:
hi = len(a)
- while lo < hi:
- mid = (lo+hi)//2
- # Use __lt__ to match the logic in list.sort() and in heapq
- if x < a[mid]: hi = mid
- else: lo = mid+1
+ # Note, the comparison uses "<" to match the
+ # __lt__() logic in list.sort() and in heapq.
+ if key is None:
+ while lo < hi:
+ mid = (lo + hi) // 2
+ if x < a[mid]:
+ hi = mid
+ else:
+ lo = mid + 1
+ else:
+ while lo < hi:
+ mid = (lo + hi) // 2
+ if x < key(a[mid]):
+ hi = mid
+ else:
+ lo = mid + 1
return lo
-def insort_left(a, x, lo=0, hi=None):
+
+def insort_left(a, x, lo=0, hi=None, *, key=None):
"""Insert item x in list a, and keep it sorted assuming a is sorted.
If x is already in a, insert it to the left of the leftmost x.
@@ -43,15 +59,17 @@ def insort_left(a, x, lo=0, hi=None):
slice of a to be searched.
"""
- lo = bisect_left(a, x, lo, hi)
+ if key is None:
+ lo = bisect_left(a, x, lo, hi)
+ else:
+ lo = bisect_left(a, key(x), lo, hi, key=key)
a.insert(lo, x)
-
-def bisect_left(a, x, lo=0, hi=None):
+def bisect_left(a, x, lo=0, hi=None, *, key=None):
"""Return the index where to insert item x in list a, assuming a is sorted.
The return value i is such that all e in a[:i] have e < x, and all e in
- a[i:] have e >= x. So if x already appears in the list, a.insert(x) will
+ a[i:] have e >= x. So if x already appears in the list, a.insert(i, x) will
insert just before the leftmost x already there.
Optional args lo (default 0) and hi (default len(a)) bound the
@@ -62,13 +80,25 @@ def bisect_left(a, x, lo=0, hi=None):
raise ValueError('lo must be non-negative')
if hi is None:
hi = len(a)
- while lo < hi:
- mid = (lo+hi)//2
- # Use __lt__ to match the logic in list.sort() and in heapq
- if a[mid] < x: lo = mid+1
- else: hi = mid
+ # Note, the comparison uses "<" to match the
+ # __lt__() logic in list.sort() and in heapq.
+ if key is None:
+ while lo < hi:
+ mid = (lo + hi) // 2
+ if a[mid] < x:
+ lo = mid + 1
+ else:
+ hi = mid
+ else:
+ while lo < hi:
+ mid = (lo + hi) // 2
+ if key(a[mid]) < x:
+ lo = mid + 1
+ else:
+ hi = mid
return lo
+
# Overwrite above definitions with a fast C implementation
try:
from _bisect import *