array2d.pyi 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. def is_valid(self, col: int, row: int) -> bool: ...
  21. def get(self, col: int, row: int, default=None) -> T | None:
  22. """Returns the value at the given position or the default value if out of bounds."""
  23. def unsafe_get(self, col: int, row: int) -> T:
  24. """Returns the value at the given position without bounds checking."""
  25. def unsafe_set(self, col: int, row: int, value: T):
  26. """Sets the value at the given position without bounds checking."""
  27. @overload
  28. def __getitem__(self, index: tuple[int, int]) -> T: ...
  29. @overload
  30. def __getitem__(self, index: vec2i) -> T: ...
  31. @overload
  32. def __getitem__(self, index: tuple[slice, slice]) -> 'array2d[T]': ...
  33. @overload
  34. def __setitem__(self, index: tuple[int, int], value: T): ...
  35. @overload
  36. def __setitem__(self, index: vec2i, value: T): ...
  37. @overload
  38. def __setitem__(self, index: tuple[slice, slice], value: int | float | str | bool | None | 'array2d[T]'): ...
  39. def map(self, f: Callable[[T], Any]) -> 'array2d': ...
  40. def copy(self) -> 'array2d[T]': ...
  41. def fill_(self, value: T) -> None: ...
  42. def apply_(self, f: Callable[[T], T]) -> None: ...
  43. def copy_(self, other: 'array2d[T] | list[T]') -> None: ...
  44. def tolist(self) -> list[list[T]]: ...
  45. # algorithms
  46. def count(self, value: T) -> int:
  47. """Counts the number of cells with the given value."""
  48. def count_neighbors(self, value: T, neighborhood: Neighborhood) -> 'array2d[int]':
  49. """Counts the number of neighbors with the given value for each cell."""
  50. def find_bounding_rect(self, value: T) -> tuple[int, int, int, int] | None:
  51. """Finds the bounding rectangle of the given value.
  52. Returns a tuple `(x, y, width, height)` or `None` if the value is not found.
  53. """