|
2 | 2 |
|
3 | 3 | from typing import NamedTuple, List |
4 | 4 | import threading |
5 | | -import sys |
6 | | - |
7 | | -if sys.version_info >= (3, 10): |
8 | | - # bisect only supports key as of 3.10 |
9 | | - from bisect import bisect_left, bisect_right |
10 | | - |
11 | | -else: |
12 | | - |
13 | | - def bisect_left(a, x, lo=0, hi=None, *, key=None): |
14 | | - """Return the index where to insert item x in list a, assuming a is sorted. |
15 | | - The return value i is such that all e in a[:i] have e < x, and all e in |
16 | | - a[i:] have e >= x. So if x already appears in the list, a.insert(i, x) will |
17 | | - insert just before the leftmost x already there. |
18 | | - Optional args lo (default 0) and hi (default len(a)) bound the |
19 | | - slice of a to be searched. |
20 | | - """ |
21 | | - |
22 | | - if lo < 0: |
23 | | - raise ValueError("lo must be non-negative") |
24 | | - if hi is None: |
25 | | - hi = len(a) |
26 | | - # Note, the comparison uses "<" to match the |
27 | | - # __lt__() logic in list.sort() and in heapq. |
28 | | - if key is None: |
29 | | - while lo < hi: |
30 | | - mid = (lo + hi) // 2 |
31 | | - if a[mid] < x: |
32 | | - lo = mid + 1 |
33 | | - else: |
34 | | - hi = mid |
35 | | - else: |
36 | | - while lo < hi: |
37 | | - mid = (lo + hi) // 2 |
38 | | - if key(a[mid]) < x: |
39 | | - lo = mid + 1 |
40 | | - else: |
41 | | - hi = mid |
42 | | - return lo |
43 | | - |
44 | | - def bisect_right(a, x, lo=0, hi=None, *, key=None): |
45 | | - """Return the index where to insert item x in list a, assuming a is sorted. |
46 | | - The return value i is such that all e in a[:i] have e <= x, and all e in |
47 | | - a[i:] have e > x. So if x already appears in the list, a.insert(i, x) will |
48 | | - insert just after the rightmost x already there. |
49 | | - Optional args lo (default 0) and hi (default len(a)) bound the |
50 | | - slice of a to be searched. |
51 | | - """ |
52 | | - |
53 | | - if lo < 0: |
54 | | - raise ValueError("lo must be non-negative") |
55 | | - if hi is None: |
56 | | - hi = len(a) |
57 | | - # Note, the comparison uses "<" to match the |
58 | | - # __lt__() logic in list.sort() and in heapq. |
59 | | - if key is None: |
60 | | - while lo < hi: |
61 | | - mid = (lo + hi) // 2 |
62 | | - if x < a[mid]: |
63 | | - hi = mid |
64 | | - else: |
65 | | - lo = mid + 1 |
66 | | - else: |
67 | | - while lo < hi: |
68 | | - mid = (lo + hi) // 2 |
69 | | - if x < key(a[mid]): |
70 | | - hi = mid |
71 | | - else: |
72 | | - lo = mid + 1 |
73 | | - return lo |
| 5 | +from bisect import bisect_left, bisect_right |
74 | 6 |
|
75 | 7 |
|
76 | 8 | class NoValidSector(Exception): |
|
0 commit comments