array2d.pyi 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 __repr__(self) -> str: ...
  19. def __iter__(self) -> Iterator[tuple[int, int, 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(self, col: int, row: int, default=None) -> T | None:
  25. """Returns the value at the given position or the default value if out of bounds."""
  26. def unsafe_get(self, col: int, row: int) -> T:
  27. """Returns the value at the given position without bounds checking."""
  28. def unsafe_set(self, col: int, row: int, value: T):
  29. """Sets the value at the given position without bounds checking."""
  30. @overload
  31. def __getitem__(self, index: tuple[int, int]) -> T: ...
  32. @overload
  33. def __getitem__(self, index: vec2i) -> T: ...
  34. @overload
  35. def __getitem__(self, index: tuple[slice, slice]) -> 'array2d[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. def map(self, f: Callable[[T], Any]) -> 'array2d': ...
  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 tolist(self) -> list[list[T]]: ...
  48. def render(self) -> str: ...
  49. # algorithms
  50. def count(self, value: T) -> int:
  51. """Counts the number of cells with the given value."""
  52. def count_neighbors(self, value: T, neighborhood: Neighborhood) -> 'array2d[int]':
  53. """Counts the number of neighbors with the given value for each cell."""
  54. def find_bounding_rect(self, value: T) -> tuple[int, int, int, int]:
  55. """Finds the bounding rectangle of the given value.
  56. Returns a tuple `(x, y, width, height)` or raise `ValueError` if the value is not found.
  57. """