Ejemplo n.º 1
0
class Queue:
    __slots__ = "ring_buffer"

    def __init__(self, capacity):
        if capacity < 0:
            print("Cannot have a negative list")
            exit()
        elif isinstance(capacity, int):
            self.ring_buffer = RingBuffer(capacity)
        else:
            print("Incorrect format of Capacity")

    def __str__(self):
        return self.ring_buffer.__str__()

    def enqueue(self, data):
        if (data == None):
            print("Data has to be sent!!")
            return -1
        else:
            self.ring_buffer.insert_keep_old(data)

    def dequeue(self):
        self.ring_buffer.remove_oldest()

    def peek(self):
        return self.ring_buffer.head

    def capacity(self):
        return self.ring_buffer.capacity()
Ejemplo n.º 2
0
    def test_remove(self):
        rb = RingBuffer(4)
        rs = RingStack(4)
        rq = RingQueue(4)
        ls = ListStack(4)
        lq = ListQueue(4)
        for name in TestRingBufferDS.names:
            rb.insert_keep_old(name)
            rs.insert(name)
            rq.insert(name)
            ls.insert(name)
            lq.insert(name)

        print("Before remove: ")
        print("RingBuffer:", rb)
        print("RingStack:", rs)
        print("RingQueue:", rq)
        print("ListStack:", ls)
        print("ListQueue:", lq)

        for i in range(2):
            rs.remove()
            rq.remove()
            ls.remove()
            lq.remove()

        print("After remove: ")
        print("RingBuffer:", rb)
        print("RingStack:", rs)
        print("RingQueue:", rq)
        print("ListStack:", ls)
        print("ListQueue:", lq)
Ejemplo n.º 3
0
class Queue:

    __slots__ = 'ring_buffer', 'max_capacity'
    """constructor with intial max capacity of 10"""
    def __init__(self, capacity=10):
        self.ring_buffer = RingBuffer(capacity)
        self.max_capacity = capacity

    """to string function uses the internal to string of ring buffer but 
       points to the end"""

    def __str__(self):
        return str(self.ring_buffer) + '<-- end '

    """ enqueue function which inserts values in the queue """

    def enqueue(self, value):
        if value is None:
            raise Exception('cannot enqueue None element in the queue')
        else:
            self.ring_buffer.insert_keep_old(value)

    """ dequeue function which removes values from the queue using FIFO 
    method
        raises an exception if dequeue is done on the empty queue"""

    def dequeue(self):
        if self.ring_buffer.size() is 0:
            raise Exception('dequeue from a empty queue is not allowed')
        else:
            return self.ring_buffer.remove_oldest()

    """ returns the current size of the stack"""

    def size(self):
        return self.ring_buffer.size()

    """ :return the max capacity of the stack """

    def capacity(self):
        return self.ring_buffer.capacity()

    """ only returns the value of the first element in the queue does not 
       removed the element from the queue
       """

    def peek(self):
        if self.size() < 1:
            raise Exception('peek into a empty queue is not allowed')
        else:
            return self.ring_buffer.start_element()
Ejemplo n.º 4
0
    def test_insert(self):
        rb = RingBuffer(4)
        rs = RingStack(4)
        rq = RingQueue(4)
        ls = ListStack(4)
        lq = ListQueue(4)
        for name in TestRingBufferDS.names:
            rb.insert_keep_old(name)
            rs.insert(name)
            rq.insert(name)
            ls.insert(name)
            lq.insert(name)

        print("RingBuffer:", rb)
        print("RingStack:", rs)
        print("RingQueue:", rq)
        print("ListStack:", ls)
        print("ListQueue:", lq)
Ejemplo n.º 5
0
class Queue:
    __slots__ = "ring_buffer", "link"

    def __init__(self, capacity):
        self.ring_buffer = RingBuffer(capacity)

    def __str__(self):
        return self.ring_buffer.__str__()

    def enqueue(self, value):
        self.ring_buffer.insert_keep_old(value)

    def dequeue(self):
        self.ring_buffer.remove_oldest()

    def peek(self):
        return self.ring_buffer.head

    def capacity(self):
        return self.ring_buffer.capacity()
Ejemplo n.º 6
0
from queue import Queue
from stack import Stack
from ListStack import ListStack
from ListQueue import ListQueue
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)")
Ejemplo n.º 7
0
class RingQueue:
    '''
    A ring buffer implementation of queue, which keeps the old data the drops the new data to insert new data
    '''
    def __init__(self, capacity):
        # store as instance variable
        self._capacity = capacity
        # create list of size capacity
        self.RingBuffer = RingBuffer(self._capacity)
        # set other instance variable defaults
        self.front = -1
        self.back = -1
        self.size = 0

    def __str__(self):
        '''
        A toString equivalent of java, which returns the string representation of the class
        '''
        # pretty print
        result = 'RingQueue'
        result += str(self.RingBuffer)
        return result

    def insert(self, val):
        '''
        Method to insert data into queue
        '''
        # if at end of list, ignore add
        if self.back is self._capacity - 1:
            return
        # update pointers during insert to keeping only oldest data
        if self.front is -1:
            self.front = 0
        self.back += 1
        self.RingBuffer.insert_keep_old(val)
        self.size += 1

    def remove(self):
        '''
        Method to remove data from queue
        '''
        # no op if empty
        if self.size is 0:
            return
        # update pointers
        if self.size > 0:
            self.RingBuffer.element[self._capacity - self.size].element = None
        else:
            self.RingBuffer.element[self._capacity - 1].element = None
        self.back -= 1
        if self.back is -1:
            self.front = -1
        self.size -= 1

    def peek(self):
        '''
        Returns front element of the buffer
        '''
        return self.RingBuffer.element[self.front]

    def capacity(self):
        '''
        Returns buffer size
        '''
        return self._capacity
Ejemplo n.º 8
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()