From 0b6168a67b5153b8e41b13d46b06d2d2447dca71 Mon Sep 17 00:00:00 2001 From: Martin Taillefer Date: Fri, 6 Dec 2024 07:03:12 -0800 Subject: [PATCH] More --- TODO.md | 4 + .../src/hashers/inline_right_range_hasher.rs | 2 +- .../src/hashers/right_range_hasher.rs | 6 +- .../src/inline_sets/inline_hash_set.rs | 8 +- .../src/macros/generator.rs | 6 +- .../src/macros/set_macros.rs | 17 +++- frozen-collections-core/src/sets/hash_set.rs | 8 +- frozen-collections/tests/omni_tests.rs | 78 +++++++++++-------- 8 files changed, 81 insertions(+), 48 deletions(-) diff --git a/TODO.md b/TODO.md index bcf4237..7b066c3 100644 --- a/TODO.md +++ b/TODO.md @@ -32,6 +32,10 @@ - Does no_std still work? +## Bug + +- Seems like fmt on our maps doesn't produce the same output as what HashMap emits (we have extra {} around everything) + ## Type System Nightmares These are some things which I haven't done yet since I can't figure out how to express these things in the diff --git a/frozen-collections-core/src/hashers/inline_right_range_hasher.rs b/frozen-collections-core/src/hashers/inline_right_range_hasher.rs index 93106e8..26d695c 100644 --- a/frozen-collections-core/src/hashers/inline_right_range_hasher.rs +++ b/frozen-collections-core/src/hashers/inline_right_range_hasher.rs @@ -30,7 +30,7 @@ where BH: BuildHasher, { fn hash(&self, value: &[T]) -> u64 { - if value.len() <= RANGE_END { + if value.len() < RANGE_END { return 0; } diff --git a/frozen-collections-core/src/hashers/right_range_hasher.rs b/frozen-collections-core/src/hashers/right_range_hasher.rs index 740d449..8e717f8 100644 --- a/frozen-collections-core/src/hashers/right_range_hasher.rs +++ b/frozen-collections-core/src/hashers/right_range_hasher.rs @@ -40,7 +40,7 @@ where { fn hash(&self, value: &String) -> u64 { let b = value.as_bytes(); - if b.len() <= self.range.end { + if b.len() < self.range.end { return 0; } @@ -55,7 +55,7 @@ where { fn hash(&self, value: &&str) -> u64 { let b = value.as_bytes(); - if b.len() <= self.range.end { + if b.len() < self.range.end { return 0; } @@ -70,7 +70,7 @@ where { fn hash(&self, value: &str) -> u64 { let b = value.as_bytes(); - if b.len() <= self.range.end { + if b.len() < self.range.end { return 0; } diff --git a/frozen-collections-core/src/inline_sets/inline_hash_set.rs b/frozen-collections-core/src/inline_sets/inline_hash_set.rs index 2b8ae0f..6c1e06f 100644 --- a/frozen-collections-core/src/inline_sets/inline_hash_set.rs +++ b/frozen-collections-core/src/inline_sets/inline_hash_set.rs @@ -137,7 +137,7 @@ where T: Hash + Eq + Clone, ST: Set, CM: CollectionMagnitude, - H: Hasher + Default, + H: Hasher, { bitor_fn!(H); } @@ -148,7 +148,7 @@ where T: Hash + Eq + Clone, ST: Set, CM: CollectionMagnitude, - H: Hasher + Default, + H: Hasher, { bitand_fn!(H); } @@ -159,7 +159,7 @@ where T: Hash + Eq + Clone, ST: Set, CM: CollectionMagnitude, - H: Hasher + Default, + H: Hasher, { bitxor_fn!(H); } @@ -169,7 +169,7 @@ where T: Hash + Eq + Clone, ST: Set, CM: CollectionMagnitude, - H: Hasher + Default, + H: Hasher, { sub_fn!(H); } diff --git a/frozen-collections-core/src/macros/generator.rs b/frozen-collections-core/src/macros/generator.rs index a761ee6..16279c1 100644 --- a/frozen-collections-core/src/macros/generator.rs +++ b/frozen-collections-core/src/macros/generator.rs @@ -352,10 +352,8 @@ impl Generator { return Ok(self.emit_inline_ordered_scan(&processed_entries)); } - let analysis = analyze_slice_keys( - processed_entries.iter().map(|x| x.parsed_key.as_bytes()), - &bh, - ); + let iter = processed_entries.iter().map(|x| x.parsed_key.as_bytes()); + let analysis = analyze_slice_keys(iter, &bh); Ok(match analysis { SliceKeyAnalysisResult::LeftHandSubslice(range) => { diff --git a/frozen-collections-core/src/macros/set_macros.rs b/frozen-collections-core/src/macros/set_macros.rs index 219eb3f..c9f1b3b 100644 --- a/frozen-collections-core/src/macros/set_macros.rs +++ b/frozen-collections-core/src/macros/set_macros.rs @@ -215,18 +215,33 @@ mod tests { let r = fz_string_set_macro(ts).unwrap().to_string(); assert!(r.contains(expected), "{r} doesn't contain {expected}"); } - check_impl(":: InlineScanSet", quote!({ "1", "2", "3", })); check_impl(":: InlineOrderedScanSet", quote!({ "1", "2", "3", "4" })); check_impl( ":: InlineOrderedScanSet", quote!({ "1", "2", "3", "4", "5", "6"}), ); + check_impl( ":: InlineHashSet", quote!({ "1", "2", "3", "4", "5", "6", "7" }), ); + check_impl( + ":: BridgeHasher", + quote!({ "1", "2", "3", "4", "5", "6", "7" }), + ); + + check_impl( + ":: PassthroughHasher", + quote!({ "1", "22", "333", "4444", "55555", "666666", "7777777" }), + ); + + check_impl( + ":: InlineLeftRangeHasher", + quote!({ "1111", "1112", "1113", "1114", "1115", "1116", "1117" }), + ); + check_impl(":: InlineScanSet", quote!({ x, "2", "3", })); check_impl(":: FacadeStringSet", quote!({ x, "2", "3", "4" })); check_impl(":: FacadeStringSet", quote!({ x, "2", "3", "4", "5", "6"})); diff --git a/frozen-collections-core/src/sets/hash_set.rs b/frozen-collections-core/src/sets/hash_set.rs index 5ffd1b6..af3e454 100644 --- a/frozen-collections-core/src/sets/hash_set.rs +++ b/frozen-collections-core/src/sets/hash_set.rs @@ -130,7 +130,7 @@ where T: Hash + Eq + Clone, ST: Set, CM: CollectionMagnitude, - H: Hasher + Default, + H: Hasher, { bitor_fn!(H); } @@ -140,7 +140,7 @@ where T: Hash + Eq + Clone, ST: Set, CM: CollectionMagnitude, - H: Hasher + Default, + H: Hasher, { bitand_fn!(H); } @@ -150,7 +150,7 @@ where T: Hash + Eq + Clone, ST: Set, CM: CollectionMagnitude, - H: Hasher + Default, + H: Hasher, { bitxor_fn!(H); } @@ -160,7 +160,7 @@ where T: Hash + Eq + Clone, ST: Set, CM: CollectionMagnitude, - H: Hasher + Default, + H: Hasher, { sub_fn!(H); } diff --git a/frozen-collections/tests/omni_tests.rs b/frozen-collections/tests/omni_tests.rs index 17e4576..a961f33 100644 --- a/frozen-collections/tests/omni_tests.rs +++ b/frozen-collections/tests/omni_tests.rs @@ -10,6 +10,29 @@ use frozen_collections_core::sets::*; use hashbrown::HashSet as HashbrownSet; use std::collections::HashMap as StdHashMap; +macro_rules! test_str { + ( $( $input:expr ),* ; $( $other:literal ),*) => { + // handle 8str cases + + let set_reference = HashbrownSet::<&str>::from_iter(vec![ $( $input, )* ].into_iter()); + let set_other = HashbrownSet::<&str>::from_iter(vec![ $( $other, )* ].into_iter()); + + let map_reference = StdHashMap::<_, _, ahash::RandomState>::from_iter(vec![ $( ($input, ()), )* ].into_iter()); + let map_other = StdHashMap::<_, _, ahash::RandomState>::from_iter(vec![ $( ($other, ()), )* ].into_iter()); + + let mut m = fz_string_map!({ $( $input: (),)* }); + test_map(&m, &map_reference, &map_other); + test_map_ops(&m, &map_reference); + test_map_iter(&m, &map_reference); + test_map_iter_mut(&mut m, &map_reference); + + let s = fz_string_set!({ $( $input,)* }); + test_set(&s, &set_reference, &set_other); + test_set_ops(&s, &set_reference, &set_other); + test_set_iter(&s, &set_reference); + } +} + macro_rules! test_all { ( $( $input:expr ),* ; $( $other:literal ),*) => { let set_reference = HashbrownSet::from_iter(vec![ $( $input, )* ].into_iter()); @@ -206,25 +229,6 @@ macro_rules! test_all { let s = hashbrown::HashSet::<_, ahash::RandomState>::from_iter(set_input.clone().into_iter()); test_set(&s, &set_reference, &set_other); - // handle 8str cases -/* - let set_reference = HashbrownSet::<&str>::from_iter(vec![ $( stringify!($input), )* ].into_iter()); - let set_other = HashbrownSet::<&str>::from_iter(vec![ $( stringify!($other), )* ].into_iter()); - - let map_reference = StdHashMap::<_, _, ahash::RandomState>::from_iter(vec![ $( (stringify!($input), ()), )* ].into_iter()); - let map_other = StdHashMap::<_, _, ahash::RandomState>::from_iter(vec![ $( (stringify!($other), ()), )* ].into_iter()); - - let mut m = fz_string_map!({ $( stringify!($input): (),)* }); - test_map(&m, &map_reference, &map_other); - test_map_ops(&m, &map_reference); - test_map_iter(&m, &map_reference); - test_map_iter_mut(&mut m, &map_reference); - - let s = fz_string_set!({ $( stringify!($input),)* }); - test_set(&s, &set_reference, &set_other); - test_set_ops(&s, &set_reference, &set_other); - test_set_iter(&s, &set_reference); -*/ // handle String cases let set_reference = HashbrownSet::::from_iter(vec![ $( $input.to_string(), )* ].into_iter()); @@ -283,20 +287,32 @@ fn test_common() { test_all!(1, 2, 3, 4, 5, 6, 7, 8, 9 ; 3, 10); test_all!(1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ; 3); test_all!(1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ; 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 20); - test_all!(11111, 11112, 11114, 11115, 111165, 111175 ; 2500, 333333333); -/* - let i = 1; - test_all!(i, 2, 3, 4, 5, 6, 7, 8, 9, 10 ; 3); - test_all!(1, i, 3, 4, 5, 6, 7, 8, 9, 10 ; 3); - test_all!(i, 2u8, 3, 4, 5, 6, 7, 8, 9, 10 ; 3); - test_all!(1u8, i, 3, 4, 5, 6, 7, 8, 9, 10 ; 3); - test_all!(i, 2, 3u8, 4, 5, 6, 7, 8, 9, 10 ; 3); - test_all!(1, i, 3u8, 4, 5, 6, 7, 8, 9, 10 ; 3); - test_all!(i, 2u8, 3u8, 4, 5, 6, 7, 8, 9, 10 ; 3); - test_all!(1u8, i, 3u8, 4, 5, 6, 7, 8, 9, 10 ; 3); -*/ + test_str!("1", "2", "3" ; "3", "4", "5"); + test_str!("0", "1" ; "0", "1"); + test_str!("3", "1", "2", "3", "3" ; "3", "4", "5"); + test_str!("1", "2", "3" ; "1", "2", "3", "4", "5"); + test_str!("1", "2", "3" ; "1", "2"); + test_str!("1", "2", "3" ; "2"); + test_str!("1", "2", "4" ; "2"); + test_str!("1", "2", "4" ; "3"); + test_str!("1", "2", "4", "1500" ; "3"); + test_str!("1", "2", "4", "1500" ; "2500"); + test_str!("1" ; "3"); + test_str!("1", "2" ; "3"); + test_str!("1", "2", "3" ; "3"); + test_str!("1", "2", "3", "4" ; "3"); + test_str!("1", "2", "3", "4", "5" ; "3"); + test_str!("1", "2", "3", "4", "5", "6" ; "3"); + test_str!("1", "2", "3", "4", "5", "6", "7" ; "3", "5"); + test_str!("1", "2", "3", "4", "5", "6", "7", "8" ; "3"); + test_str!("1", "2", "3", "4", "5", "6", "7", "8", "9" ; "3", "10"); + test_str!("1", "2", "3", "4", "5", "6", "7", "8", "9", "10" ; "3"); + test_str!("1", "2", "3", "4", "5", "6", "7", "8", "9", "10" ; "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "20"); + test_str!("11111", "11112", "11114", "11115", "111165", "111175" ; "2500", "333333333"); + test_str!("11111", "11112", "11114", "11115", "111165", "111175", "111185" ; "2500", "333333333"); + test_str!("1", "22", "333", "4444", "55555", "666666", "7777777" ; "2500", "333333333"); } #[test]