Permutation

Struct Permutation 

Source
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>

Source

pub fn len(&self) -> usize

Returns the length of the inner permutation vector.

Source

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());
Source

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);
Source

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());
Source

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);
Source

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 permutation
Source

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);
Source

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>

Source§

fn clone(&self) -> Permutation<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for Permutation<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: PartialEq> PartialEq for Permutation<T>

Source§

fn eq(&self, other: &Permutation<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T> StructuralPartialEq for Permutation<T>

Auto Trait Implementations§

§

impl<T> Freeze for Permutation<T>

§

impl<T> RefUnwindSafe for Permutation<T>
where T: RefUnwindSafe,

§

impl<T> Send for Permutation<T>
where T: Send,

§

impl<T> Sync for Permutation<T>
where T: Sync,

§

impl<T> Unpin for Permutation<T>
where T: Unpin,

§

impl<T> UnwindSafe for Permutation<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.