pub struct Permutation<T> {
pub permu: Vec<T>,
}Expand description
Contains a permutation vector and methods to generate permutations.
Fields§
§permu: Vec<T>Implementations§
Source§impl<T> Permutation<T>
impl<T> Permutation<T>
Sourcepub fn from_vec(vec: Vec<T>) -> Result<Permutation<T>, Error>
pub fn from_vec(vec: Vec<T>) -> Result<Permutation<T>, Error>
Initializes a Permutation with the given vector.
§Errors
If the given vector is not a permutation the function will return
a NotPermutation error.
§Example
use permu_rs::permutation::Permutation;
let permu_ok = Permutation::<u8>::from_vec(vec![0,4,2,1,3]).unwrap();
// Returns an error as the given vector is not a permutation
let permu_err = Permutation::<u8>::from_vec(vec![5,4,2,1,3]);
assert!(permu_err.is_err());Sourcepub fn from_vec_unsec(vec: Vec<T>) -> Permutation<T>
pub fn from_vec_unsec(vec: Vec<T>) -> Permutation<T>
Initializes a Permutation with the given vector. No checking is done to the given vector, the permutation can be initialized with a vector that is not a permutation.
§Example
use permu_rs::permutation::Permutation;
let vec : Vec<u16> = vec![0,1,2,3,4];
let permu : Permutation<u16> = Permutation::from_vec_unsec(vec);Sourcepub fn random(length: usize) -> Permutation<T>
pub fn random(length: usize) -> Permutation<T>
Generates a random permutation of the length given.
§Panics
If the length given is grater than the maximum value that T can hold,
the method will panic.
§Example
use permu_rs::permutation::Permutation;
let rand_permu : Permutation<u16> = Permutation::random(8);
assert!(rand_permu.is_permu());
assert_eq!(8, rand_permu.permu.len());Sourcepub fn identity(length: usize) -> Permutation<T>
pub fn identity(length: usize) -> Permutation<T>
Returns an identity permutation of the length given.
§Panics
If the length given is grater than the maximum value that T can hold,
the method will panic.
§Example
use permu_rs::permutation::Permutation;
let identity : Permutation<u8> = Permutation::identity(5);
assert_eq!(vec![0,1,2,3,4], identity.permu);Sourcepub fn is_permu(&self) -> bool
pub fn is_permu(&self) -> bool
Checks if the vector inside Permutation is really a permutation.
§Example
use permu_rs::permutation::Permutation;
let permu1 : Permutation<u8> = Permutation::from_vec_unsec(vec![0,1,2,3]);
let permu2 : Permutation<u8> = Permutation::from_vec_unsec(vec![1,2,3]);
let permu3 : Permutation<u8> = Permutation::from_vec_unsec(vec![0,1,4,3]);
let permu4 : Permutation<u8> = Permutation::from_vec_unsec(vec![0,1,1,3]);
assert!(permu1.is_permu());
assert!(!permu2.is_permu()); // Not permutation
assert!(!permu3.is_permu()); // Not permutation
assert!(!permu4.is_permu()); // Not permutationSourcepub fn invert(&self, output: &mut Permutation<T>) -> Result<(), Error>
pub fn invert(&self, output: &mut Permutation<T>) -> Result<(), Error>
Fills a given output Permutation with the inverted permutation of the current
permutation. inverted(permutation) = permutation^(-1).
§Errors
Returns a LengthError if the both permutations are not of the same length.
§Example
use permu_rs::permutation::Permutation;
// Init permutations
let permu = Permutation::<u8>::from_vec(vec![0,2,3,1]).unwrap();
let target = Permutation::<u8>::from_vec(vec![0,3,1,2]).unwrap();
// Target permutation, filled with zeros
let mut output = Permutation::from_vec_unsec(vec![0u8; permu.len()]);
// Calculate the inverted permutation of `permu`
permu.invert(&mut output).unwrap();
println!("permu: {:?}", permu);
println!("invert: {:?}", output);
assert_eq!(target, output);Sourcepub fn compose_with(
&self,
other: &Permutation<T>,
result: &mut Permutation<T>,
) -> Result<(), Error>
pub fn compose_with( &self, other: &Permutation<T>, result: &mut Permutation<T>, ) -> Result<(), Error>
Composes the current Permutation with a given permutation with the given one.
In other words: result = permu[other].
§Example
use permu_rs::permutation::Permutation;
let mut permu = Permutation::<u8>::random(4);
let identity = Permutation::<u8>::identity(4);
let mut result = Permutation::<u8>::random(4);
let other = Permutation::<u8>::random(4);
permu.compose_with(&identity, &mut result).unwrap();
assert_eq!(permu, result);
permu.compose_with(&other, &mut result).unwrap();
let mut aux = other.clone();
other.invert(&mut aux);
let mut result2 = result.clone();
result.compose_with(&aux, &mut result2).unwrap();
assert_eq!(permu, result2);Trait Implementations§
Source§impl<T: Clone> Clone for Permutation<T>
impl<T: Clone> Clone for Permutation<T>
Source§fn clone(&self) -> Permutation<T>
fn clone(&self) -> Permutation<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more