Skip to content

Commit

Permalink
Merge pull request #12 from coriolinus/cleanup
Browse files Browse the repository at this point in the history
Cleanup
  • Loading branch information
coriolinus authored May 19, 2020
2 parents 6f3ea66 + db3dd8e commit f7f7b71
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
[package]
name = "counter"
version = "0.5.0"
version = "0.5.1"
authors = ["Peter Goodspeed-Niklaus <peter.r.goodspeedniklaus@gmail.com>"]
description = "Simple package to count generic iterables"
repository = "https://github.com/coriolinus/counter-rs"
documentation = "https://docs.rs/counter"
readme = "README.md"
keywords = ["count"]
license = "MIT"
edition = "2018"

[dependencies]
num-traits = "0.2"
Expand Down
37 changes: 15 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,6 @@
//! assert!(counter.into_map() == expected);
//! ```

#[cfg(test)]
#[macro_use]
extern crate maplit;

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

use std::borrow::Borrow;
Expand Down Expand Up @@ -195,7 +190,7 @@ where
I: IntoIterator<Item = T>,
{
for item in iterable.into_iter() {
let entry = self.map.entry(item).or_insert(N::zero());
let entry = self.map.entry(item).or_insert_with(N::zero);
*entry += N::one();
}
}
Expand Down Expand Up @@ -284,7 +279,7 @@ where
items.sort_by(|&(ref a_item, ref a_count), &(ref b_item, ref b_count)| {
match b_count.cmp(&a_count) {
Ordering::Equal => tiebreaker(&a_item, &b_item),
unequal @ _ => unequal,
unequal => unequal,
}
});
items
Expand Down Expand Up @@ -334,7 +329,7 @@ where
/// ```
fn add_assign(&mut self, rhs: Self) {
for (key, value) in rhs.map.iter() {
let entry = self.map.entry(key.clone()).or_insert(N::zero());
let entry = self.map.entry(key.clone()).or_insert_with(N::zero);
*entry += value.clone();
}
}
Expand Down Expand Up @@ -362,10 +357,9 @@ where
/// let expect = [('a', 4), ('b', 3)].iter().cloned().collect::<HashMap<_, _>>();
/// assert_eq!(e.into_map(), expect);
/// ```
fn add(self, rhs: Counter<T, N>) -> Self::Output {
let mut counter = self.clone();
counter += rhs;
counter
fn add(mut self, rhs: Counter<T, N>) -> Self::Output {
self += rhs;
self
}
}

Expand Down Expand Up @@ -503,15 +497,14 @@ where
/// let expect = [('a', 3), ('b', 2)].iter().cloned().collect::<HashMap<_, _>>();
/// assert_eq!(e.into_map(), expect);
/// ```
fn bitor(self, rhs: Counter<T, N>) -> Self::Output {
fn bitor(mut self, rhs: Counter<T, N>) -> Self::Output {
use std::cmp::max;

let mut counter = self.clone();
for (key, value) in rhs.map.iter() {
let entry = counter.map.entry(key.clone()).or_insert(N::zero());
let entry = self.map.entry(key.clone()).or_insert_with(N::zero);
*entry = max(&*entry, value).clone();
}
counter
self
}
}

Expand Down Expand Up @@ -694,10 +687,9 @@ where
/// let expect = [('a', 2)].iter().cloned().collect::<HashMap<_, _>>();
/// assert_eq!(e.into_map(), expect);
/// ```
fn sub(self, rhs: I) -> Self::Output {
let mut ctr = self.clone();
ctr.subtract(rhs);
ctr
fn sub(mut self, rhs: I) -> Self::Output {
self.subtract(rhs);
self
}
}

Expand Down Expand Up @@ -742,7 +734,7 @@ where
fn from_iter<I: IntoIterator<Item = (T, N)>>(iter: I) -> Self {
let mut cnt = Counter::new();
for (item, item_count) in iter.into_iter() {
let entry = cnt.map.entry(item).or_insert(N::zero());
let entry = cnt.map.entry(item).or_insert_with(N::zero);
*entry += item_count;
}
cnt
Expand All @@ -751,6 +743,7 @@ where

#[cfg(test)]
mod tests {
use maplit::hashmap;
use super::*;
use std::collections::HashMap;

Expand Down Expand Up @@ -994,7 +987,7 @@ mod tests {

impl Inty {
pub fn new(i: usize) -> Inty {
Inty { i: i }
Inty { i }
}
}

Expand Down

0 comments on commit f7f7b71

Please sign in to comment.