[mypy] Add/fix type annotations in data_structures/heap/skew_heap.py (#5634)

* Add abstract base class Comparable

* [mypy] Fix type annotations (strict mode)

* Fix a typo

* Remove Comparable class and set bound to bool
This commit is contained in:
Dylan Buchi 2021-10-31 05:38:24 -03:00 committed by GitHub
parent 21c99d2ae2
commit 99983c91ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,9 +2,9 @@
from __future__ import annotations from __future__ import annotations
from typing import Generic, Iterable, Iterator, TypeVar from typing import Any, Generic, Iterable, Iterator, TypeVar
T = TypeVar("T") T = TypeVar("T", bound=bool)
class SkewNode(Generic[T]): class SkewNode(Generic[T]):
@ -51,7 +51,7 @@ class SkewHeap(Generic[T]):
values. Both operations take O(logN) time where N is the size of the values. Both operations take O(logN) time where N is the size of the
structure. structure.
Wiki: https://en.wikipedia.org/wiki/Skew_heap Wiki: https://en.wikipedia.org/wiki/Skew_heap
Visualisation: https://www.cs.usfca.edu/~galles/visualization/SkewHeap.html Visualization: https://www.cs.usfca.edu/~galles/visualization/SkewHeap.html
>>> list(SkewHeap([2, 3, 1, 5, 1, 7])) >>> list(SkewHeap([2, 3, 1, 5, 1, 7]))
[1, 1, 2, 3, 5, 7] [1, 1, 2, 3, 5, 7]
@ -70,14 +70,16 @@ class SkewHeap(Generic[T]):
""" """
def __init__(self, data: Iterable[T] | None = ()) -> None: def __init__(self, data: Iterable[T] | None = ()) -> None:
""" """
>>> sh = SkewHeap([3, 1, 3, 7]) >>> sh = SkewHeap([3, 1, 3, 7])
>>> list(sh) >>> list(sh)
[1, 3, 3, 7] [1, 3, 3, 7]
""" """
self._root: SkewNode[T] | None = None self._root: SkewNode[T] | None = None
for item in data: if data:
self.insert(item) for item in data:
self.insert(item)
def __bool__(self) -> bool: def __bool__(self) -> bool:
""" """
@ -103,7 +105,7 @@ class SkewHeap(Generic[T]):
>>> list(sh) >>> list(sh)
[1, 3, 3, 7] [1, 3, 3, 7]
""" """
result = [] result: list[Any] = []
while self: while self:
result.append(self.pop()) result.append(self.pop())
@ -127,7 +129,7 @@ class SkewHeap(Generic[T]):
""" """
self._root = SkewNode.merge(self._root, SkewNode(value)) self._root = SkewNode.merge(self._root, SkewNode(value))
def pop(self) -> T: def pop(self) -> T | None:
""" """
Pop the smallest value from the heap and return it. Pop the smallest value from the heap and return it.
@ -146,7 +148,9 @@ class SkewHeap(Generic[T]):
IndexError: Can't get top element for the empty heap. IndexError: Can't get top element for the empty heap.
""" """
result = self.top() result = self.top()
self._root = SkewNode.merge(self._root.left, self._root.right) self._root = (
SkewNode.merge(self._root.left, self._root.right) if self._root else None
)
return result return result
@ -172,7 +176,7 @@ class SkewHeap(Generic[T]):
raise IndexError("Can't get top element for the empty heap.") raise IndexError("Can't get top element for the empty heap.")
return self._root.value return self._root.value
def clear(self): def clear(self) -> None:
""" """
Clear the heap. Clear the heap.