1
2 """A ring-buffer data structure"""
3
5 """A data structure that stores a number N of elements. The
6 N+1-element overwrites the first and so on ...."""
7
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
18 """ Inserts am element into the ring-buffer
19 """
20
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
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
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