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
use crate::Counter;
use num_traits::Zero;
use std::hash::{BuildHasher, Hash};
use std::ops::{BitOr, BitOrAssign};
impl<T, N, S> BitOr for Counter<T, N, S>
where
T: Hash + Eq,
N: Ord + Zero,
S: BuildHasher,
{
type Output = Counter<T, N, S>;
/// Returns the union of `self` and `rhs` as a new `Counter`.
///
/// `out = c | d;` -> `out[x] == max(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', 3), ('b', 2)].iter().cloned().collect::<HashMap<_, _>>();
/// assert_eq!(e.into_map(), expect);
/// ```
fn bitor(mut self, rhs: Counter<T, N, S>) -> Self::Output {
for (key, rhs_value) in rhs.map {
let entry = self.map.entry(key).or_insert_with(N::zero);
// We want to update the value of the now occupied entry in `self` with the maximum of
// its current value and `rhs_value`. If that max is `rhs_value`, we can just update
// the value of the entry. If the max is the current value, we do nothing. Note that
// `Ord::max()` returns the second argument (here `rhs_value`) if its two arguments are
// equal, justifying the use of the weak inequality below instead of a strict
// inequality.
//
// Doing it this way with an inequality instead of actually using `std::cmp::max()`
// lets us avoid trying (and failing) to move the non-copy value out of the entry in
// order to pass it as an argument to `std::cmp::max()`, while still holding a mutable
// reference to the value slot in the entry.
//
// And while using the inequality seemingly only requires the bound `N: PartialOrd`, we
// nevertheless prefer to require `Ord` as though we were using `std::cmp::max()`
// because the semantics of `BitOr` for `Counter` really do not make sense if there are
// possibly non-comparable values of type `N`.
if rhs_value >= *entry {
*entry = rhs_value;
}
}
self
}
}
impl<T, N, S> BitOrAssign for Counter<T, N, S>
where
T: Hash + Eq,
N: Ord + Zero,
S: BuildHasher,
{
/// Updates `self` with the union of `self` and `rhs`
///
/// `c |= d;` -> `c[x] == max(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', 3), ('b', 2)].iter().cloned().collect::<HashMap<_, _>>();
/// assert_eq!(c.into_map(), expect);
/// ```
fn bitor_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);
}
}
}
}