import math import unittest from utils.literal import Literal from utils.memory_cell import MemoryCell from utils.memory_array import MemoryArray from vorlesung.L07_hashtable.hashtable import HashTableOpenAddressing #Goldener Schnitt a = Literal((math.sqrt(5) - 1) / 2) # Hashfunktion nach multiplikativer Methode def h(x: MemoryCell, m: Literal) -> Literal: with MemoryCell(int(x * a)) as integer_part, MemoryCell(x * a) as full_product: with MemoryCell(full_product - integer_part) as fractional_part: return Literal(abs(int(fractional_part * m))) # Quadratische Sondierung def f(x: MemoryCell, i: Literal, m: Literal) -> Literal: c1 = 1 c2 = 5 with MemoryCell(h(x, m)) as initial_hash, MemoryCell(c2 * int(i) * int(i)) as quadratic_offset: with MemoryCell(initial_hash + quadratic_offset) as probe_position: probe_position += Literal(c1 * int(i)) # Linear component return probe_position % m # Symmetrische quadratische Sondierung def fs(x: MemoryCell, i: Literal, m: Literal) -> Literal: with MemoryCell(h(x, m)) as base_hash, MemoryCell(int(i) * int(i)) as square: if int(i) % 2 == 0: # gerades i: Vorwärtssondierung with MemoryCell(base_hash + square) as position: return position % m else: # ungerades i: Rückwärtssondierung with MemoryCell(base_hash - square) as position: return position % m if __name__ == "__main__": print("*** Aufgabe 3 ***") size = Literal(20) print(f"Anlage einer Hash-Tabelle mit offener Adressierung mit Größe {size}") ht = HashTableOpenAddressing(size, f) print("Einfügen der Werte aus seq0.txt") for cell in MemoryArray.create_array_from_file("data/seq0.txt"): if not ht.insert(cell): print(f"Einfügen von {cell} nicht möglich") print(ht) print(f"Belegungsfaktor: {ht.alpha()}") with MemoryCell(52) as cell: print(f"Suche nach {cell}") if ht.search(cell): print(f"{cell} gefunden, wird gelöscht.") ht.delete(cell) else: print(f"{cell} nicht gefunden") print(ht) print(f"Belegungsfaktor: {ht.alpha()}") print("Einfügen von 24") with MemoryCell(24) as cell: if not ht.insert(cell): print(f"Einfügen von {cell} nicht möglich") print(ht) print(f"Belegungsfaktor: {ht.alpha()}") print() print("*** Aufgabe 4 ***") size = Literal(90) print(f"Anlage einer Hash-Tabelle mit offener Adressierung mit Größe {size}") ht = HashTableOpenAddressing(size, f) print("Einfügen der Werte aus seq1.txt") for cell in MemoryArray.create_array_from_file("data/seq1.txt"): if not ht.insert(cell): print(f"Einfügen von {cell} nicht möglich") print(ht) print(f"Belegungsfaktor: {ht.alpha()}") print() print("*** Aufgabe 5 ***") size = Literal(89) print(f"Anlage einer Hash-Tabelle mit offener Adressierung mit Größe {size}") ht = HashTableOpenAddressing(size, f) print("Einfügen der Werte aus seq1.txt") for cell in MemoryArray.create_array_from_file("data/seq1.txt"): if not ht.insert(cell): print(f"Einfügen von {cell} nicht möglich") print(ht) print(f"Belegungsfaktor: {ht.alpha()}") print() print("*** Aufgabe 6 ***") size = Literal(90) print(f"Anlage einer Hash-Tabelle mit offener Adressierung mit Größe {size}") print("Verwendung der symmetrischen quadratischen Sondierung") ht = HashTableOpenAddressing(size, fs) print("Einfügen der Werte aus seq1.txt") for cell in MemoryArray.create_array_from_file("data/seq1.txt"): if not ht.insert(cell): print(f"Einfügen von {cell} nicht möglich") print(ht) print(f"Belegungsfaktor: {ht.alpha()}") print()