Search code examples
pythonbufferconstant-time

Efficient circular buffer with constant-time access


In a machine learning project written in python, I need an efficient circular buffer like collections.deque but with constant-time access to any element like numpy.array. The problem is that deque is apparently a linked list. Is there something efficient readily implemented in a python library that I am not aware of for this use-case please?

I could simply have a modified fixed-size numpy.array with a moving 0 index in my use-case, I guess, but that's for my python culture as it is not the first time I need something like that.


Solution

  • The numpy_ringbuffer module at https://pypi.org/project/numpy-ringbuffer/ uses buffers backed by a NumPy array. It should satisfy your efficiency requirements.