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>
impl<const N: usize> Permutation<N>
Sourcepub fn identity() -> Self
pub fn identity() -> Self
The identity permutation that doesn’t reorder anything (leaves everything unchanged)
Sourcepub fn is_identity(&self) -> bool
pub fn is_identity(&self) -> bool
Check if the permutation is equal to the identity
Source§impl<const N: usize> Permutation<N>
Misc. Math
impl<const N: usize> Permutation<N>
Misc. Math
Sourcepub fn rotate_left(n: usize) -> Self
pub fn rotate_left(n: usize) -> Self
The permutation corresponding to slice::rotate_left(n)
Sourcepub fn rotate_right(n: usize) -> Self
pub fn rotate_right(n: usize) -> Self
The permutation corresponding to slice::rotate_right(n)
Sourcepub fn min_subperm_len(&self) -> usize
pub fn min_subperm_len(&self) -> usize
Computes the M such that self only changes the first M elements and leaves the rest alone
Sourcepub fn from_subperm<const M: usize>(sub: Permutation<M>) -> Self
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
Sourcepub fn from_fn<F: FnMut(usize) -> usize>(f: F) -> Result<Self, InvalidArrFnRepr>
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).
Sourcepub fn as_fn(&self) -> impl Fn(usize) -> usize + '_
pub fn as_fn(&self) -> impl Fn(usize) -> usize + '_
Converts a permutation to its function representation, borrowing from self
Sourcepub fn to_fn(self) -> impl Fn(usize) -> usize + 'static
pub fn to_fn(self) -> impl Fn(usize) -> usize + 'static
Converts a permutation to its function representation, moving self
Sourcepub fn apply_in_place<T>(&self, slice: &mut [T])
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()
Sourcepub fn sign(&self) -> PermutationSign
pub fn sign(&self) -> PermutationSign
Computes the permutation sign
Source§impl<const N: usize> Permutation<N>
Representations
impl<const N: usize> Permutation<N>
Representations
Sourcepub fn from_array_repr(arr: [usize; N]) -> Result<Self, InvalidArrFnRepr>
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
Sourcepub fn from_one_based_array_repr(
arr: [NonZeroUsize; N],
) -> Result<Self, InvalidArrFnRepr>
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)
Sourcepub fn to_array_repr(self) -> [usize; N]
pub fn to_array_repr(self) -> [usize; N]
Obtain the array representation for a permutation (see from_array_repr)
Sourcepub fn from_cycles_repr(cycles: Cycles<N>) -> Result<Self, InvalidCycleRepr>
pub fn from_cycles_repr(cycles: Cycles<N>) -> Result<Self, InvalidCycleRepr>
Construct a permutation from its cycles representation
Sourcepub fn to_cycles_repr(self) -> Cycles<N>
pub fn to_cycles_repr(self) -> Cycles<N>
Obtain the cycles representation for a permutation
Sourcepub fn from_swaps_repr<const M: usize, O: SwapsReprOrder>(
swaps: Swaps<M, O>,
) -> Result<Self, InvalidSwapRepr>
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
Sourcepub fn to_swaps_repr(self) -> Swaps<N, SequentialApplicationOrder>
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
impl<const N: usize> Permutation<N>
Construct a permutations that would sort a slice/array when applyed
Sourcepub fn from_custom_sort<T: Ord, S: SortArray<N>>(arr: &[T; N]) -> Self
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
Sourcepub fn from_sort_unstable<T: Ord>(arr: &[T; N]) -> Self
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
Sourcepub fn from_sort<T: Ord>(arr: &[T; N]) -> Self
Available on crate feature std only.
pub fn from_sort<T: Ord>(arr: &[T; N]) -> Self
std only.Construct a permutation that would sort arr according to the sort method given by StdSort
Sourcepub fn from_custom_sort_slice<T: Ord, S: SortSlice>(
sl: &[T],
) -> Result<Self, TooLongSliceForPermutation>
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
Sourcepub fn from_sort_unstable_slice<T: Ord>(
sl: &[T],
) -> Result<Self, TooLongSliceForPermutation>
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
Sourcepub fn from_sort_slice<T: Ord>(
sl: &[T],
) -> Result<Self, TooLongSliceForPermutation>
Available on crate feature std only.
pub fn from_sort_slice<T: Ord>( sl: &[T], ) -> Result<Self, TooLongSliceForPermutation>
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>
impl<const N: usize> Clone for Permutation<N>
Source§fn clone(&self) -> Permutation<N>
fn clone(&self) -> Permutation<N>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<const N: usize> Debug for Permutation<N>
impl<const N: usize> Debug for Permutation<N>
Source§impl<const N: usize> Distribution<Permutation<N>> for Standard
Available on crate feature rand only.
impl<const N: usize> Distribution<Permutation<N>> for Standard
rand only.Source§fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Permutation<N>
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Permutation<N>
T, using rng as the source of randomness.Source§fn sample_iter<R>(self, rng: R) -> DistIter<Self, R, T>
fn sample_iter<R>(self, rng: R) -> DistIter<Self, R, T>
T, using rng as
the source of randomness. Read more