avp26/u7/hash_table.py
2026-10-29 14:04:04 +00:00

55 lines
1.5 KiB
Python

class HashTable:
def __init__(self, capacity=10):
if type(capacity) is not int:
raise TypeError("Kapacita musí byť celé číslo (int)")
if capacity <= 0:
raise ValueError("Kapacita musí byť kladné číslo")
self.__capacity = capacity
self._size = 0
self.empty = object()
self.deleted = object()
self.slots = [self.empty] * self.__capacity
@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á")