This commit is contained in:
Patrik Tulach 2026-10-27 13:54:15 +01:00
parent b163b91a1a
commit e2afd1cabd

View File

@ -1,13 +1,14 @@
"""Hash tabuľka s lineárnym sondovaním a reťazcovými kľúčmi."""
_DELETED = object()
_DELETED = object() # značka odstránenej položky
class HashTable:
def __init__(self, capacity=10):
if type(capacity) is not int:
raise TypeError("Kapacita musí byť celé číslo.")
if capacity <= 0:
raise ValueError("Kapacita musí byť kladné číslo.")
raise ValueError("Kapacita musí byť kladná.")
self._slots = [None] * capacity
self._size = 0
@ -18,13 +19,14 @@ class HashTable:
@staticmethod
def _check_key(key):
if not isinstance(key, str):
raise TypeError("Klúč musí byť typu string.")
raise TypeError("Kľúč musí byť reťazec.")
def _is_active(self, index, key):
slot = self._slots[index]
return slot is not None and slot is not _DELETED and slot[0] == key
def hash(self, key):
"""Vráť polynomiálny hash kľúča modulo aktuálna kapacita."""
self._check_key(key)
h = 0
for char in key:
@ -49,6 +51,10 @@ class HashTable:
return first_deleted
raise RuntimeError("Tabuľka je plná.")
def load_factor(self):
"""Vráť podiel počtu aktívnych položiek a kapacity."""
return len(self) / self.capacity
def _resize(self, new_capacity):
old_slots = self._slots
self._slots = [None] * new_capacity