From 6e2634b9ff51199b59cdadb5961c8c29164eaf8b Mon Sep 17 00:00:00 2001 From: TheHakerTech Date: Mon, 1 Jul 2024 19:00:15 +0300 Subject: [PATCH] Added uqueue --- examples/uqueue.md | 0 ufpy/uqueue.py | 55 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 examples/uqueue.md diff --git a/examples/uqueue.md b/examples/uqueue.md new file mode 100644 index 0000000..e69de29 diff --git a/ufpy/uqueue.py b/ufpy/uqueue.py index e69de29..84cbfb5 100644 --- a/ufpy/uqueue.py +++ b/ufpy/uqueue.py @@ -0,0 +1,55 @@ +from __future__ import annotations +from typing import Generic, TypeVar, Any + +__all__ = ( + "UQueue", +) + +VT = TypeVar("VT") + +class UQueue(Generic[VT]): + def __init__(self, *__list): + self.__list: list[VT] = list(__list) + self.__index = 0 + + def pop(self) -> VT: + return self.__list.pop(0) + + def push(self, value: VT) -> VT: + self.__list.append(value) + return value + + @property + def head(self) -> None: + return self.__list[-1] if self else None + + def copy(self) -> UQueue: + return UQueue(self.__list.copy()) + + def set_head(self, value: VT) -> VT: + if self: + self.__list[-1] = value + return value + else: + raise IndexError("Index out of range") + + def is_empty(self) -> bool: + return len(self) == 0 + + def __len__(self) -> int: + return len(self.__list) + + def __nonzero__(self) -> bool: + return not self.is_empty() + + def __str__(self) -> str: + return str(self.__list) + + def __iter__(self) -> UQueue: + return self + + def __next__(self) -> VT: + if self.is_empty(): + raise StopIteration + else: + return self.__list.pop(0) \ No newline at end of file