u7
This commit is contained in:
parent
b163b91a1a
commit
e2afd1cabd
@ -1,13 +1,14 @@
|
|||||||
"""Hash tabuľka s lineárnym sondovaním a reťazcovými kľúčmi."""
|
"""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:
|
class HashTable:
|
||||||
def __init__(self, capacity=10):
|
def __init__(self, capacity=10):
|
||||||
if type(capacity) is not int:
|
if type(capacity) is not int:
|
||||||
raise TypeError("Kapacita musí byť celé číslo.")
|
raise TypeError("Kapacita musí byť celé číslo.")
|
||||||
if capacity <= 0:
|
if capacity <= 0:
|
||||||
raise ValueError("Kapacita musí byť kladné číslo.")
|
raise ValueError("Kapacita musí byť kladná.")
|
||||||
self._slots = [None] * capacity
|
self._slots = [None] * capacity
|
||||||
self._size = 0
|
self._size = 0
|
||||||
|
|
||||||
@ -18,13 +19,14 @@ class HashTable:
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def _check_key(key):
|
def _check_key(key):
|
||||||
if not isinstance(key, str):
|
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):
|
def _is_active(self, index, key):
|
||||||
slot = self._slots[index]
|
slot = self._slots[index]
|
||||||
return slot is not None and slot is not _DELETED and slot[0] == key
|
return slot is not None and slot is not _DELETED and slot[0] == key
|
||||||
|
|
||||||
def hash(self, key):
|
def hash(self, key):
|
||||||
|
"""Vráť polynomiálny hash kľúča modulo aktuálna kapacita."""
|
||||||
self._check_key(key)
|
self._check_key(key)
|
||||||
h = 0
|
h = 0
|
||||||
for char in key:
|
for char in key:
|
||||||
@ -49,6 +51,10 @@ class HashTable:
|
|||||||
return first_deleted
|
return first_deleted
|
||||||
raise RuntimeError("Tabuľka je plná.")
|
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):
|
def _resize(self, new_capacity):
|
||||||
old_slots = self._slots
|
old_slots = self._slots
|
||||||
self._slots = [None] * new_capacity
|
self._slots = [None] * new_capacity
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user