Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 1 | """Bisection algorithms.""" |
Guido van Rossum | 4e16098 | 1992-09-02 20:43:20 +0000 | [diff] [blame] | 2 | |
Raymond Hettinger | 871934d | 2020-10-19 22:04:01 -0700 | [diff] [blame] | 3 | |
| 4 | def insort_right(a, x, lo=0, hi=None, *, key=None): |
Tim Peters | 36cdad1 | 2000-12-29 02:06:45 +0000 | [diff] [blame] | 5 | """Insert item x in list a, and keep it sorted assuming a is sorted. |
| 6 | |
| 7 | If x is already in a, insert it to the right of the rightmost x. |
| 8 | |
| 9 | Optional args lo (default 0) and hi (default len(a)) bound the |
| 10 | slice of a to be searched. |
| 11 | """ |
Raymond Hettinger | 871934d | 2020-10-19 22:04:01 -0700 | [diff] [blame] | 12 | if key is None: |
| 13 | lo = bisect_right(a, x, lo, hi) |
| 14 | else: |
| 15 | lo = bisect_right(a, key(x), lo, hi, key=key) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 16 | a.insert(lo, x) |
Guido van Rossum | 4e16098 | 1992-09-02 20:43:20 +0000 | [diff] [blame] | 17 | |
Raymond Hettinger | 871934d | 2020-10-19 22:04:01 -0700 | [diff] [blame] | 18 | |
| 19 | def bisect_right(a, x, lo=0, hi=None, *, key=None): |
Tim Peters | 36cdad1 | 2000-12-29 02:06:45 +0000 | [diff] [blame] | 20 | """Return the index where to insert item x in list a, assuming a is sorted. |
| 21 | |
| 22 | The return value i is such that all e in a[:i] have e <= x, and all e in |
Sergey Golitsynskiy | 6a1e9c2 | 2020-07-11 19:18:31 -0400 | [diff] [blame] | 23 | a[i:] have e > x. So if x already appears in the list, a.insert(i, x) will |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 24 | insert just after the rightmost x already there. |
Tim Peters | 36cdad1 | 2000-12-29 02:06:45 +0000 | [diff] [blame] | 25 | |
| 26 | Optional args lo (default 0) and hi (default len(a)) bound the |
| 27 | slice of a to be searched. |
| 28 | """ |
| 29 | |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 30 | if lo < 0: |
| 31 | raise ValueError('lo must be non-negative') |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 32 | if hi is None: |
| 33 | hi = len(a) |
Raymond Hettinger | 871934d | 2020-10-19 22:04:01 -0700 | [diff] [blame] | 34 | # Note, the comparison uses "<" to match the |
| 35 | # __lt__() logic in list.sort() and in heapq. |
| 36 | if key is None: |
| 37 | while lo < hi: |
| 38 | mid = (lo + hi) // 2 |
| 39 | if x < a[mid]: |
| 40 | hi = mid |
| 41 | else: |
| 42 | lo = mid + 1 |
| 43 | else: |
| 44 | while lo < hi: |
| 45 | mid = (lo + hi) // 2 |
| 46 | if x < key(a[mid]): |
| 47 | hi = mid |
| 48 | else: |
| 49 | lo = mid + 1 |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 50 | return lo |
Tim Peters | 36cdad1 | 2000-12-29 02:06:45 +0000 | [diff] [blame] | 51 | |
Raymond Hettinger | 871934d | 2020-10-19 22:04:01 -0700 | [diff] [blame] | 52 | |
| 53 | def insort_left(a, x, lo=0, hi=None, *, key=None): |
Tim Peters | 36cdad1 | 2000-12-29 02:06:45 +0000 | [diff] [blame] | 54 | """Insert item x in list a, and keep it sorted assuming a is sorted. |
| 55 | |
| 56 | If x is already in a, insert it to the left of the leftmost x. |
| 57 | |
| 58 | Optional args lo (default 0) and hi (default len(a)) bound the |
| 59 | slice of a to be searched. |
| 60 | """ |
| 61 | |
Raymond Hettinger | 871934d | 2020-10-19 22:04:01 -0700 | [diff] [blame] | 62 | if key is None: |
| 63 | lo = bisect_left(a, x, lo, hi) |
| 64 | else: |
| 65 | lo = bisect_left(a, key(x), lo, hi, key=key) |
Tim Peters | 36cdad1 | 2000-12-29 02:06:45 +0000 | [diff] [blame] | 66 | a.insert(lo, x) |
| 67 | |
Raymond Hettinger | 871934d | 2020-10-19 22:04:01 -0700 | [diff] [blame] | 68 | def bisect_left(a, x, lo=0, hi=None, *, key=None): |
Tim Peters | 36cdad1 | 2000-12-29 02:06:45 +0000 | [diff] [blame] | 69 | """Return the index where to insert item x in list a, assuming a is sorted. |
| 70 | |
| 71 | The return value i is such that all e in a[:i] have e < x, and all e in |
Sergey Golitsynskiy | 6a1e9c2 | 2020-07-11 19:18:31 -0400 | [diff] [blame] | 72 | a[i:] have e >= x. So if x already appears in the list, a.insert(i, x) will |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 73 | insert just before the leftmost x already there. |
Tim Peters | 36cdad1 | 2000-12-29 02:06:45 +0000 | [diff] [blame] | 74 | |
| 75 | Optional args lo (default 0) and hi (default len(a)) bound the |
| 76 | slice of a to be searched. |
| 77 | """ |
| 78 | |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 79 | if lo < 0: |
| 80 | raise ValueError('lo must be non-negative') |
Tim Peters | 36cdad1 | 2000-12-29 02:06:45 +0000 | [diff] [blame] | 81 | if hi is None: |
| 82 | hi = len(a) |
Raymond Hettinger | 871934d | 2020-10-19 22:04:01 -0700 | [diff] [blame] | 83 | # Note, the comparison uses "<" to match the |
| 84 | # __lt__() logic in list.sort() and in heapq. |
| 85 | if key is None: |
| 86 | while lo < hi: |
| 87 | mid = (lo + hi) // 2 |
| 88 | if a[mid] < x: |
| 89 | lo = mid + 1 |
| 90 | else: |
| 91 | hi = mid |
| 92 | else: |
| 93 | while lo < hi: |
| 94 | mid = (lo + hi) // 2 |
| 95 | if key(a[mid]) < x: |
| 96 | lo = mid + 1 |
| 97 | else: |
| 98 | hi = mid |
Tim Peters | 36cdad1 | 2000-12-29 02:06:45 +0000 | [diff] [blame] | 99 | return lo |
Raymond Hettinger | 0c41027 | 2004-01-05 10:13:35 +0000 | [diff] [blame] | 100 | |
Raymond Hettinger | 871934d | 2020-10-19 22:04:01 -0700 | [diff] [blame] | 101 | |
Raymond Hettinger | 0c41027 | 2004-01-05 10:13:35 +0000 | [diff] [blame] | 102 | # Overwrite above definitions with a fast C implementation |
| 103 | try: |
Raymond Hettinger | 9dbc670 | 2009-03-31 17:51:51 +0000 | [diff] [blame] | 104 | from _bisect import * |
Brett Cannon | cd171c8 | 2013-07-04 17:43:24 -0400 | [diff] [blame] | 105 | except ImportError: |
Raymond Hettinger | 0c41027 | 2004-01-05 10:13:35 +0000 | [diff] [blame] | 106 | pass |
Victor Stinner | 1018fad | 2016-11-24 23:31:59 +0100 | [diff] [blame] | 107 | |
| 108 | # Create aliases |
| 109 | bisect = bisect_right |
| 110 | insort = insort_right |