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

Source Code for Module PyFoam.Basics.RingBuffer

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