Skip to content

Commit 3f53df1

Browse files
committed
README - more details
1 parent 76666e0 commit 3f53df1

File tree

1 file changed

+62
-1
lines changed

1 file changed

+62
-1
lines changed

README.md

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
[![Build Status](https://travis-ci.com/asimihsan/permutation-iterator-rs.svg?branch=master)](https://travis-ci.com/asimihsan/permutation-iterator-rs)
44
[![Crate](https://img.shields.io/crates/v/permutation_iterator.svg)](https://crates.io/crates/permutation_iterator)
55
[![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+
68

79
A Rust library for iterating over random permutations without fully materializing them into memory.
810

@@ -18,8 +20,67 @@ Add this to your `Cargo.toml`:
1820
permutation_iterator = "0.1.0"
1921
```
2022

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+
2182

22-
# License
83+
## License
2384

2485
`permutation-iterator` is distributed under the terms of the Apache License (Version 2.0). See [LICENSE](LICENSE) for
2586
details.

0 commit comments

Comments
 (0)