-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c9d2bf0
commit 927b627
Showing
3 changed files
with
63 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
mod add; | ||
mod index; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use crate::Counter; | ||
|
||
use num_traits::Zero; | ||
|
||
use std::hash::Hash; | ||
use std::ops::{Add, AddAssign}; | ||
|
||
impl<T, N> AddAssign for Counter<T, N> | ||
where | ||
T: Hash + Eq, | ||
N: Zero + AddAssign, | ||
{ | ||
/// Add another counter to this counter. | ||
/// | ||
/// `c += d;` -> `c[x] += d[x]` for all `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', 4), ('b', 3)].iter().cloned().collect::<HashMap<_, _>>(); | ||
/// assert_eq!(c.into_map(), expect); | ||
/// ``` | ||
fn add_assign(&mut self, rhs: Self) { | ||
for (key, value) in rhs.map { | ||
let entry = self.map.entry(key).or_insert_with(N::zero); | ||
*entry += value; | ||
} | ||
} | ||
} | ||
|
||
impl<T, N> Add for Counter<T, N> | ||
where | ||
T: Clone + Hash + Eq, | ||
N: AddAssign + Zero, | ||
{ | ||
type Output = Counter<T, N>; | ||
|
||
/// Add two counters together. | ||
/// | ||
/// `out = c + d;` -> `out[x] == c[x] + d[x]` for all `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', 4), ('b', 3)].iter().cloned().collect::<HashMap<_, _>>(); | ||
/// assert_eq!(e.into_map(), expect); | ||
/// ``` | ||
fn add(mut self, rhs: Counter<T, N>) -> Self::Output { | ||
self += rhs; | ||
self | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters