Skip to content

Commit

Permalink
made swaps and swaps_array public
Browse files Browse the repository at this point in the history
  • Loading branch information
wainwrightmark committed Sep 20, 2024
1 parent 78d46a6 commit d317de8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,10 @@ The following table shows how many elements can fit into each type as well as th

There are three different methods for calculating a permutation:

`calculate_incomplete` will calculate the permuation for any array whose elements implement `Ord` but it is comparatively slow. It will even work if the array contains duplicate elements but keep in mind that permuations describing such arrays will not be unique.
`calculate_incomplete` will calculate the permutation for any array whose elements implement `Ord` but it is comparatively slow. It will even work if the array contains duplicate elements but keep in mind that permuations describing such arrays will not be unique.

`try_calculate` and `calculate_unchecked` both expect arrays of elements and functions mapping those elements to `u8`. Every element should map to a different `u8` in the range `0..ELEMENTS`. If this condition is not met, `try_calculate` will return `None` and `calculate_unchecked` will panic or loop forever. *Do not use it on user input*




## Contributing

Contributions are welcome! Open a pull request to fix a bug, or [open an issue][]
Expand Down
26 changes: 24 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,33 @@ impl<I: Inner, const ELEMENTS: usize> Permutation<I, ELEMENTS> {
self.0.is_zero()
}

fn swaps(&self) -> SwapsIterator<I> {
/// The swaps represented by this permutation
/// ```
/// let permutation = importunate::Permutation::<u8, 5>::try_from_inner(&42).unwrap();
/// let mut arr = [0,1,2,3,4];
///
/// for (i, swap) in permutation.swaps().enumerate() {
/// arr.swap(i, usize::from(swap) + i);
/// }
///
/// assert_eq!(arr, [2, 1, 4, 3, 0])
/// ```
pub fn swaps(&self) -> SwapsIterator<I> {
SwapsIterator::new(self)
}

fn swaps_array(&self) -> [u8; ELEMENTS] {
/// The array of swaps represented by this permutation
/// ```
/// let permutation = importunate::Permutation::<u8, 5>::try_from_inner(&42).unwrap();
/// let mut arr = [0,1,2,3,4];
///
/// for (i, swap) in permutation.swaps_array().into_iter().enumerate() {
/// arr.swap(i, usize::from(swap) + i);
/// }
///
/// assert_eq!(arr, [2, 1, 4, 3, 0])
/// ```
pub fn swaps_array(&self) -> [u8; ELEMENTS] {
let mut swaps = [0; ELEMENTS];

for (i, swap) in self.swaps().enumerate() {
Expand Down

0 comments on commit d317de8

Please sign in to comment.