pyppin.containers.ring_buffer

[View Source]

Classes

RingBuffer(capacity[, value])

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.

property capacity: int[source]
insert(index: int, value: T) None[source]

The insert() operation should insert (value) at position (index), pushing higher-indexed items to the right. Because this is a ring buffer, this means that the lowest-indexed item may well fall off.

clear() None -- remove all items from S[source]
append(value: T) None[source]

S.append(value) – append value to the end of the sequence

extend(values: Iterable[T]) None[source]

S.extend(iterable) – extend sequence by appending elements from the iterable