46 lines
1.2 KiB
Python
46 lines
1.2 KiB
Python
from typing import 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
|
|
"""
|
|
|
|
def __len__(self) -> int:
|
|
"""
|
|
:return: The number of items in the queue
|
|
"""
|
|
|
|
def __iter__(self) -> Self:
|
|
"""
|
|
:return: An iterator over the queue
|
|
"""
|
|
|
|
def __next__(self) -> T:
|
|
"""
|
|
:return: The next item in the queue
|
|
"""
|
|
|
|
def __setitem__(self, key: T, value: float) -> None:
|
|
"""
|
|
Inserts a new item into the queue
|
|
:param key: The item to insert
|
|
:param value: The priority of the item
|
|
"""
|
|
|
|
|
|
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:
|
|
"""
|
|
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:
|
|
"""
|
|
:param key: The item to delete from the queue
|
|
"""
|