Exemplo n.º 1
0
print("Test case of RingBuffer")
print(
    "Assumed RingBuffer capacity is 5 for testing. All the test cases below are according to size 5"
)
sizeOfRB = int(input("Enter size of RingBuffer"))
list1 = RingBuffer(sizeOfRB)
print("Adding 1")
list1.insert_keep_new("1")
print("Adding 2")
list1.insert_keep_new("2")
print("Adding 3")
list1.insert_keep_old("3")
print("Adding 4")
list1.insert_keep_old("4")
print("Orignal")
list1.print()

print("After insert_keep_new(5)")
list1.insert_keep_new("5")
list1.print()
print("After insert_keep_new(6)")
list1.insert_keep_new("6")
list1.print()
print("After insert_keep_old(7)")
list1.insert_keep_old("7")
list1.print()
print("After insert_keep_old(8)")
list1.insert_keep_old("8")
list1.print()
print("Capacity is ", list1.capacity())
print("Testing remove_newest()")
Exemplo n.º 2
0
class Stack:
    '''
    This is a list based implementation of a stack which will keep newest data
    and drop anything oldest that there is not room for
    '''

    __slots__ = "RB","capacity1", "top1"
    def __init__(self, capacity):
        """
        initlizes the Stack class
        :param own: slef object
        :param capacity: Capacity of the Stack
        """
        # create list of size capacity
        #self.list_stack = [None] * capacity
        self.RB = RingBuffer(capacity)
        # store as instance variable
        self.capacity1 = capacity
        # set other instance variable defaults
        self.top1 = None

    def __str__(self):
        """
        Convert Stack in a string format
        :param own: slef object
        :return: returns stack in the String format
        """
        if self.capacity1==0 or self.capacity1<0:
            print("Capacity of Stack is 0 or less than 1. Can't print Stack")
            return
        self.RB.print()

    def push(self, val):
        """
        push operation of the stack.
        :param own: slef object
        :return: returns the pushed element
        """
        if self.capacity1==0 or self.capacity1<0:
            print("Capacity of Stack is 0 or less than 1. Can't use this Stack")
            return
        if self.RB.sizeOf() == self.capacity1:
            print("As Stack is full, Removing top ", self.top1)
        self.top1=self.RB.insert_keep_new(val)
        return self.top1

    def pop(self):
        """
        pop operation of the stack.
        :param own: slef object
        :return: returns the poped element
        """
        if self.capacity1==0 or self.capacity1<0:
            print("Capacity of Stack is 0 or less than 1. Can't use this Stack")
            return
        if self.size()==0 :
            print("Stack is empty")
            return

        self.top1= self.RB.remove_newest()

    def peek(self):
        """
        peek operation of the stack.
        :param own: self object
        :return: returns the element at the top
        """
        if self.capacity1==0 or self.capacity1<0:
            print("Capacity of Stack is 0 or less than 1. Can't use this Stack")
            return

        if self.size()==0 :
            print("Stack is empty")
            return
        return self.top1

    def size(self):
        """
        check size of the stack
        :param own: self object
        :return: returns the size of the stack
        """
        return self.RB.sizeOf()

    def capacity(self):
        """
        check capacity of the stack
        :param own: self object
        :return: returns the capacity of the stack
        """
        return self.capacity1
Exemplo n.º 3
0
class Queue:
    '''
    This is a list based implementation of a Queue
    '''

    __slots__ = "RB", "capacity1", "front", "back"

    def __init__(self, capacity):
        """
               initlizes the Queue class
               :param own: slef object
               :param capacity: Capacity of the Queue
         """
        # create list of size capacity
        #self.list_stack = [None] * capacity
        self.RB = RingBuffer(capacity)
        # store as instance variable
        self.capacity1 = capacity
        # set other instance variable defaults
        self.front = None
        self.back = None
        #self.sizeIs = 0

    def __str__(self):
        """
        Convert Queue in a string format
        :param own: slef object
        :return: returns Queue in the String format
        """
        if self.capacity1 == 0 or self.capacity1 < 0:
            print(
                "Capacity of Queue is 0 or less than 1. Can't print this Queue"
            )
            return
        if self.size() == 0:
            print("Queue is empty")
            return
        self.RB.print()

    def is_empty(self):
        """
        check if empty
        :param own: slef object
        :return: returns the true if Queue is empty or false if not
        """
        return self.front == None

    def enqueue(self, val):
        """
        Addes the element to the Queue
        :param own: slef object
        :param val: Element to be added
        """
        if self.capacity1 == 0 or self.capacity1 < 0:
            print(
                "Capacity of Queue is 0 or less than 1. Can't use this Queue")
            return
        #self.RB.insert_keep_new(val)
        if self.is_empty() == True:
            self.front = self.RB.insert_keep_old(val)
            self.back = self.front
            return
        elif self.RB.sizeOf() == self.capacity1:
            print("Queue is full. Can't add any value")
            return
        else:
            self.back = self.RB.insert_keep_old(val)

    def dequeue(self):
        """
        Removes the element from the Queue
        :param own: slef object
        """
        if self.capacity1 == 0 or self.capacity1 < 0:
            print(
                "Capacity of Queue is 0 or less than 1. Can't use this Queue")
            return
        #print("Is empty in deque", self.is_empty())
        if self.is_empty() == True:
            print("Queue is empty")
            return
        elif self.RB.sizeOf() == 1:
            self.front = None
            self.back = None
            self.RB.clear()
            return
        self.front = self.RB.remove_oldest()

    def peek(self):
        """
        Peek operation for the Queue
        :param own: slef object
        :Return : the first element
        """
        if self.is_empty() == True:
            print("Queue is empty")
            return
        return self.front

    def capacity(self):
        """
        Checks the capacity of the Queue
        :param own: slef object
        :Return : the capacity of the Queue
        """
        return self.capacity1

    def size(self):
        """
        Checks the size of the Queue
        :param own: slef object
        :Return : the size of the Queue
        """
        return self.RB.sizeOf()