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;
6use osom_lib_try_clone::TryClone;
7
8use crate::{
9    bytell::{configuration::BytellConfig, hash_table::BytellHashTable, hash_to_index::FibonacciHashToIndex},
10    helpers::{DefaultHashBuilder, MaxLoadFactor},
11};
12
13/// The default configuration for [`BytellHashTable`].
14///
15/// It uses [`FibonacciHashToIndex`] as the default hash-to-index policy.
16///
17/// Additionally it uses `0.9375` as the default max load factor.
18#[reprc]
19#[must_use]
20pub struct DefaultBytellConfig<TAllocator: Allocator> {
21    hash_to_index: FibonacciHashToIndex,
22    build_hasher: DefaultHashBuilder,
23    allocator: TAllocator,
24}
25
26unsafe impl<TAllocator: Allocator + Send> Send for DefaultBytellConfig<TAllocator>
27where
28    SipHashBuilder: Send,
29    FibonacciHashToIndex: Send,
30    MaxLoadFactor: Send,
31{
32}
33
34unsafe impl<TAllocator: Allocator + Sync> Sync for DefaultBytellConfig<TAllocator>
35where
36    SipHashBuilder: Sync,
37    FibonacciHashToIndex: Sync,
38    MaxLoadFactor: Sync,
39{
40}
41
42impl<TAllocator: Allocator> DefaultBytellConfig<TAllocator> {
43    /// Creates a new [`DefaultBytellConfig`] with the default allocator.
44    #[inline(always)]
45    pub fn new() -> Self
46    where
47        TAllocator: Default,
48    {
49        Self::with_allocator(TAllocator::default())
50    }
51
52    /// Creates a new [`DefaultBytellConfig`] with the specified allocator.
53    #[inline]
54    pub fn with_allocator(allocator: TAllocator) -> Self {
55        #[allow(clippy::default_constructed_unit_structs)]
56        {
57            Self {
58                hash_to_index: FibonacciHashToIndex::default(),
59                build_hasher: DefaultHashBuilder::new(),
60                allocator: allocator,
61            }
62        }
63    }
64}
65
66impl<TAllocator: Allocator + TryClone> TryClone for DefaultBytellConfig<TAllocator> {
67    type Error = <TAllocator as TryClone>::Error;
68
69    fn try_clone(&self) -> Result<Self, Self::Error> {
70        Ok(Self {
71            hash_to_index: self.hash_to_index,
72            build_hasher: self.build_hasher,
73            allocator: self.allocator.try_clone()?,
74        })
75    }
76}
77
78impl<TAllocator: Allocator + Default> Default for DefaultBytellConfig<TAllocator> {
79    fn default() -> Self {
80        Self::new()
81    }
82}
83
84impl<TAllocator: Allocator + TryClone> BytellConfig for DefaultBytellConfig<TAllocator> {
85    type ConcreteHashToIndex = FibonacciHashToIndex;
86    type ConcreteBuildHasher = DefaultHashBuilder;
87    type ConcreteAllocator = TAllocator;
88
89    fn build_hasher(&self) -> &Self::ConcreteBuildHasher {
90        &self.build_hasher
91    }
92
93    fn hash_to_index(&self) -> &Self::ConcreteHashToIndex {
94        &self.hash_to_index
95    }
96
97    fn hash_to_index_mut(&mut self) -> &mut Self::ConcreteHashToIndex {
98        &mut self.hash_to_index
99    }
100
101    fn allocator_mut(&mut self) -> &mut Self::ConcreteAllocator {
102        &mut self.allocator
103    }
104
105    fn load_factor(&self) -> MaxLoadFactor {
106        MaxLoadFactor::new(0.9375)
107    }
108}
109
110/// An alias for [`BytellHashTable`] with [`DefaultBytellConfig`].
111pub type DefaultBytellHashTable<TKey, TValue, TAllocator> =
112    BytellHashTable<TKey, TValue, DefaultBytellConfig<TAllocator>>;
113
114#[cfg(feature = "std")]
115use osom_lib_alloc::std_allocator::StdAllocator;
116
117#[cfg(feature = "std")]
118#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
119/// An alias for [`DefaultBytellHashTable`] with [`StdAllocator`]. Requires `std` feature.
120pub type StdBytellHashTable<TKey, TValue> = DefaultBytellHashTable<TKey, TValue, StdAllocator>;