jtentor

DemoQueue3 - SpecialQueue.py

May 18th, 2020
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.55 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 SpecialQueue(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.     def toArray(self):
  45.         result = numpy.empty(len(self), numpy.object)
  46.         pos = self.__head
  47.         for i in range(len(self)):
  48.             result[i] = self.__data[pos]
  49.             pos = self.__next__(pos)
  50.         return result
  51.  
  52.  
  53.     def join(self, other):
  54.         result = SpecialQueue((len(self) + len(other)) * 2)
  55.  
  56.         for e in self.toArray():
  57.             result.append(e)
  58.  
  59.         for e in other.toArray():
  60.             result.append(e)
  61.         return result
  62.  
  63.  
  64. if __name__ == "__main__":
  65.     pass
Add Comment
Please, Sign In to add comment