-
Notifications
You must be signed in to change notification settings - Fork 0
/
ringbuffer.py
51 lines (37 loc) · 1006 Bytes
/
ringbuffer.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
#
# Minimal ring buffer implementation
#
class RingBuffer:
def __init__(self, size):
self.size = size
self.buffer = {}
self.head = 0
self.tail = 0
self.length = 0
self.clear()
def put(self, item):
self.buffer[self.tail] = item
self.tail = (self.tail + 1) % len(self.buffer)
if self.length == len(self.buffer):
self.head = self.tail
else:
self.length += 1
def get(self):
result = self.buffer[self.head]
self.head = (self.head + 1) % len(self.buffer)
self.length -= 1
return result
def clear(self):
self.tail = 0
self.head = 0
self.length = 0
for i in range(self.size):
self.buffer[i] = None
def isempty(self):
if self.length == 0:
return 1
return 0
def isfull(self):
if self.length == len(self.buffer):
return 1
return 0