-
Notifications
You must be signed in to change notification settings - Fork 296
/
duplicate_impl.rs
160 lines (146 loc) · 4.15 KB
/
duplicate_impl.rs
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
use std::collections::HashMap;
use std::hash::Hash;
use std::fmt;
/// An iterator adapter to filter out duplicate elements.
///
/// See [`.duplicate_by()`](../trait.Itertools.html#method.duplicate) for more information.
#[derive(Clone)]
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct DuplicateBy<I: Iterator, V, F> {
iter: I,
// Use a hashmap for the entry API
used: HashMap<V, bool>,
f: F,
}
impl<I, V, F> fmt::Debug for DuplicateBy<I, V, F>
where I: Iterator + fmt::Debug,
V: fmt::Debug + Hash + Eq,
{
debug_fmt_fields!(DuplicateBy, iter, used);
}
/// Create a new `DuplicateBy` iterator.
pub fn duplicate_by<I, V, F>(iter: I, f: F) -> DuplicateBy<I, V, F>
where V: Eq + Hash,
F: FnMut(&I::Item) -> V,
I: Iterator,
{
DuplicateBy {
iter,
used: HashMap::new(),
f,
}
}
impl<I, V, F> Iterator for DuplicateBy<I, V, F>
where I: Iterator,
V: Eq + Hash,
F: FnMut(&I::Item) -> V
{
type Item = I::Item;
fn next(&mut self) -> Option<Self::Item> {
while let Some(v) = self.iter.next() {
let key = (self.f)(&v);
match self.used.get_mut(&key) {
None => { self.used.insert(key, false); },
Some(true) => (),
Some(produced) => {
*produced = true;
return Some(v);
},
}
}
None
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let (low, hi) = self.iter.size_hint();
((low > 0 && self.used.is_empty()) as usize, hi)
}
}
impl<I, V, F> DoubleEndedIterator for DuplicateBy<I, V, F>
where I: DoubleEndedIterator,
V: Eq + Hash,
F: FnMut(&I::Item) -> V
{
fn next_back(&mut self) -> Option<Self::Item> {
while let Some(v) = self.iter.next_back() {
let key = (self.f)(&v);
match self.used.get_mut(&key) {
None => { self.used.insert(key, false); },
Some(true) => (),
Some(produced) => {
*produced = true;
return Some(v);
},
}
}
None
}
}
impl<I> Iterator for Duplicate<I>
where I: Iterator,
I::Item: Eq + Hash
{
type Item = I::Item;
fn next(&mut self) -> Option<Self::Item> {
while let Some(v) = self.iter.iter.next() {
match self.iter.used.get_mut(&v) {
None => { self.iter.used.insert(v, false); },
Some(true) => (),
Some(produced) => {
*produced = true;
return Some(v);
},
}
}
None
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let (low, hi) = self.iter.iter.size_hint();
((low > 0 && self.iter.used.is_empty()) as usize, hi)
}
}
impl<I> DoubleEndedIterator for Duplicate<I>
where I: DoubleEndedIterator,
I::Item: Eq + Hash
{
fn next_back(&mut self) -> Option<Self::Item> {
while let Some(v) = self.iter.iter.next_back() {
match self.iter.used.get_mut(&v) {
None => { self.iter.used.insert(v, false); },
Some(true) => (),
Some(produced) => {
*produced = true;
return Some(v);
},
}
}
None
}
}
/// An iterator adapter to filter out duplicate elements.
///
/// See [`.duplicate()`](../trait.Itertools.html#method.duplicate) for more information.
#[derive(Clone)]
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct Duplicate<I: Iterator> {
iter: DuplicateBy<I, I::Item, ()>,
}
impl<I> fmt::Debug for Duplicate<I>
where I: Iterator + fmt::Debug,
I::Item: Hash + Eq + fmt::Debug,
{
debug_fmt_fields!(Duplicate, iter);
}
pub fn duplicate<I>(iter: I) -> Duplicate<I>
where I: Iterator,
I::Item: Eq + Hash,
{
Duplicate {
iter: DuplicateBy {
iter,
used: HashMap::new(),
f: (),
}
}
}