mirror of
https://github.com/jlengrand/Coding4Interviews.git
synced 2026-03-10 08:11:24 +00:00
Just moves HMWithItem to a better location in the file
This commit is contained in:
@@ -68,6 +68,60 @@ class HashMap():
|
||||
|
||||
return h % self._hash_size
|
||||
|
||||
class HashMapWithItem():
|
||||
"""
|
||||
This implementation USES _HashMapItems
|
||||
Also, this implementation will return an error in case of a collision.
|
||||
Data will not be lost, but another key will have to be used.
|
||||
"""
|
||||
def __init__(self, hash_size=513):
|
||||
self._hash_size = hash_size
|
||||
self._size = 0
|
||||
self.hmap = [None] * self._hash_size
|
||||
|
||||
def add(self, key, value):
|
||||
"""
|
||||
Adds the provided value to the hashmap.
|
||||
Raises an Exception if a collision is detected
|
||||
"""
|
||||
# item to be saved in the HM
|
||||
item = _HashMapItem(key, value)
|
||||
|
||||
my_key = self._hash(key)
|
||||
if self.hmap[my_key] == None:
|
||||
self.hmap[my_key] = item
|
||||
self._size += 1
|
||||
else:
|
||||
raise Exception("Collision detected at index %d", key)
|
||||
|
||||
def get(self, key):
|
||||
"""
|
||||
Finds the element in the hash table that may contain the id for
|
||||
the string we are looking for
|
||||
"""
|
||||
my_key = self._hash(key)
|
||||
return self.hmap[my_key].v # returns the value
|
||||
|
||||
def size(self):
|
||||
return self._size
|
||||
|
||||
def _hash(self, value):
|
||||
"""
|
||||
Generates a hash for the given value.
|
||||
The input is expected to be a String, with only ASCII characters.
|
||||
|
||||
# hash function taken from HT3.
|
||||
# We shift and add : << 4 is a *16
|
||||
"""
|
||||
if len(value) < 1:
|
||||
raise Exception("Size of value must be greater than one")
|
||||
|
||||
h = 0
|
||||
for letter in value:
|
||||
h = (h << 4) + ord(letter)
|
||||
|
||||
return h % self._hash_size
|
||||
|
||||
class HMTableCollision(HashMap):
|
||||
"""
|
||||
Extension of the previous HashMap implementation that takes care of
|
||||
@@ -129,59 +183,6 @@ class HMTableCollision(HashMap):
|
||||
|
||||
return h % self._hash_size
|
||||
|
||||
class HashMapWithItem():
|
||||
"""
|
||||
This implementation USES _HashMapItems
|
||||
Also, this implementation will return an error in case of a collision.
|
||||
Data will not be lost, but another key will have to be used.
|
||||
"""
|
||||
def __init__(self, hash_size=513):
|
||||
self._hash_size = hash_size
|
||||
self._size = 0
|
||||
self.hmap = [None] * self._hash_size
|
||||
|
||||
def add(self, key, value):
|
||||
"""
|
||||
Adds the provided value to the hashmap.
|
||||
Raises an Exception if a collision is detected
|
||||
"""
|
||||
# item to be saved in the HM
|
||||
item = _HashMapItem(key, value)
|
||||
|
||||
my_key = self._hash(key)
|
||||
if self.hmap[my_key] == None:
|
||||
self.hmap[my_key] = item
|
||||
self._size += 1
|
||||
else:
|
||||
raise Exception("Collision detected at index %d", key)
|
||||
|
||||
def get(self, key):
|
||||
"""
|
||||
Finds the element in the hash table that may contain the id for
|
||||
the string we are looking for
|
||||
"""
|
||||
my_key = self._hash(key)
|
||||
return self.hmap[my_key].v # returns the value
|
||||
|
||||
def size(self):
|
||||
return self._size
|
||||
|
||||
def _hash(self, value):
|
||||
"""
|
||||
Generates a hash for the given value.
|
||||
The input is expected to be a String, with only ASCII characters.
|
||||
|
||||
# hash function taken from HT3.
|
||||
# We shift and add : << 4 is a *16
|
||||
"""
|
||||
if len(value) < 1:
|
||||
raise Exception("Size of value must be greater than one")
|
||||
|
||||
h = 0
|
||||
for letter in value:
|
||||
h = (h << 4) + ord(letter)
|
||||
|
||||
return h % self._hash_size
|
||||
|
||||
class HMNeighbourCollision():
|
||||
def __init__(self, hash_size=513):
|
||||
|
||||
Reference in New Issue
Block a user