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
use crate::Counter;
impl<'a, T, N, S> IntoIterator for &'a Counter<T, N, S> {
type Item = (&'a T, &'a N);
type IntoIter = std::collections::hash_map::Iter<'a, T, N>;
fn into_iter(self) -> Self::IntoIter {
self.map.iter()
}
}
impl<T, N, S> IntoIterator for Counter<T, N, S> {
type Item = (T, N);
type IntoIter = std::collections::hash_map::IntoIter<T, N>;
/// Consumes the `Counter` to produce an iterator that owns the values it returns.
///
/// # Examples
/// ```rust
/// # use counter::Counter;
///
/// let counter: Counter<_> = "aaab".chars().collect();
///
/// let vec: Vec<_> = counter.into_iter().collect();
///
/// for (item, count) in &vec {
/// if item == &'a' {
/// assert_eq!(count, &3);
/// }
/// if item == &'b' {
/// assert_eq!(count, &1);
/// }
/// }
/// ```
fn into_iter(self) -> Self::IntoIter {
self.map.into_iter()
}
}
impl<'a, T, N, S> IntoIterator for &'a mut Counter<T, N, S> {
type Item = (&'a T, &'a mut N);
type IntoIter = std::collections::hash_map::IterMut<'a, T, N>;
/// Creates an iterator that provides mutable references to the counts, but keeps the keys immutable.
///
/// # Examples
/// ```rust
/// # use counter::Counter;
///
/// let mut counter: Counter<_> = "aaab".chars().collect();
///
/// for (item, count) in &mut counter {
/// if *item == 'a' {
/// // 'a' is so great it counts as 2
/// *count *= 2;
/// }
/// }
///
/// assert_eq!(counter[&'a'], 6);
/// assert_eq!(counter[&'b'], 1);
/// ```
fn into_iter(self) -> Self::IntoIter {
self.map.iter_mut()
}
}