1
+ use alloc:: vec:: IntoIter ;
1
2
use core:: cmp:: { Ord , Ordering , Reverse } ;
2
3
use core:: mem:: { replace, transmute, MaybeUninit } ;
3
- use alloc :: vec :: IntoIter ;
4
+
4
5
5
6
fn k_smallest_dynamic < T , I : Iterator < Item = T > > (
6
7
iter : I ,
@@ -29,10 +30,11 @@ where
29
30
move |a| Reverse ( f ( a) )
30
31
}
31
32
32
- pub ( crate ) fn k_smallest < T : Ord , I : Iterator < Item = T > > (
33
- iter : I ,
34
- k : usize ,
35
- ) -> IntoIter < T > {
33
+ pub ( crate ) fn k_smallest < T , I > ( iter : I , k : usize ) -> IntoIter < T >
34
+ where
35
+ T : Ord ,
36
+ I : Iterator < Item = T > ,
37
+ {
36
38
k_smallest_dynamic ( iter, k, T :: cmp)
37
39
}
38
40
@@ -87,9 +89,9 @@ struct MaxHeap<'a, T, C> {
87
89
len : usize ,
88
90
}
89
91
90
- impl < ' a , A , C > Extend < A > for MaxHeap < ' a , A , C >
92
+ impl < ' a , T , C > MaxHeap < ' a , T , C >
91
93
where
92
- C : Fn ( & A , & A ) -> Ordering ,
94
+ C : Fn ( & T , & T ) -> Ordering ,
93
95
{
94
96
fn extend < T : IntoIterator < Item = A > > ( & mut self , iter : T ) {
95
97
let mut iter = iter. into_iter ( ) ;
@@ -140,10 +142,7 @@ impl<'a, T, C> MaxHeap<'a, T, C> {
140
142
}
141
143
142
144
/// Sift the element currently at `origin` **away** from the root until it is properly ordered
143
- fn sift_down ( & mut self , origin : usize )
144
- where
145
- C : Fn ( & T , & T ) -> Ordering ,
146
- {
145
+ fn sift_down ( & mut self , origin : usize ) {
147
146
fn children_of ( n : usize ) -> ( usize , usize ) {
148
147
( 2 * n, 2 * n + 1 )
149
148
}
@@ -172,13 +171,7 @@ impl<'a, T, C> MaxHeap<'a, T, C> {
172
171
173
172
/// Insert the given element into the heap without changing its size
174
173
/// The displaced element is returned, i.e. either the input or previous max
175
- fn push_pop ( & mut self , val : T ) -> Option < T >
176
- where
177
- C : Fn ( & T , & T ) -> Ordering ,
178
- {
179
- if self . storage . is_empty ( ) {
180
- Some ( val)
181
- } else if self . compare ( self . get ( 0 ) , Some ( & val) ) == Some ( Ordering :: Greater ) {
174
+ fn push_pop ( & mut self , val : T ) -> Option < T > {
182
175
let out = replace ( & mut self . storage [ 0 ] , MaybeUninit :: new ( val) ) ;
183
176
self . sift_down ( 0 ) ;
184
177
// SAFETY: This has been moved out of storage[0]
@@ -192,20 +185,14 @@ impl<'a, T, C> MaxHeap<'a, T, C> {
192
185
}
193
186
194
187
/// Establishes the heap property from an arbitrary order
195
- fn heapify ( & mut self )
196
- where
197
- C : Fn ( & T , & T ) -> Ordering ,
198
- {
188
+ fn heapify ( & mut self ) {
199
189
for i in ( 0 ..( self . len / 2 + 1 ) ) . rev ( ) {
200
190
self . sift_down ( i) ;
201
191
}
202
192
}
203
193
204
194
/// Lift the comparator through Option so we don't need to do as much bounds checking inside `sift_down`
205
- fn compare ( & self , a : Option < & T > , b : Option < & T > ) -> Option < Ordering >
206
- where
207
- C : Fn ( & T , & T ) -> Ordering ,
208
- {
195
+ fn compare ( & self , a : Option < & T > , b : Option < & T > ) -> Option < Ordering > {
209
196
( self . comparator ) ( a?, b?) . into ( )
210
197
}
211
198
0 commit comments