|
| 1 | +use core::iter::FusedIterator; |
| 2 | +use core::{array, fmt}; |
| 3 | + |
| 4 | +use crate::combinations::{n_and_count, remaining_for}; |
| 5 | +use crate::lazy_buffer::LazyBuffer; |
| 6 | + |
| 7 | +/// An iterator to iterate through all combinations in an iterator of `Clone`-able items that |
| 8 | +/// produces arrays of a specific size. |
| 9 | +/// |
| 10 | +/// See [`.array_combinations()`](crate::Itertools::array_combinations) for more |
| 11 | +/// information. |
| 12 | +#[derive(Clone)] |
| 13 | +#[must_use = "this iterator adaptor is not lazy but does nearly nothing unless consumed"] |
| 14 | +pub struct ArrayCombinations<I: Iterator, const K: usize> |
| 15 | +where |
| 16 | + I::Item: Clone, |
| 17 | +{ |
| 18 | + indices: [usize; K], |
| 19 | + pool: LazyBuffer<I>, |
| 20 | + first: bool, |
| 21 | +} |
| 22 | + |
| 23 | +/// Create a new `ArrayCombinations` from a clonable iterator. |
| 24 | +pub fn array_combinations<I: Iterator, const K: usize>(iter: I) -> ArrayCombinations<I, K> |
| 25 | +where |
| 26 | + I::Item: Clone, |
| 27 | +{ |
| 28 | + let indices = array::from_fn(|i| i); |
| 29 | + let pool = LazyBuffer::new(iter); |
| 30 | + |
| 31 | + ArrayCombinations { |
| 32 | + indices, |
| 33 | + pool, |
| 34 | + first: true, |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +impl<I: Iterator, const K: usize> ArrayCombinations<I, K> |
| 39 | +where |
| 40 | + I::Item: Clone, |
| 41 | +{ |
| 42 | + /// Returns the (current) length of the pool from which combination elements are |
| 43 | + /// selected. This value can change between invocations of [`next`](Combinations::next). |
| 44 | + #[inline] |
| 45 | + pub fn n(&self) -> usize { |
| 46 | + self.pool.len() |
| 47 | + } |
| 48 | + |
| 49 | + /// Initialises the iterator by filling a buffer with elements from the |
| 50 | + /// iterator. Returns true if there are no combinations, false otherwise. |
| 51 | + fn init(&mut self) -> bool { |
| 52 | + self.pool.prefill(K); |
| 53 | + let done = K > self.n(); |
| 54 | + if !done { |
| 55 | + self.first = false; |
| 56 | + } |
| 57 | + |
| 58 | + done |
| 59 | + } |
| 60 | + |
| 61 | + /// Increments indices representing the combination to advance to the next |
| 62 | + /// (in lexicographic order by increasing sequence) combination. For example |
| 63 | + /// if we have n=4 & k=2 then `[0, 1] -> [0, 2] -> [0, 3] -> [1, 2] -> ...` |
| 64 | + /// |
| 65 | + /// Returns true if we've run out of combinations, false otherwise. |
| 66 | + fn increment_indices(&mut self) -> bool { |
| 67 | + if K == 0 { |
| 68 | + return true; // Done |
| 69 | + } |
| 70 | + |
| 71 | + // Scan from the end, looking for an index to increment |
| 72 | + let mut i: usize = K - 1; |
| 73 | + |
| 74 | + // Check if we need to consume more from the iterator |
| 75 | + if self.indices[i] == self.pool.len() - 1 { |
| 76 | + _ = self.pool.get_next(); // may change pool size |
| 77 | + } |
| 78 | + |
| 79 | + while self.indices[i] == i + self.pool.len() - K { |
| 80 | + if i > 0 { |
| 81 | + i -= 1; |
| 82 | + } else { |
| 83 | + // Reached the last combination |
| 84 | + return true; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // Increment index, and reset the ones to its right |
| 89 | + self.indices[i] += 1; |
| 90 | + for j in i + 1..K { |
| 91 | + self.indices[j] = self.indices[j - 1] + 1; |
| 92 | + } |
| 93 | + |
| 94 | + // If we've made it this far, we haven't run out of combos |
| 95 | + false |
| 96 | + } |
| 97 | + |
| 98 | + /// Returns the n-th item or the number of successful steps. |
| 99 | + pub(crate) fn try_nth(&mut self, n: usize) -> Result<<Self as Iterator>::Item, usize> |
| 100 | + where |
| 101 | + I::Item: Clone, |
| 102 | + { |
| 103 | + let done = if self.first { |
| 104 | + self.init() |
| 105 | + } else { |
| 106 | + self.increment_indices() |
| 107 | + }; |
| 108 | + if done { |
| 109 | + return Err(0); |
| 110 | + } |
| 111 | + for i in 0..n { |
| 112 | + if self.increment_indices() { |
| 113 | + return Err(i + 1); |
| 114 | + } |
| 115 | + } |
| 116 | + Ok(self.pool.get_array(self.indices)) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +impl<I: Iterator, const K: usize> Iterator for ArrayCombinations<I, K> |
| 121 | +where |
| 122 | + I::Item: Clone, |
| 123 | +{ |
| 124 | + type Item = [I::Item; K]; |
| 125 | + |
| 126 | + fn next(&mut self) -> Option<Self::Item> { |
| 127 | + let done = if self.first { |
| 128 | + self.init() |
| 129 | + } else { |
| 130 | + self.increment_indices() |
| 131 | + }; |
| 132 | + |
| 133 | + (!done).then(|| self.pool.get_array(self.indices)) |
| 134 | + } |
| 135 | + |
| 136 | + fn nth(&mut self, n: usize) -> Option<Self::Item> { |
| 137 | + self.try_nth(n).ok() |
| 138 | + } |
| 139 | + |
| 140 | + fn size_hint(&self) -> (usize, Option<usize>) { |
| 141 | + let (mut low, mut upp) = self.pool.size_hint(); |
| 142 | + low = remaining_for(low, self.first, &self.indices).unwrap_or(usize::MAX); |
| 143 | + upp = upp.and_then(|upp| remaining_for(upp, self.first, &self.indices)); |
| 144 | + (low, upp) |
| 145 | + } |
| 146 | + |
| 147 | + #[inline] |
| 148 | + fn count(self) -> usize { |
| 149 | + n_and_count(self.pool, self.first, &self.indices).1 |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +impl<I, const K: usize> fmt::Debug for ArrayCombinations<I, K> |
| 154 | +where |
| 155 | + I: Iterator + fmt::Debug, |
| 156 | + I::Item: Clone + fmt::Debug, |
| 157 | +{ |
| 158 | + debug_fmt_fields!(ArrayCombinations, indices, pool, first); |
| 159 | +} |
| 160 | + |
| 161 | +impl<I, const K: usize> FusedIterator for ArrayCombinations<I, K> |
| 162 | +where |
| 163 | + I: FusedIterator, |
| 164 | + I::Item: Clone, |
| 165 | +{ |
| 166 | +} |
0 commit comments