* cton-util: fix some clippy unnecessary pass-by-value warnings * clippy: ignore too many arguments / cyclomatic complexity in module since these functions are taking args coming from the command line, i dont think this is actually a valid lint, morally the arguments are all from one structure * cton-util: take care of remaining clippy warnings * cton-reader: fix all non-suspicious clippy warnings * cton-reader: disable clippy at site of suspicious lint * cton-frontend: disable clippy at the site of an invalid lint * cton-frontend: fix clippy warnings, or ignore benign ones * clippy: ignore the camelcase word WebAssembly in docs * cton-wasm: fix clippy complaints or ignore benign ones * cton-wasm tests: fix clippy complaints * cretonne: starting point turns off all clippy warnings * cretonne: clippy fixes, or lower allow() to source of problem * cretonne: more clippy fixes * cretonne: fix or disable needless_lifetimes lint this linter is buggy when the declared lifetime is used for another type constraint. * cretonne: fix clippy complaint about Pass::NoPass * rustfmt * fix prev minor api changes clippy suggested * add clippy to test-all * cton-filetests: clippy fixes * simplify clippy reporting in test-all * cretonne: document clippy allows better * cretonne: fix some more clippy lints * cretonne: fix clippy lints (mostly doc comments) * cretonne: allow all needless_lifetimes clippy warnings remove overrides at the false positives * rustfmt
78 lines
2.1 KiB
Rust
78 lines
2.1 KiB
Rust
//! Rearrange the elements in a slice according to a predicate.
|
|
|
|
/// Rearrange the elements of the mutable slice `s` such that elements where `p(t)` is true precede
|
|
/// the elements where `p(t)` is false.
|
|
///
|
|
/// The order of elements is not preserved, unless the slice is already partitioned.
|
|
///
|
|
/// Returns the number of elements where `p(t)` is true.
|
|
pub fn partition_slice<T, F>(s: &mut [T], mut p: F) -> usize
|
|
where
|
|
F: FnMut(&T) -> bool,
|
|
{
|
|
// Count the length of the prefix where `p` returns true.
|
|
let mut count = match s.iter().position(|t| !p(t)) {
|
|
Some(t) => t,
|
|
None => return s.len(),
|
|
};
|
|
|
|
// Swap remaining `true` elements into place.
|
|
//
|
|
// This actually preserves the order of the `true` elements, but the `false` elements get
|
|
// shuffled.
|
|
for i in count + 1..s.len() {
|
|
if p(&s[i]) {
|
|
s.swap(count, i);
|
|
count += 1;
|
|
}
|
|
}
|
|
|
|
count
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::partition_slice;
|
|
use std::vec::Vec;
|
|
|
|
fn check(x: &[u32], want: &[u32]) {
|
|
assert_eq!(x.len(), want.len());
|
|
let want_count = want.iter().cloned().filter(|&x| x % 10 == 0).count();
|
|
let mut v = Vec::new();
|
|
v.extend(x.iter().cloned());
|
|
let count = partition_slice(&mut v[..], |&x| x % 10 == 0);
|
|
assert_eq!(v, want);
|
|
assert_eq!(count, want_count);
|
|
}
|
|
|
|
#[test]
|
|
fn empty() {
|
|
check(&[], &[]);
|
|
}
|
|
|
|
#[test]
|
|
fn singles() {
|
|
check(&[0], &[0]);
|
|
check(&[1], &[1]);
|
|
check(&[10], &[10]);
|
|
}
|
|
|
|
#[test]
|
|
fn doubles() {
|
|
check(&[0, 0], &[0, 0]);
|
|
check(&[0, 5], &[0, 5]);
|
|
check(&[5, 0], &[0, 5]);
|
|
check(&[5, 4], &[5, 4]);
|
|
}
|
|
|
|
#[test]
|
|
fn longer() {
|
|
check(&[1, 2, 3], &[1, 2, 3]);
|
|
check(&[1, 2, 10], &[10, 2, 1]); // Note: 2, 1 order not required.
|
|
check(&[1, 10, 2], &[10, 1, 2]); // Note: 1, 2 order not required.
|
|
check(&[1, 20, 10], &[20, 10, 1]);
|
|
check(&[1, 20, 3, 10], &[20, 10, 3, 1]);
|
|
check(&[20, 3, 10, 1], &[20, 10, 3, 1]);
|
|
}
|
|
}
|