jtentor icon

DemoQueue3 - SpecialQueue.py

jtentor | PRO | 05/18/20 01:49:22 PM UTC | 0 ⭐ | 444 👁️ | Never ⏰ | []
Python |

1.55 KB

|

None

|

0 👍

/

0 👎

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
__author__ = 'Julio Tentor <[email protected]>'
 
 
import numpy
 
 
class SpecialQueue(object):
    def __init__(self, capacity = 10):
        self.__capacity = capacity
        self.__data = numpy.empty(self.__capacity, numpy.object)
        self.__head = 0
        self.__tail = 0
        self.__count = 0
 
 
    def __len__(self):
        return self.__count
 
 
    def __next__(self, pos):
        pos += 1
        return 0 if (pos >= self.__capacity) else pos
 
 
    def append(self, x):
        if self.__count >= self.__capacity:
            raise ValueError("ERROR La cola está llena...")
        self.__data[self.__tail] = x
        self.__tail = self.__next__(self.__tail)
        self.__count += 1
 
 
    def popleft(self):
        if self.__count <= 0:
            raise ValueError("ERROR La cola está vacía...")
        result = self.__data[self.__head]
        self.__head = self.__next__(self.__head)
        self.__count -= 1
        return result
 
 
    def toArray(self):
        result = numpy.empty(len(self), numpy.object)
        pos = self.__head
        for i in range(len(self)):
            result[i] = self.__data[pos]
            pos = self.__next__(pos)
        return result
 
 
    def join(self, other):
        result = SpecialQueue((len(self) + len(other)) * 2)
 
        for e in self.toArray():
            result.append(e)
 
        for e in other.toArray():
            result.append(e)
        return result
 
 
if __name__ == "__main__":
    pass

Comments