jtentor

DemoQueue1 - Queue.py

May 18th, 2020
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.06 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3. __author__ = 'Julio Tentor <jtentor@fi.unju.edu.ar>'
  4.  
  5.  
  6. import numpy
  7.  
  8.  
  9. class Queue(object):
  10.     def __init__(self, capacity = 10):
  11.         self.__capacity = capacity
  12.         self.__data = numpy.empty(self.__capacity, numpy.object)
  13.         self.__head = 0
  14.         self.__tail = 0
  15.         self.__count = 0
  16.  
  17.  
  18.     def __len__(self):
  19.         return self.__count
  20.  
  21.  
  22.     def __next__(self, pos):
  23.         pos += 1
  24.         return 0 if (pos >= self.__capacity) else pos
  25.  
  26.  
  27.     def append(self, x):
  28.         if self.__count >= self.__capacity:
  29.             raise ValueError("ERROR La cola está llena...")
  30.         self.__data[self.__tail] = x
  31.         self.__tail = self.__next__(self.__tail)
  32.         self.__count += 1
  33.  
  34.  
  35.     def popleft(self):
  36.         if self.__count <= 0:
  37.             raise ValueError("ERROR La cola está vacía...")
  38.         result = self.__data[self.__head]
  39.         self.__head = self.__next__(self.__head)
  40.         self.__count -= 1
  41.         return result
  42.  
  43.  
  44. if __name__ == "__main__":
  45.     pass
Add Comment
Please, Sign In to add comment