-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashtable.py
133 lines (102 loc) · 3.68 KB
/
hashtable.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import random
from integer import Integer
class Slot:
def __init__(self, key, value):
self.key = key
self.value = value
def __str__(self):
return f"{self.key}: {self.value}"
class DirectAddressTable:
def __init__(self, slots=None):
self.slots: list = slots
def __str__(self):
ret_val = "{"
ret_val += "".join(s for s in self.slots)
return ret_val + "}"
def insert(self, slot: Slot):
if self.slots is None:
self.slots = [slot]
else:
self.slots.append(slot)
def search(self, key):
return [s for s in self.slots if s.key is key]
def delete(self, key):
search_index = self.slots.index(self.search(key)[0])
self.slots.pop(search_index)
# def char_to_int(key):
# tmp_int = Integer()
# return key if isinstance(key, int) else \
# [tmp_int.add(ord(s) * 128 ** len(key) - i) for i, s in enumerate(key)] if isinstance(key, str) else None
def char_to_int(key):
return key if isinstance(key, int) else \
None if not isinstance(key, str) else Integer().addAll(*((ord(s) * (128 ** len(key) - i)) for i, s in enumerate(key)))
class HashDirectAddressMultiplication:
REMOVED = 'ENTFERNT'
def __init__(self, size, keys=None):
self.payload = dict()
self.size = size
self.constant = random.randint(1, 999) * 0.001
if keys is not None and type(keys) in (tuple, list, set):
[self.insert(key) for key in keys]
def hash_multiplicate(self, key: int) -> int:
return int(self.size * (key * self.constant % 1))
def hash(self, key, probe=0) -> int:
return (self.hash_multiplicate(key) + (probe ** 2 if probe != 0 else probe)) % self.size
def insert(self, key) -> int:
key = char_to_int(key)
probe = 0
while probe is not self.size:
actual_hash = self.hash(key, probe)
if self.payload.get(actual_hash) is None \
or self.payload[actual_hash] is self.REMOVED:
self.payload[actual_hash] = key
return actual_hash
probe += 1
else:
print("Overflow")
def search(self, key):
key = char_to_int(key)
probe = 0
actual_hash = 0
while self.payload.get(actual_hash) is not None \
or probe is not self.size:
actual_hash = self.hash(key, probe)
if self.payload.get(actual_hash) == key:
return actual_hash
probe += 1
return None
def delete(self, key):
key = char_to_int(key)
probe = 0
actual_hash = 0
while self.payload[actual_hash] is not None \
or probe is not self.size:
actual_hash = self.hash(key, probe)
if self.payload[actual_hash] == key:
self.payload[actual_hash] = self.REMOVED
return
probe += 1
else:
print("Overflow")
def get_max(hashtabelle: dict, max=None):
ret_val = None
for i in hashtabelle.items():
if ret_val is None or i[1] > ret_val[1]:
ret_val = i
if max is not None and ret_val[1] >= max:
break
return ret_val
adressTable = DirectAddressTable()
adressTable.insert(Slot("key", 2))
adressTable.insert(Slot("key", 3))
adressTable.insert(Slot("ke2", 5))
adressTable.delete("key")
print(adressTable.search("key")[0])
hashTable = HashDirectAddressMultiplication(11, (10, 22, 31, 4, 15, 28, 17, 88, 59))
print(hashTable.payload)
hashTable.insert(10)
hashTable.insert("abc")
print(hashTable.payload)
print(hashTable.search(10))
hashTable.delete("abc")
print(hashTable.payload)