array2d.pyi 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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: T | Callable[[vec2i], T] | None = 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. def get[R](self, col: int, row: int, default: R = None) -> T | R:
  25. """Gets the value at the given position. If the position is out of bounds, return the default value."""
  26. @overload
  27. def __getitem__(self, index: tuple[int, int]) -> T: ...
  28. @overload
  29. def __getitem__(self, index: vec2i) -> T: ...
  30. @overload
  31. def __getitem__(self, index: tuple[slice, slice]) -> array2d[T]: ...
  32. @overload
  33. def __getitem__(self, mask: array2d[bool]) -> list[T]: ...
  34. @overload
  35. def __setitem__(self, index: tuple[int, int], value: T): ...
  36. @overload
  37. def __setitem__(self, index: vec2i, value: T): ...
  38. @overload
  39. def __setitem__(self, index: tuple[slice, slice], value: int | float | str | bool | None | 'array2d[T]'): ...
  40. @overload
  41. def __setitem__(self, mask: array2d[bool], value: T): ...
  42. def map[R](self, f: Callable[[T], R]) -> array2d[R]: ...
  43. def copy(self) -> 'array2d[T]': ...
  44. def fill_(self, value: T) -> None: ...
  45. def apply_(self, f: Callable[[T], T]) -> None: ...
  46. def copy_(self, other: array2d[T] | list[T]) -> None: ...
  47. def render(self) -> str: ...
  48. def all(self: array2d[bool]) -> bool: ...
  49. def any(self: array2d[bool]) -> bool: ...
  50. @staticmethod
  51. def fromlist(data: list[list[T]]) -> array2d[T]: ...
  52. def tolist(self) -> list[list[T]]: ...
  53. # algorithms
  54. def count(self, value: T) -> int:
  55. """Counts the number of cells with the given value."""
  56. def count_neighbors(self, value: T, neighborhood: Neighborhood) -> array2d[int]:
  57. """Counts the number of neighbors with the given value for each cell."""
  58. def get_bounding_rect(self, value: T) -> tuple[int, int, int, int]:
  59. """Gets the bounding rectangle of the given value.
  60. Returns a tuple `(x, y, width, height)` or raise `ValueError` if the value is not found.
  61. """
  62. def convolve(self: array2d[int], kernel: array2d[int], padding: int) -> array2d[int]:
  63. """Convolves the array with the given kernel."""
  64. def get_connected_components(self, value: T, neighborhood: Neighborhood) -> tuple[array2d[int], int]:
  65. """Gets connected components of the grid.
  66. Returns the `visited` array and the number of connected components,
  67. where `0` means unvisited, and non-zero means the index of the connected component.
  68. """