array2d.pyi 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. from typing import Callable, Any, Generic, TypeVar, Literal, overload, Iterator
  2. from linalg import vec2i
  3. Neighborhood = Literal['Moore', 'von Neumann']
  4. class array2d[T]:
  5. @property
  6. def n_cols(self) -> int: ...
  7. @property
  8. def n_rows(self) -> int: ...
  9. @property
  10. def width(self) -> int: ...
  11. @property
  12. def height(self) -> int: ...
  13. @property
  14. def numel(self) -> int: ...
  15. def __new__(cls, n_cols: int, n_rows: int, default: Callable[[vec2i], T] = None): ...
  16. def __eq__(self, other: object) -> array2d[bool]: ... # type: ignore
  17. def __ne__(self, other: object) -> array2d[bool]: ... # type: ignore
  18. def __repr__(self) -> str: ...
  19. def __iter__(self) -> Iterator[tuple[vec2i, T]]: ...
  20. @overload
  21. def is_valid(self, col: int, row: int) -> bool: ...
  22. @overload
  23. def is_valid(self, pos: vec2i) -> bool: ...
  24. @overload
  25. def get[R](self, col: int, row: int, default: R) -> T | R: ...
  26. @overload
  27. def get[R](self, pos: vec2i, default: R) -> T | R: ...
  28. @overload
  29. def __getitem__(self, index: tuple[int, int]) -> T: ...
  30. @overload
  31. def __getitem__(self, index: vec2i) -> T: ...
  32. @overload
  33. def __getitem__(self, index: tuple[slice, slice]) -> array2d[T]: ...
  34. @overload
  35. def __getitem__(self, mask: array2d[bool]) -> list[T]: ...
  36. @overload
  37. def __setitem__(self, index: tuple[int, int], value: T): ...
  38. @overload
  39. def __setitem__(self, index: vec2i, value: T): ...
  40. @overload
  41. def __setitem__(self, index: tuple[slice, slice], value: int | float | str | bool | None | 'array2d[T]'): ...
  42. @overload
  43. def __setitem__(self, mask: array2d[bool], value: T): ...
  44. def map[R](self, f: Callable[[T], R]) -> array2d[R]: ...
  45. def copy(self) -> 'array2d[T]': ...
  46. def fill_(self, value: T) -> None: ...
  47. def apply_(self, f: Callable[[T], T]) -> None: ...
  48. def copy_(self, other: array2d[T] | list[T]) -> None: ...
  49. def render(self) -> str: ...
  50. def all(self: array2d[bool]) -> bool: ...
  51. def any(self: array2d[bool]) -> bool: ...
  52. @staticmethod
  53. def fromlist(data: list[list[T]]) -> array2d[T]: ...
  54. def tolist(self) -> list[list[T]]: ...
  55. # algorithms
  56. def count(self, value: T) -> int:
  57. """Counts the number of cells with the given value."""
  58. def count_neighbors(self, value: T, neighborhood: Neighborhood) -> array2d[int]:
  59. """Counts the number of neighbors with the given value for each cell."""
  60. def get_bounding_rect(self, value: T) -> tuple[int, int, int, int]:
  61. """Gets the bounding rectangle of the given value.
  62. Returns a tuple `(x, y, width, height)` or raise `ValueError` if the value is not found.
  63. """
  64. def convolve(self: array2d[int], kernel: array2d[int], padding: int) -> array2d[int]:
  65. """Convolves the array with the given kernel."""
  66. def get_connected_components(self, value: T, neighborhood: Neighborhood) -> tuple[array2d[int], int]:
  67. """Gets connected components of the grid.
  68. Returns the `visited` array and the number of connected components,
  69. where `0` means unvisited, and non-zero means the index of the connected component.
  70. """