Skip to content

Commit

Permalink
impls/add.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-ha458 committed Aug 29, 2023
1 parent c9d2bf0 commit 927b627
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 56 deletions.
1 change: 1 addition & 0 deletions src/impls.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mod add;
mod index;
62 changes: 62 additions & 0 deletions src/impls/add.rs
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
}
}
56 changes: 0 additions & 56 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,62 +582,6 @@ where
}
}

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
}
}

impl<T, N> SubAssign for Counter<T, N>
where
T: Hash + Eq,
Expand Down

0 comments on commit 927b627

Please sign in to comment.