new
This commit is contained in:
parent
d6e54f42b2
commit
3281c53834
@ -1,4 +1,5 @@
|
||||
class HashTable:
|
||||
|
||||
def __init__(self, capacity=10):
|
||||
if type(capacity) is not int:
|
||||
raise TypeError("Kapacita musí byť celé číslo (int)")
|
||||
@ -14,6 +15,41 @@ class HashTable:
|
||||
@property
|
||||
def capacity(self):
|
||||
return self.__capacity
|
||||
|
||||
def _check_key(self, key):
|
||||
if not isinstance(key, str):
|
||||
raise TypeError("Kľúč musí byť reťazec (str)")
|
||||
|
||||
def hash(self, key):
|
||||
self._check_key(key)
|
||||
h = 0
|
||||
for char in key:
|
||||
h = (31 * h + ord(char)) % self.__capacity
|
||||
return h
|
||||
|
||||
def probe(self, key):
|
||||
self._check_key(key)
|
||||
start_index = self.hash(key)
|
||||
first_deleted = None
|
||||
|
||||
for krok in range(self.__capacity):
|
||||
index = (start_index + krok) % self.__capacity
|
||||
slot = self.slots[index]
|
||||
|
||||
if slot is self.empty:
|
||||
if first_deleted is not None:
|
||||
return first_deleted
|
||||
return index
|
||||
|
||||
elif slot is self.deleted:
|
||||
if first_deleted is None:
|
||||
first_deleted = index
|
||||
|
||||
else:
|
||||
if slot[0] == key:
|
||||
return index
|
||||
|
||||
if first_deleted is not None:
|
||||
return first_deleted
|
||||
|
||||
raise RuntimeError("Tabuľka je plná")
|
||||
Loading…
Reference in New Issue
Block a user