pub trait Permutation<'a> {
type Permutator: Iterator;
// Required method
fn permutation(&'a mut self) -> Self::Permutator;
}Expand description
Create a permutation iterator that permute data in place.
Built-in implementation return an object of
HeapPermutation for slice/array and Vec.
It return an object of HeapPermutationCellIter
on data type of Rc<RefCell<&mut [T]>>.
§Example
For typical permutation:
use permutator::copy::Permutation;
let mut data = vec![1, 2, 3];
data.permutation().for_each(|p| {
// call multiple times. It'll print [2, 1, 3], [3, 1, 2],
// [1, 3, 2], [2, 3, 1], and [3, 2, 1] respectively.
println!("{:?}", p);
});For k-permutation:
use permutator::copy::{Combination, Permutation};
let data = [1, 2, 3, 4, 5];
let k = 3;
data.combination(k).for_each(|mut combination| {
// print the first combination
println!("{:?}", combination);
combination.permutation().for_each(|permuted| {
// print permutation of each combination
println!("{:?}", permuted);
});
});
// All k-permutation printed§See
- HeapPermutation for more detail about how to use HeapPermutation.
- HeapPermutationCellIter for more detail about how to use HeapPermutationCellIter
- Example implementation on foreign type
Required Associated Types§
Sourcetype Permutator: Iterator
type Permutator: Iterator
A permutation generator for a collection of data.
§See
Required Methods§
Sourcefn permutation(&'a mut self) -> Self::Permutator
fn permutation(&'a mut self) -> Self::Permutator
Create a permutation based on Heap’s algorithm. It return HeapPermutation object.
Implementations on Foreign Types§
Source§impl<'a, T> Permutation<'a> for *mut [T]where
T: 'a + Copy,
Generate permutation a mutable pointer to slice of T
It return HeapPermutation
but it include an original value as first value return by Iterator.
impl<'a, T> Permutation<'a> for *mut [T]where
T: 'a + Copy,
Generate permutation a mutable pointer to slice of T
It return HeapPermutation
but it include an original value as first value return by Iterator.
§Warning
This implementation hid unsafe inside the permutation function but doesn’t provide any additional safety. User need to treat the return object as unsafe.
§Breaking change from 0.3.x to 0.4
Since version 0.4.0, the first result return by this iterator will be the original value
Source§type Permutator = Chain<Once<()>, HeapPermutationRefIter<'a, T>>
type Permutator = Chain<Once<()>, HeapPermutationRefIter<'a, T>>
Use HeapPermutation as permutation generator
fn permutation(&'a mut self) -> Chain<Once<()>, HeapPermutationRefIter<'_, T>>
Source§impl<'a, T> Permutation<'a> for [T]where
T: 'a + Copy,
Generate permutation on an array or slice of T
It return HeapPermutation
but it include an original value as first value return by Iterator.
impl<'a, T> Permutation<'a> for [T]where
T: 'a + Copy,
Generate permutation on an array or slice of T
It return HeapPermutation
but it include an original value as first value return by Iterator.
§Breaking change from 0.3.x to 0.4
Since version 0.4.0, the first result return by this iterator will be the original value
Source§type Permutator = Chain<Once<Vec<T>>, HeapPermutationIterator<'a, T>>
type Permutator = Chain<Once<Vec<T>>, HeapPermutationIterator<'a, T>>
Use HeapPermutation as permutation generator
fn permutation( &'a mut self, ) -> Chain<Once<Vec<T>>, HeapPermutationIterator<'a, T>>
Source§impl<'a, T> Permutation<'a> for Rc<RefCell<&'a mut [T]>>where
T: 'a + Copy,
Generate a sharable permutation inside Rc<RefCell<&mut [T]>>
It return HeapPermutationCellIter
but it include an original value as first value return by Iterator.
impl<'a, T> Permutation<'a> for Rc<RefCell<&'a mut [T]>>where
T: 'a + Copy,
Generate a sharable permutation inside Rc<RefCell<&mut [T]>>
It return HeapPermutationCellIter
but it include an original value as first value return by Iterator.
§Breaking change from 0.3.x to 0.4
Since version 0.4.0, the first result return by this iterator will be the original value
Source§type Permutator = Chain<Once<()>, HeapPermutationCellIter<'a, T>>
type Permutator = Chain<Once<()>, HeapPermutationCellIter<'a, T>>
Use HeapPermutationCellIter as permutation generator
fn permutation(&'a mut self) -> Chain<Once<()>, HeapPermutationCellIter<'_, T>>
Source§impl<'a, T> Permutation<'a> for Vec<T>where
T: 'a + Copy,
Generate permutation on a Vec of T
It return HeapPermutation
but it include an original value as first value return by Iterator.
impl<'a, T> Permutation<'a> for Vec<T>where
T: 'a + Copy,
Generate permutation on a Vec of T
It return HeapPermutation
but it include an original value as first value return by Iterator.
§Breaking change from 0.3.x to 0.4
Since version 0.4.0, the first result return by this iterator will be the original value
Source§type Permutator = Chain<Once<Vec<T>>, HeapPermutationIterator<'a, T>>
type Permutator = Chain<Once<Vec<T>>, HeapPermutationIterator<'a, T>>
Use HeapPermutation as permutation generator