Permutation

Struct Permutation 

Source
pub struct Permutation<const N: usize> { /* private fields */ }
Expand description

The main type, representing a rearrangement of N objects.

Implementations§

Source§

impl<const N: usize> Permutation<N>

Source

pub fn identity() -> Self

The identity permutation that doesn’t reorder anything (leaves everything unchanged)

Source

pub fn is_identity(&self) -> bool

Check if the permutation is equal to the identity

Source

pub fn compose(self, rhs: Self) -> Self

Compute the permutation that is equivalent to first applying self and then rhs

Source

pub fn inverse(self) -> Self

Compute the permutation that reverts the changes made by self

Source§

impl<const N: usize> Permutation<N>

Misc. Math

Source

pub fn rotate_left(n: usize) -> Self

The permutation corresponding to slice::rotate_left(n)

Source

pub fn rotate_right(n: usize) -> Self

The permutation corresponding to slice::rotate_right(n)

Source

pub fn min_subperm_len(&self) -> usize

Computes the M such that self only changes the first M elements and leaves the rest alone

Source

pub fn from_subperm<const M: usize>(sub: Permutation<M>) -> Self

Constructs a permutation that changes the first M elements according to sub and leaves the rest alone

§Panics
  • if M > N
Source

pub fn from_fn<F: FnMut(usize) -> usize>(f: F) -> Result<Self, InvalidArrFnRepr>

Constructs a permutation from its function representation

The function representation is basically the same as the array representation: The equivalence is arr[i] == f(i).

Source

pub fn as_fn(&self) -> impl Fn(usize) -> usize + '_

Converts a permutation to its function representation, borrowing from self

Source

pub fn to_fn(self) -> impl Fn(usize) -> usize + 'static

Converts a permutation to its function representation, moving self

Source

pub fn apply<T>(&self, array: [T; N]) -> [T; N]

Applies the reordering given by self to array

Source

pub fn apply_in_place<T>(&self, slice: &mut [T])

Applies the permutation self to the first N elements of slice

See also Swaps::apply_in_place

§Panics
  • if slice.len() > self.min_subperm_len()
Source

pub fn sign(&self) -> PermutationSign

Computes the permutation sign

Source

pub fn is_even(&self) -> bool

Checks if the permutation is even, i.e. if the sign is positive

Source

pub fn is_odd(&self) -> bool

Checks if the permutation is odd, i.e. if the sign is negative

Source§

impl<const N: usize> Permutation<N>

Representations

Source

pub fn from_array_repr(arr: [usize; N]) -> Result<Self, InvalidArrFnRepr>

Construct a permutation from its array representation

  • The array is interpreted as the one-line notation.
  • Elements start at zero, unlike in mathematics where they usually start at 1
Source

pub fn from_one_based_array_repr( arr: [NonZeroUsize; N], ) -> Result<Self, InvalidArrFnRepr>

Same as from_array_repr but uses the more familliar one-based values (used in mathematics)

Source

pub fn to_array_repr(self) -> [usize; N]

Obtain the array representation for a permutation (see from_array_repr)

Source

pub fn from_cycles_repr(cycles: Cycles<N>) -> Result<Self, InvalidCycleRepr>

Construct a permutation from its cycles representation

Source

pub fn to_cycles_repr(self) -> Cycles<N>

Obtain the cycles representation for a permutation

Source

pub fn from_swaps_repr<const M: usize, O: SwapsReprOrder>( swaps: Swaps<M, O>, ) -> Result<Self, InvalidSwapRepr>

Construct a permutation from its representation as a composition as M or less swaps

Source

pub fn to_swaps_repr(self) -> Swaps<N, SequentialApplicationOrder>

Represent a permutation as N or less swaps

Source§

impl<const N: usize> Permutation<N>

Construct a permutations that would sort a slice/array when applyed

Source

pub fn from_custom_sort<T: Ord, S: SortArray<N>>(arr: &[T; N]) -> Self

Construct a permutation that would sort arr according to the sort method given by S

Source

pub fn from_sort_unstable<T: Ord>(arr: &[T; N]) -> Self

Construct a permutation that would sort arr according to the sort method given by CoreSortUnstable

Source

pub fn from_sort<T: Ord>(arr: &[T; N]) -> Self

Available on crate feature std only.

Construct a permutation that would sort arr according to the sort method given by StdSort

Source

pub fn from_custom_sort_slice<T: Ord, S: SortSlice>( sl: &[T], ) -> Result<Self, TooLongSliceForPermutation>

Construct a permutation that would sort sl according to the sort method given by S

Source

pub fn from_sort_unstable_slice<T: Ord>( sl: &[T], ) -> Result<Self, TooLongSliceForPermutation>

Construct a permutation that would sort sl according to the sort method given by CoreSortUnstable

Source

pub fn from_sort_slice<T: Ord>( sl: &[T], ) -> Result<Self, TooLongSliceForPermutation>

Available on crate feature std only.

Construct a permutation that would sort sl according to the sort method given by StdSort

Trait Implementations§

Source§

impl<const N: usize> Clone for Permutation<N>

Source§

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

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<const N: usize> Debug for Permutation<N>

Source§

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

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

impl<const N: usize> Distribution<Permutation<N>> for Standard

Available on crate feature rand only.
Source§

fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Permutation<N>

Generate a random value of T, using rng as the source of randomness.
Source§

fn sample_iter<R>(self, rng: R) -> DistIter<Self, R, T>
where R: Rng, Self: Sized,

Create an iterator that generates random values of T, using rng as the source of randomness. Read more
Source§

fn map<F, S>(self, func: F) -> DistMap<Self, F, T, S>
where F: Fn(T) -> S, Self: Sized,

Create a distribution of values of ‘S’ by mapping the output of Self through the closure F Read more
Source§

impl<const N: usize> Hash for Permutation<N>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<const N: usize> PartialEq for Permutation<N>

Source§

fn eq(&self, other: &Permutation<N>) -> 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<const N: usize> Copy for Permutation<N>

Source§

impl<const N: usize> Eq for Permutation<N>

Source§

impl<const N: usize> StructuralPartialEq for Permutation<N>

Auto Trait Implementations§

§

impl<const N: usize> Freeze for Permutation<N>

§

impl<const N: usize> RefUnwindSafe for Permutation<N>

§

impl<const N: usize> Send for Permutation<N>

§

impl<const N: usize> Sync for Permutation<N>

§

impl<const N: usize> Unpin for Permutation<N>

§

impl<const N: usize> UnwindSafe for Permutation<N>

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.