Skip to content

Commit 6551ccd

Browse files
committed
Simplifying bounds/usings
1 parent e5a7709 commit 6551ccd

File tree

1 file changed

+13
-26
lines changed

1 file changed

+13
-26
lines changed

src/k_smallest.rs

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
use alloc::vec::IntoIter;
12
use core::cmp::{Ord, Ordering, Reverse};
23
use core::mem::{replace, transmute, MaybeUninit};
3-
use alloc::vec::IntoIter;
4+
45

56
fn k_smallest_dynamic<T, I: Iterator<Item = T>>(
67
iter: I,
@@ -29,10 +30,11 @@ where
2930
move |a| Reverse(f(a))
3031
}
3132

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+
{
3638
k_smallest_dynamic(iter, k, T::cmp)
3739
}
3840

@@ -87,9 +89,9 @@ struct MaxHeap<'a, T, C> {
8789
len: usize,
8890
}
8991

90-
impl<'a, A, C> Extend<A> for MaxHeap<'a, A, C>
92+
impl<'a, T, C> MaxHeap<'a, T, C>
9193
where
92-
C: Fn(&A, &A) -> Ordering,
94+
C: Fn(&T, &T) -> Ordering,
9395
{
9496
fn extend<T: IntoIterator<Item = A>>(&mut self, iter: T) {
9597
let mut iter = iter.into_iter();
@@ -140,10 +142,7 @@ impl<'a, T, C> MaxHeap<'a, T, C> {
140142
}
141143

142144
/// 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) {
147146
fn children_of(n: usize) -> (usize, usize) {
148147
(2 * n, 2 * n + 1)
149148
}
@@ -172,13 +171,7 @@ impl<'a, T, C> MaxHeap<'a, T, C> {
172171

173172
/// Insert the given element into the heap without changing its size
174173
/// 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> {
182175
let out = replace(&mut self.storage[0], MaybeUninit::new(val));
183176
self.sift_down(0);
184177
// SAFETY: This has been moved out of storage[0]
@@ -192,20 +185,14 @@ impl<'a, T, C> MaxHeap<'a, T, C> {
192185
}
193186

194187
/// 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) {
199189
for i in (0..(self.len / 2 + 1)).rev() {
200190
self.sift_down(i);
201191
}
202192
}
203193

204194
/// 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> {
209196
(self.comparator)(a?, b?).into()
210197
}
211198

0 commit comments

Comments
 (0)