Skip to main content

osom_lib_hash_tables/bytell/
defaults.rs

1//! Contains the default, recommended configuration for the bytell hash table.
2
3use osom_lib_alloc::traits::Allocator;
4use osom_lib_hashes::siphash::SipHashBuilder;
5use osom_lib_reprc::macros::reprc;
6
7use crate::{
8    bytell::{configuration::BytellConfig, hash_table::BytellHashTable, hash_to_index::FibonacciHashToIndex},
9    helpers::MaxLoadFactor,
10};
11
12/// The default configuration for [`BytellHashTable`].
13///
14/// It uses [`SipHashBuilder`] as the default hasher and [`FibonacciHashToIndex`] as the default hash-to-index policy.
15///
16/// Additionally it uses `0.9375` as the default max load factor.
17#[reprc]
18#[must_use]
19pub struct DefaultBytellConfig<TAllocator: Allocator> {
20    hash_to_index: FibonacciHashToIndex,
21    build_hasher: SipHashBuilder,
22    allocator: TAllocator,
23    load_factor: MaxLoadFactor,
24}
25
26impl<TAllocator: Allocator> DefaultBytellConfig<TAllocator> {
27    /// Creates a new [`DefaultBytellConfig`] with the default allocator.
28    #[inline(always)]
29    pub fn new() -> Self {
30        Self::with_allocator(TAllocator::default())
31    }
32
33    /// Creates a new [`DefaultBytellConfig`] with the specified allocator.
34    #[inline]
35    pub fn with_allocator(allocator: TAllocator) -> Self {
36        #[allow(clippy::default_constructed_unit_structs)]
37        {
38            Self {
39                hash_to_index: FibonacciHashToIndex::default(),
40                build_hasher: SipHashBuilder::with_keys(1, 2),
41                allocator: allocator,
42                load_factor: MaxLoadFactor::new(0.9375),
43            }
44        }
45    }
46}
47
48impl<TAllocator: Allocator> Clone for DefaultBytellConfig<TAllocator> {
49    fn clone(&self) -> Self {
50        Self {
51            hash_to_index: self.hash_to_index,
52            build_hasher: self.build_hasher.clone(),
53            allocator: self.allocator.clone(),
54            load_factor: self.load_factor,
55        }
56    }
57}
58
59impl<TAllocator: Allocator> Default for DefaultBytellConfig<TAllocator> {
60    fn default() -> Self {
61        Self::new()
62    }
63}
64
65impl<TAllocator: Allocator> BytellConfig for DefaultBytellConfig<TAllocator> {
66    type ConcreteHashToIndex = FibonacciHashToIndex;
67    type ConcreteBuildHasher = SipHashBuilder;
68    type ConcreteAllocator = TAllocator;
69
70    fn build_hasher(&self) -> &Self::ConcreteBuildHasher {
71        &self.build_hasher
72    }
73
74    fn hash_to_index(&self) -> &Self::ConcreteHashToIndex {
75        &self.hash_to_index
76    }
77
78    fn hash_to_index_mut(&mut self) -> &mut Self::ConcreteHashToIndex {
79        &mut self.hash_to_index
80    }
81
82    fn allocator(&self) -> &Self::ConcreteAllocator {
83        &self.allocator
84    }
85
86    fn load_factor(&self) -> MaxLoadFactor {
87        self.load_factor
88    }
89}
90
91/// An alias for [`BytellHashTable`] with [`DefaultBytellConfig`].
92pub type DefaultBytellHashTable<TKey, TValue, TAllocator> =
93    BytellHashTable<TKey, TValue, DefaultBytellConfig<TAllocator>>;
94
95#[cfg(feature = "std")]
96use osom_lib_alloc::std_allocator::StdAllocator;
97
98#[cfg(feature = "std")]
99#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
100/// An alias for [`DefaultBytellHashTable`] with [`StdAllocator`]. Requires `std` feature.
101pub type StdBytellHashTable<TKey, TValue> = DefaultBytellHashTable<TKey, TValue, StdAllocator>;