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
use crate::Counter;
use num_traits::Zero;
use std::hash::{BuildHasher, Hash};
use std::ops::{BitAnd, BitAndAssign};
impl<T, N, S> BitAnd for Counter<T, N, S>
where
T: Hash + Eq,
N: Ord + Zero,
S: BuildHasher + Default,
{
type Output = Counter<T, N, S>;
/// Returns the intersection of `self` and `rhs` as a new `Counter`.
///
/// `out = c & d;` -> `out[x] == min(c[x], d[x])`
///
/// ```rust
/// # use counter::Counter;
/// # use std::collections::HashMap;
/// let c = "aaab".chars().collect::<Counter<_>>();
/// let d = "abb".chars().collect::<Counter<_>>();
///
/// let e = c & d;
///
/// let expect = [('a', 1), ('b', 1)].iter().cloned().collect::<HashMap<_, _>>();
/// assert_eq!(e.into_map(), expect);
/// ```
fn bitand(self, mut rhs: Counter<T, N, S>) -> Self::Output {
use std::cmp::min;
let mut counter = Counter::new();
for (key, lhs_count) in self.map {
if let Some(rhs_count) = rhs.remove(&key) {
let count = min(lhs_count, rhs_count);
counter.map.insert(key, count);
}
}
counter
}
}
impl<T, N, S> BitAndAssign for Counter<T, N, S>
where
T: Hash + Eq,
N: Ord + Zero,
S: BuildHasher,
{
/// Updates `self` with the intersection of `self` and `rhs`
///
/// `c &= d;` -> `c[x] == min(c[x], d[x])`
///
/// ```rust
/// # use counter::Counter;
/// # use std::collections::HashMap;
/// let mut c = "aaab".chars().collect::<Counter<_>>();
/// let d = "abb".chars().collect::<Counter<_>>();
///
/// c &= d;
///
/// let expect = [('a', 1), ('b', 1)].iter().cloned().collect::<HashMap<_, _>>();
/// assert_eq!(c.into_map(), expect);
/// ```
fn bitand_assign(&mut self, mut rhs: Counter<T, N, S>) {
for (key, rhs_count) in rhs.drain() {
if rhs_count < self[&key] {
self.map.insert(key, rhs_count);
}
}
}
}