-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathringbuf.py
More file actions
49 lines (38 loc) · 1.21 KB
/
ringbuf.py
File metadata and controls
49 lines (38 loc) · 1.21 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
class ByteRing:
SIZE = 32
def __init__(self):
self.reset()
def get_byte(self, offset):
return self.buf[offset]
def reset(self):
self.buf = bytearray(self.SIZE)
self.head = 0 # next write
self.tail = 0 # next read
def push(self, b: int) -> bool:
"""Push one byte. Returns False if full."""
nxt = (self.head + 1) % self.SIZE
if nxt == self.tail:
return False # full
self.buf[self.head] = b & 0xFF
self.head = nxt
return True
def pop(self) -> int | None:
"""Pop one byte. Returns None if empty."""
if self.head == self.tail:
return None # empty
b = self.buf[self.tail]
self.tail = (self.tail + 1) % self.SIZE
return b
def __len__(self):
if self.head >= self.tail:
return self.head - self.tail
else:
return self.SIZE - (self.tail - self.head)
def empty(self) -> bool:
return self.head == self.tail
def full(self) -> bool:
return (self.head + 1) % self.SIZE == self.tail
def get_head(self) -> int:
return self.head
def get_tail(self) -> int:
return self.tail