Skip to main content

osom_lib_hash_tables/abseil/
defaults.rs

1//! Contains the default, recommended configuration for the abseil hash table.
2
3use osom_lib_alloc::traits::Allocator;
4use osom_lib_reprc::macros::reprc;
5use osom_lib_try_clone::TryClone;
6
7use super::hash_table::AbseilHashTable;
8use crate::{
9    abseil::configuration::AbseilConfig,
10    helpers::{DefaultHashBuilder, MaxLoadFactor},
11};
12
13/// The default configuration for [`AbseilHashTable`].
14///
15/// It uses sip hash as the default hasher.
16///
17/// Additionally it uses `0.875` as the default max load factor.
18#[reprc]
19#[must_use]
20pub struct DefaultAbseilConfig<TAllocator: Allocator> {
21    build_hasher: DefaultHashBuilder,
22    allocator: TAllocator,
23}
24
25unsafe impl<TAllocator: Allocator + Send> Send for DefaultAbseilConfig<TAllocator> where MaxLoadFactor: Send {}
26unsafe impl<TAllocator: Allocator + Sync> Sync for DefaultAbseilConfig<TAllocator> where MaxLoadFactor: Sync {}
27
28impl<TAllocator: Allocator> DefaultAbseilConfig<TAllocator> {
29    /// Creates a new [`DefaultAbseilConfig`] with the default allocator.
30    #[inline(always)]
31    pub fn new() -> Self
32    where
33        TAllocator: Default,
34    {
35        Self::with_allocator(TAllocator::default())
36    }
37
38    /// Creates a new [`DefaultAbseilConfig`] with the specified allocator.
39    #[inline]
40    pub fn with_allocator(allocator: TAllocator) -> Self {
41        Self {
42            build_hasher: DefaultHashBuilder::new(),
43            allocator,
44        }
45    }
46}
47
48impl<TAllocator: Allocator + Default> Default for DefaultAbseilConfig<TAllocator> {
49    fn default() -> Self {
50        Self::new()
51    }
52}
53
54impl<TAllocator: Allocator + TryClone> TryClone for DefaultAbseilConfig<TAllocator> {
55    type Error = <TAllocator as TryClone>::Error;
56
57    fn try_clone(&self) -> Result<Self, Self::Error> {
58        let allocator = self.allocator.try_clone()?;
59        Ok(Self {
60            build_hasher: self.build_hasher,
61            allocator,
62        })
63    }
64}
65
66impl<TAllocator: Allocator> AbseilConfig for DefaultAbseilConfig<TAllocator> {
67    type ConcreteBuildHasher = DefaultHashBuilder;
68
69    type ConcreteAllocator = TAllocator;
70
71    fn build_hasher(&self) -> &Self::ConcreteBuildHasher {
72        &self.build_hasher
73    }
74
75    fn allocator_mut(&mut self) -> &mut Self::ConcreteAllocator {
76        &mut self.allocator
77    }
78
79    fn load_factor(&self) -> MaxLoadFactor {
80        MaxLoadFactor::new(0.875)
81    }
82}
83
84/// An alias for [`AbseilHashTable`] with [`DefaultAbseilConfig`].
85pub type DefaultAbseilHashTable<TKey, TValue, TAllocator> =
86    AbseilHashTable<TKey, TValue, DefaultAbseilConfig<TAllocator>>;
87
88#[cfg(feature = "std")]
89use osom_lib_alloc::std_allocator::StdAllocator;
90
91#[cfg(feature = "std")]
92#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
93/// An alias for [`DefaultAbseilHashTable`] with [`StdAllocator`]. Requires `std` feature.
94pub type StdAbseilHashTable<TKey, TValue> = DefaultAbseilHashTable<TKey, TValue, StdAllocator>;