1
0

array2d.pyi 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. from typing import Callable, Any, Generic, TypeVar, Literal, overload, Iterator
  2. T = TypeVar('T')
  3. Neighborhood = Literal['Moore', 'von Neumann']
  4. class array2d(Generic[T]):
  5. def __init__(self, n_cols: int, n_rows: int, default=None): ...
  6. @property
  7. def width(self) -> int: ...
  8. @property
  9. def height(self) -> int: ...
  10. @property
  11. def numel(self) -> int: ...
  12. def is_valid(self, col: int, row: int) -> bool: ...
  13. def get(self, col: int, row: int, default=None) -> T | None: ...
  14. @overload
  15. def __getitem__(self, index: tuple[int, int]) -> T: ...
  16. @overload
  17. def __getitem__(self, index: tuple[slice, slice]) -> 'array2d[T]': ...
  18. @overload
  19. def __setitem__(self, index: tuple[int, int], value: T): ...
  20. @overload
  21. def __setitem__(self, index: tuple[slice, slice], value: int | float | str | bool | None | 'array2d[T]'): ...
  22. def __len__(self) -> int: ...
  23. def __iter__(self) -> Iterator[tuple[int, int, T]]: ...
  24. def __eq__(self, other: 'array2d') -> bool: ...
  25. def __ne__(self, other: 'array2d') -> bool: ...
  26. def __repr__(self): ...
  27. def tolist(self) -> list[list[T]]: ...
  28. def map(self, f: Callable[[T], Any]) -> 'array2d': ...
  29. def copy(self) -> 'array2d[T]': ...
  30. def fill_(self, value: T) -> None: ...
  31. def apply_(self, f: Callable[[T], T]) -> None: ...
  32. def copy_(self, other: 'array2d[T] | list[T]') -> None: ...
  33. # algorithms
  34. def count(self, value: T) -> int:
  35. """Counts the number of cells with the given value."""
  36. def count_neighbors(self, value: T, neighborhood: Neighborhood = 'Moore') -> 'array2d[int]':
  37. """Counts the number of neighbors with the given value for each cell."""
  38. def find_bounding_rect(self, value: T) -> tuple[int, int, int, int] | None:
  39. """Finds the bounding rectangle of the given value.
  40. Returns a tuple `(x, y, width, height)` or `None` if the value is not found.
  41. """