diff --git a/setup.py b/setup.py index 6e4bf24..194b1de 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ name='videolib', author='Abhinau Kumar', author_email='ab.kumr98@gmail.com', - 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'], diff --git a/videolib/bufferlib.py b/videolib/buffer.py similarity index 72% rename from videolib/bufferlib.py rename to videolib/buffer.py index b0c3d43..a5e65d4 100644 --- a/videolib/bufferlib.py +++ b/videolib/buffer.py @@ -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: ''' @@ -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: ''' @@ -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: ''' @@ -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]