Crate rusty_perm

Source
Expand description

Rusty permutation that supports no-std and compile-time checked size.

§Cargo features

To import this crate to your project,

[dependencies]
rusty-perm = "0.2"

It has the following cargo features.

  • std (default): enable the standard library.
  • rand (default): enable random sampling of permutation.

To restrict the crate to no_std, you can disable the default features.

[dependencies]
rusty-perm = { version = "0.2", default-features = false }

§Import this crate

To import members from this crate,

use rusty_perm::{prelude::*, PermD, PermS};

Both PermD and PermS represent permutations, except that PermS has an embedded compile-time size in type signature. The static size prevents from applying permutation on arrays of wrong sizes in compile-time, and saves some runtime overheads.

§Identity

The identity permutation can be constructed with static or dynamic size.

use rusty_perm::{PermD, PermS};
let perm1 = PermS::<10>::identity();
let perm2 = PermD::identity(10);

§Build by sorting slices and arrays

It can extracts the permutation by sorting an array.

use rusty_perm::{prelude::*, PermS};

// `perm` is an operator that maps [9, 6, -1, 4] to [-1, 4, 6, 9].
let perm = PermS::from_sort(&[9, 6, -1, 4]);

// Apply same permutation on another array
let mut array = [1, 2, 3, 4];
perm.apply(&mut array);
assert_eq!(array, [3, 4, 2, 1]);

You can sort with custom comparing or key function by from_sort_by, from_sort_by_key and from_sort_by_cached_key.

use rusty_perm::{prelude::*, PermS};

// `perm` is an operator that maps [9, 6, -1, 4] to [9, 6, 4, -1].
let perm = PermS::from_sort_by_key(&[9, 6, -1, 4], |val| -val);

// Apply same permutation on another array
let mut array = [1, 2, 3, 4];
perm.apply(&mut array);
assert_eq!(array, [1, 2, 4, 3]);

§Build by indices

The permutation can be constructed by demonstrating the sorted indices.

use rusty_perm::{prelude::*, PermD};
let perm = PermD::from_indices([2, 0, 1]).unwrap();

let mut array = [-9, -5, 3];
perm.apply(&mut array);
assert_eq!(array, [3, -9, -5]);

§Inverse and composition

The example demonstrates the inverse and composition of permutations.

use rusty_perm::{prelude::*, PermD, PermS};

// Construct the permutation, its inverse and compose them
let perm = PermS::from_indices([2, 0, 1]).unwrap();
let inverse = perm.inverse();
let composition = &inverse * &perm;

// Check that composition with its inverse is identity
assert_eq!(PermD::identity(3), composition);

Modules§

prelude
Re-export of common traits.
size
Permutation size markers.

Structs§

Perm
Generic permutation data structure.

Traits§

PermApply
The permutation operator on slice-like types.
PermFromIndices
An operator that builds a permutation from a list indexes.
PermFromSorting
An operator that builds a permutation type by sorting slice-like types.
PermProduct
The permutation composition operator.
Permutation
An abstract representation of permutation data structure.

Type Aliases§

Perm0
Permutation type with static size 0.
Perm1
Permutation type with static size 1.
Perm2
Permutation type with static size 2.
Perm4
Permutation type with static size 4.
Perm5
Permutation type with static size 5.
Perm6
Permutation type with static size 6.
Perm7
Permutation type with static size 7.
Perm8
Permutation type with static size 8.
Perm9
Permutation type with static size 9.
Perm10
Permutation type with static size 10.
Perm11
Permutation type with static size 11.
Perm12
Permutation type with static size 12.
Perm13
Permutation type with static size 13.
Perm14
Permutation type with static size 14.
Perm15
Permutation type with static size 15.
Perm16
Permutation type with static size 16.
Perm17
Permutation type with static size 17.
Perm18
Permutation type with static size 18.
Perm19
Permutation type with static size 19.
Perm20
Permutation type with static size 20.
Perm21
Permutation type with static size 21.
Perm22
Permutation type with static size 22.
Perm23
Permutation type with static size 23.
Perm24
Permutation type with static size 24.
Perm25
Permutation type with static size 25.
Perm26
Permutation type with static size 26.
Perm27
Permutation type with static size 27.
Perm28
Permutation type with static size 28.
Perm29
Permutation type with static size 29.
Perm30
Permutation type with static size 30.
Perm31
Permutation type with static size 31.
Perm32
Permutation type with static size 32.
PermD
Permutation type with runtime size.
PermS
Permutation type with static size known in compile time.