|
| 1 | +use std::cmp::{max, min}; |
| 2 | +use std::collections::HashMap; |
| 3 | +use std::hash::Hash; |
| 4 | +use std::fmt; |
| 5 | + |
| 6 | +/// An iterator adapter to filter out duplicate elements. |
| 7 | +/// |
| 8 | +/// See [`.duplicate_by()`](../trait.Itertools.html#method.duplicate) for more information. |
| 9 | +#[derive(Clone)] |
| 10 | +#[must_use = "iterator adaptors are lazy and do nothing unless consumed"] |
| 11 | +pub struct DuplicateBy<I: Iterator, V, F> { |
| 12 | + iter: I, |
| 13 | + used: HashMap<V, bool>, |
| 14 | + pending: usize, |
| 15 | + f: F, |
| 16 | +} |
| 17 | + |
| 18 | +impl<I, V, F> fmt::Debug for DuplicateBy<I, V, F> |
| 19 | + where I: Iterator + fmt::Debug, |
| 20 | + V: fmt::Debug + Hash + Eq, |
| 21 | +{ |
| 22 | + debug_fmt_fields!(DuplicateBy, iter, used); |
| 23 | +} |
| 24 | + |
| 25 | +/// Create a new `DuplicateBy` iterator. |
| 26 | +pub fn duplicate_by<I, V, F>(iter: I, f: F) -> DuplicateBy<I, V, F> |
| 27 | + where V: Eq + Hash, |
| 28 | + F: FnMut(&I::Item) -> V, |
| 29 | + I: Iterator, |
| 30 | +{ |
| 31 | + DuplicateBy { |
| 32 | + iter, |
| 33 | + used: HashMap::new(), |
| 34 | + pending: 0, |
| 35 | + f, |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +impl<I, V, F> Iterator for DuplicateBy<I, V, F> |
| 40 | + where I: Iterator, |
| 41 | + V: Eq + Hash, |
| 42 | + F: FnMut(&I::Item) -> V |
| 43 | +{ |
| 44 | + type Item = I::Item; |
| 45 | + |
| 46 | + fn next(&mut self) -> Option<Self::Item> { |
| 47 | + while let Some(v) = self.iter.next() { |
| 48 | + let key = (self.f)(&v); |
| 49 | + match self.used.get_mut(&key) { |
| 50 | + None => { |
| 51 | + self.used.insert(key, false); |
| 52 | + self.pending += 1; |
| 53 | + }, |
| 54 | + Some(true) => (), |
| 55 | + Some(produced) => { |
| 56 | + *produced = true; |
| 57 | + self.pending -= 1; |
| 58 | + return Some(v); |
| 59 | + }, |
| 60 | + } |
| 61 | + } |
| 62 | + None |
| 63 | + } |
| 64 | + |
| 65 | + #[inline] |
| 66 | + fn size_hint(&self) -> (usize, Option<usize>) { |
| 67 | + let (_, hi) = self.iter.size_hint(); |
| 68 | + // There are `hi` number of items left in the base iterator. In the best case scenario, |
| 69 | + // these items are exactly the same as the ones pending (i.e items seen exactly once so |
| 70 | + // far), plus (hi - pending) / 2 pairs of never seen before items. |
| 71 | + let hi = hi.map(|hi| { |
| 72 | + let max_pending = min(self.pending, hi); |
| 73 | + let max_new = max(hi - self.pending, 0) / 2; |
| 74 | + max_pending + max_new |
| 75 | + }); |
| 76 | + // The lower bound is always 0 since we might only get unique items from now on |
| 77 | + (0, hi) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +impl<I, V, F> DoubleEndedIterator for DuplicateBy<I, V, F> |
| 82 | + where I: DoubleEndedIterator, |
| 83 | + V: Eq + Hash, |
| 84 | + F: FnMut(&I::Item) -> V |
| 85 | +{ |
| 86 | + fn next_back(&mut self) -> Option<Self::Item> { |
| 87 | + while let Some(v) = self.iter.next_back() { |
| 88 | + let key = (self.f)(&v); |
| 89 | + match self.used.get_mut(&key) { |
| 90 | + None => { |
| 91 | + self.used.insert(key, false); |
| 92 | + self.pending += 1; |
| 93 | + }, |
| 94 | + Some(true) => (), |
| 95 | + Some(produced) => { |
| 96 | + *produced = true; |
| 97 | + self.pending -= 1; |
| 98 | + return Some(v); |
| 99 | + }, |
| 100 | + } |
| 101 | + } |
| 102 | + None |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +impl<I> Iterator for Duplicate<I> |
| 107 | + where I: Iterator, |
| 108 | + I::Item: Eq + Hash |
| 109 | +{ |
| 110 | + type Item = I::Item; |
| 111 | + |
| 112 | + fn next(&mut self) -> Option<Self::Item> { |
| 113 | + while let Some(v) = self.iter.iter.next() { |
| 114 | + match self.iter.used.get_mut(&v) { |
| 115 | + None => { |
| 116 | + self.iter.used.insert(v, false); |
| 117 | + self.iter.pending += 1; |
| 118 | + }, |
| 119 | + Some(true) => (), |
| 120 | + Some(produced) => { |
| 121 | + *produced = true; |
| 122 | + self.iter.pending -= 1; |
| 123 | + return Some(v); |
| 124 | + }, |
| 125 | + } |
| 126 | + } |
| 127 | + None |
| 128 | + } |
| 129 | + |
| 130 | + #[inline] |
| 131 | + fn size_hint(&self) -> (usize, Option<usize>) { |
| 132 | + let (_, hi) = self.iter.iter.size_hint(); |
| 133 | + // There are `hi` number of items left in the base iterator. In the best case scenario, |
| 134 | + // these items are exactly the same as the ones pending (i.e items seen exactly once so |
| 135 | + // far), plus (hi - pending) / 2 pairs of never seen before items. |
| 136 | + let hi = hi.map(|hi| { |
| 137 | + let max_pending = min(self.iter.pending, hi); |
| 138 | + let max_new = max(hi - self.iter.pending, 0) / 2; |
| 139 | + max_pending + max_new |
| 140 | + }); |
| 141 | + // The lower bound is always 0 since we might only get unique items from now on |
| 142 | + (0, hi) |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +impl<I> DoubleEndedIterator for Duplicate<I> |
| 147 | + where I: DoubleEndedIterator, |
| 148 | + I::Item: Eq + Hash |
| 149 | +{ |
| 150 | + fn next_back(&mut self) -> Option<Self::Item> { |
| 151 | + while let Some(v) = self.iter.iter.next_back() { |
| 152 | + match self.iter.used.get_mut(&v) { |
| 153 | + None => { |
| 154 | + self.iter.used.insert(v, false); |
| 155 | + self.iter.pending += 1; |
| 156 | + }, |
| 157 | + Some(true) => (), |
| 158 | + Some(produced) => { |
| 159 | + *produced = true; |
| 160 | + self.iter.pending -= 1; |
| 161 | + return Some(v); |
| 162 | + }, |
| 163 | + } |
| 164 | + } |
| 165 | + None |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +/// An iterator adapter to filter out duplicate elements. |
| 170 | +/// |
| 171 | +/// See [`.duplicate()`](../trait.Itertools.html#method.duplicate) for more information. |
| 172 | +#[derive(Clone)] |
| 173 | +#[must_use = "iterator adaptors are lazy and do nothing unless consumed"] |
| 174 | +pub struct Duplicate<I: Iterator> { |
| 175 | + iter: DuplicateBy<I, I::Item, ()>, |
| 176 | +} |
| 177 | + |
| 178 | +impl<I> fmt::Debug for Duplicate<I> |
| 179 | + where I: Iterator + fmt::Debug, |
| 180 | + I::Item: Hash + Eq + fmt::Debug, |
| 181 | +{ |
| 182 | + debug_fmt_fields!(Duplicate, iter); |
| 183 | +} |
| 184 | + |
| 185 | +pub fn duplicate<I>(iter: I) -> Duplicate<I> |
| 186 | + where I: Iterator, |
| 187 | + I::Item: Eq + Hash, |
| 188 | +{ |
| 189 | + Duplicate { |
| 190 | + iter: DuplicateBy { |
| 191 | + iter, |
| 192 | + used: HashMap::new(), |
| 193 | + pending: 0, |
| 194 | + f: (), |
| 195 | + } |
| 196 | + } |
| 197 | +} |
0 commit comments