# This file is part of Beremiz
# Copyright (C) 2021: Edouard TISSERANT
# See COPYING file for copyrights details.
# Based on Eelco Hoogendoorn stackoverflow answer about RingBuffer with numpy
class RingBuffer(object):
def __init__(self, width=None, size=65536, padding=None):
self.padding = size if padding is None else padding
shape = (self.size+self.padding,)
self.buffer = np.zeros(shape)
"""this is an O(n) operation"""
data = data[-self.padding:]
if self.remaining < n: self.compact()
self.buffer[self.counter+self.size:][:n] = data
return self.counter if not self.full else self.size
return self.padding-self.counter
"""this is always an O(1) operation"""
return self.buffer[self.counter:][:self.size]
note: only when this function is called, is an O(size) performance hit incurred,
and this cost is amortized over the whole padding space
self.buffer[:self.size] = self.view