Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement counter with ahash #48

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ keywords = ["count", "multiset", "bag", "counter"]
license = "MIT"
edition = "2021"

[features]
default = ["ahash"]
ahash = ["dep:ahash"]

[dependencies]
ahash = {version = "0.8.3", optional = true}
num-traits = "0.2"
serde = { version = "1.0.188", optional = true}

Expand Down
4 changes: 2 additions & 2 deletions src/impls/add_iterable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ where
/// let new_counter = counter + "aeeeee".chars();
/// let expected: HashMap<char, usize> = [('a', 2), ('b', 2), ('c', 3), ('e', 5)]
/// .iter().cloned().collect();
/// assert_eq!(new_counter.into_map(), expected);
/// assert_eq!(new_counter.into_map(), expected.into_iter().collect());
/// ```
fn add(mut self, rhs: I) -> Self::Output {
self.update(rhs);
Expand All @@ -47,7 +47,7 @@ where
/// counter += "aeeeee".chars();
/// let expected: HashMap<char, usize> = [('a', 2), ('b', 2), ('c', 3), ('e', 5)]
/// .iter().cloned().collect();
/// assert_eq!(counter.into_map(), expected);
/// assert_eq!(counter.into_map(), expected.into_iter().collect());
/// ```
fn add_assign(&mut self, rhs: I) {
self.update(rhs);
Expand Down
4 changes: 2 additions & 2 deletions src/impls/add_self.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ where
/// let e = c + d;
///
/// let expect = [('a', 4), ('b', 3)].iter().cloned().collect::<HashMap<_, _>>();
/// assert_eq!(e.into_map(), expect);
/// assert_eq!(e.into_map(), expect.into_iter().collect());
/// ```
fn add(mut self, rhs: Counter<T, N>) -> Self::Output {
self += rhs;
Expand All @@ -51,7 +51,7 @@ where
/// c += d;
///
/// let expect = [('a', 4), ('b', 3)].iter().cloned().collect::<HashMap<_, _>>();
/// assert_eq!(c.into_map(), expect);
/// assert_eq!(c.into_map(), expect.into_iter().collect());
/// ```
fn add_assign(&mut self, rhs: Self) {
for (key, value) in rhs.map {
Expand Down
4 changes: 4 additions & 0 deletions src/impls/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ use crate::Counter;

use num_traits::Zero;

#[cfg(feature="ahash")]
use ahash::{HashMap,HashMapExt};
#[cfg(not(feature="ahash"))]
use std::collections::HashMap;

use std::hash::Hash;

impl<T, N> Counter<T, N>
Expand Down
4 changes: 4 additions & 0 deletions src/impls/deref.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use crate::Counter;

#[cfg(feature="ahash")]
use ahash::HashMap;
#[cfg(not(feature="ahash"))]
use std::collections::HashMap;

use std::hash::Hash;
use std::ops::{Deref, DerefMut};

Expand Down
6 changes: 3 additions & 3 deletions src/impls/extend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ where
/// let mut counter = "abbccc".chars().collect::<Counter<_>>();
/// counter.extend("bccddd".chars());
/// let expect = [('a', 1), ('b', 3), ('c', 5), ('d', 3)].iter().cloned().collect::<HashMap<_, _>>();
/// assert_eq!(counter.into_map(), expect);
/// assert_eq!(counter.into_map(), expect.into_iter().collect());
/// ```
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
self.update(iter);
Expand All @@ -40,7 +40,7 @@ where
/// counter.extend([('a', 1), ('b', 2), ('c', 3), ('a', 4)].iter().cloned());
/// let expect = [('a', 6), ('b', 4), ('c', 6)].iter()
/// .cloned().collect::<HashMap<_, _>>();
/// assert_eq!(counter.into_map(), expect);
/// assert_eq!(counter.into_map(), expect.into_iter().collect());
/// ```
fn extend<I: IntoIterator<Item = (T, N)>>(&mut self, iter: I) {
for (item, item_count) in iter {
Expand All @@ -66,7 +66,7 @@ where
/// counter.extend(&another);
/// let expect = [('a', 1), ('b', 3), ('c', 5), ('d', 3)].iter()
/// .cloned().collect::<HashMap<_, _>>();
/// assert_eq!(counter.into_map(), expect);
/// assert_eq!(counter.into_map(), expect.into_iter().collect());
/// ```
fn extend<I: IntoIterator<Item = (&'a T, &'a N)>>(&mut self, iter: I) {
for (item, item_count) in iter {
Expand Down
4 changes: 2 additions & 2 deletions src/impls/from_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ where
/// # use std::collections::HashMap;
/// let counter = "abbccc".chars().collect::<Counter<_>>();
/// let expect = [('a', 1), ('b', 2), ('c', 3)].iter().cloned().collect::<HashMap<_, _>>();
/// assert_eq!(counter.into_map(), expect);
/// assert_eq!(counter.into_map(), expect.into_iter().collect());
/// ```
///
fn from_iter<I: IntoIterator<Item = T>>(iterable: I) -> Self {
Expand All @@ -62,7 +62,7 @@ where
/// .cloned().collect::<Counter<_>>();
/// let expect = [('a', 5), ('b', 2), ('c', 3)].iter()
/// .cloned().collect::<HashMap<_, _>>();
/// assert_eq!(counter.into_map(), expect);
/// assert_eq!(counter.into_map(), expect.into_iter().collect());
/// ```
fn from_iter<I: IntoIterator<Item = (T, N)>>(iter: I) -> Self {
let mut cnt = Counter::new();
Expand Down
4 changes: 2 additions & 2 deletions src/impls/intersection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ where
/// let e = c & d;
///
/// let expect = [('a', 1), ('b', 1)].iter().cloned().collect::<HashMap<_, _>>();
/// assert_eq!(e.into_map(), expect);
/// assert_eq!(e.into_map(), expect.into_iter().collect());
/// ```
fn bitand(self, mut rhs: Counter<T, N>) -> Self::Output {
use std::cmp::min;
Expand Down Expand Up @@ -59,7 +59,7 @@ where
/// c &= d;
///
/// let expect = [('a', 1), ('b', 1)].iter().cloned().collect::<HashMap<_, _>>();
/// assert_eq!(c.into_map(), expect);
/// assert_eq!(c.into_map(), expect.into_iter().collect());
/// ```
fn bitand_assign(&mut self, mut rhs: Counter<T, N>) {
for (key, rhs_count) in rhs.drain() {
Expand Down
4 changes: 2 additions & 2 deletions src/impls/sub_iterable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ where
/// let e = c - "abb".chars();
///
/// let expect = [('a', 2)].iter().cloned().collect::<HashMap<_, _>>();
/// assert_eq!(e.into_map(), expect);
/// assert_eq!(e.into_map(), expect.into_iter().collect());
/// ```
fn sub(mut self, rhs: I) -> Self::Output {
self.subtract(rhs);
Expand All @@ -49,7 +49,7 @@ where
/// c -= "abb".chars();
///
/// let expect = [('a', 2)].iter().cloned().collect::<HashMap<_, _>>();
/// assert_eq!(c.into_map(), expect);
/// assert_eq!(c.into_map(), expect.into_iter().collect());
/// ```
fn sub_assign(&mut self, rhs: I) {
self.subtract(rhs);
Expand Down
4 changes: 2 additions & 2 deletions src/impls/sub_self.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ where
/// let e = c - d;
///
/// let expect = [('a', 2)].iter().cloned().collect::<HashMap<_, _>>();
/// assert_eq!(e.into_map(), expect);
/// assert_eq!(e.into_map(), expect.into_iter().collect());
/// ```
fn sub(mut self, rhs: Counter<T, N>) -> Self::Output {
self -= rhs;
Expand Down Expand Up @@ -59,7 +59,7 @@ where
/// c -= d;
///
/// let expect = [('a', 2)].iter().cloned().collect::<HashMap<_, _>>();
/// assert_eq!(c.into_map(), expect);
/// assert_eq!(c.into_map(), expect.into_iter().collect());
/// ```
fn sub_assign(&mut self, rhs: Self) {
for (key, value) in rhs.map {
Expand Down
4 changes: 2 additions & 2 deletions src/impls/union.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ where
/// let e = c | d;
///
/// let expect = [('a', 3), ('b', 2)].iter().cloned().collect::<HashMap<_, _>>();
/// assert_eq!(e.into_map(), expect);
/// assert_eq!(e.into_map(), expect.into_iter().collect());
/// ```
fn bitor(mut self, rhs: Counter<T, N>) -> Self::Output {
for (key, rhs_value) in rhs.map {
Expand Down Expand Up @@ -72,7 +72,7 @@ where
/// c |= d;
///
/// let expect = [('a', 3), ('b', 2)].iter().cloned().collect::<HashMap<_, _>>();
/// assert_eq!(c.into_map(), expect);
/// assert_eq!(c.into_map(), expect.into_iter().collect());
/// ```
fn bitor_assign(&mut self, mut rhs: Counter<T, N>) {
for (key, rhs_count) in rhs.drain() {
Expand Down
13 changes: 9 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
//! counter.extend(&another);
//! let expect = [('a', 1), ('b', 3), ('c', 5), ('d', 3)].iter()
//! .cloned().collect::<HashMap<_, _>>();
//! assert_eq!(counter.into_map(), expect);
//! assert_eq!(counter.into_map(), expect.into_iter().collect());
//! ```
//! ## Get items with keys
//!
Expand Down Expand Up @@ -271,15 +271,20 @@
//! # use std::collections::HashMap;
//! let counter: Counter<_, i8> = "abbccc".chars().collect();
//! let expected: HashMap<char, i8> = [('a', 1), ('b', 2), ('c', 3)].iter().cloned().collect();
//! assert!(counter.into_map() == expected);
//! assert!(counter.into_map() == expected.into_iter().collect());
//! ```

#![allow(clippy::must_use_candidate)]
mod impls;

use num_traits::{One, Zero};

use std::collections::{BinaryHeap, HashMap};
#[cfg(feature="ahash")]
use ahash::HashMap;
#[cfg(not(feature="ahash"))]
use std::collections::HashMap;

use std::collections::BinaryHeap;
use std::hash::Hash;
use std::iter;
use std::ops::{AddAssign, SubAssign};
Expand Down Expand Up @@ -361,7 +366,7 @@ where
/// let mut counter = "abbccc".chars().collect::<Counter<_>>();
/// counter.subtract("abba".chars());
/// let expect = [('c', 3)].iter().cloned().collect::<HashMap<_, _>>();
/// assert_eq!(counter.into_map(), expect);
/// assert_eq!(counter.into_map(), expect.into_iter().collect());
/// ```
pub fn subtract<I>(&mut self, iterable: I)
where
Expand Down
33 changes: 19 additions & 14 deletions src/unit_tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use crate::Counter;
use maplit::hashmap;
#[cfg(feature="ahash")]
use ahash::{HashMap,HashMapExt};
#[cfg(not(feature="ahash"))]
use std::collections::HashMap;

#[test]
fn test_creation() {
let _: Counter<usize> = Counter::new();
Expand All @@ -27,7 +31,8 @@ fn test_update() {
'b' => 2,
'c' => 3,
};
assert!(counter.map == expected);

assert!(counter.map == expected.into_iter().collect());

counter.update("aeeeee".chars());
let expected = hashmap! {
Expand All @@ -36,7 +41,7 @@ fn test_update() {
'c' => 3,
'e' => 5,
};
assert!(counter.map == expected);
assert!(counter.map == expected.into_iter().collect());
}

#[test]
Expand All @@ -47,7 +52,7 @@ fn test_add_update_iterable() {
'b' => 2,
'c' => 3,
};
assert!(counter.map == expected);
assert!(counter.map == expected.into_iter().collect());

counter += "aeeeee".chars();
let expected = hashmap! {
Expand All @@ -56,7 +61,7 @@ fn test_add_update_iterable() {
'c' => 3,
'e' => 5,
};
assert!(counter.map == expected);
assert!(counter.map == expected.into_iter().collect());
}

#[test]
Expand All @@ -67,7 +72,7 @@ fn test_add_update_counter() {
'b' => 2,
'c' => 3,
};
assert!(counter.map == expected);
assert!(counter.map == expected.into_iter().collect());

let other = "aeeeee".chars().collect::<Counter<_>>();
counter += other;
Expand All @@ -77,7 +82,7 @@ fn test_add_update_counter() {
'c' => 3,
'e' => 5,
};
assert!(counter.map == expected);
assert!(counter.map == expected.into_iter().collect());
}

#[test]
Expand All @@ -88,7 +93,7 @@ fn test_subtract() {
'a' => 1,
'c' => 1,
};
assert!(counter.map == expected);
assert!(counter.map == expected.into_iter().collect());
}

#[test]
Expand All @@ -99,7 +104,7 @@ fn test_sub_update_iterable() {
'a' => 1,
'c' => 1,
};
assert!(counter.map == expected);
assert!(counter.map == expected.into_iter().collect());
}

#[test]
Expand All @@ -111,7 +116,7 @@ fn test_sub_update_counter() {
'a' => 1,
'c' => 1,
};
assert!(counter.map == expected);
assert!(counter.map == expected.into_iter().collect());
}

#[test]
Expand All @@ -122,7 +127,7 @@ fn test_from_iter_simple() {
'b' => 2,
'c' => 3,
};
assert!(counter.map == expected);
assert!(counter.map == expected.into_iter().collect());
}

#[test]
Expand Down Expand Up @@ -156,7 +161,7 @@ fn test_extend_simple() {
'c' => 5,
'd' => 3,
};
assert!(counter.map == expected);
assert!(counter.map == expected.into_iter().collect());
}

#[test]
Expand All @@ -170,7 +175,7 @@ fn test_extend_tuple() {
'c' => 5,
'd' => 3,
};
assert_eq!(counter.map, expected);
assert_eq!(counter.map, expected.into_iter().collect());
}

#[test]
Expand Down Expand Up @@ -226,7 +231,7 @@ fn test_collect() {
'b' => 2,
'c' => 3,
};
assert!(counter.map == expected);
assert!(counter.map == expected.into_iter().collect());
}

#[test]
Expand All @@ -237,5 +242,5 @@ fn test_non_usize_count() {
'b' => 2,
'c' => 3,
};
assert!(counter.map == expected);
assert!(counter.map == expected.into_iter().collect());
}
Loading