Removed public functions, public functions take usize
This commit is contained in:
parent
6cea19bcc3
commit
cbff0068ad
12
src/lib.rs
12
src/lib.rs
|
@ -1,6 +1,6 @@
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
|
|
||||||
pub fn partition_by<T, F: FnMut(&T, &T) -> Ordering>
|
fn partition_by<T, F: FnMut(&T, &T) -> Ordering>
|
||||||
(slice: &mut [T], start: isize, end: isize, compare: &mut F) -> isize {
|
(slice: &mut [T], start: isize, end: isize, compare: &mut F) -> isize {
|
||||||
|
|
||||||
let mut i = start;
|
let mut i = start;
|
||||||
|
@ -15,7 +15,7 @@ pub fn partition_by<T, F: FnMut(&T, &T) -> Ordering>
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn quick_sort_by<T, F: FnMut(&T, &T) -> Ordering>
|
fn quick_sort_by<T, F: FnMut(&T, &T) -> Ordering>
|
||||||
(slice: &mut [T], start: isize, end: isize, compare: &mut F) {
|
(slice: &mut [T], start: isize, end: isize, compare: &mut F) {
|
||||||
|
|
||||||
if start < end {
|
if start < end {
|
||||||
|
@ -27,7 +27,7 @@ pub fn quick_sort_by<T, F: FnMut(&T, &T) -> Ordering>
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn partial_sort_by<T, F: FnMut(&T, &T) -> Ordering>
|
pub fn partial_sort_by<T, F: FnMut(&T, &T) -> Ordering>
|
||||||
(slice: &mut [T], k: isize, compare: &mut F) {
|
(slice: &mut [T], k: usize, compare: &mut F) {
|
||||||
|
|
||||||
let mut start = 0;
|
let mut start = 0;
|
||||||
let mut end = slice.len() as isize - 1;
|
let mut end = slice.len() as isize - 1;
|
||||||
|
@ -37,7 +37,7 @@ pub fn partial_sort_by<T, F: FnMut(&T, &T) -> Ordering>
|
||||||
let index = partition_by(slice, start, end, compare);
|
let index = partition_by(slice, start, end, compare);
|
||||||
let rank = index + 1;
|
let rank = index + 1;
|
||||||
|
|
||||||
if rank >= k {
|
if rank >= k as isize {
|
||||||
end = index - 1;
|
end = index - 1;
|
||||||
} else if index - start > end - index {
|
} else if index - start > end - index {
|
||||||
quick_sort_by(slice, index + 1, end, compare);
|
quick_sort_by(slice, index + 1, end, compare);
|
||||||
|
@ -64,11 +64,11 @@ pub fn partial_sort_by<T, F: FnMut(&T, &T) -> Ordering>
|
||||||
///
|
///
|
||||||
/// The implementation is based on [this gist](https://gist.github.com/mesuvash/4095565).
|
/// The implementation is based on [this gist](https://gist.github.com/mesuvash/4095565).
|
||||||
pub fn partial_sort<T: Ord>(slice: &mut [T], k: usize) {
|
pub fn partial_sort<T: Ord>(slice: &mut [T], k: usize) {
|
||||||
partial_sort_by(slice, k as isize, &mut |x, y| x.cmp(y));
|
partial_sort_by(slice, k, &mut |x, y| x.cmp(y));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn partial_sort_by_key<T, O: Ord, F: FnMut(&T) -> O>(slice: &mut [T], k: usize, key: &mut F) {
|
pub fn partial_sort_by_key<T, O: Ord, F: FnMut(&T) -> O>(slice: &mut [T], k: usize, key: &mut F) {
|
||||||
partial_sort_by(slice, k as isize, &mut |x, y| key(x).cmp(&key(y)));
|
partial_sort_by(slice, k, &mut |x, y| key(x).cmp(&key(y)));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
Loading…
Reference in New Issue