-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhashing.py
More file actions
125 lines (87 loc) · 3.84 KB
/
hashing.py
File metadata and controls
125 lines (87 loc) · 3.84 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
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
"""Module that holds classes related to hashing.
This module contains the `HashTable` class. `HashTable` represents a hash table.
Examples:
Creating a `HashTable`, adding key-value pairs, and retrieving values:
t = HashTable()
t.insert('a', 1)
t.insert('b', 2)
t.insert('c', 3)
x = t.get_value('a')
Removing elements from a `HashTable`:
t.delete('a')
"""
from dalpy.sets import Set
class HashTable:
"""Represents a hash table that stores key-value pairs.
Examples:
To initialize a `HashTable`:
t = HashTable()
To insert key-value pairs:
t.insert('a', 1)
t.insert('b', 2)
To check if `t` contains a key:
if t.contains_key('a'):
# Do something
To remove a key-value pair:
t.delete('a')
To retrieve the keys of a `HashTable`:
keys = t.keys()
"""
def __init__(self):
"""Initializes an empty `HashTable` in `O(1)` time."""
self.__table = dict()
def insert(self, key, value):
"""Inserts a key-value pair into this `HashTable`.
One should assume that this runs in `O(1)` time w/r/t the number of entries in this `HashTable`.
Args:
key: A key. If its a new key, then a new entry will be added to this `HashTable` a new pair is added. This
can be of any type.
value: A value to be associated with `key`. If `key` is already in this `HashTable`, then it will now have
`value` associated with it and the number of entries in the `HashTable` will not change. This can
be of any type including `None`.
"""
self.__table[key] = value
def contains_key(self, key):
"""Returns whether this `HashTable` contains a particular key.
One should assume that this runs in `O(1)` time w/r/t the number of entries in this `HashTable`.
Args:
key: The key being searched.
Returns:
`True` if `key` is in this `HashTable` and `False` otherwise.
"""
return key in self.__table
def get_value(self, key):
"""Gets the value associated with a key in the `HashTable`.
One should assume that this runs in `O(1)` time w/r/t the number of entries in this `HashTable`.
Args:
key: The key whose value is being retrieved. This can be of any type.
Returns:
The value associated with `key` if `key` is in this `HashTable`.
Raises:
KeyError: If `key` is not in this `HashTable`.
"""
if key not in self.__table:
raise KeyError(f'{key} not in table')
return self.__table[key]
def delete(self, key):
"""Removes the key-value pair with a particular key in the `HashTable`.
One should assume that this runs in `O(1)` time w/r/t the number of entries in this `HashTable`.
Args:
key: The key specifying which key-value pair in the table should be removed. This can be of any type.
Returns:
The value that was previously associated with `key` if `key` is in this `HashTable` (before the key-value
pair was removed).
Raises:
KeyError: If `key` is not in this `HashTable`.
"""
if key not in self.__table:
raise KeyError(f'cannot delete {key} as it does not exist in table')
return self.__table.pop(key)
def keys(self):
"""Returns a `Set` of keys in the `HashTable`.
Note that the returned `Set` will become invalid when new keys are added or removed. One should assume that this
runs in `O(n)` time where `n` is the number of entries in this `HashTable`.
Returns:
A `Set` of keys in the `HashTable`.
"""
return Set(*self.__table)