controls/DebugVariablePanel/RingBuffer.py
changeset 2742 5f7445b582d4
parent 2741 3cc5663af196
child 3750 f62625418bff
--- a/controls/DebugVariablePanel/RingBuffer.py	Mon Jun 14 16:48:39 2021 +0200
+++ b/controls/DebugVariablePanel/RingBuffer.py	Wed Jun 16 12:15:02 2021 +0200
@@ -12,36 +12,32 @@
 
 
 class RingBuffer(object):
-    def __init__(self, width=None, size=65536, padding=None):
+    def __init__(self, width=None, size=131072, padding=None):
         self.size = size
         self.padding = size if padding is None else padding
         shape = (self.size+self.padding,)
         if width :
             shape += (width,)
         self.buffer = np.zeros(shape)
-        self.counter = 0
-        self.full = False
+        self.cursor = 0
 
     def append(self, data):
         """this is an O(n) operation"""
-        data = data[-self.padding:]
+        data = data[-self.size:]
         n = len(data)
-        if self.remaining < n: self.compact()
-        self.buffer[self.counter+self.size:][:n] = data
-        self.counter += n
+        if self.size + self.padding - self.cursor < n:
+            self.compact()
+        self.buffer[self.cursor:][:n] = data
+        self.cursor += n
 
     @property
     def count(self):
-        return self.counter if not self.full else self.size
-
-    @property
-    def remaining(self):
-        return self.padding-self.counter
+        return min(self.size, self.cursor)
 
     @property
     def view(self):
         """this is always an O(1) operation"""
-        return self.buffer[self.counter:][:self.size]
+        return self.buffer[max(0, self.cursor - self.size):][:self.count]
 
     def compact(self):
         """
@@ -49,7 +45,6 @@
         and this cost is amortized over the whole padding space
         """
         print 'compacting'
-        self.buffer[:self.size] = self.view
-        self.counter = 0
-        self.full = True
+        self.buffer[:self.count] = self.view
+        self.cursor -= self.size