[SciPy-user] array like circular buffer?
Bill Dandreta
wjdandreta at att.net
Thu Oct 27 17:22:21 CDT 2005
John Hunter wrote:
> John> I would like to have a fixed size array like data structure
> John> that has circular buffer semantics. Eg, when empty you can
> John> call a "put" method that starts in the beginning and fills
> John> the sucker up, and when you overflow the end it starts
> John> pushing the data from the 0 index off the stack -- FIFO.
> John> Ideally, it would support slicing and friends.
>
> skip> How about subclassing list and proving relevant methods that
> skip> adjusted all indices by modulo the list length?
>
>I was hoping/guessing someone had already done it for me <win
>
I think it is called a circular queue, here is a generator version:
>>> def circularQueue(X):
... while True:
... x=X.pop(0)
... X.append(x)
... yield x
...
>>>
>>> X=['a','b','c']
>>> for x in circularQueue(X):
>>> i=0 ... print x,X
... i+=1
... if i>9:break
...
a ['b', 'c', 'a']
b ['c', 'a', 'b']
c ['a', 'b', 'c']
a ['b', 'c', 'a']
b ['c', 'a', 'b']
c ['a', 'b', 'c']
a ['b', 'c', 'a']
b ['c', 'a', 'b']
c ['a', 'b', 'c']
a ['b', 'c', 'a']
>>>
Bill
More information about the SciPy-user
mailing list