#iterator #random #min-max #max #min

no-std importunate

Methods for returning random elements from an iterator

4 releases

new 0.1.3 Dec 6, 2024
0.1.2 Sep 20, 2024
0.1.1 Feb 3, 2023
0.1.0 Feb 3, 2023

#299 in Algorithms

Download history 112/week @ 2024-09-14 52/week @ 2024-09-21 12/week @ 2024-09-28 5/week @ 2024-10-26 22/week @ 2024-11-02 2/week @ 2024-11-09 4/week @ 2024-11-16 2/week @ 2024-11-23 61/week @ 2024-11-30

75 downloads per month

MIT license

53KB
1K SLoC

importunate

GITHUB Crates.io GitHub Workflow Status docs

Apply and manipulate permutations of small, const sized, distinct sets.

  • calculate a Permutation and apply it later
  • Get element_at_index or index_of_element
  • combine two Permutations, including the built in ones: reverse, rotate_right and rotate_left
  • invert (undo) a Permutation
  • convert to_le_byte_array or try_from_le_byte_array to store in as few bytes as mathematically possible

no_std by default. Features for serde and arbitrary

The name of the crate is an anagram of 'permutation'.


This crate works with Cargo with a Cargo.toml like:

[dependencies]
importunate = "0.1.3"

Getting started

use importunate::*;

fn main() {
    let arr1 = [2,0,1,3];
    let mut arr2 = ["zero", "one", "two", "three"];
    let perm = Permutation::<u8,4>::calculate_unchecked(&arr1, |&x|x);
    perm.apply(arr2);

    assert_eq!(arr2,["two","zero", "one",  "three"] );
}

Permutations take two generic parameters, the Inner type (an unsigned integer) and the number of Elements.

The number of elements must not be greater than the maximum allowed for that inner type. The following table shows how many elements can fit into each type as well as the minimum number of bytes needed.

Max Elements Bytes Type
5 1 u8
8 2 u16
10 3 u32
12 4
14 5 u64
16 6
18 7
20 8
22 9 u128
24 10
25 11
27 12
29 13
30 14
32 15
34 16

There are three different methods for calculating a permutation:

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 to discuss a new feature or change.

Check out the Contributing section in the docs for more info.

License

This project is proudly licensed under the MIT license (LICENSE or http://opensource.org/licenses/MIT).

importunate can be distributed according to the MIT license. Contributions will be accepted under the same license.

Authors

Dependencies

~140–390KB