|
| 1 | +from typing import Generic, TypeVar |
| 2 | + |
| 3 | +T = TypeVar("T") |
| 4 | + |
| 5 | + |
| 6 | +class RingBuffer(Generic[T]): |
| 7 | + def __init__(self, capacity: int) -> None: |
| 8 | + self._capacity = capacity |
| 9 | + self._init_buffer() |
| 10 | + |
| 11 | + def __str__(self) -> str: |
| 12 | + if self.is_empty(): |
| 13 | + return "[]" |
| 14 | + |
| 15 | + if self._tail > self._head: |
| 16 | + buffer_state = self._buffer[self._head : self._tail] |
| 17 | + else: |
| 18 | + buffer_state = self._buffer[self._head :] + self._buffer[: self._tail] |
| 19 | + return str(buffer_state) |
| 20 | + |
| 21 | + def __repr__(self) -> str: |
| 22 | + return self.__str__() |
| 23 | + |
| 24 | + def __len__(self) -> int: |
| 25 | + return self._size |
| 26 | + |
| 27 | + def _init_buffer(self) -> None: |
| 28 | + self._buffer = [None] * self._capacity |
| 29 | + self._head = 0 |
| 30 | + self._tail = 0 |
| 31 | + self._size = 0 |
| 32 | + |
| 33 | + def clear(self) -> None: |
| 34 | + self._init_buffer() |
| 35 | + |
| 36 | + def push(self, item: T) -> None: |
| 37 | + if self.is_full(): |
| 38 | + raise IndexError("Ring buffer is full") |
| 39 | + |
| 40 | + self._buffer[self._tail] = item |
| 41 | + self._tail = (self._tail + 1) % self._capacity |
| 42 | + self._size = min(self._size + 1, self._capacity) |
| 43 | + |
| 44 | + def get(self) -> T: |
| 45 | + if self.is_empty(): |
| 46 | + raise IndexError("Ring buffer is empty") |
| 47 | + |
| 48 | + item = self._buffer[self._head] |
| 49 | + self._head = (self._head + 1) % self._capacity |
| 50 | + self._size -= 1 |
| 51 | + return item |
| 52 | + |
| 53 | + def is_full(self) -> bool: |
| 54 | + return self._size == self._capacity |
| 55 | + |
| 56 | + def is_empty(self) -> bool: |
| 57 | + return self._size == 0 |
| 58 | + |
| 59 | + |
| 60 | +if __name__ == "__main__": |
| 61 | + import unittest |
| 62 | + |
| 63 | + class TestRingBuffer(unittest.TestCase): |
| 64 | + def test_push(self): |
| 65 | + ring_buffer = RingBuffer[int](3) |
| 66 | + ring_buffer.push(7) |
| 67 | + ring_buffer.push(3) |
| 68 | + self.assertEqual("[7, 3]", str(ring_buffer)) |
| 69 | + ring_buffer.push(10) |
| 70 | + self.assertEqual("[7, 3, 10]", str(ring_buffer)) |
| 71 | + |
| 72 | + def test_get(self): |
| 73 | + ring_buffer = RingBuffer[int](3) |
| 74 | + ring_buffer.push(17) |
| 75 | + ring_buffer.push(35) |
| 76 | + self.assertEqual(17, ring_buffer.get()) |
| 77 | + self.assertEqual(35, ring_buffer.get()) |
| 78 | + |
| 79 | + def test_clear(self): |
| 80 | + ring_buffer = RingBuffer[int](3) |
| 81 | + ring_buffer.push(6) |
| 82 | + ring_buffer.push(11) |
| 83 | + self.assertEqual("[6, 11]", str(ring_buffer)) |
| 84 | + ring_buffer.clear() |
| 85 | + self.assertEqual("[]", str(ring_buffer)) |
| 86 | + self.assertEqual(0, len(ring_buffer)) |
| 87 | + |
| 88 | + def test_push_with_full_error(self): |
| 89 | + ring_buffer = RingBuffer[int](1) |
| 90 | + ring_buffer.push(7) |
| 91 | + self.assertRaises(IndexError, ring_buffer.push, 9) |
| 92 | + |
| 93 | + ring_buffer = RingBuffer[int](2) |
| 94 | + ring_buffer.push(23) |
| 95 | + ring_buffer.push(14) |
| 96 | + self.assertRaises(IndexError, ring_buffer.push, 11) |
| 97 | + |
| 98 | + def test_get_with_empty_error(self): |
| 99 | + ring_buffer = RingBuffer[int](1) |
| 100 | + self.assertRaises(IndexError, ring_buffer.get) |
| 101 | + ring_buffer.push(15) |
| 102 | + ring_buffer.get() |
| 103 | + self.assertRaises(IndexError, ring_buffer.get) |
| 104 | + |
| 105 | + def test_size(self): |
| 106 | + ring_buffer = RingBuffer[int](3) |
| 107 | + ring_buffer.push(5) |
| 108 | + self.assertEqual(1, len(ring_buffer)) |
| 109 | + ring_buffer.push(3) |
| 110 | + self.assertEqual(2, len(ring_buffer)) |
| 111 | + ring_buffer.get() |
| 112 | + self.assertEqual(1, len(ring_buffer)) |
| 113 | + |
| 114 | + def test_str(self): |
| 115 | + ring_buffer = RingBuffer[int](3) |
| 116 | + self.assertEqual("[]", str(ring_buffer)) |
| 117 | + ring_buffer.push(10) |
| 118 | + self.assertEqual("[10]", str(ring_buffer)) |
| 119 | + ring_buffer.get() |
| 120 | + ring_buffer.push(11) |
| 121 | + ring_buffer.push(9) |
| 122 | + self.assertEqual("[11, 9]", str(ring_buffer)) |
| 123 | + ring_buffer.push(7) |
| 124 | + self.assertEqual("[11, 9, 7]", str(ring_buffer)) |
| 125 | + |
| 126 | + def test_with_str_type(self): |
| 127 | + ring_buffer = RingBuffer[str](3) |
| 128 | + ring_buffer.push("mug") |
| 129 | + ring_buffer.push("cup") |
| 130 | + self.assertEqual("['mug', 'cup']", str(ring_buffer)) |
| 131 | + |
| 132 | + suite = unittest.TestLoader().loadTestsFromTestCase(TestRingBuffer) |
| 133 | + unittest.TextTestRunner(verbosity=2).run(suite) |
0 commit comments