Crate refinery [−] [src]
This crate implements a simple and fast algorithm for building and refining a partition of the set
[0usize, n)
. The most important type is Partition
and its most important function is refine
,
which refines the Partition
according to some subset. There is also the function
refine_with_callback
that does the same thing, but also lets you peek into what it's doing: you
provide it with a callback function which will be invoked every time a set of the partition is
split in two.
Example
use refinery::Partition; // Start with the single-element partition [0, 1, 2, 3, 4, 5, 6]. let mut part = Partition::simple(7); // Refine it, bit by bit... part.refine(&[0, 1, 2, 3]); let result: &[&[usize]] = &[&[0, 1, 2, 3], &[4, 5, 6]]; assert_eq!(&part.iter().collect::<Vec<_>>()[..], result); part.refine(&[3, 4, 5]); // Note that the order of the sets is not defined (although each individual set is guaranteed to be // sorted), so this assert is not really kosher. let result: &[&[usize]] = &[&[3], &[4, 5], &[0, 1, 2], &[6]]; assert_eq!(&part.iter().collect::<Vec<_>>()[..], result); // Refine it again, and this time print out some information each time a set gets split. let alert = |original: &[usize], parts: (&[usize], &[usize])| { println!("splitting {:?} into {:?} and {:?}", original, parts.0, parts.1); }; // This should print: // splitting [0, 1, 2] into [2] and [0, 1] // splitting [4, 5] into [4] and [5] // Note that it doesn't say anything about splitting [3] into [3] because that isn't interesting. part.refine_with_callback(&[2, 3, 4], alert);
Structs
Partition |
A partition of a set of |
PartitionIter |
An iterator over the sets in a |