-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0706-design-hashmap.rs
45 lines (37 loc) · 1.19 KB
/
0706-design-hashmap.rs
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
struct MyHashMap {
buckets: Vec<Vec<(i32,i32)>>,
}
// Prime number of buckets to reduce collisions
const N_BUCKETS: usize = 1031;
impl MyHashMap {
fn new() -> Self {
Self{ buckets: vec![vec![]; N_BUCKETS] }
}
fn hash(key: i32) -> usize {
key as usize % N_BUCKETS
}
fn find_entry(&mut self, key: i32) -> (&mut Vec<(i32, i32)>, Result<usize, usize>) {
let bucket = &mut self.buckets[Self::hash(key)];
let result = bucket.binary_search_by(|(k, v)| k.cmp(&key));
(bucket, result)
}
fn put(&mut self, key: i32, value: i32) {
match self.find_entry(key) {
(bucket, Ok(index)) => bucket[index] = (key, value),
(bucket, Err(index)) => bucket.insert(index, (key, value)),
}
}
fn get(&self, key: i32) -> i32 {
let bucket = &self.buckets[Self::hash(key)];
match bucket.binary_search_by(|(k, v)| k.cmp(&key)) {
Ok(index) => bucket[index].1,
Err(index) => -1,
}
}
fn remove(&mut self, key: i32) {
match self.find_entry(key) {
(bucket, Ok(index)) => { bucket.remove(index); },
_ => (),
}
}
}