From e01b5b5e2a347e8cd4ca6686910d8f441960cb86 Mon Sep 17 00:00:00 2001 From: marina <138340846+bt3gl-cryptographer@users.noreply.github.com> Date: Mon, 7 Aug 2023 16:35:36 -0700 Subject: [PATCH] Update hash_map_array.py --- hash_objects/hash_map_array.py | 43 ++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/hash_objects/hash_map_array.py b/hash_objects/hash_map_array.py index ac36d80..d2f954b 100644 --- a/hash_objects/hash_map_array.py +++ b/hash_objects/hash_map_array.py @@ -3,25 +3,6 @@ # author: bt3gl -class HashMap: - - def __init__(self, key_space): - self.key_space = key_space - self.table = [Bucket() for _ in range(self.key_space)] - - def put(self, key: int, value: int): - hash_key = key % self.key_space - self.table[hash_key].put(key, value) - - def get(self, key: int): - hash_key = key % self.key_space - return self.table[hash_key].get(key) - - def remove(self, key: int): - hash_key = key % self.key_space - self.table[hash_key].remove(key) - - class Bucket: def __init__(self): @@ -46,7 +27,29 @@ class Bucket: def remove(self, key): for i, k in enumerate(self.bucket): if key == k[0]: - # del is an O(n) operation, as we would copy all the i: elements + # del is an O(N) operation, as we would copy all the i: elements # to make it O(1) we could swap the element we want to remove # with the last element in the bucket del self.bucket[i] + + +class HashMap: + + def __init__(self, key_space): + self.key_space = key_space + self.table = [Bucket() for _ in range(self.key_space)] + + def put(self, key: int, value: int): + hash_key = key % self.key_space + self.table[hash_key].put(key, value) + + def get(self, key: int): + hash_key = key % self.key_space + return self.table[hash_key].get(key) + + def remove(self, key: int): + hash_key = key % self.key_space + self.table[hash_key].remove(key) + + +