Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
thats a fully functioning ring buffer right there
  • Loading branch information
clifhodges13 committed Jun 27, 2020
commit 97eddcc78f4d41a31f77cfb20cabf10ab101897e
28 changes: 25 additions & 3 deletions ring_buffer/ring_buffer.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
class RingBuffer:
def __init__(self, capacity):
pass
self.capacity = capacity
self.buffer = []
self.oldest = None

def append(self, item):
pass
# if buffer is empty,
if len(self.buffer) == 0:
# add item to list and
self.buffer.append(item)
# make new item the "oldest"
self.oldest = item
return self.oldest
# if buffer is at capacity, replace oldest item with new item
elif len(self.buffer) == self.capacity:
# get the index of the oldest item
index_oldest = self.buffer.index(self.oldest)
# replace the oldest with the new item
self.buffer[index_oldest] = item
if (index_oldest+1) >= len(self.buffer):
self.oldest = self.buffer[0]
else:
self.oldest = self.buffer[index_oldest+1]
# if buffer is not at capacity, add item to end of buffer
elif len(self.buffer) < self.capacity:
self.buffer.append(item)
return item

def get(self):
pass
return self.buffer