-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashtable.py
More file actions
87 lines (70 loc) · 2.16 KB
/
Copy pathhashtable.py
File metadata and controls
87 lines (70 loc) · 2.16 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 data_structures.linked_list import LinkedList
class Hashtable:
"""
A hashtable implementation.
"""
def __init__(self, size=1024):
"""
Initializes the hashtable with a specified size.
"""
self._size = size
self._buckets = [None] * size
def _hash(self, key):
"""
Computes the hash value for the given key.
"""
index = sum(ord(char) for char in key)
index = (index * 599) % self._size
return index
def set(self, key, value):
"""
Sets the key-value pair in the hashtable.
"""
index = self._hash(key)
bucket = self._buckets[index]
if bucket is None:
bucket = LinkedList()
self._buckets[index] = bucket
current = bucket.head
while current:
if current.value[0] == key:
current.value[1] = value # Update value
return
current = current.next
bucket.insert([key, value]) # No existing key found, add new entry
def get(self, key):
"""
Retrieves the value associated with the given key.
"""
index = self._hash(key)
bucket = self._buckets[index]
if bucket is None:
return None
current = bucket.head
while current:
if current.value[0] == key:
return current.value[1]
current = current.next
return None
def keys(self):
"""
Returns a list of all keys in the hashtable.
"""
key_list = []
for bucket in self._buckets:
current = bucket.head if bucket else None
while current:
key_list.append(current.value[0])
current = current.next
return key_list
def has(self, key):
"""
Checks if the given key exists in the hashtable.
"""
for bucket in self._buckets:
current = bucket.head if bucket else None
while current:
if current.value[0] == key:
return True
current = current.next
return False