-
-
Notifications
You must be signed in to change notification settings - Fork 50.3k
Expand file tree
/
Copy pathcoalesced_hashing.py
More file actions
176 lines (143 loc) · 5.21 KB
/
coalesced_hashing.py
File metadata and controls
176 lines (143 loc) · 5.21 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env python3
"""
Coalesced hashing (hybrid of open addressing + chaining inside the table).
Reference: [https://en.wikipedia.org/wiki/Hash_table#Coalesced_hashing](https://en.wikipedia.org/wiki/Hash_table#Coalesced_hashing)
"""
from __future__ import annotations
from collections.abc import Iterator, MutableMapping
from dataclasses import dataclass
from typing import Generic, TypeVar
KEY = TypeVar("KEY")
VAL = TypeVar("VAL")
@dataclass(slots=True)
class _Node(Generic[KEY, VAL]): # noqa: UP046
key: KEY
val: VAL
next: int # -1 means end of chain
class CoalescedHashMap(MutableMapping[KEY, VAL]):
"""
Coalesced hashing stores the chain pointers inside the array.
This implementation uses:
- Primary area: all indices, chaining occurs via `next` pointers.
- Free slot choice: highest-index free slot (easy to explain + deterministic).
>>> ch = CoalescedHashMap(5)
>>> ch["a"] = 1
>>> ch["b"] = 2
>>> ch["a"]
1
>>> len(ch)
2
"""
def __init__(self, capacity: int = 8, capacity_factor: float = 0.8) -> None:
if capacity < 1:
raise ValueError("capacity must be >= 1")
if not (0.0 < capacity_factor < 1.0):
raise ValueError("capacity_factor must be between 0 and 1")
self._capacity_factor = capacity_factor
self._table: list[_Node[KEY, VAL] | None] = [None] * capacity
self._len = 0
def _home(self, key: KEY) -> int:
return hash(key) % len(self._table)
def _is_full(self) -> bool:
return self._len >= int(len(self._table) * self._capacity_factor)
def _find_free_from_end(self) -> int:
for i in range(len(self._table) - 1, -1, -1):
if self._table[i] is None:
return i
return -1
def _resize(self, new_capacity: int) -> None:
old_items = list(self.items())
self._table = [None] * new_capacity
self._len = 0
for k, v in old_items:
self[k] = v
def __setitem__(self, key: KEY, val: VAL) -> None:
if self._is_full():
self._resize(len(self._table) * 2)
home = self._home(key)
node = self._table[home]
if node is None:
self._table[home] = _Node(key, val, -1)
self._len += 1
return
# Search chain for update.
cur = home
while True:
# Explicitly type the current node to satisfy mypy
current_node = self._table[cur]
if current_node is None:
# Should not happen if logic is correct, but handles None safety
break
if current_node.key == key:
current_node.val = val
return
if current_node.next == -1:
break
cur = current_node.next
# Insert new node at a free slot and link it.
free = self._find_free_from_end()
if free == -1:
self._resize(len(self._table) * 2)
self[key] = val
return
self._table[free] = _Node(key, val, -1)
# Link the previous end of chain to the new free slot
# We re-fetch the node at 'cur' to be safe
if (tail_node := self._table[cur]) is not None:
tail_node.next = free
self._len += 1
def __getitem__(self, key: KEY) -> VAL:
home = self._home(key)
cur = home
while cur != -1:
node = self._table[cur]
if node is None:
break
if node.key == key:
return node.val
cur = node.next
raise KeyError(key)
def __delitem__(self, key: KEY) -> None:
home = self._home(key)
prev = -1
cur = home
while cur != -1:
node = self._table[cur]
if node is None:
break
if node.key == key:
# If deleting head: copy next node into home if exists
# (keeps chains valid).
if prev == -1:
if node.next == -1:
self._table[cur] = None
else:
nxt = node.next
next_node = self._table[nxt]
# Must assert next_node is not None for mypy
if next_node is not None:
self._table[cur] = _Node(
next_node.key,
next_node.val,
next_node.next,
)
self._table[nxt] = None
else:
# Update previous node's next pointer
prev_node = self._table[prev]
if prev_node is not None:
prev_node.next = node.next
self._table[cur] = None
self._len -= 1
return
prev, cur = cur, node.next
raise KeyError(key)
def __iter__(self) -> Iterator[KEY]:
for node in self._table:
if node is not None:
yield node.key
def __len__(self) -> int:
return self._len
if __name__ == "__main__":
import doctest
doctest.testmod()