mmstick / permutate Goto Github PK
View Code? Open in Web Editor NEWGeneric permutator written in Rust that permutates both lists of lists and singular lists using references.
License: MIT License
Generic permutator written in Rust that permutates both lists of lists and singular lists using references.
License: MIT License
Hello mmstick, thanks for this crate!
I started to adapt it for the tuples usage and it is "usable" for now. But still 100% behind the remaining features and documentation, let alone the binary and benchmarking.
In any case, I'll be happy to point you for this feature. I intend to PR whenever it's eventually ready.
Best regards,
I'm using Permutator like this:
let list: &[&[&i32]] = &[&[&0, &1, &2]];
let permutator = Permutator::new(&list);
for permutation in permutator {
println!("permutation: {:?}", permutation);
}
Result is:
permutation: [0, 0, 0]
permutation: [0, 0, 1]
permutation: [0, 0, 2]
permutation: [0, 1, 0]
permutation: [0, 1, 1]
permutation: [0, 1, 2]
permutation: [0, 2, 0]
permutation: [0, 2, 1]
permutation: [0, 2, 2]
permutation: [1, 0, 0]
permutation: [1, 0, 1]
permutation: [1, 0, 2]
permutation: [1, 1, 0]
permutation: [1, 1, 1]
permutation: [1, 1, 2]
permutation: [1, 2, 0]
permutation: [1, 2, 1]
permutation: [1, 2, 2]
permutation: [2, 0, 0]
permutation: [2, 0, 1]
permutation: [2, 0, 2]
permutation: [2, 1, 0]
permutation: [2, 1, 1]
permutation: [2, 1, 2]
permutation: [2, 2, 0]
permutation: [2, 2, 1]
permutation: [2, 2, 2]
Numbers should not be repeated, that's combinations instead of permutations. Is it possible to just obtain an iterator where elements are just permutated between one another?
Expected output is:
permutation: [0, 1, 2]
permutation: [0, 2, 1]
permutation: [1, 0, 2]
permutation: [1, 2, 0]
permutation: [2, 0, 1]
permutation: [2, 1, 0]
The current Iterator::next
impl returns a new Vec
for every iteration.
It would be nice if there was also an API like fn next(output: &mut [T]) -> bool
that populated the given output buffer with the values of the next iteration, and returned false at the end of iteration.
The closest analogy to this in the stdlib is std::io::Read::read
, although that's only for u8
buffers.
I was referred to your crate for handling permutations but have been told if I want the 36th permutation for example, the iterator would likely calculate every permutation up until that point? Would it be possible to support avoiding that?
Use case is for permutating long strings with various rules, if I calculate the first 10 trillion permutations and terminate the program, as long as I had the current iteration number it should be possible to provide that to resume progress on my permutations when running the program again and providing the iteration number? It would be nice not to do all that processing if it can be avoided :)
Example of how I'd assume it might be approached. A string with the length of 3 where the first two elements are "a" ... "z"
and the third 0 ... 9
. I'd have aa0
, aa1
and so forth, if I wanted the 36th permutation it'd would probably do something like:
// Gets the individual index per permutation char representing a string
fn permutation_index(element_sizes:Vec<u32>, index:u32) -> Vec<usize> {
let mut r = element_sizes.iter().rev().scan(index, |x, y|
if *x == 0 { None } else {
let (div, rem) = (*x / y, *x % y);
*x = div;
Some(rem as usize)
}
).collect::<Vec<usize>>();
// flip the order back to the original direction
r.reverse();
r
}
fn main() {
// [a...z, a...z, 0...9], [0, 0, 0] == "aa0", [0, 0, 1] == "aa1"
let element_sizes = vec![26, 26, 10];
let nth_permutation = 36;
let index = permutation_index(element_sizes, nth_permutation);
// [3, 6] "ad6"
println!("done {:?}", index);
}
Some users on IRC suggested:
This crate could benefit from specializing
Iterator::nth()
.
You could add a #[derive(serialize, deserialze)] on your Iterator, save it and open it whenever you need
Technically for my case, it might make more sense to just provide the iteration value such as "ad6" or it's index [3, 6] or [0, 3, 6] depending on what works better for your crate.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.