From 414b3517c51845daa7dde506a9b4ba941b21b964 Mon Sep 17 00:00:00 2001 From: chris-ha458 Date: Sat, 16 Sep 2023 21:55:17 +0900 Subject: [PATCH 01/11] init with_capacity --- src/impls/create.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/impls/create.rs b/src/impls/create.rs index 552b329..f76685b 100644 --- a/src/impls/create.rs +++ b/src/impls/create.rs @@ -17,6 +17,13 @@ where zero: N::zero(), } } + #[must_use] + pub fn with_capacity(capacity: usize) -> Self { + Counter { + map: HashMap::with_capacity(capacity), + zero: N::zero(), + } + } } impl Default for Counter From d3d812f3e4b95b5ea8dd797392f5d243721bde9a Mon Sep 17 00:00:00 2001 From: chris-ha458 Date: Sat, 16 Sep 2023 21:58:21 +0900 Subject: [PATCH 02/11] init_with_capacity should this be default? --- src/impls/from_iterator.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/impls/from_iterator.rs b/src/impls/from_iterator.rs index aa76917..6957554 100644 --- a/src/impls/from_iterator.rs +++ b/src/impls/from_iterator.rs @@ -20,6 +20,19 @@ where counter.update(iterable); counter } + pub fn init_with_capacity(iterable: I) -> Self + where + I: IntoIterator, + { + let iterator = iterable.into_iter(); + let (lower_bound, upper_bound) = iterator.size_hint(); + + let capacity = upper_bound.unwrap_or(lower_bound); + let mut counter: Counter = Counter::with_capacity(capacity); + + counter.update(iterator); + counter + } } impl iter::FromIterator for Counter From cba2d34038d854f4ebb9c623d78a5cd26260b754 Mon Sep 17 00:00:00 2001 From: chris-ha458 Date: Sat, 16 Sep 2023 22:02:36 +0900 Subject: [PATCH 03/11] add test for capacity --- src/unit_tests.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/unit_tests.rs b/src/unit_tests.rs index c97a774..543877c 100644 --- a/src/unit_tests.rs +++ b/src/unit_tests.rs @@ -13,6 +13,20 @@ fn test_creation() { expected.insert(&ONE, 1); assert!(counter.map == expected); } + +#[test] +fn test_creation_with_capacity() { + let initializer = &[1]; + let counter = Counter::init_with_capacity(initializer); + + let mut expected = HashMap::with_capacity(1); + static ONE: usize = 1; + expected.insert(&ONE, 1); + assert!(counter.map == expected); + assert!(counter.map.capacity() == expected.capacity()); +} + + #[test] fn test_update() { let mut counter = Counter::init("abbccc".chars()); From 8370da8ce54b7134d58ad9648d5dd32631c8e2ad Mon Sep 17 00:00:00 2001 From: chris-ha458 Date: Sat, 16 Sep 2023 22:06:10 +0900 Subject: [PATCH 04/11] Update from_iterator.rs --- src/impls/from_iterator.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/impls/from_iterator.rs b/src/impls/from_iterator.rs index 6957554..09522cb 100644 --- a/src/impls/from_iterator.rs +++ b/src/impls/from_iterator.rs @@ -20,6 +20,9 @@ where counter.update(iterable); counter } + + /// Create a new `Counter` initialized with the given iterable. + /// Allocate with iterator size hints, defaulting to the lowerbound pub fn init_with_capacity(iterable: I) -> Self where I: IntoIterator, From c1743135e233e6d087d080cd5388319b6b967fad Mon Sep 17 00:00:00 2001 From: chris-ha458 Date: Mon, 18 Sep 2023 19:04:59 +0900 Subject: [PATCH 05/11] adjust attribute and docs --- src/impls/create.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/impls/create.rs b/src/impls/create.rs index f76685b..93d7a54 100644 --- a/src/impls/create.rs +++ b/src/impls/create.rs @@ -17,7 +17,12 @@ where zero: N::zero(), } } - #[must_use] + + /// 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), From 495d4fa66310d1feb0dce98d89b81cc12bc3b3a1 Mon Sep 17 00:00:00 2001 From: chris-ha458 Date: Mon, 18 Sep 2023 19:06:55 +0900 Subject: [PATCH 06/11] `init_with_capacity` take explicit capacity --- src/impls/from_iterator.rs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/impls/from_iterator.rs b/src/impls/from_iterator.rs index f235ed9..bed9eb9 100644 --- a/src/impls/from_iterator.rs +++ b/src/impls/from_iterator.rs @@ -22,17 +22,12 @@ where /// Create a new `Counter` initialized with the given iterable. /// Allocate with iterator size hints, defaulting to the lowerbound - pub fn init_with_capacity(iterable: I) -> Self + pub fn init_with_capacity(iterable: I, capacity: usize) -> Self where I: IntoIterator, { - let iterator = iterable.into_iter(); - let (lower_bound, upper_bound) = iterator.size_hint(); - - let capacity = upper_bound.unwrap_or(lower_bound); let mut counter: Counter = Counter::with_capacity(capacity); - - counter.update(iterator); + counter.update(iterable); counter } } From 8c7ae4a69ef23822f3d6fb19fda18d6f522a2ba1 Mon Sep 17 00:00:00 2001 From: chris-ha458 Date: Mon, 18 Sep 2023 19:10:17 +0900 Subject: [PATCH 07/11] fix test --- src/unit_tests.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/unit_tests.rs b/src/unit_tests.rs index 543877c..87dbc47 100644 --- a/src/unit_tests.rs +++ b/src/unit_tests.rs @@ -17,7 +17,7 @@ fn test_creation() { #[test] fn test_creation_with_capacity() { let initializer = &[1]; - let counter = Counter::init_with_capacity(initializer); + let counter = Counter::init_with_capacity(initializer,1); let mut expected = HashMap::with_capacity(1); static ONE: usize = 1; @@ -26,7 +26,6 @@ fn test_creation_with_capacity() { assert!(counter.map.capacity() == expected.capacity()); } - #[test] fn test_update() { let mut counter = Counter::init("abbccc".chars()); From be7a2d9f5098d28ae8f5a033c49fe1afab68a8b1 Mon Sep 17 00:00:00 2001 From: Chris Ha Date: Tue, 19 Sep 2023 17:22:48 +0900 Subject: [PATCH 08/11] simplify unit_test Co-authored-by: Peter Goodspeed-Niklaus --- src/unit_tests.rs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/unit_tests.rs b/src/unit_tests.rs index 5abc9ea..4a850f7 100644 --- a/src/unit_tests.rs +++ b/src/unit_tests.rs @@ -15,14 +15,8 @@ fn test_creation() { #[test] fn test_creation_with_capacity() { - let initializer = &[1]; - let counter = Counter::init_with_capacity(initializer,1); - - let mut expected = HashMap::with_capacity(1); - static ONE: usize = 1; - expected.insert(&ONE, 1); - assert!(counter.map == expected); - assert!(counter.map.capacity() == expected.capacity()); + let counter = Counter::with_capacity(1); + assert_eq!(counter.map.capacity(), 1); } #[test] From 65814921fba1a1e449080df75d03654b7cd3d6f1 Mon Sep 17 00:00:00 2001 From: chris-ha458 Date: Tue, 19 Sep 2023 17:27:05 +0900 Subject: [PATCH 09/11] Update unit_tests.rs --- src/unit_tests.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/unit_tests.rs b/src/unit_tests.rs index 4a850f7..f36767d 100644 --- a/src/unit_tests.rs +++ b/src/unit_tests.rs @@ -15,8 +15,8 @@ fn test_creation() { #[test] fn test_creation_with_capacity() { - let counter = Counter::with_capacity(1); - assert_eq!(counter.map.capacity(), 1); + let counter: Counter = Counter::with_capacity(3); + assert_eq!(counter.map.capacity(), 3); } #[test] From f5cc079436cbf82b97f6d570184627bd12db09eb Mon Sep 17 00:00:00 2001 From: chris-ha458 Date: Tue, 19 Sep 2023 17:28:11 +0900 Subject: [PATCH 10/11] omit `init_with_capacity` --- src/impls/from_iterator.rs | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/impls/from_iterator.rs b/src/impls/from_iterator.rs index bed9eb9..1745563 100644 --- a/src/impls/from_iterator.rs +++ b/src/impls/from_iterator.rs @@ -19,17 +19,6 @@ where { Self::from_iter(iterable) } - - /// Create a new `Counter` initialized with the given iterable. - /// Allocate with iterator size hints, defaulting to the lowerbound - pub fn init_with_capacity(iterable: I, capacity: usize) -> Self - where - I: IntoIterator, - { - let mut counter: Counter = Counter::with_capacity(capacity); - counter.update(iterable); - counter - } } impl iter::FromIterator for Counter From 8c2aeeb9d9545c023f183ee8232b5592a6fe86fc Mon Sep 17 00:00:00 2001 From: chris-ha458 Date: Tue, 19 Sep 2023 17:28:18 +0900 Subject: [PATCH 11/11] cargo fmt --- src/unit_tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/unit_tests.rs b/src/unit_tests.rs index f36767d..b9d61ff 100644 --- a/src/unit_tests.rs +++ b/src/unit_tests.rs @@ -15,7 +15,7 @@ fn test_creation() { #[test] fn test_creation_with_capacity() { - let counter: Counter = Counter::with_capacity(3); + let counter: Counter = Counter::with_capacity(3); assert_eq!(counter.map.capacity(), 3); }