Skip to content

Commit

Permalink
Merge pull request #40 from chris-ha458/with_capacity
Browse files Browse the repository at this point in the history
With capacity
  • Loading branch information
coriolinus authored Sep 19, 2023
2 parents bb4b8f2 + 8c2aeeb commit fee2fc1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
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 {
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

0 comments on commit fee2fc1

Please sign in to comment.