partition_iterator/
lib.rs

1mod 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        // Make sure the number of elements is equal to the
15        // Stirling number of the second kind
16
17        let mut stirling = Vec::new();
18        stirling.push(vec![]);  // n = 0
19        stirling.push(vec![1]); // n = 1
20        stirling.push(vec![1,1]); // n = 2
21        stirling.push(vec![1,3,1]); // n = 3
22        stirling.push(vec![1,7,6,1]); // n = 4
23        stirling.push(vec![1,15,25,10,1]); // n = 5
24        stirling.push(vec![1,31,90,65,15,1]); // n = 6
25        stirling.push(vec![1,63,301,350,140,21,1]); // n = 7
26
27        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![]);  // n = 0
42        stirling.push(vec![1]); // n = 1
43        stirling.push(vec![1,1]); // n = 2
44        stirling.push(vec![1,3,1]); // n = 3
45        stirling.push(vec![1,7,6,1]); // n = 4
46        stirling.push(vec![1,15,25,10,1]); // n = 5
47        stirling.push(vec![1,31,90,65,15,1]); // n = 6
48        stirling.push(vec![1,63,301,350,140,21,1]); // n = 7
49
50        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}