-
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.
split sub.rs into _iterable and _self
- Loading branch information
1 parent
0c467d0
commit 76304ed
Showing
3 changed files
with
60 additions
and
53 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,5 +1,6 @@ | ||
mod add; | ||
mod index; | ||
mod intersection; | ||
mod sub; | ||
mod sub_iterable; | ||
mod sub_self; | ||
mod union; |
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,57 @@ | ||
use crate::Counter; | ||
|
||
use num_traits::{One, Zero}; | ||
|
||
use std::hash::Hash; | ||
use std::ops::{Sub, SubAssign}; | ||
|
||
impl<I, T, N> Sub<I> for Counter<T, N> | ||
where | ||
I: IntoIterator<Item = T>, | ||
T: Hash + Eq, | ||
N: PartialOrd + SubAssign + Zero + One, | ||
{ | ||
type Output = Self; | ||
/// Consume `self` producing a `Counter` like `self` with the counts of the | ||
/// elements of `I` subtracted, keeping only positive values. | ||
/// | ||
/// ```rust | ||
/// # use counter::Counter; | ||
/// # use std::collections::HashMap; | ||
/// let c = "aaab".chars().collect::<Counter<_>>(); | ||
/// let e = c - "abb".chars(); | ||
/// | ||
/// let expect = [('a', 2)].iter().cloned().collect::<HashMap<_, _>>(); | ||
/// assert_eq!(e.into_map(), expect); | ||
/// ``` | ||
fn sub(mut self, rhs: I) -> Self::Output { | ||
self.subtract(rhs); | ||
self | ||
} | ||
} | ||
|
||
impl<I, T, N> SubAssign<I> for Counter<T, N> | ||
where | ||
I: IntoIterator<Item = T>, | ||
T: Hash + Eq, | ||
N: PartialOrd + SubAssign + Zero + One, | ||
{ | ||
/// Directly subtract the counts of the elements of `I` from `self`, | ||
/// 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<_>>(); | ||
/// c -= "abb".chars(); | ||
/// | ||
/// let expect = [('a', 2)].iter().cloned().collect::<HashMap<_, _>>(); | ||
/// assert_eq!(c.into_map(), expect); | ||
/// ``` | ||
fn sub_assign(&mut self, rhs: I) { | ||
self.subtract(rhs); | ||
} | ||
} |
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