@@ -14,6 +14,8 @@ mod _bisect {
14
14
lo : OptionalArg < PyObjectRef > ,
15
15
#[ pyarg( any, optional) ]
16
16
hi : OptionalArg < PyObjectRef > ,
17
+ #[ pyarg( named, default ) ]
18
+ key : Option < PyObjectRef > ,
17
19
}
18
20
19
21
// Handles objects that implement __index__ and makes sure index fits in needed isize.
@@ -66,17 +68,21 @@ mod _bisect {
66
68
#[ inline]
67
69
#[ pyfunction]
68
70
fn bisect_left (
69
- BisectArgs { a, x, lo, hi } : BisectArgs ,
71
+ BisectArgs { a, x, lo, hi, key } : BisectArgs ,
70
72
vm : & VirtualMachine ,
71
73
) -> PyResult < usize > {
72
74
let ( mut lo, mut hi) = as_usize ( lo, hi, a. length ( vm) ?, vm) ?;
73
75
74
76
while lo < hi {
75
77
// Handles issue 13496.
76
78
let mid = ( lo + hi) / 2 ;
77
- if a. get_item ( & mid, vm) ?
78
- . rich_compare_bool ( & x, PyComparisonOp :: Lt , vm) ?
79
- {
79
+ let a_mid = a. get_item ( & mid, vm) ?;
80
+ let comp = if let Some ( ref key) = key {
81
+ vm. invoke ( key, ( a_mid, ) ) ?
82
+ } else {
83
+ a_mid
84
+ } ;
85
+ if comp. rich_compare_bool ( & x, PyComparisonOp :: Lt , vm) ? {
80
86
lo = mid + 1 ;
81
87
} else {
82
88
hi = mid;
@@ -96,15 +102,21 @@ mod _bisect {
96
102
#[ inline]
97
103
#[ pyfunction]
98
104
fn bisect_right (
99
- BisectArgs { a, x, lo, hi } : BisectArgs ,
105
+ BisectArgs { a, x, lo, hi, key } : BisectArgs ,
100
106
vm : & VirtualMachine ,
101
107
) -> PyResult < usize > {
102
108
let ( mut lo, mut hi) = as_usize ( lo, hi, a. length ( vm) ?, vm) ?;
103
109
104
110
while lo < hi {
105
111
// Handles issue 13496.
106
112
let mid = ( lo + hi) / 2 ;
107
- if x. rich_compare_bool ( & * a. get_item ( & mid, vm) ?, PyComparisonOp :: Lt , vm) ? {
113
+ let a_mid = a. get_item ( & mid, vm) ?;
114
+ let comp = if let Some ( ref key) = key {
115
+ vm. invoke ( key, ( a_mid, ) ) ?
116
+ } else {
117
+ a_mid
118
+ } ;
119
+ if x. rich_compare_bool ( & * comp, PyComparisonOp :: Lt , vm) ? {
108
120
hi = mid;
109
121
} else {
110
122
lo = mid + 1 ;
@@ -120,13 +132,19 @@ mod _bisect {
120
132
/// Optional args lo (default 0) and hi (default len(a)) bound the
121
133
/// slice of a to be searched.
122
134
#[ pyfunction]
123
- fn insort_left ( BisectArgs { a, x, lo, hi } : BisectArgs , vm : & VirtualMachine ) -> PyResult {
135
+ fn insort_left ( BisectArgs { a, x, lo, hi, key } : BisectArgs , vm : & VirtualMachine ) -> PyResult {
136
+ let x = if let Some ( ref key) = key {
137
+ vm. invoke ( key, ( x, ) ) ?
138
+ } else {
139
+ x
140
+ } ;
124
141
let index = bisect_left (
125
142
BisectArgs {
126
143
a : a. clone ( ) ,
127
144
x : x. clone ( ) ,
128
145
lo,
129
146
hi,
147
+ key,
130
148
} ,
131
149
vm,
132
150
) ?;
@@ -140,13 +158,19 @@ mod _bisect {
140
158
/// Optional args lo (default 0) and hi (default len(a)) bound the
141
159
/// slice of a to be searched
142
160
#[ pyfunction]
143
- fn insort_right ( BisectArgs { a, x, lo, hi } : BisectArgs , vm : & VirtualMachine ) -> PyResult {
161
+ fn insort_right ( BisectArgs { a, x, lo, hi, key } : BisectArgs , vm : & VirtualMachine ) -> PyResult {
162
+ let x = if let Some ( ref key) = key {
163
+ vm. invoke ( key, ( x, ) ) ?
164
+ } else {
165
+ x
166
+ } ;
144
167
let index = bisect_right (
145
168
BisectArgs {
146
169
a : a. clone ( ) ,
147
170
x : x. clone ( ) ,
148
171
lo,
149
172
hi,
173
+ key,
150
174
} ,
151
175
vm,
152
176
) ?;
0 commit comments