partition_iterator/
lib.rs1mod seqit;
2mod partit;
3
4pub use crate::partit::PartitionIter;
5
6
7#[cfg(test)]
8mod test {
9 use super::seqit;
10 use super::PartitionIter;
11
12 #[test]
13 fn seq_count() {
14 let mut stirling = Vec::new();
18 stirling.push(vec![]); stirling.push(vec![1]); stirling.push(vec![1,1]); stirling.push(vec![1,3,1]); stirling.push(vec![1,7,6,1]); stirling.push(vec![1,15,25,10,1]); stirling.push(vec![1,31,90,65,15,1]); stirling.push(vec![1,63,301,350,140,21,1]); for n in 1..=7 {
28 for (k, res) in std::iter::zip(1..=n, &stirling[n]) {
29 let c = seqit::IncSeqIterator::new(n as u32, k as u32).count();
30 assert_eq!(c, *res);
31 }
32 }
33
34 let c = seqit::IncSeqIterator::new(10,5).count();
35 assert_eq!(c, 42525);
36 }
37
38 #[test]
39 fn kpartition_test() {
40 let mut stirling = Vec::new();
41 stirling.push(vec![]); stirling.push(vec![1]); stirling.push(vec![1,1]); stirling.push(vec![1,3,1]); stirling.push(vec![1,7,6,1]); stirling.push(vec![1,15,25,10,1]); stirling.push(vec![1,31,90,65,15,1]); stirling.push(vec![1,63,301,350,140,21,1]); for n in 1..=7 {
51 let ls:Vec<_> = (1..=n).collect();
52 for (k, res) in std::iter::zip(1..=n, &stirling[n]) {
53 let c = ls.iter().kpartitions(k as u32).count();
54 assert_eq!(c, *res);
55 }
56 }
57 }
58
59 #[test]
60 fn partition_test() {
61 let bell = vec![1, 1, 2, 5, 15, 52, 203, 877, 4140];
62 for (n, res) in bell.iter().enumerate() {
63 let ls:Vec<_> = (1..=n).collect();
64 let c = ls.iter().partitions().count();
65 assert_eq!(c, *res, "n={} c={} res={}", n, c, *res);
66 }
67 }
68}