定义一个固定大小的缓存,当它被填满时,新加入的元素自动覆盖第一个元素,这种方法经常用在存储日志和历史信息的程序中。
[root@xiaoxiong cb6]# cat cb2_6_11_sol_1.py
class RingBuffer(object):
""" class that implements a not-yet-full buffer """
def __init__(self, size_max):
self.max = size_max
self.data = []
class __Full(object):
""" class that implements a full buffer """
def append(self, x):
""" Append an element overwriting the oldest one. """
self.data[self.cur] = x
self.cur = (self.cur+1) % self.max
def tolist(self):
""" return list of elements in correct order. """
return self.data[self.cur:] + self.data[:self.cur]
def append(self, x):
""" append an element at the