pyppin.containers.ring_buffer¶
Classes
|
A RingBuffer is a ring, or circular, array of fixed size. |
- class pyppin.containers.ring_buffer.RingBuffer(capacity: int, value: Optional[Iterable[T]] = None)[source]¶
Bases:
MutableSequence
[T
]A RingBuffer is a ring, or circular, array of fixed size.
It’s easiest to imagine as a literally circular array with [capacity] slots in it. If you append a further item, it starts to overwrite all items. Thus its size is always <= its capacity.
This is a very useful data structure for when you want to keep the last N results that you’ve seen, or similar windowing operations.
If you need to reason about complicated slice sets or the like with a ring buffer, remember this simple mnemonic: if X is a ring buffer and A is an ordinary list, which start out with the same contents, then
setoperation(X) = setoperation(A)[-X.capacity:]
i.e., it should always give you the same thing that you would get from a list, just truncated to the most recent <capacity> elements.
RingBuffer does not support setting of extended slices, because this is a rarely-used functionality that’s a lot of work to get right.
- Parameters
capacity – The size of the ring buffer.
value – (Optional) initial values with which to populate the buffer.