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
37pub trait SeedableFromU64SeedExt {
38    fn seed_from_u64(seed: u64) -> Self;
39}
40
41impl SeedableFromU64SeedExt for PlSeedableRandomStateQuality {
42    fn seed_from_u64(seed: u64) -> Self {
43        PlSeedableRandomStateQuality::with_seed(seed, SharedSeed::global_fixed())
44    }
45}
46
47pub trait InitHashMaps {
48    type HashMap;
49
50    fn new() -> Self::HashMap;
51
52    fn with_capacity(capacity: usize) -> Self::HashMap;
53}
54
55impl<K, V> InitHashMaps for PlHashMap<K, V> {
56    type HashMap = Self;
57
58    fn new() -> Self::HashMap {
59        Self::with_capacity_and_hasher(0, Default::default())
60    }
61
62    fn with_capacity(capacity: usize) -> Self {
63        Self::with_capacity_and_hasher(capacity, Default::default())
64    }
65}
66impl<K> InitHashMaps for PlHashSet<K> {
67    type HashMap = Self;
68
69    fn new() -> Self::HashMap {
70        Self::with_capacity_and_hasher(0, Default::default())
71    }
72
73    fn with_capacity(capacity: usize) -> Self {
74        Self::with_capacity_and_hasher(capacity, Default::default())
75    }
76}
77
78impl<K> InitHashMaps for PlIndexSet<K> {
79    type HashMap = Self;
80
81    fn new() -> Self::HashMap {
82        Self::with_capacity_and_hasher(0, Default::default())
83    }
84
85    fn with_capacity(capacity: usize) -> Self::HashMap {
86        Self::with_capacity_and_hasher(capacity, Default::default())
87    }
88}
89
90impl<K, V> InitHashMaps for PlIndexMap<K, V> {
91    type HashMap = Self;
92
93    fn new() -> Self::HashMap {
94        Self::with_capacity_and_hasher(0, Default::default())
95    }
96
97    fn with_capacity(capacity: usize) -> Self::HashMap {
98        Self::with_capacity_and_hasher(capacity, Default::default())
99    }
100}