3
3
[ ![ Build Status] ( https://travis-ci.com/asimihsan/permutation-iterator-rs.svg?branch=master )] ( https://travis-ci.com/asimihsan/permutation-iterator-rs )
4
4
[ ![ Crate] ( https://img.shields.io/crates/v/permutation_iterator.svg )] ( https://crates.io/crates/permutation_iterator )
5
5
[ ![ API] ( https://docs.rs/permutation_iterator/badge.svg )] ( https://docs.rs/permutation_iterator )
6
+ ![ License] ( https://img.shields.io/crates/l/permutation_iterator.svg )
7
+
6
8
7
9
A Rust library for iterating over random permutations without fully materializing them into memory.
8
10
@@ -18,8 +20,67 @@ Add this to your `Cargo.toml`:
18
20
permutation_iterator = " 0.1.0"
19
21
```
20
22
23
+ ## Example
24
+
25
+ ### Random, single integer range
26
+
27
+ Here is how to iterate over a random permutation of integers in the range ` [0, max) ` , i.e. ` 0 ` inclusive to ` max `
28
+ exclusive. Every time you run this you will get a different permutation.
29
+
30
+ ``` rust
31
+ use permutation_iterator :: Permutor ;
32
+
33
+ fn main () {
34
+ let max = 10 ;
35
+ let permutor = Permutor :: new (max );
36
+ for permuted in permutor {
37
+ println! (" {}" , permuted );
38
+ }
39
+ }
40
+ ```
41
+
42
+ ### Deterministic, single integer range
43
+
44
+ You can also pass in a ` key ` in order to iterate over a deterministically random permutation. Every time you run this
45
+ you will get the same permutation:
46
+
47
+ ``` rust
48
+ use permutation_iterator :: Permutor ;
49
+
50
+ fn main () {
51
+ let max = 10 ;
52
+ let key : [u8 ; 32 ] = [0xBA ; 32 ];
53
+ let permutor = Permutor :: new_with_slice_key (max , key );
54
+ for permuted in permutor {
55
+ println! (" {}" , permuted );
56
+ }
57
+ }
58
+ ```
59
+
60
+ ### Random, pair of integers
61
+
62
+ If you have e.g. two vectors of integers and you want to iterate over a random permutation of pairs from these lists
63
+ you can use:
64
+
65
+ ``` rust
66
+ use permutation_iterator :: RandomPairPermutor ;
67
+
68
+ fn main () {
69
+ let xs = [1 , 2 , 3 ];
70
+ let ys = [4 , 5 , 6 , 7 , 8 ];
71
+
72
+ let permutor = RandomPairPermutor :: new (xs . len () as u32 , ys . len () as u32 );
73
+ for (i , j ) in permutor {
74
+ println! (" ({}, {})" , xs [i as usize ], ys [j as usize ]);
75
+ }
76
+ }
77
+ ```
78
+
79
+ ## Implementation details
80
+
81
+
21
82
22
- # License
83
+ ## License
23
84
24
85
` permutation-iterator ` is distributed under the terms of the Apache License (Version 2.0). See [ LICENSE] ( LICENSE ) for
25
86
details.
0 commit comments