-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashPQ.py
319 lines (281 loc) · 8.05 KB
/
HashPQ.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/usr/bin/python
from threading import Event
import time
import Queue
try:
from collections import OrderedDict
except:
# If could not, import it from local directory
from OrderedDict import OrderedDict
DEFAULT_PRIORITY = 0
DEFAULT_KEY = 0
class HashPQ(object):
__slots__ = [ '__capacity',\
'__size',\
'__dic',\
'__lock',\
]
def __new__(cls, *args, **kwargs):
return super(HashPQ, cls).__new__(cls, *args, **kwargs)
def __str__(self):
return self.__dic.__str__()
def __repr__(self):
return self.__dic.__str__()
def __iter__(self):
pass
def __call__(self):
pass
def __init__(self, capacity=None, *args, **kwargs):
self.__capacity = capacity
self.__size = 0
self.__dic = {}
self.__lock = Event()
def qsize(self):
return self.__size
def get(self):
if not self.__dic:
self.__lock.wait()
return get_nowait()
def get_nowait(self):
if not self.__dic:
raise EmptyHashPQ
# sort keys in list
keyList = self.__dic.keys()
keyList.sort()
highestPriority = keyList[-1]
# get high priority list
highestPriorityList = self.__dic[highestPriority]
item = highestPriorityList.popitem(last=False)
self.__size -= 1
if not self.__size:
self.__lock.clear()
#else:
# self.__lock.set()
if not highestPriorityList:
del self.__dic[highestPriority]
return item[1]
def get_by_key(self, key, priority=None):
if not self.__dic:
self.__lock.wait()
value = None
if priority:
try:
value = self.__dic[priority].pop(key)
except:
return None
try:
for priority in self.__dic.keys():
if key in self.__dic[priority]:
priorityList = self.__dic[priority]
value = self.__dic[priority].pop(key)
break
except:
return None
self.__size -= 1
if not self.__size:
self.__lock.clear()
else:
self.__lock.set()
if not self.__dic[priority]:
del self.__dic[priority]
return value
def get_as_list(self):
pass
def get_as_list_nowait(self):
pass
def pick(self):
pass
def pick_nowait(self):
pass
def pick_as_list(self):
# retrun none if hashpq is empty
# FIXME
if not self.__dic:
self.__lock.wait()
queue_list = self.__dic.keys()
queue_list.sort(reverse=True)
item_list = []
for queue in queue_list:
print queue
for item in self.__dic[queue].items():
print item
item_list.append(item)
return item_list
def pick_as_list_nowait(self):
# retrun none if hashpq is empty
# FIXME
if not self.__dic:
raise EmptyHashPQ
queue_list = self.__dic.keys()
queue_list.sort(reverse=True)
item_list = []
for queue in queue_list:
print queue
for item in self.__dic[queue].items():
print item
item_list.append(item)
return item_list
def pick_by_key_nowait(self, key, priority=None):
if not self.__dic:
raise EmptyHashPQ
value = None
if priority:
try:
value = self.__dic[priority][key]
except:
return None
try:
for priority in self.__dic.keys():
if key in self.__dic[priority]:
value = self.__dic[priority][key]
break
except:
return None
return value
def get_dic(self):
return self.__dic
def pick_by_key(self, key, priority=None):
if not self.__dic:
self.__lock.wait()
value = None
if priority:
try:
value = self.__dic[priority][key]
except:
return None
try:
for priority in self.__dic.keys():
if key in self.__dic[priority]:
value = self.__dic[priority][key]
break
except:
return None
return value
def get_by_key_nowait(self, key, priority=None):
if not self.__dic:
raise EmptyHashPQ
value = None
if priority:
try:
value = self.__dic[priority].pop(key)
except:
return None
try:
for priority in self.__dic.keys():
if key in self.__dic[priority]:
value = self.__dic[priority].pop(key)
break
except:
return None
self.__size -= 1
if not self.__size:
self.__lock.clear()
else:
self.__lock.set()
if not self.__dic[priority]:
del self.__dic[priority]
return value
def put(self, item):
if not item:
return False
if self.__size == self.__capacity:
self.__lock.clear()
self.__lock.wait()
return put_nowait()
def put_nowait(self, item):
if not item:
return False
if self.__size == self.__capacity:
raise FullHashPQ
if self.__size == self.__capacity:
raise FullHashPQ
try:
priority = item[0]
key = item[1]
value = item[2]
except IndexError:
try:
priority = DEFAULT_PRIORITY
key = item[0]
value = item[1]
except IndexError:
priority = DEFAULT_PRIORITY
key = DEFAULT_KEY
value = item[0]
except:
return False
if priority not in self.__dic.keys():
self.__dic[priority] = OrderedDict()
if key not in self.__dic[priority]:
self.__size += 1
self.__dic[priority][key] = value
self.__lock.set()
def put_nowait_with_kwa(self, **kwargs):
if not kwargs:
return False
if self.__size == self.__capacity:
raise FullHashPQ
if 'priority' in kwargs:
priority = kwargs['priority']
else:
priority = DEFAULT_PRIORITY
if 'key' in kwargs:
key = kwargs['key']
else:
key = DEFAULT_KEY
if 'value' in kwargs:
value = kwargs['value']
else:
raise Exception
return False
if priority not in self.__dic.keys():
self.__dic[priority] = OrderedDict()
if key not in self.__dic[priority]:
self.__size += 1
self.__dic[priority][key] = value
self.__lock.set()
class HashPQException(Exception):
pass
class EmptyHashPQ(HashPQException):
pass
class FullHashPQ(HashPQException):
pass
class InvalidHashKey(HashPQException):
pass
if __name__ == '__main__':
pq = HashPQ()
pq.put_nowait((1, 3, 4))
pq.put_nowait((1, 4, 3))
pq.put_nowait((1, 4, 6))
pq.put_nowait((1, 4, 6))
pq.put_nowait((4, 4, 6))
pq.put_nowait((1, 4, 6))
pq.put_nowait((3, 4, 6))
pq.put_nowait((1, 4, 6))
pq.put_nowait((5, 4, 6))
pq.put_nowait((1, 4, 6))
pq.put_nowait((1, 4, 6))
pq.put_nowait((5, 4, 6))
pq.put_nowait((8, 4, 7))
pq.put_nowait((3, 6))
pq.put_nowait((2,))
pq.put_nowait(("2",))
pq.put_nowait_with_kwa(priority=3, value=3, key=3)
pq.get_dic()
print "get_as_list:", pq.get_as_list()
print pq.qsize()
print pq
print pq.get_nowait()
print pq
#print pq.get_by_key(3)
#print pq
print pq.get_nowait()
print pq
print pq.get_nowait()
print pq
print pq.get_nowait()
print pq
print pq.get_nowait()
print pq
print pq.get_nowait()
print pq