Last active
May 7, 2019 17:54
-
-
Save edwintcloud/c63e2dedb6d192b37b671460a1fea86b to your computer and use it in GitHub Desktop.
Circular Buffer in Python Part 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def size(self): | |
"""Return the size of the CircularBuffer | |
Runtime: O(1) Space: O(1)""" | |
if self.tail >= self.head: | |
return self.tail - self.head | |
return self.max_size - self.head - self.tail | |
def is_empty(self): | |
"""Return True if the head of the CircularBuffer is equal to the tail, | |
otherwise return False | |
Runtime: O(1) Space: O(1)""" | |
return self.tail == self.head | |
def is_full(self): | |
"""Return True if the tail of the CircularBuffer is one before the head, | |
otherwise return False | |
Runtime: O(1) Space: O(1)""" | |
return self.tail == (self.head-1) % self.max_size |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment