from SoSe24.algodat.foundation import AlgoDatArray, AlgoDatValue, read_int_sequence, MinusInf from time import perf_counter as pfc #Goldener Schnitt import math a = (math.sqrt(5) - 1) / 2 def h(x, m): return int(x*a - int(x*a) * m) def f(x, i, m): return (h(x, m) + i + 14*i*i) % m def f1(x, i, m): if i % 2 == 0: return (h(x, m) + i*i) % m return ((h(x, m) - i*i) % m + m) % m class HashTable: def __init__(self, m, h, f=None): self.m = m self.h = h self.f = f self.table = AlgoDatArray(m) def insert(self, x): i = 0 while i < self.m: j = self.f(x.value, i, self.m) if self.is_free(j): self.table[j].value = x.value return True i += 1 return False def search(self, x): i = 0 while i < self.m: j = f(x, i, self.m) if self.table[j] == x: return True if self.table[j] == None: return False i += 1 return False def delete(self, x): i = 0 while i < self.m: j = f(x, i, self.m) if self.table[j].value == x: self.table[j].value = "DELETED" return True if self.table[j].value is None: return False i += 1 return False def __str__(self): return str(self.table) def alpha(self): i=0 used = 0 while i < self.m: used += 0 if self.is_free(i) else 1 i += 1 return used / self.m def is_free(self, i): if self.table[i] == None: return True if self.table[i] == "DELETED": return True return False if __name__ == "__main__": z = read_int_sequence("../../seq1.txt") start = pfc() hash = HashTable(31, h, f) for i in z: hash.insert(i) print(hash) print(f"Alpha: {hash.alpha()}") hash.delete(34) hash.search(47) hash.search(243) print(hash) print(f"Alpha: {hash.alpha()}") print(f"Dauer: {pfc() - start:.4f}s") AlgoDatValue.summary()