1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
use std::hash::Hash;
use std::hash::Hasher;
use smallvec::SmallVec;
use parking_lot::Mutex;
use std::mem;
use std::ops::{Deref, DerefMut};
pub const TABLE_AMOUNT: usize = 4;
pub const DEFAULT_TABLE_CAPACITY: usize = 2;
pub const LOAD_FACTOR: f64 = 0.85;
fn calculate_index(hash: u64, bits: usize) -> usize {
(hash % (1 << bits)) as usize
}
fn calculate_hash<K: Hash>(key: &K) -> u64 {
let mut hash_state = fxhash::FxHasher64::default();
key.hash(&mut hash_state);
hash_state.finish()
}
struct Table<K, V>
where
K: Hash + Eq
{
data: Vec<Vec<Entry<K, V>>>,
capacity: usize,
amount: usize,
}
impl<K, V> Table<K, V>
where
K: Hash + Eq
{
fn new(capacity: usize) -> Self {
let true_capacity = 1 << capacity;
let mut storage = Vec::with_capacity(true_capacity);
for _ in 0..true_capacity {
storage.push(Vec::new())
}
Self {
data: storage,
capacity,
amount: 0,
}
}
fn load_factor(&self) -> f64 {
let true_capacity = 1 << self.capacity;
self.amount as f64 / f64::from(true_capacity)
}
fn is_overloaded(&self) -> bool {
self.load_factor() > LOAD_FACTOR
}
fn insert(&mut self, k: K, v: V, hash: u64) {
let index = calculate_index(hash, self.capacity);
let evec = &mut self.data[index];
let was_empty = evec.is_empty();
let entry = Entry {
key: k,
value: v,
};
evec.push(entry);
self.amount += 1;
if !was_empty && self.is_overloaded() {
self.realloc(self.capacity + 1)
}
}
fn insert_no_check(&mut self, k: K, v: V, hash: u64) {
let index = calculate_index(hash, self.capacity);
let evec = &mut self.data[index];
let entry = Entry {
key: k,
value: v,
};
evec.push(entry);
self.amount += 1;
}
fn realloc(&mut self, new_capacity: usize) {
let new_true_capacity = 1 << new_capacity;
let mut new_storage = Vec::with_capacity(new_true_capacity);
for _ in 0..new_true_capacity {
new_storage.push(Vec::new())
}
self.capacity = new_capacity;
let old_storage = mem::replace(&mut self.data, new_storage);
self.amount = 0;
for entry in old_storage.into_iter().flatten() {
let hash = calculate_hash(&entry.key);
self.insert_no_check(entry.key, entry.value, hash);
}
}
fn len(&self) -> usize {
self.amount
}
fn capacity(&self) -> usize {
1 << self.capacity
}
fn find_location(&self, hash: u64) -> Option<(usize, usize)> {
let primary_index = calculate_index(hash, self.capacity);
let evec = &self.data[primary_index];
for (i, entry) in evec.iter().enumerate() {
let ekh = calculate_hash(&entry.key);
if ekh == hash {
return Some((primary_index, i));
}
}
None
}
fn get_with_location(&self, location: (usize, usize)) -> &Entry<K, V> {
let evec = &self.data[location.0];
&evec[location.1]
}
}
#[derive(PartialEq, Eq)]
struct Entry<K, V>
where
K: Hash + Eq
{
key: K,
value: V,
}
#[derive(Debug)]
pub struct TableStat {
len: usize,
capacity: usize
}
#[derive(Debug)]
pub struct DHashMap2Stat {
pub tables: Vec<TableStat>,
}
#[derive(Default)]
pub struct DHashMap2<K, V>
where
K: Hash + Eq
{
tables: SmallVec<[Mutex<Table<K, V>>; TABLE_AMOUNT]>,
}
impl<K, V> DHashMap2<K, V>
where
K: Hash + Eq
{
pub fn new() -> Self {
let true_table_amount = 1 << TABLE_AMOUNT;
let tables = (0..true_table_amount).map(|_| Mutex::new(Table::new(DEFAULT_TABLE_CAPACITY))).collect();
Self {
tables,
}
}
pub fn stat(&self) -> DHashMap2Stat {
let mut stat = DHashMap2Stat {
tables: Vec::new(),
};
for locked_table in &self.tables {
let table = locked_table.lock();
let tablestat = TableStat {
len: table.len(),
capacity: table.capacity(),
};
stat.tables.push(tablestat);
}
stat
}
pub fn insert(&self, k: K, v: V) {
let hash = calculate_hash(&k);
let index = calculate_index(hash, TABLE_AMOUNT);
self.tables[index].lock().insert(k, v, hash);
}
pub fn get(&self, k: &K) -> Option<DHashMap2Ref<K, V>> {
let hash = calculate_hash(&k);
let index = calculate_index(hash, TABLE_AMOUNT);
let lock = self.tables[index].lock();
if let Some(location) = lock.find_location(hash) {
Some(DHashMap2Ref {
lock,
location,
})
} else {
None
}
}
}
pub struct DHashMap2Ref<'a, K, V>
where
K: Hash + Eq,
{
lock: parking_lot::MutexGuard<'a, Table<K, V>>,
location: (usize, usize),
}
impl<'a, K, V> Deref for DHashMap2Ref<'a, K, V>
where
K: Hash + Eq,
{
type Target = V;
#[inline(always)]
fn deref(&self) -> &V {
&self.lock.get_with_location(self.location).value
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn insert_then_stat_64() {
let map = DHashMap2::new();
for i in 0..64 {
map.insert(i, i * 2);
}
println!("Map statistics: {:?}", map.stat());
}
}