-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathhash_chapter3_class_impl.py
More file actions
136 lines (101 loc) · 3.94 KB
/
Copy pathhash_chapter3_class_impl.py
File metadata and controls
136 lines (101 loc) · 3.94 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
from common import DUMMY, EMPTY
from dict_reimpl_common import BaseDictImpl, Slot
class AlmostPythonDictBase(BaseDictImpl):
START_SIZE = 8
def __init__(self, pairs=None):
BaseDictImpl.__init__(self)
self._keys_set = set()
if pairs:
for k, v in pairs:
self[k] = v
def lookdict(self, key):
hash_code = hash(key)
idx = hash_code % len(self.slots)
while self.slots[idx].key is not EMPTY:
if self.slots[idx].hash_code == hash_code and self.slots[idx].key == key:
return idx
idx = (idx + 1) % len(self.slots)
raise KeyError()
def __getitem__(self, key):
idx = self.lookdict(key)
return self.slots[idx].value
def __delitem__(self, key):
idx = self.lookdict(key)
self.used -= 1
self.slots[idx].key = DUMMY
self.slots[idx].value = EMPTY
self._keys_set.remove(key)
def resize(self):
old_slots = self.slots
new_size = self.find_nearest_size(2 * self.used)
self.slots = [Slot() for _ in range(new_size)]
for slot in old_slots:
if slot.key is not EMPTY and slot.key is not DUMMY:
idx = slot.hash_code % len(self.slots)
while self.slots[idx].key is not EMPTY:
idx = (idx + 1) % len(self.slots)
self.slots[idx] = Slot(slot.hash_code, slot.key, slot.value)
self.fill = self.used
def keys(self):
return self._keys_set
def __len__(self):
return len(self.keys())
class AlmostPythonDictImplementationRecycling(AlmostPythonDictBase):
def __setitem__(self, key, value):
hash_code = hash(key)
idx = hash_code % len(self.slots)
target_idx = None
while self.slots[idx].key is not EMPTY:
if self.slots[idx].hash_code == hash_code and self.slots[idx].key == key:
target_idx = idx
break
if target_idx is None and self.slots[idx].key is DUMMY:
target_idx = idx
idx = (idx + 1) % len(self.slots)
if target_idx is None:
target_idx = idx
if self.slots[target_idx].key is EMPTY:
self.used += 1
self.fill += 1
elif self.slots[target_idx].key is DUMMY:
self.used += 1
self.slots[target_idx] = Slot(hash_code, key, value)
if self.fill * 3 >= len(self.slots) * 2:
self.resize()
self._keys_set.add(key)
class AlmostPythonDictImplementationNoRecycling(AlmostPythonDictBase):
def __setitem__(self, key, value):
hash_code = hash(key)
idx = hash_code % len(self.slots)
target_idx = None
while self.slots[idx].key is not EMPTY:
if self.slots[idx].hash_code == hash_code and\
self.slots[idx].key == key:
target_idx = idx
break
idx = (idx + 1) % len(self.slots)
if target_idx is None:
target_idx = idx
if self.slots[target_idx].key is EMPTY:
self.used += 1
self.fill += 1
self.slots[target_idx] = Slot(hash_code, key, value)
if self.fill * 3 >= len(self.slots) * 2:
self.resize()
self._keys_set.add(key)
class AlmostPythonDictImplementationNoRecyclingSimplerVersion(AlmostPythonDictBase):
def __setitem__(self, key, value):
hash_code = hash(key)
idx = hash_code % len(self.slots)
while self.slots[idx].key is not EMPTY:
if self.slots[idx].hash_code == hash_code and\
self.slots[idx].key == key:
break
idx = (idx + 1) % len(self.slots)
if self.slots[idx].key is EMPTY:
self.used += 1
self.fill += 1
self.slots[idx] = Slot(hash_code, key, value)
if self.fill * 3 >= len(self.slots) * 2:
self.resize()
self._keys_set.add(key)