-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash_table.py
More file actions
83 lines (68 loc) · 2.27 KB
/
hash_table.py
File metadata and controls
83 lines (68 loc) · 2.27 KB
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
from tool_box.array import Array # import Array from array.py
from tool_box.singly_linked_list import SLL # import SLL from singly_linked_list.py
class HashNode:
def __init__(self, key, data):
self.key = key
self.data = data
self.next = None
class HashTable:
""" Closed Addressing: Separate Chaining """
def __init__(self, size):
self.table_size = size
self.table = Array(size)
def __setitem__(self, key, data):
index = self.hash_function(key)
self.create_sll(index)
sll = self.table[index]
sll.add_first(HashNode(key, data))
def __getitem__(self, key):
index = self.hash_function(key)
if self.table[index] is not None:
sll = self.table[index]
for i in sll:
if i.data.key == key:
return i.data.data
raise KeyError("not found")
def __contains__(self, key):
index = self.hash_function(key)
sll = self.table[index]
for i in sll:
if i.data.key == key:
return True
return False
def __iter__(self):
for item in self.table:
if item is not None:
for node in item:
yield node
def __delitem__(self, key):
index = self.hash_function(key)
sll = self.table[index]
for node in sll:
if node.data.key == key:
del sll[node]
def __len__(self):
return self.table_size
def create_sll(self, index):
if self.table[index] is None:
self.table[index] = SLL()
def key_to_int(self, key):
# DJB2
hash_value = 5381
for char in key:
hash_value = (hash_value * 33) + ord(char)
return hash_value
def hash_function(self, key):
integer = self.key_to_int(key)
return integer % self.table_size
def insert_last(self, key, data):
index = self.hash_function(key)
if self.table[index] is None:
self.table[index] = HashNode(key, data)
else:
current = self.table[index]
while current:
if current.key == key:
current.data = data
return
current = current.next