Level 1 Fixed-capacity ring buffer
Build RingBuffer(capacity), a FIFO queue stored in a list of exactly capacity slots allocated once, with a read index, a write index and a size (or another way to tell "full" from "empty", e.g. a size counter). Indexes wrap around modulo capacity.
try_push(item) -> bool: appenditemat the back. If the buffer is full, leave it unchanged and returnFalse(the overflow policy here is "reject the newest").try_pop(): remove and return the front item. RaiseIndexErrorif the buffer is empty.__len__(),is_empty(),is_full(), andcapacityas an attribute.
capacity >= 1. Items can be anything, including None. Every operation must be O(1): no list.pop(0), no insert(0, ...), no shifting.
rb = RingBuffer(2)
rb.try_push("a") # True
rb.try_push("b") # True
rb.try_push("c") # False (full)
rb.try_pop() # "a"
rb.try_push("c") # True (wraps around into slot 0)
[rb.try_pop(), rb.try_pop()] # ["b", "c"]
rb.try_pop() # IndexError