Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-utils/src/partitioned.rs
6939 views
1
use hashbrown::hash_map::{HashMap, RawEntryBuilder, RawEntryBuilderMut};
2
3
use crate::aliases::PlRandomState;
4
use crate::hashing::hash_to_partition;
5
6
pub struct PartitionedHashMap<K, V, S = PlRandomState> {
7
inner: Vec<HashMap<K, V, S>>,
8
}
9
10
impl<K, V, S> PartitionedHashMap<K, V, S> {
11
pub fn new(inner: Vec<HashMap<K, V, S>>) -> Self {
12
Self { inner }
13
}
14
15
#[inline(always)]
16
pub fn raw_entry_mut(&mut self, h: u64) -> RawEntryBuilderMut<'_, K, V, S> {
17
self.raw_entry_and_partition_mut(h).0
18
}
19
20
#[inline(always)]
21
pub fn raw_entry(&self, h: u64) -> RawEntryBuilder<'_, K, V, S> {
22
self.raw_entry_and_partition(h).0
23
}
24
25
#[inline]
26
pub fn raw_entry_and_partition(&self, h: u64) -> (RawEntryBuilder<'_, K, V, S>, usize) {
27
let partition = hash_to_partition(h, self.inner.len());
28
let current_table = unsafe { self.inner.get_unchecked(partition) };
29
(current_table.raw_entry(), partition)
30
}
31
32
#[inline]
33
pub fn raw_entry_and_partition_mut(
34
&mut self,
35
h: u64,
36
) -> (RawEntryBuilderMut<'_, K, V, S>, usize) {
37
let partition = hash_to_partition(h, self.inner.len());
38
let current_table = unsafe { self.inner.get_unchecked_mut(partition) };
39
(current_table.raw_entry_mut(), partition)
40
}
41
42
pub fn inner(&self) -> &[HashMap<K, V, S>] {
43
self.inner.as_ref()
44
}
45
46
pub fn inner_mut(&mut self) -> &mut Vec<HashMap<K, V, S>> {
47
&mut self.inner
48
}
49
}
50
51