Skip to content

Commit

Permalink
Merge pull request #1 from abhinaukumar/circular_buffer_update
Browse files Browse the repository at this point in the history
Circular buffer updates
  • Loading branch information
abhinaukumar authored Mar 7, 2023
2 parents 899635d + 61b9c80 commit 6d64e74
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
name='videolib',
author='Abhinau Kumar',
author_email='[email protected]',
version='0.1.1',
version='0.1.2',
url='https://github.com/abhinaukumar/videolib',
description='Package for easy Video IO and color conversion in Python.',
install_requires=['numpy', 'scikit-video', 'matplotlib'],
Expand Down
29 changes: 20 additions & 9 deletions videolib/bufferlib.py → videolib/buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def __init__(self, buf_size: int) -> None:
raise ValueError('Buffer size must be positive')
self.buf_size: int = buf_size
self._buf: List = []
self._top_index: int = -1
self._back_index: int = -1

def isempty(self) -> bool:
'''
Expand All @@ -40,8 +40,8 @@ def append(self, item: Any) -> None:
warnings.warn('Appending to empty buffer. Filling with given value')
self.fill(item)
else:
self._top_index = (self._top_index + 1) % self.buf_size
self._buf[self._top_index] = copy.copy(item)
self._back_index = (self._back_index + 1) % self.buf_size
self._buf[self._back_index] = copy.copy(item)

def fill(self, item: Any) -> None:
'''
Expand All @@ -53,14 +53,14 @@ def fill(self, item: Any) -> None:
self.clear()
for i in range(self.buf_size):
self._buf.append(copy.copy(item))
self._top_index = self.buf_size - 1
self._back_index = self.buf_size - 1

def clear(self) -> None:
'''
Clear circular buffer.
'''
self._buf.clear()
self._top_index = -1
self._back_index = -1

def check_append(self, item: Any) -> None:
'''
Expand All @@ -87,19 +87,30 @@ def __next__(self) -> Any:
'''
if self._iter_index == self.buf_size:
raise StopIteration
item = self._buf[self._top_index - self._iter_index]
item = self._buf[self._back_index - self._iter_index]
self._iter_index += 1
return item

def top(self) -> Any:
def back(self) -> Any:
'''
Get top element of circular buffer.
'''
return self._buf[self._top_index]
if self.isempty():
raise IndexError('Empty buffer has no back')
return self._buf[self._back_index]

def front(self) -> Any:
'''
Get top element of circular buffer.
'''
if self.isempty():
raise IndexError('Empty buffer has no front')
return self._buf[(self._back_index+1) % self.buf_size]
def center(self) -> Any:
'''
Get center element of circular buffer.
'''
center_index = (self._top_index + self.buf_size//2 + 1) % self.buf_size
if self.isempty():
raise IndexError('Empty buffer has no center')
center_index = (self._back_index + self.buf_size//2 + 1) % self.buf_size
return self._buf[center_index]

0 comments on commit 6d64e74

Please sign in to comment.