Trait SliceCombinationsExt

Source
pub trait SliceCombinationsExt<T> {
    // Required methods
    fn for_each_product<F>(&self, r: usize, f: F)
       where F: FnMut(&[T]);
    fn for_each_permutations<F>(&self, r: usize, f: F)
       where F: FnMut(&[T]);
    fn for_each_combinations<F>(&self, r: usize, f: F)
       where F: FnMut(&[T]);
    fn for_each_combinations_with_replacement<F>(&self, r: usize, f: F)
       where F: FnMut(&[T]);
    fn next_permutation(&mut self) -> bool
       where T: Ord;
    fn prev_permutation(&mut self) -> bool
       where T: Ord;
    fn next_combination(&mut self, r: usize) -> bool
       where T: Ord;
    fn prev_combination(&mut self, r: usize) -> bool
       where T: Ord;
}

Required Methods§

Source

fn for_each_product<F>(&self, r: usize, f: F)
where F: FnMut(&[T]),

Source

fn for_each_permutations<F>(&self, r: usize, f: F)
where F: FnMut(&[T]),

Source

fn for_each_combinations<F>(&self, r: usize, f: F)
where F: FnMut(&[T]),

Source

fn for_each_combinations_with_replacement<F>(&self, r: usize, f: F)
where F: FnMut(&[T]),

Source

fn next_permutation(&mut self) -> bool
where T: Ord,

Source

fn prev_permutation(&mut self) -> bool
where T: Ord,

Source

fn next_combination(&mut self, r: usize) -> bool
where T: Ord,

Source

fn prev_combination(&mut self, r: usize) -> bool
where T: Ord,

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<T> SliceCombinationsExt<T> for [T]
where T: Clone,

Source§

fn for_each_product<F>(&self, r: usize, f: F)
where F: FnMut(&[T]),

choose r elements from n independently

§Example
let n = vec![1, 2, 3, 4];
let mut p = Vec::new();
let mut q = Vec::new();
n.for_each_product(2, |v| p.push(v.to_vec()));
for x in n.iter().cloned() {
    for y in n.iter().cloned() {
        q.push(vec![x, y]);
    }
}
assert_eq!(p, q);
Source§

fn for_each_permutations<F>(&self, r: usize, f: F)
where F: FnMut(&[T]),

choose r elements from n independently

§Example
let n = vec![1, 2, 3, 4];
let mut p = Vec::new();
let mut q = Vec::new();
n.for_each_product(2, |v| p.push(v.to_vec()));
for x in n.iter().cloned() {
    for y in n.iter().cloned() {
        q.push(vec![x, y]);
    }
}
assert_eq!(p, q);
Source§

fn for_each_combinations<F>(&self, r: usize, f: F)
where F: FnMut(&[T]),

choose distinct r elements from n in any order

§Example
let n = vec![1, 2, 3, 4];
let mut p = Vec::new();
let mut q = Vec::new();
n.for_each_permutations(2, |v| p.push(v.to_vec()));
for (i, x) in n.iter().cloned().enumerate() {
    for (j, y) in n.iter().cloned().enumerate() {
        if i != j {
            q.push(vec![x, y]);
        }
    }
}
assert_eq!(p, q);
Source§

fn for_each_combinations_with_replacement<F>(&self, r: usize, f: F)
where F: FnMut(&[T]),

choose r elements from n in sorted order

§Example
let n = vec![1, 2, 3, 4];
let mut p = Vec::new();
let mut q = Vec::new();
n.for_each_combinations_with_replacement(2, |v| p.push(v.to_vec()));
for (i, x) in n.iter().cloned().enumerate() {
    for y in n[i..].iter().cloned() {
        q.push(vec![x, y]);
    }
}
assert_eq!(p, q);
Source§

fn next_permutation(&mut self) -> bool
where T: Ord,

Permute the elements into next permutation in lexicographical order. Return whether such a next permutation exists.

Source§

fn prev_permutation(&mut self) -> bool
where T: Ord,

Permute the elements into previous permutation in lexicographical order. Return whether such a previous permutation exists.

Source§

fn next_combination(&mut self, r: usize) -> bool
where T: Ord,

Permute the elements into next combination choosing r elements in lexicographical order. Return whether such a next combination exists.

Source§

fn prev_combination(&mut self, r: usize) -> bool
where T: Ord,

Permute the elements into previous combination choosing r elements in lexicographical order. Return whether such a previous combination exists.

Implementors§