rusty-perm
Rusty permutation that supports no-std
and compile-time checked size.
Cargo Features
To import this crate to your project,
[]
= "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.
[]
= { = "0.2", = false }
Usage
Import this crate
To import members from this crate,
use ;
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 ;
let perm1 = identity;
let perm2 = identity;
Build by sorting slices and arrays
It can extracts the permutation by sorting an array.
use ;
// `perm` is an operator that maps [9, 6, -1, 4] to [-1, 4, 6, 9].
let perm = from_sort;
// Apply same permutation on another array
let mut array = ;
perm.apply;
assert_eq!;
You can sort with custom comparing or key function by from_sort_by
, from_sort_by_key
and from_sort_by_cached_key
.
use ;
// `perm` is an operator that maps [9, 6, -1, 4] to [9, 6, 4, -1].
let perm = from_sort_by_key;
// Apply same permutation on another array
let mut array = ;
perm.apply;
assert_eq!;
Build by indices
The permutation can be constructed by demonstrating the sorted indices.
use ;
let perm = from_indices.unwrap;
let mut array = ;
perm.apply;
assert_eq!;
Inverse and composition
The example demonstrates the inverse and composition of permutations.
use ;
// Construct the permutation, its inverse and compose them
let perm = from_indices.unwrap;
let inverse = perm.inverse;
let composition = &inverse * &perm;
// Check that composition with its inverse is identity
assert_eq!;
License
Apache 2.0 and MIT dual license.