1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
use crate::set::ordered_integer_set::{OrderedIntegerSet, ContiguousIntegerSet}; use std::ops::Index; pub type Partition = OrderedIntegerSet<usize>; #[derive(Clone, PartialEq, Debug)] pub struct IntegerPartitions { partitions: Vec<Partition> } impl IntegerPartitions { pub fn new(partitions: Vec<Partition>) -> IntegerPartitions { IntegerPartitions { partitions } } pub fn num_partitions(&self) -> usize { self.partitions.len() } pub fn iter(&self) -> IntegerPartitionIter { IntegerPartitionIter { iter: self.partitions.iter() } } pub fn union(&self) -> Partition { let intervals: Vec<ContiguousIntegerSet<usize>> = self.partitions.iter().flat_map(|p| p.get_intervals_by_ref().clone()).collect(); OrderedIntegerSet::from_contiguous_integer_sets(intervals) } } impl Index<usize> for IntegerPartitions { type Output = Partition; fn index(&self, index: usize) -> &Self::Output { &self.partitions[index] } } pub struct IntegerPartitionIter<'a> { iter: std::slice::Iter<'a, Partition>, } impl<'a> Iterator for IntegerPartitionIter<'a> { type Item = &'a Partition; fn next(&mut self) -> Option<Self::Item> { self.iter.next() } }