Perm

Struct Perm 

Source
pub struct Perm { /* private fields */ }
Expand description

Represents a reordering operation on an array.

See the Permute trait for more information.

Implementations§

Source§

impl Perm

Source

pub fn eye(n: usize) -> Perm

Construct the identity perm of a given length.

Source

pub fn len(&self) -> usize

Get the length of the permutation.

Source

pub fn argsort<T: Ord>(xs: &[T]) -> Perm

Compute the Perm that, when applied to the input slice, would (stably) sort it.

Source

pub fn argsort_unstable<T: Ord>(xs: &[T]) -> Perm

Compute a Perm that, when applied to the input slice, would sort it. (not necessarily stably)

Source

pub fn from_vec(vec: Vec<usize>) -> Result<Perm, InvalidPermutationError>

Construct a perm. Useful for literals in unit tests.

The representation accepted by this is comparable to indexing with an integer array in numpy. If the kth element of the permutation vector is value, then applying the permutation will pull the data at index value into index k.

This performs O(n log n) validation on the data to verify that it satisfies the invariants of Perm. It also inverts the perm (an O(n) operation).

Source

pub fn from_raw_inv(inv: Vec<usize>) -> Result<Perm, InvalidPermutationError>

Construct a perm from the vector internally used to represent it.

The format taken by this method is actually the inverse of the format accepted by from_vec. If the kth element of the permutation vector is value, then applying the permutation will push the data at index k over to index value. This format is generally trickier to think about, but is superior to the from_vec representation in terms of efficiency.

This performs O(n log n) validation on the data to verify that it satisfies the invariants of Perm.

Source

pub unsafe fn from_raw_inv_unchecked(inv: Vec<usize>) -> Perm

No-op constructor. Still performs checking in debug builds.

§Safety

inv must contain every element in (0..inv.len()), or else the behavior is undefined.

Source

pub fn append_mut(&mut self, other: &Perm)

Construct a permutation of length a.len() + b.len().

Mathematically speaking, this computes the “direct sum” of two permutations.

The inserted elements will be shifted by this permutation’s length, so that they operate on an entirely independent set of data from the existing elements.

Source

pub fn random(n: usize) -> Perm

Construct a random permutation of the given length.

Source

pub fn into_vec(self) -> Vec<usize>

Recover the vector representation of the permutation.

See Perm::from_vec for more details about this representation.

This has a runtime cost of O(n), because Perm does not actually store this vector. See Perm::into_raw_inv for a constant-time alternative.

Source

pub fn into_raw_inv(self) -> Vec<usize>

Obtain the vector that is internally used to represent the permutation.

This representation is actually the inverse of the representation produced by Perm::into_vec. See Perm::from_raw_inv for more information.

Source

pub fn inverted(&self) -> Perm

Get the inverse of this permutation.

Source

pub fn shift_right(self, amt: usize) -> Self

Compose with the permutation that shifts elements forward (performing self first)

To construct the shift permutation itself, use Perm::eye(n).shift_right(amt).

Source

pub fn shift_left(self, amt: usize) -> Self

Compose with the permutation that shifts elements backward (performing self first)

To construct the shift permutation itself, use Perm::eye(n).shift_left(amt).

Source

pub fn shift_signed(self, n: isize) -> Self

Compose with the permutation that shifts elements to the right by a signed offset.

Source

pub fn permute_index(&self, i: usize) -> usize

Apply the permutation to an index. O(1).

Calling this on the indices contained in a sparse-format data structure will produce the same indices as if the corresponding dense-format data structure were permuted.

§Panics

Panics if i is out of bounds for the permutation length.

Source

pub fn with_outer(&self, slower: &Perm) -> Perm

Construct the outer product of self and slower, with self being the fast (inner) index.

The resulting Perm will permute blocks of size self.len() according to slower, and will permute elements within each block by self.

Source

pub fn with_inner(&self, faster: &Perm) -> Perm

Construct the outer product of self and faster, with self being the slow (outer) index.

Source

pub fn pow_unsigned(&self, exp: u64) -> Perm

Compute the permutation that applies this permutation exp times in a row.

This uses exponentiation by squaring to run in O(log(exp)) time.

Source

pub fn pow_signed(&self, exp: i64) -> Perm

Compute the permutation that applies this permutation exp times in a row.

This version of the function takes a signed value, so that negative values can produce powers of the inverse.

This uses exponentiation by squaring to run in O(log(exp)) time.

Source§

impl Perm

Source

pub fn then(&self, other: &Perm) -> Perm

Flipped group operator, which composes left-to-right.

Very simply. a.then(b) == b.of(a). The flipped order can feel more natural when using method syntax, or if you are dealing with matrix equations written in a row-centric formalism.

Additionally, it has a straightforward relation to the group action:

x.permuted_by(a).permuted_by(b) == x.permuted_by(a.then(b))
Source

pub fn of(&self, other: &Perm) -> Perm

Conventional group operator, which composes right-to-left.

Trait Implementations§

Source§

impl Clone for Perm

Source§

fn clone(&self) -> Perm

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 Debug for Perm

Source§

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

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

impl Hash for Perm

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 PartialEq for Perm

Source§

fn eq(&self, other: &Perm) -> 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 Permute for Perm

Source§

fn permuted_by(self, other: &Perm) -> Perm

Source§

impl Eq for Perm

Source§

impl StructuralPartialEq for Perm

Auto Trait Implementations§

§

impl Freeze for Perm

§

impl RefUnwindSafe for Perm

§

impl Send for Perm

§

impl Sync for Perm

§

impl Unpin for Perm

§

impl UnwindSafe for Perm

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.