Update desired Python API, add mostly failing PureQueue tests for TDD

This commit is contained in:
Michael Bradley 2025-01-08 22:54:40 +13:00
parent 0995e6db90
commit b4ca806d76
Signed by: MichaelBradley
SSH key fingerprint: SHA256:cj/YZ5VT+QOKncqSkx+ibKTIn0Obg7OIzwzl9BL8EO8
4 changed files with 104 additions and 33 deletions

View file

@ -1,12 +1,14 @@
from typing import Self
from abc import ABC
from typing import Hashable, Iterable, Self
class PureQueue[T]:
"""A min-queue that allows duplicates and provides a minimal API allowing insertions and the ability to iterate over the queue in-order"""
def __init__(self, items: dict[T, float] | None = None) -> None:
"""
:param items: An optional mapping of items to priorities to initialize the queue with
"""
type Comparable = float | int
class Queue[Data](ABC):
"""
Common queue methods providing a minimal API for iteration.
"""
def __len__(self) -> int:
"""
@ -18,12 +20,44 @@ class PureQueue[T]:
:return: An iterator over the queue
"""
def __next__(self) -> T:
def __next__(self) -> Data:
"""
:return: The next item in the queue
"""
def __setitem__(self, key: T, value: float) -> None:
class PureQueue[Data: Comparable](Queue[Data]):
"""
A min-queue that directly orders its items, allowing duplicates.
"""
def __init__(self, items: Iterable[Data] | None = None) -> None:
"""
:param items: An optional list of priorities with which to initialize the queue
"""
def pop(self) -> Data:
"""
:return: The next item in the queue
"""
def insert(self, item: Data) -> None:
"""
:param item: Item to insert into the queue
"""
class KeyedQueue[Data, Priority: Comparable](Queue[Data]):
"""
A min-queue that allows arbitrary data associated with some priority, allowing duplicates of both data and priority.
"""
def __init__(self, items: dict[Data, Priority] | Iterable[tuple[Data, Priority]] | None = None) -> None:
"""
:param items: An optional mapping/list of items to priorities with which to initialize the queue
"""
def __setitem__(self, key: Data, value: Priority) -> None:
"""
Inserts a new item into the queue
:param key: The item to insert
@ -31,16 +65,25 @@ class PureQueue[T]:
"""
class KeyedQueue[T](PureQueue[T]):
"""A min-queue that disallows duplicates but offers the ability to update priorities and delete arbitrary items"""
def __setitem__(self, key: T, value: float) -> None:
class IndexedQueue[Data: Hashable, Priority: Comparable](Queue[Data]):
"""
A min-queue that allows arbitrary data associated with some priority.
Disallows duplicate data but offers the ability to update priorities and delete arbitrary items.
"""
def __init__(self, items: dict[Data, Priority] | Iterable[tuple[Data, Priority]] | None = None) -> None:
"""
:param items: An optional mapping/list of items to priorities with which to initialize the queue
"""
def __setitem__(self, key: Data, value: Priority) -> None:
"""
Inserts an item into the queue, or updates its priority if it already exists
:param key: The item to insert or update
:param value: The priority of the item
"""
def __delitem__(self, key: T) -> None:
def __delitem__(self, key: Data) -> None:
"""
:param key: The item to delete from the queue
"""