array2d.pyi 3.4 KB

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