|
|
|
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
explore_steps = 0 |
|
|
|
|
|
|
|
|
|
|
|
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, hash_function, exploratory_function=None): |
|
|
|
|
|
self.m = m |
|
|
|
|
|
self.h = hash_function |
|
|
|
|
|
self.f = exploratory_function |
|
|
|
|
|
self.n = 0 |
|
|
|
|
|
self.table = AlgoDatArray(m) |
|
|
|
|
|
|
|
|
|
|
|
def insert(self, x): |
|
|
|
|
|
global explore_steps |
|
|
|
|
|
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 |
|
|
|
|
|
self.n += 1 |
|
|
|
|
|
return True |
|
|
|
|
|
i += 1 |
|
|
|
|
|
explore_steps += 1 |
|
|
|
|
|
return False |
|
|
|
|
|
|
|
|
|
|
|
def search(self, x): |
|
|
|
|
|
global explore_steps |
|
|
|
|
|
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 |
|
|
|
|
|
explore_steps += 1 |
|
|
|
|
|
return False |
|
|
|
|
|
|
|
|
|
|
|
def delete(self, x): |
|
|
|
|
|
global explore_steps |
|
|
|
|
|
i = 0 |
|
|
|
|
|
while i < self.m: |
|
|
|
|
|
j = f(x, i, self.m) |
|
|
|
|
|
if self.table[j].value == x: |
|
|
|
|
|
self.table[j].value = "DELETED" |
|
|
|
|
|
self.n -= 1 |
|
|
|
|
|
return True |
|
|
|
|
|
if self.table[j].value is None: |
|
|
|
|
|
return False |
|
|
|
|
|
i += 1 |
|
|
|
|
|
explore_steps += 1 |
|
|
|
|
|
return False |
|
|
|
|
|
|
|
|
|
|
|
def __str__(self): |
|
|
|
|
|
return str(self.table) |
|
|
|
|
|
|
|
|
|
|
|
def alpha(self): |
|
|
|
|
|
return self.n / 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 algodat_value in z: |
|
|
|
|
|
hash.insert(algodat_value) |
|
|
|
|
|
print(hash) |
|
|
|
|
|
print(f"Alpha: {hash.alpha()}") |
|
|
|
|
|
hash.delete(11) |
|
|
|
|
|
hash.search(47) |
|
|
|
|
|
hash.search(243) |
|
|
|
|
|
print(hash) |
|
|
|
|
|
print(f"Alpha: {hash.alpha()}") |
|
|
|
|
|
print(f"Dauer: {pfc() - start:.4f}s") |
|
|
|
|
|
print(f"Sondierungsschritte: {explore_steps}") |
|
|
|
|
|
AlgoDatValue.summary() |