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]);
{
    let mut result: Vec<_> = part.iter().collect();
    result.sort(); // The order of sets isn't defined, so we need to sort it before comparing.
    let expected: &[&[usize]] = &[&[1, 2, 0], &[3], &[4, 5], &[6]];
    assert_eq!(&result[..], expected);
}

// Refine it again, and this time print out some information each time a set gets split.
let alert = |p: &Partition, intersection: usize, difference: usize| {
    println!("splitting {:?} from {:?}", p.part(intersection), p.part(difference));
};
// This should print:
//     splitting [2] from [0, 1]
//     splitting [4] from [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 usizes. See the crate documentation for more information.

PartitionIter

An iterator over the sets in a Partition.