blob: d37da74f7b4055c6cb476d55b22d90a9cf8769bb [file] [log] [blame]
Guido van Rossum4acc25b2000-02-02 15:10:15 +00001"""Bisection algorithms."""
Guido van Rossum4e160981992-09-02 20:43:20 +00002
Raymond Hettinger871934d2020-10-19 22:04:01 -07003
4def insort_right(a, x, lo=0, hi=None, *, key=None):
Tim Peters36cdad12000-12-29 02:06:45 +00005 """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 Hettinger871934d2020-10-19 22:04:01 -070012 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 Rossum4acc25b2000-02-02 15:10:15 +000016 a.insert(lo, x)
Guido van Rossum4e160981992-09-02 20:43:20 +000017
Raymond Hettinger871934d2020-10-19 22:04:01 -070018
19def bisect_right(a, x, lo=0, hi=None, *, key=None):
Tim Peters36cdad12000-12-29 02:06:45 +000020 """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 Golitsynskiy6a1e9c22020-07-11 19:18:31 -040023 a[i:] have e > x. So if x already appears in the list, a.insert(i, x) will
Guido van Rossumd8faa362007-04-27 19:54:29 +000024 insert just after the rightmost x already there.
Tim Peters36cdad12000-12-29 02:06:45 +000025
26 Optional args lo (default 0) and hi (default len(a)) bound the
27 slice of a to be searched.
28 """
29
Georg Brandl2ee470f2008-07-16 12:55:28 +000030 if lo < 0:
31 raise ValueError('lo must be non-negative')
Guido van Rossum4acc25b2000-02-02 15:10:15 +000032 if hi is None:
33 hi = len(a)
Raymond Hettinger871934d2020-10-19 22:04:01 -070034 # 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 Rossum4acc25b2000-02-02 15:10:15 +000050 return lo
Tim Peters36cdad12000-12-29 02:06:45 +000051
Raymond Hettinger871934d2020-10-19 22:04:01 -070052
53def insort_left(a, x, lo=0, hi=None, *, key=None):
Tim Peters36cdad12000-12-29 02:06:45 +000054 """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 Hettinger871934d2020-10-19 22:04:01 -070062 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 Peters36cdad12000-12-29 02:06:45 +000066 a.insert(lo, x)
67
Raymond Hettinger871934d2020-10-19 22:04:01 -070068def bisect_left(a, x, lo=0, hi=None, *, key=None):
Tim Peters36cdad12000-12-29 02:06:45 +000069 """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 Golitsynskiy6a1e9c22020-07-11 19:18:31 -040072 a[i:] have e >= x. So if x already appears in the list, a.insert(i, x) will
Guido van Rossumd8faa362007-04-27 19:54:29 +000073 insert just before the leftmost x already there.
Tim Peters36cdad12000-12-29 02:06:45 +000074
75 Optional args lo (default 0) and hi (default len(a)) bound the
76 slice of a to be searched.
77 """
78
Georg Brandl2ee470f2008-07-16 12:55:28 +000079 if lo < 0:
80 raise ValueError('lo must be non-negative')
Tim Peters36cdad12000-12-29 02:06:45 +000081 if hi is None:
82 hi = len(a)
Raymond Hettinger871934d2020-10-19 22:04:01 -070083 # 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 Peters36cdad12000-12-29 02:06:45 +000099 return lo
Raymond Hettinger0c410272004-01-05 10:13:35 +0000100
Raymond Hettinger871934d2020-10-19 22:04:01 -0700101
Raymond Hettinger0c410272004-01-05 10:13:35 +0000102# Overwrite above definitions with a fast C implementation
103try:
Raymond Hettinger9dbc6702009-03-31 17:51:51 +0000104 from _bisect import *
Brett Cannoncd171c82013-07-04 17:43:24 -0400105except ImportError:
Raymond Hettinger0c410272004-01-05 10:13:35 +0000106 pass
Victor Stinner1018fad2016-11-24 23:31:59 +0100107
108# Create aliases
109bisect = bisect_right
110insort = insort_right