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
use crate::Counter;
use num_traits::Zero;
use std::hash::{BuildHasher, Hash};
use std::ops::{Sub, SubAssign};
impl<T, N, S> Sub for Counter<T, N, S>
where
T: Hash + Eq,
N: PartialOrd + PartialEq + SubAssign + Zero,
S: BuildHasher,
{
type Output = Counter<T, N, S>;
/// Subtract (keeping only positive values).
///
/// `out = c - d;` -> `out[x] == c[x] - d[x]` for all `x`,
/// keeping only items with a value greater than [`N::zero()`].
///
/// [`N::zero()`]:
/// https://docs.rs/num-traits/latest/num_traits/identities/trait.Zero.html#tymethod.zero
///
/// ```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', 2)].iter().cloned().collect::<HashMap<_, _>>();
/// assert_eq!(e.into_map(), expect);
/// ```
fn sub(mut self, rhs: Counter<T, N, S>) -> Self::Output {
self -= rhs;
self
}
}
impl<T, N, S> SubAssign for Counter<T, N, S>
where
T: Hash + Eq,
N: PartialOrd + PartialEq + SubAssign + Zero,
S: BuildHasher,
{
/// Subtract (keeping only positive values).
///
/// `c -= d;` -> `c[x] -= d[x]` for all `x`,
/// keeping only items with a value greater than [`N::zero()`].
///
/// [`N::zero()`]:
/// https://docs.rs/num-traits/latest/num_traits/identities/trait.Zero.html#tymethod.zero
///
/// ```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', 2)].iter().cloned().collect::<HashMap<_, _>>();
/// assert_eq!(c.into_map(), expect);
/// ```
fn sub_assign(&mut self, rhs: Self) {
for (key, value) in rhs.map {
let mut remove = false;
if let Some(entry) = self.map.get_mut(&key) {
if *entry >= value {
*entry -= value;
} else {
remove = true;
}
if *entry == N::zero() {
remove = true;
}
}
if remove {
self.map.remove(&key);
}
}
}
}