Skip to content

Commit

Permalink
create 0706 in rust
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammed785 committed Mar 4, 2023
1 parent 191c85a commit 913a3ac
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions rust/0706-design-hashmap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,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); },
_ => (),
}
}
}

0 comments on commit 913a3ac

Please sign in to comment.