-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmcQueue.py
More file actions
187 lines (149 loc) · 5.09 KB
/
Copy pathmcQueue.py
File metadata and controls
187 lines (149 loc) · 5.09 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
177
178
179
180
181
182
import mcLock
import time
#The lock is temporary, so the lock should be unlock after a moment.
class mcQueue:
def __init__(this , mc , name , maxVolume = 2**32 - 1):
this.name = name
this.index_name = "mc_queue_" + name +"_index_"
this.mcQueue_top = "mc_queue_top_" + name
this.mcQueue_tail = "mc_queue_tail_" + name
this.mcQueue_lock = "mc_queue_" + name
this.mc = mcLock.mcLock( mc , this.mcQueue_lock )
this.maxVolume = maxVolume + 1
#initialize mc
this.mc.add( this.mcQueue_top , 0 )
this.mc.add( this.mcQueue_tail , 0 )
def isEmpty(this):
try:
ret = this.mc.get( this.mcQueue_top ) == this.mc.get( this.mcQueue_tail )
return ret
except Exception , e:
print e
return True
def getSize(this):
try:
topindex = this.mc.get( this.mcQueue_top )
tailindex = this.mc.get( this.mcQueue_tail )
if topindex == None or tailindex == None:
return -1
size = (tailindex - topindex + this.maxVolume) % this.maxVolume
return size
except Exception , e:
print e
return -1
def getTopIndex(this):
try:
index = this.mc.get( this.mcQueue_top )
if index == None : index = -1
return index
except Exception , e:
print e
return -1
def getTailIndex(this):
try:
index = this.mc.get( this.mcQueue_tail )
if index == None : index = -1
return index
except Exception , e:
print e
return -1
def getTop(this):
try:
topindex = this.mc.get( this.mcQueue_top )
if topindex == None:
return None
topindex = ( topindex + 1 ) % this.maxVolume
ret = this.mc.get( this.index_name + str(topindex) )
return ret
except Exception , e:
print e
return None
def getTail(this):
try:
tailindex = this.mc.get( this.mcQueue_tail )
if tailindex == None:
return None
ret = this.mc.get( this.index_name + str(tailindex) )
return ret
except Exception , e:
print e
return None
def push(this , val):
try:
this.mc.lock()
topindex = this.mc.get( this.mcQueue_top )
tailindex = this.mc.get( this.mcQueue_tail )
if topindex == None or tailindex == None:
return False
tailindex = ( tailindex + 1 ) % this.maxVolume
if( topindex == tailindex ):
this.mc.unlock()
return False
else:
this.mc.set( this.mcQueue_tail , tailindex )
this.mc.set( this.index_name + str(tailindex) , val )
this.mc.unlock()
return True
except Exception , e:
print e
this.mc.unlock()
return False
def pop(this):
if this.isEmpty():
return None
try:
this.mc.lock()
topindex = this.mc.get( this.mcQueue_top )
tailindex = this.mc.get( this.mcQueue_tail )
if topindex == None or tailindex == None:
return None
if( topindex == tailindex ):
this.mc.unlock()
return None
topindex = ( topindex + 1 ) % this.maxVolume
ret = this.mc.get( this.index_name + str(topindex) )
this.mc.delete(this.index_name + str(topindex))
this.mc.set( this.mcQueue_top , topindex )
this.mc.unlock()
return ret
except Exception , e:
print e
this.mc.unlock()
return None
def popif(this , val):
if this.isEmpty():
return False
try:
this.mc.lock()
topindex = this.mc.get( this.mcQueue_top )
tailindex = this.mc.get( this.mcQueue_tail )
if topindex == None or tailindex == None:
return False
if( topindex == tailindex ):
this.mc.unlock()
return False
topindex = ( topindex + 1 ) % this.maxVolume
topVal = this.mc.get( this.index_name + str(topindex) )
if topVal == val:
this.mc.delete(this.index_name + str(topindex))
this.mc.set( this.mcQueue_top , topindex )
ret = True
else:
ret = False
this.mc.unlock()
return ret
except Exception , e:
print e
this.mc.unlock()
return False
def clear(this):
try:
while this.pop() != None: pass
this.mc.lock()
this.mc.set( this.mcQueue_top , 0 )
this.mc.set( this.mcQueue_tail , 0 )
this.mc.unlock()
except Exception , e:
print e
this.mc.unlock()
return None