-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhashy_perfection_table.py
158 lines (129 loc) · 4.26 KB
/
hashy_perfection_table.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
""" Hash Table ADT using a Perfect Hash Table """
from __future__ import annotations
__author__ = 'Brendon Taylor'
__since__ = '22/08/2024'
from data_structures.referential_array import ArrayR
from typing import Generic, Union, TypeVar
K = TypeVar('K')
V = TypeVar('V')
class HashyPerfectionTable(Generic[K, V]):
"""
HashyPerfectionTable holds a perfect hash function for a small set of known keys.
The expected keys can be found within constants.py in the PlayerStats enum.
Type Arguments:
- K: Key Type. In most cases should be string.
Otherwise `hash` should be overwritten.
- V: Value Type.
Unless stated otherwise, all methods have O(1) complexity.
"""
def __init__(self) -> None:
"""
Initialise the Hash Table.
Note: Our default table size 13, if you increase it to 19, you will not get full marks for approach.
"""
self.array: ArrayR[Union[tuple[K, V], None]] = ArrayR(13)
self.count: int = 0
def hash(self, key: K) -> int:
"""
Hash a key for insert/retrieve/update into the hashtable.
Complexity:
Best Case Complexity:
Worst Case Complexity:
"""
raise NotImplementedError
def __len__(self) -> int:
"""
Returns number of elements in the hash table
"""
return self.count
def keys(self) -> ArrayR[K]:
"""
Returns all keys in the hash table.
:complexity: O(N) where N is self.table_size.
"""
res = ArrayR(len(self.array))
i = 0
for x in range(len(self)):
if self.array[x] is not None:
res[i] = self.array[x][0]
i += 1
return res
def values(self) -> ArrayR[V]:
"""
Returns all values in the hash table.
:complexity: O(N) where N is self.table_size.
"""
res = ArrayR(len(self.array))
i = 0
for x in range(len(self)):
if self.array[x] is not None:
res[i] = self.array[x][1]
i += 1
return res
def __contains__(self, key: K) -> bool:
"""
Checks to see if the given key is in the Hash Table
Complexity:
Best Case Complexity: O(self[key])
Worst Case Complexity: O(self[key])
"""
try:
_ = self[key]
except KeyError:
return False
else:
return True
def __getitem__(self, key: K) -> V:
"""
Get the value at a certain key
Complexity:
Best Case Complexity: O(hash)
Worst Case Complexity: O(hash)
Raises:
KeyError: When the key doesn't exist.
"""
position: int = self.hash(key)
if self.array[position] is None:
raise KeyError(f"{key} not found")
return self.array[position][1]
def __setitem__(self, key: K, data: V) -> None:
"""
Set an (key, value) pair in our hash table.
Complexity:
Best Case Complexity: O(hash)
Worst Case Complexity: O(hash)
Raises:
KeyError: When the key doesn't exist.
"""
position: int = self.hash(key)
if self.array[position] is None:
self.count += 1
self.array[position] = (key, data)
def __delitem__(self, key: K) -> None:
"""
Deletes a (key, value) pair in our hash table.
Complexity:
Best Case Complexity: O(hash)
Worst Case Complexity: O(hash)
Raises:
KeyError: When the key doesn't exist.
"""
position: int = self.hash(key)
self.array[position] = None
self.count -= 1
def is_empty(self) -> bool:
return self.count == 0
def is_full(self) -> bool:
return self.count == len(self.array)
def __str__(self) -> str:
"""
Complexity:
Best Case Complexity: O(N) where N is the length of the array.
Worst Case Complexity: O(N * (str(key) + str(value))) where N is the length of the array.
"""
result: str = ""
for item in self.array:
if item is not None:
(key, value) = item
result += "(" + str(key) + "," + str(value) + ")\n"
return result