Package PyFoam :: Package Basics :: Module RingBuffer
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.Basics.RingBuffer

 1  #  ICE Revision: $Id$  
 2  """A ring-buffer data structure""" 
 3   
4 -class RingBuffer(object):
5 """A data structure that stores a number N of elements. The 6 N+1-element overwrites the first and so on ....""" 7
8 - def __init__(self,nr=1000):
9 """ 10 @param nr: Number of elements to store 11 """ 12 self.nr=nr 13 self.list=[None]*nr 14 self.point=0 15 self.full=False
16
17 - def insert(self,dings):
18 """ Inserts am element into the ring-buffer 19 """ 20 # print "Inserting at",self.point,":",dings 21 self.list[self.point]=dings 22 self.point+=1 23 if self.point==self.nr: 24 self.point=0 25 self.full=True
26
27 - def last(self):
28 """@return: the latest element in the buffer, None if 29 nothing was inserted into the buffer""" 30 if self.point>0: 31 return self.list[self.point-1] 32 elif self.full: 33 return self.list[-1] 34 else: 35 return None
36
37 - def dump(self):
38 """@return: A list with all the values in the ring buffer in the correct order 39 (starting with the oldest)""" 40 result=[] 41 42 if self.full: 43 for i in range(self.point,self.nr): 44 result+=self.list[i]+"\n" 45 for i in range(self.point): 46 result+=self.list[i]+"\n" 47 48 return result
49