Skip to main content

polars_utils/
aliases.rs

1use foldhash::SharedSeed;
2
3pub type PlRandomState = foldhash::quality::RandomState;
4pub type PlSeedableRandomStateQuality = foldhash::quality::SeedableRandomState;
5pub type PlRandomStateQuality = foldhash::quality::RandomState;
6pub type PlFixedStateQuality = foldhash::quality::FixedState;
7
8pub type PlHashMap<K, V> = hashbrown::HashMap<K, V, PlRandomState>;
9pub type PlHashSet<V> = hashbrown::HashSet<V, PlRandomState>;
10pub type PlIndexMap<K, V> = indexmap::IndexMap<K, V, PlRandomState>;
11pub type PlIndexSet<K> = indexmap::IndexSet<K, PlRandomState>;
12
13/// HashMap container with a getter that clears the HashMap.
14#[derive(Default)]
15pub struct ScratchHashMap<K, V>(PlHashMap<K, V>);
16
17impl<K, V> ScratchHashMap<K, V> {
18    /// Clear the HashMap and return a mutable reference to it.
19    pub fn get(&mut self) -> &mut PlHashMap<K, V> {
20        self.0.clear();
21        &mut self.0
22    }
23}
24
25/// HashSet container with a getter that clears the HashSet.
26#[derive(Default)]
27pub struct ScratchHashSet<K>(PlHashSet<K>);
28
29impl<K> ScratchHashSet<K> {
30    /// Clear the HashSet and return a mutable reference to it.
31    pub fn get(&mut self) -> &mut PlHashSet<K> {
32        self.0.clear();
33        &mut self.0
34    }
35}
36
37#[derive(Default)]
38pub struct ScratchIndexSet<K>(PlIndexSet<K>);
39
40impl<K> ScratchIndexSet<K> {
41    /// Clear the IndexSet and return a mutable reference to it.
42    pub fn get(&mut self) -> &mut PlIndexSet<K> {
43        self.0.clear();
44        &mut self.0
45    }
46}
47
48#[derive(Default)]
49pub struct ScratchIndexMap<K, V>(PlIndexMap<K, V>);
50
51impl<K, V> ScratchIndexMap<K, V> {
52    /// Clear the IndexMap and return a mutable reference to it.
53    pub fn get(&mut self) -> &mut PlIndexMap<K, V> {
54        self.0.clear();
55        &mut self.0
56    }
57}
58
59pub trait SeedableFromU64SeedExt {
60    fn seed_from_u64(seed: u64) -> Self;
61}
62
63impl SeedableFromU64SeedExt for PlSeedableRandomStateQuality {
64    fn seed_from_u64(seed: u64) -> Self {
65        PlSeedableRandomStateQuality::with_seed(seed, SharedSeed::global_fixed())
66    }
67}
68
69pub trait InitHashMaps {
70    type HashMap;
71
72    fn new() -> Self::HashMap;
73
74    fn with_capacity(capacity: usize) -> Self::HashMap;
75}
76
77impl<K, V> InitHashMaps for PlHashMap<K, V> {
78    type HashMap = Self;
79
80    fn new() -> Self::HashMap {
81        Self::with_capacity_and_hasher(0, Default::default())
82    }
83
84    fn with_capacity(capacity: usize) -> Self {
85        Self::with_capacity_and_hasher(capacity, Default::default())
86    }
87}
88impl<K> InitHashMaps for PlHashSet<K> {
89    type HashMap = Self;
90
91    fn new() -> Self::HashMap {
92        Self::with_capacity_and_hasher(0, Default::default())
93    }
94
95    fn with_capacity(capacity: usize) -> Self {
96        Self::with_capacity_and_hasher(capacity, Default::default())
97    }
98}
99
100impl<K> InitHashMaps for PlIndexSet<K> {
101    type HashMap = Self;
102
103    fn new() -> Self::HashMap {
104        Self::with_capacity_and_hasher(0, Default::default())
105    }
106
107    fn with_capacity(capacity: usize) -> Self::HashMap {
108        Self::with_capacity_and_hasher(capacity, Default::default())
109    }
110}
111
112impl<K, V> InitHashMaps for PlIndexMap<K, V> {
113    type HashMap = Self;
114
115    fn new() -> Self::HashMap {
116        Self::with_capacity_and_hasher(0, Default::default())
117    }
118
119    fn with_capacity(capacity: usize) -> Self::HashMap {
120        Self::with_capacity_and_hasher(capacity, Default::default())
121    }
122}