-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnativeDictionary.py
More file actions
80 lines (68 loc) · 2.28 KB
/
nativeDictionary.py
File metadata and controls
80 lines (68 loc) · 2.28 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
import hashlib
import math
class NativeDictionary:
def __init__(self, sz):
self.size = sz
self.slots = [None] * self.size
self.values = [None] * self.size
self.step = 1
def hash_fun(self, key):
'''
в качестве key поступают строки!
всегда возвращает корректный индекс слота
'''
hash = hashlib.sha1()
hash.update(key.encode())
hash = int(hash.hexdigest()[:math.ceil(
self.size / 16)], 16) # hex to int
return hash % self.size
def is_key(self, key):
'''
возвращает True если ключ имеется,
иначе False
'''
# return self.slots[self.seek_slot(key)] != None
return self.slots[self.seek_slot(key)] == key
def put(self, key, value):
'''
гарантированно записываем
значение value по ключу key.
при переполнении массива слотов вызывает ошибку TypeError - надо делать resize????
'''
slot = self.seek_slot(key)
# if slot == None:
# return None
self.slots[slot] = key
self.values[slot] = value
return slot
def get(self, key):
'''
возвращает value для key,
или None если ключ не найден
'''
slot = self.seek_slot(key)
if self.slots[slot] == key:
return self.values[slot]
else:
return None
def seek_slot(self, key):
'''
находит индекс пустого слота для значения
'''
slot = self.hash_fun(key)
max_loop = 10 * self.size
while self.slots[slot] != None and self.slots[slot] != key:
# repair collision
max_loop -= 1
if max_loop < 0:
return None
# if abs(self.step) >= self.size:
# return None
slot += self.step
if slot >= self.size:
slot -= self.size
return slot
def get_slots(self):
return self.slots
def get_values(self):
return self.values