Skip to content

Commit

Permalink
split sub.rs into _iterable and _self
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-ha458 committed Aug 29, 2023
1 parent 0c467d0 commit 76304ed
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 53 deletions.
3 changes: 2 additions & 1 deletion src/impls.rs
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;
57 changes: 57 additions & 0 deletions src/impls/sub_iterable.rs
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);
}
}
53 changes: 1 addition & 52 deletions src/impls/sub.rs → src/impls/sub_self.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::Counter;

use num_traits::{One, Zero};
use num_traits::Zero;

use std::hash::Hash;
use std::ops::{Sub, SubAssign};
Expand Down Expand Up @@ -80,54 +80,3 @@ where
}
}
}

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

0 comments on commit 76304ed

Please sign in to comment.