From d317de8ced39487f92150e3240f95fb319432436 Mon Sep 17 00:00:00 2001 From: Mark Wainwright Date: Fri, 20 Sep 2024 08:57:35 +0100 Subject: [PATCH] made `swaps` and `swaps_array` public --- README.md | 5 +---- src/lib.rs | 26 ++++++++++++++++++++++++-- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index d490e1a..1cbae7e 100644 --- a/README.md +++ b/README.md @@ -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][] diff --git a/src/lib.rs b/src/lib.rs index 1614bb9..fd779c4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -168,11 +168,33 @@ impl Permutation { self.0.is_zero() } - fn swaps(&self) -> SwapsIterator { + /// The swaps represented by this permutation + /// ``` + /// let permutation = importunate::Permutation::::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 { SwapsIterator::new(self) } - fn swaps_array(&self) -> [u8; ELEMENTS] { + /// The array of swaps represented by this permutation + /// ``` + /// let permutation = importunate::Permutation::::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() {