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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
use std::iter::Iterator;
use std::mem::MaybeUninit;
#[derive(Clone, Debug)]
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct Combinations<I, const K: usize>
where
I: Iterator,
I::Item: Clone,
{
iter: I,
buffer: Vec<I::Item>,
indices: [usize; K],
first: bool,
}
impl<I, const K: usize> Combinations<I, K>
where
I: Iterator,
I::Item: Clone,
{
pub(crate) fn new(mut iter: I) -> Self {
let mut indices = [0; K];
#[allow(clippy::clippy::needless_range_loop)]
for i in 0..K {
indices[i] = i;
}
Self {
buffer: iter.by_ref().take(K).collect(),
indices,
first: true,
iter,
}
}
}
impl<I, const K: usize> Iterator for Combinations<I, K>
where
I: Iterator,
I::Item: Clone,
{
type Item = [I::Item; K];
fn next(&mut self) -> Option<[I::Item; K]> {
if self.first {
if K > self.buffer.len() {
return None;
}
self.first = false;
} else if K == 0 {
return None;
} else {
if self.indices[0] == self.buffer.len() - K {
match self.iter.next() {
Some(x) => self.buffer.push(x),
None => return None,
}
}
let mut i = 0;
while i < K - 1 && self.indices[i] + 1 == self.indices[i + 1] {
i += 1;
}
self.indices[i] += 1;
for j in 0..i {
self.indices[j] = j;
}
}
let mut out: [MaybeUninit<I::Item>; K] = MaybeUninit::uninit_array();
self.indices.iter().enumerate().for_each(|(oi, i)| {
out[oi] = MaybeUninit::new(self.buffer[*i].clone());
});
Some(unsafe { out.as_ptr().cast::<[I::Item; K]>().read() })
}
}
#[cfg(test)]
mod test {
use crate::IterExt;
#[test]
fn order() {
let mut combinations = (1..6).combinations();
assert_eq!(combinations.next(), Some([1, 2, 3]));
assert_eq!(combinations.next(), Some([1, 2, 4]));
assert_eq!(combinations.next(), Some([1, 3, 4]));
assert_eq!(combinations.next(), Some([2, 3, 4]));
assert_eq!(combinations.next(), Some([1, 2, 5]));
assert_eq!(combinations.next(), Some([1, 3, 5]));
assert_eq!(combinations.next(), Some([2, 3, 5]));
assert_eq!(combinations.next(), Some([1, 4, 5]));
assert_eq!(combinations.next(), Some([2, 4, 5]));
assert_eq!(combinations.next(), Some([3, 4, 5]));
assert_eq!(combinations.next(), None);
}
#[test]
fn none_on_size_too_big() {
let mut combinations = (1..2).combinations::<2>();
assert_eq!(combinations.next(), None);
}
#[test]
fn empty_arr_on_n_zero() {
let mut combinations = (1..5).combinations();
assert_eq!(combinations.next(), Some([]));
assert_eq!(combinations.next(), None);
}
}