Skip to content

Commit

Permalink
Added comments to files
Browse files Browse the repository at this point in the history
  • Loading branch information
PolleAnker committed Apr 25, 2021
1 parent b06e804 commit 4b60fc0
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 27 deletions.
3 changes: 3 additions & 0 deletions Chaining.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 8,7 @@ def __init__(self, hashFunction):
self.arr = [[] for i in range(self.MAX)]

def __setitem__(self, key, val):
"Set an item / index in hash map to be found with the key and hold the value val"
h = HashFunctions.get_hash(key, self.MAX, self.hashFunction)
foundelement = False
'If a key already exists at an index, add it in linked list form (Chaining approach)'
Expand All @@ -20,13 21,15 @@ def __setitem__(self, key, val):
self.arr[h].append((key, val))

def __getitem__(self, key):
"Takes a key as input, which it finds in the hash map, returning its corresponding value."
h = HashFunctions.get_hash(key, self.MAX, self.hashFunction)
'If the element[0] (the key is a match) return the value at element[0] (the value connected to the key)'
for element in self.arr[h]:
if element[0] == key:
return element[1]

def __delitem__(self, key):
"Delete the given key and corresponding value from the hash map"
h = HashFunctions.get_hash(key, self.MAX, self.hashFunction)
'Find the appropiate element, if it matches the key, delete the index'
for index, element in enumerate(self.arr[h]):
Expand Down
3 changes: 2 additions & 1 deletion HashFunctions.py
Original file line number Diff line number Diff line change
@@ -1,6 1,7 @@
"Hash function, to be called in the collision handling scripts as get_hash"
"File holding get_hash functions for hashing a key to an index in the hash map"

def get_hash(key, size, hashMode):
"""Hashes input key to an index in the hash map. size is the size of the hash map. hashMode can be either 'Division, Multiplication, Universal or Prime'"""
if hashMode == "Division":
h = 0
for char in key:
Expand Down
23 changes: 0 additions & 23 deletions HashTable.py

This file was deleted.

15 changes: 12 additions & 3 deletions OpenAddressing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 2,19 @@
import HashFunctions

class OpenAddressing:
"""Hash map using 'open addressing' to handle collisions.
It takes a desired hash function ('Division, Multiplication, Universal or Prime')
and desired probing method ('Linear, Quadratic or Double) as input"""
def __init__(self, hashFunction, probeMode):
self.hashFunction = hashFunction
self.probeMode = probeMode
self.MAX = 10
self.arr = [None for i in range(self.MAX)]


"MAIN FUNCTIONS (Set, Get and Delete items)"
def __setitem__(self, key, val):
'Set the given index, h, to be the key,val pair. Handle collision by checking if it is already occupied'
"Set an item / index in hash map to be found with the key and hold the value val"
h = HashFunctions.get_hash(key, self.MAX, self.hashFunction)
if self.arr[h] is None:
self.arr[h] = (key, val)
Expand All @@ -18,6 23,7 @@ def __setitem__(self, key, val):
self.arr[new_h] = (key,val)

def __getitem__(self, key):
"Takes a key as input, which it finds in the hash map, returning its corresponding value."
h = HashFunctions.get_hash(key, self.MAX, self.hashFunction)
if self.arr[h] is None:
return
Expand All @@ -30,6 36,7 @@ def __getitem__(self, key):
return element[1]

def __delitem__(self, key):
"Delete the given key and corresponding value from the hash map"
h = HashFunctions.get_hash(key, self.MAX, self.hashFunction)
probingRange = self.get_probing_range(h)
for probeIndex in probingRange:
Expand All @@ -39,12 46,14 @@ def __delitem__(self, key):
if self.arr[probeIndex] is None:
raise Exception("Element to delete not found")


"HELPER FUNCTIONS (For assisting in finding new hash map indices in case of collision)"
def get_probing_range(self, index):
'Set probing range to be from the index (hashed key) to the length of the hashmap, and from the start to the index'
"Set probing range to be from the index (hashed key) to the length of the hashmap, and from the start to the index"
return [*range(index, len(self.arr))] [*range(0, index)]

def find_open_slot(self, key, index):
'Calculate the new hash using the step, probeIndex as the new hashed value'
"Calculate a new hash map index in case of collision"
probingRange = self.get_probing_range(index)
for probeIndex in probingRange:

Expand Down

0 comments on commit 4b60fc0

Please sign in to comment.