2018-03-20 23:48:58 +00:00
|
|
|
#!/usr/bin/env python3
|
2020-09-28 21:41:04 +00:00
|
|
|
from .number_theory.prime_numbers import next_prime
|
2018-03-20 23:48:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
class HashTable:
|
|
|
|
"""
|
2020-09-10 08:31:26 +00:00
|
|
|
Basic Hash Table example with open addressing and linear probing
|
2018-03-20 23:48:58 +00:00
|
|
|
"""
|
|
|
|
|
2022-10-12 16:37:00 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
size_table: int,
|
|
|
|
charge_factor: int | None = None,
|
|
|
|
lim_charge: float | None = None,
|
|
|
|
) -> None:
|
2018-03-20 23:48:58 +00:00
|
|
|
self.size_table = size_table
|
|
|
|
self.values = [None] * self.size_table
|
|
|
|
self.lim_charge = 0.75 if lim_charge is None else lim_charge
|
|
|
|
self.charge_factor = 1 if charge_factor is None else charge_factor
|
2022-10-12 16:37:00 +00:00
|
|
|
self.__aux_list: list = []
|
|
|
|
self._keys: dict = {}
|
2018-03-20 23:48:58 +00:00
|
|
|
|
|
|
|
def keys(self):
|
|
|
|
return self._keys
|
|
|
|
|
|
|
|
def balanced_factor(self):
|
2021-09-07 11:37:03 +00:00
|
|
|
return sum(1 for slot in self.values if slot is not None) / (
|
2019-10-05 05:14:13 +00:00
|
|
|
self.size_table * self.charge_factor
|
|
|
|
)
|
2018-03-20 23:48:58 +00:00
|
|
|
|
|
|
|
def hash_function(self, key):
|
|
|
|
return key % self.size_table
|
|
|
|
|
|
|
|
def _step_by_step(self, step_ord):
|
|
|
|
|
2020-01-03 14:25:36 +00:00
|
|
|
print(f"step {step_ord}")
|
2022-10-15 17:29:42 +00:00
|
|
|
print(list(range(len(self.values))))
|
2018-03-20 23:48:58 +00:00
|
|
|
print(self.values)
|
|
|
|
|
|
|
|
def bulk_insert(self, values):
|
|
|
|
i = 1
|
|
|
|
self.__aux_list = values
|
|
|
|
for value in values:
|
|
|
|
self.insert_data(value)
|
|
|
|
self._step_by_step(i)
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
def _set_value(self, key, data):
|
|
|
|
self.values[key] = data
|
|
|
|
self._keys[key] = data
|
|
|
|
|
2020-03-13 12:46:52 +00:00
|
|
|
def _collision_resolution(self, key, data=None):
|
2018-03-20 23:48:58 +00:00
|
|
|
new_key = self.hash_function(key + 1)
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
while self.values[new_key] is not None and self.values[new_key] != key:
|
2018-03-20 23:48:58 +00:00
|
|
|
|
|
|
|
if self.values.count(None) > 0:
|
|
|
|
new_key = self.hash_function(new_key + 1)
|
|
|
|
else:
|
|
|
|
new_key = None
|
|
|
|
break
|
|
|
|
|
|
|
|
return new_key
|
|
|
|
|
|
|
|
def rehashing(self):
|
|
|
|
survivor_values = [value for value in self.values if value is not None]
|
|
|
|
self.size_table = next_prime(self.size_table, factor=2)
|
|
|
|
self._keys.clear()
|
2019-10-05 05:14:13 +00:00
|
|
|
self.values = [None] * self.size_table # hell's pointers D: don't DRY ;/
|
2020-03-16 10:19:13 +00:00
|
|
|
for value in survivor_values:
|
|
|
|
self.insert_data(value)
|
2018-03-20 23:48:58 +00:00
|
|
|
|
|
|
|
def insert_data(self, data):
|
|
|
|
key = self.hash_function(data)
|
|
|
|
|
|
|
|
if self.values[key] is None:
|
|
|
|
self._set_value(key, data)
|
|
|
|
|
|
|
|
elif self.values[key] == data:
|
|
|
|
pass
|
|
|
|
|
|
|
|
else:
|
2020-03-13 12:46:52 +00:00
|
|
|
collision_resolution = self._collision_resolution(key, data)
|
|
|
|
if collision_resolution is not None:
|
|
|
|
self._set_value(collision_resolution, data)
|
2018-03-20 23:48:58 +00:00
|
|
|
else:
|
|
|
|
self.rehashing()
|
|
|
|
self.insert_data(data)
|