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

With capacity #40

Merged
merged 13 commits into from
Sep 19, 2023
12 changes: 12 additions & 0 deletions src/impls/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ where
zero: N::zero(),
}
}

/// Create a new, empty `Counter` with the specified capacity.
///
/// Note that `capacity` in this case indicates how many distinct items may be counted without reallocation.
/// It is not related to the total number of items which may be counted.
/// For example, `"aaa"` requires a capacity of 1. `"abc"` requires a capacity of 3.
pub fn with_capacity(capacity: usize) -> Self {
coriolinus marked this conversation as resolved.
Show resolved Hide resolved
Counter {
map: HashMap::with_capacity(capacity),
zero: N::zero(),
}
}
}

impl<T, N> Default for Counter<T, N>
Expand Down
7 changes: 7 additions & 0 deletions src/unit_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ fn test_creation() {
expected.insert(&ONE, 1);
assert!(counter.map == expected);
}

#[test]
fn test_creation_with_capacity() {
let counter: Counter<usize, usize> = Counter::with_capacity(3);
assert_eq!(counter.map.capacity(), 3);
}

#[test]
fn test_update() {
let mut counter = "abbccc".chars().collect::<Counter<_>>();
Expand Down