| 1 |
use crate::Counter; |
| 2 |
|
| 3 |
use num_traits::Zero; |
| 4 |
|
| 5 |
use std::hash::Hash; |
| 6 |
use std::ops::{BitAnd, BitAndAssign}; |
| 7 |
|
| 8 |
impl<T, N> BitAnd for Counter<T, N> |
| 9 |
where |
| 10 |
T: Hash + Eq, |
| 11 |
N: Ord + Zero, |
| 12 |
{ |
| 13 |
type Output = Counter<T, N>; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
fn bitand(self, mut rhs: Counter<T, N>) -> Self::Output { |
| 31 |
use std::cmp::min; |
| 32 |
|
| 33 |
let mut counter = Counter::new(); |
| 34 |
for (key, lhs_count) in self.map { |
| 35 |
if let Some(rhs_count) = rhs.remove(&key) { |
| 36 |
let count = min(lhs_count, rhs_count); |
| 37 |
counter.map.insert(key, count); |
| 38 |
} |
| 39 |
} |
| 40 |
counter |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
impl<T, N> BitAndAssign for Counter<T, N> |
| 45 |
where |
| 46 |
T: Hash + Eq, |
| 47 |
N: Ord + Zero, |
| 48 |
{ |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
fn bitand_assign(&mut self, mut rhs: Counter<T, N>) { |
| 65 |
for (key, rhs_count) in rhs.drain() { |
| 66 |
if rhs_count < self[&key] { |
| 67 |
self.map.insert(key, rhs_count); |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
} |