itertools/
combinations.rs1use core::array;
2use core::borrow::BorrowMut;
3use std::fmt;
4use std::iter::FusedIterator;
5
6use super::lazy_buffer::LazyBuffer;
7use alloc::vec::Vec;
8
9use crate::adaptors::checked_binomial;
10
11pub type Combinations<I> = CombinationsGeneric<I, Vec<usize>>;
13pub type ArrayCombinations<I, const K: usize> = CombinationsGeneric<I, [usize; K]>;
15
16pub fn combinations<I: Iterator>(iter: I, k: usize) -> Combinations<I>
18where
19 I::Item: Clone,
20{
21 Combinations::new(iter, (0..k).collect())
22}
23
24pub fn array_combinations<I: Iterator, const K: usize>(iter: I) -> ArrayCombinations<I, K>
26where
27 I::Item: Clone,
28{
29 ArrayCombinations::new(iter, array::from_fn(|i| i))
30}
31
32#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
36pub struct CombinationsGeneric<I: Iterator, Idx> {
37 indices: Idx,
38 pool: LazyBuffer<I>,
39 first: bool,
40}
41
42pub trait PoolIndex<T>: BorrowMut<[usize]> {
45 type Item;
46
47 fn extract_item<I: Iterator<Item = T>>(&self, pool: &LazyBuffer<I>) -> Self::Item
48 where
49 T: Clone;
50
51 fn len(&self) -> usize {
52 self.borrow().len()
53 }
54}
55
56impl<T> PoolIndex<T> for Vec<usize> {
57 type Item = Vec<T>;
58
59 fn extract_item<I: Iterator<Item = T>>(&self, pool: &LazyBuffer<I>) -> Vec<T>
60 where
61 T: Clone,
62 {
63 pool.get_at(self)
64 }
65}
66
67impl<T, const K: usize> PoolIndex<T> for [usize; K] {
68 type Item = [T; K];
69
70 fn extract_item<I: Iterator<Item = T>>(&self, pool: &LazyBuffer<I>) -> [T; K]
71 where
72 T: Clone,
73 {
74 pool.get_array(*self)
75 }
76}
77
78impl<I, Idx> Clone for CombinationsGeneric<I, Idx>
79where
80 I: Iterator + Clone,
81 I::Item: Clone,
82 Idx: Clone,
83{
84 clone_fields!(indices, pool, first);
85}
86
87impl<I, Idx> fmt::Debug for CombinationsGeneric<I, Idx>
88where
89 I: Iterator + fmt::Debug,
90 I::Item: fmt::Debug,
91 Idx: fmt::Debug,
92{
93 debug_fmt_fields!(Combinations, indices, pool, first);
94}
95
96impl<I: Iterator, Idx: PoolIndex<I::Item>> CombinationsGeneric<I, Idx> {
97 fn new(iter: I, indices: Idx) -> Self {
99 Self {
100 indices,
101 pool: LazyBuffer::new(iter),
102 first: true,
103 }
104 }
105
106 #[inline]
108 pub fn k(&self) -> usize {
109 self.indices.len()
110 }
111
112 #[inline]
115 pub fn n(&self) -> usize {
116 self.pool.len()
117 }
118
119 #[inline]
121 pub(crate) fn src(&self) -> &LazyBuffer<I> {
122 &self.pool
123 }
124
125 pub(crate) fn n_and_count(self) -> (usize, usize) {
127 let Self {
128 indices,
129 pool,
130 first,
131 } = self;
132 let n = pool.count();
133 (n, remaining_for(n, first, indices.borrow()).unwrap())
134 }
135
136 fn init(&mut self) -> bool {
139 self.pool.prefill(self.k());
140 let done = self.k() > self.n();
141 if !done {
142 self.first = false;
143 }
144
145 done
146 }
147
148 fn increment_indices(&mut self) -> bool {
154 let indices = self.indices.borrow_mut();
156
157 if indices.is_empty() {
158 return true; }
160 let mut i: usize = indices.len() - 1;
162
163 if indices[i] == self.pool.len() - 1 {
165 self.pool.get_next(); }
167
168 while indices[i] == i + self.pool.len() - indices.len() {
169 if i > 0 {
170 i -= 1;
171 } else {
172 return true;
174 }
175 }
176
177 indices[i] += 1;
179 for j in i + 1..indices.len() {
180 indices[j] = indices[j - 1] + 1;
181 }
182 false
184 }
185
186 pub(crate) fn try_nth(&mut self, n: usize) -> Result<<Self as Iterator>::Item, usize>
188 where
189 I: Iterator,
190 I::Item: Clone,
191 {
192 let done = if self.first {
193 self.init()
194 } else {
195 self.increment_indices()
196 };
197 if done {
198 return Err(0);
199 }
200 for i in 0..n {
201 if self.increment_indices() {
202 return Err(i + 1);
203 }
204 }
205 Ok(self.indices.extract_item(&self.pool))
206 }
207}
208
209impl<I, Idx> Iterator for CombinationsGeneric<I, Idx>
210where
211 I: Iterator,
212 I::Item: Clone,
213 Idx: PoolIndex<I::Item>,
214{
215 type Item = Idx::Item;
216 fn next(&mut self) -> Option<Self::Item> {
217 let done = if self.first {
218 self.init()
219 } else {
220 self.increment_indices()
221 };
222
223 if done {
224 return None;
225 }
226
227 Some(self.indices.extract_item(&self.pool))
228 }
229
230 fn nth(&mut self, n: usize) -> Option<Self::Item> {
231 self.try_nth(n).ok()
232 }
233
234 fn size_hint(&self) -> (usize, Option<usize>) {
235 let (mut low, mut upp) = self.pool.size_hint();
236 low = remaining_for(low, self.first, self.indices.borrow()).unwrap_or(usize::MAX);
237 upp = upp.and_then(|upp| remaining_for(upp, self.first, self.indices.borrow()));
238 (low, upp)
239 }
240
241 #[inline]
242 fn count(self) -> usize {
243 self.n_and_count().1
244 }
245}
246
247impl<I, Idx> FusedIterator for CombinationsGeneric<I, Idx>
248where
249 I: Iterator,
250 I::Item: Clone,
251 Idx: PoolIndex<I::Item>,
252{
253}
254
255impl<I: Iterator> Combinations<I> {
256 pub(crate) fn reset(&mut self, k: usize) {
261 self.first = true;
262
263 if k < self.indices.len() {
264 self.indices.truncate(k);
265 for i in 0..k {
266 self.indices[i] = i;
267 }
268 } else {
269 for i in 0..self.indices.len() {
270 self.indices[i] = i;
271 }
272 self.indices.extend(self.indices.len()..k);
273 self.pool.prefill(k);
274 }
275 }
276}
277
278fn remaining_for(n: usize, first: bool, indices: &[usize]) -> Option<usize> {
280 let k = indices.len();
281 if n < k {
282 Some(0)
283 } else if first {
284 checked_binomial(n, k)
285 } else {
286 indices.iter().enumerate().try_fold(0usize, |sum, (i, n0)| {
305 sum.checked_add(checked_binomial(n - 1 - *n0, k - i)?)
306 })
307 }
308}