-
Notifications
You must be signed in to change notification settings - Fork 1
/
Dict_mutable.py
executable file
·251 lines (228 loc) · 6.9 KB
/
Dict_mutable.py
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
from typing import Callable
from typing import Any
class Entry:
"""Defines a dictionary element that consists of keywords and values"""
def __init__(self, key: int, value: int) -> None:
"""
Create an instance of Entry
:param key:int
:param value:int
"""
self.key = key
self.value = value
class Dict:
""" Mutable dictionary based on hash-map, open address implementation """
def __init__(self) -> None:
"""Create an instance of dictionary"""
# The length of the hashTable
self.len = 1000
# HashTable is a list of dictionaries to store
self.hashTable: list[Any] = [None for i in range(self.len)]
# Dictionary size
self.dict_size = 0
def get(self, key: int) -> Any:
"""
getting value by key
:param key: int
:return: Any
"""
if self.find(key) != -1:
return self.hashTable[self.find(key)].value
else:
print("The key element does not exist")
return -1
def member(self, key: int) -> int:
"""
Is a member of a dictionary, 1 means existence, 0 means non-existence
:param key: int
:return: int
"""
if self.find(key) != -1:
return 1
else:
return 0
def add(self, item: Any) -> None:
"""
Add a new element,use linear detection for conflicts
:param item: Any
:return: None
"""
if item is not None:
j = item.key % self.len
if self.hashTable[j] is None:
self.hashTable[j] = item
self.dict_size += 1
else:
i = 0
while self.hashTable[j] is not None:
j = (j + 1) % self.len
i += 1
if i == self.len:
print("Failed to insert "
"because hashTable is full")
break
self.hashTable[j] = item
self.dict_size += 1
def find(self, key: int) -> int:
"""
Find the hash table position of the element
:param key: int
:return: int
"""
j = key % self.len
if self.hashTable[j] is not None:
if self.hashTable[j].key == key:
return j
else:
i = 0
while self.hashTable[j] != key:
j = (j + 1) % self.len
i += 1
if i == self.len:
print("There is no element "
"with the value key in the dictionary")
return -1
return j
return -1
def remove(self, key: int) -> None:
"""
Remove an element by key for dictionaries
:param key: int
:return: None
"""
if self.find(key) != -1:
index = self.find(key)
self.hashTable[index] = None
self.dict_size -= 1
else:
print("element doesn't exist")
def size(self) -> int:
"""
Gets the size of the dictionary
:return: int
"""
return self.dict_size
def to_list(self) -> dict[int, int]:
"""
Conversion to built-in list
:return:dict[int,int]
"""
res = {}
i = 0
while i < self.len:
if self.hashTable[i] is not None:
res[self.hashTable[i].key] = self.hashTable[i].value
i += 1
return res
def from_list(self, a: dict[int, int]) -> None:
"""
Conversion from built-in list
:param a: Dict
:return: None
"""
for key, value in a.items():
x = Entry(key, value)
self.add(x)
def filter(self, p: Callable[[Any], Any]) -> None:
"""
Filter dictionary by specific predicate
:param p:Callable[[Any],Any]
:return: None
"""
i = 0
while i < self.len:
if self.hashTable[i] is None:
i += 1
else:
if not p(self.hashTable[i].value):
self.hashTable[i] = None
i += 1
else:
i += 1
def map(self, f: Callable[[Any], Any]) -> None:
"""
Map structure by specific function
:param f: Callable[[Any], Any]
:return: None
"""
i = 0
while i < self.len:
if self.hashTable[i] is not None:
self.hashTable[i].value = f(self.hashTable[i].value)
i += 1
else:
i += 1
def reduce(self, f: Callable[[Any, int], Any], initial_state: int) -> int:
"""
Reduce process elements and build a value by the function
:param f: Callable[[Any, int], Any]
:param initial_state: int
:return: int
"""
state = initial_state
i = 0
while i < self.len:
if self.hashTable[i] is not None:
state = f(state, self.hashTable[i].value)
i += 1
else:
i += 1
return state
def empty(self) -> None:
"""
Data structure should be a monoid and implement empty
:return: None
"""
self.hashTable = [None for i in range(self.len)]
def concat(self, dict_x: 'Dict') -> 'Dict':
"""
Data structure should be a monoid and implement concat
:param dict_x: Dict
:return: Dict
"""
self.dict_size = self.dict_size + dict_x.size()
self.len = self.len + dict_x.len
d1 = self.hashTable
self.hashTable = [None for i in range(self.len)]
for x in d1:
if x is not None:
self.add(x)
for y in dict_x.hashTable:
if y is not None:
self.add(y)
return self
def __iter__(self) -> 'Next':
"""
Data structure should be an iterator
:return:Next
"""
return Next(self.hashTable)
class Next:
"""To define a multi-iteration type,
__iter__ is required to return a new iterator,
not self, that is, not its own iterator."""
def __init__(self, hashTable: list[int]) -> None:
"""
Create an instance of Next
:param hashTable: list[int]
"""
self.hashTable = hashTable
self.current = 0
def __iter__(self) -> 'Next':
"""
Implement iter(self).
:return: 'Next'
"""
return self
def __next__(self) -> int:
"""
Implement next(self).
:return: int
"""
if self.current < len(self.hashTable):
x = self.current
item = self.hashTable[x]
self.current += 1
return item
else:
raise StopIteration