Skip to content

Commit

Permalink
Added uqueue
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAihopGG committed Jul 1, 2024
1 parent c63432f commit 6e2634b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
Empty file added examples/uqueue.md
Empty file.
55 changes: 55 additions & 0 deletions ufpy/uqueue.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 6e2634b

Please sign in to comment.