-
Notifications
You must be signed in to change notification settings - Fork 2
/
circularQueue.py
74 lines (64 loc) · 2.02 KB
/
circularQueue.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
# circularQueue.py
import uasyncio as asyncio
import canmessage
class circularQueue:
def __init__(self, capacity: int) -> None:
self.capacity = capacity
self.queue = [None] * capacity
self.tail = -1
self.head = 0
self.size = 0
self.hwm = 0
self.dropped = 0
self.puts = 0
self.gets = 0
self.lock = asyncio.Lock()
async def available(self) -> bool:
await self.lock.acquire()
size = self.size
self.lock.release()
return size > 0
async def enqueue(self, item: canmessage.canmessage) -> None:
if self.size == self.capacity:
self.dropped = self.dropped + 1
print('queue is full')
else:
await self.lock.acquire()
self.tail = (self.tail + 1) % self.capacity
self.queue[self.tail] = item
self.size = self.size + 1
self.hwm = self.hwm + 1 if self.size > self.hwm else self.hwm
self.puts += 1
self.lock.release()
# print('enqueued new message')
async def dequeue(self) -> canmessage.canmessage | None:
if self.size == 0:
print('nothing to dequeue')
return None
else:
await self.lock.acquire()
tmp = self.queue[self.head]
self.queue[self.head] = None
self.head = (self.head + 1) % self.capacity
self.size = self.size - 1
self.gets += 1
self.lock.release()
# print('item dequeued')
return tmp
# def peek(self):
# if self.size == 0:
# return None
# else:
# return self.queue[self.head]
# def empty(self):
# self.tail = -1
# self.head = 0
# self.size = 0
# self.hwm = 0
# self.dropped = 0
# def display(self):
# index = self.head
#
# for i in range(self.size):
# print(self.queue[index])
# index = (index + 1) % self.capacity