array2d.pyi 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. from typing import Callable, Any, Generic, TypeVar, Literal, overload
  2. T = TypeVar('T')
  3. Neighborhood = Literal['moore', 'von_neumann']
  4. class array2d(Generic[T]):
  5. def __init__(self, n_cols: int, n_rows: int, default=None): ...
  6. @property
  7. def width(self) -> int: ...
  8. @property
  9. def height(self) -> int: ...
  10. @property
  11. def numel(self) -> int: ...
  12. def is_valid(self, col: int, row: int) -> bool: ...
  13. def get(self, col: int, row: int, default=None): ...
  14. @overload
  15. def __getitem__(self, index: tuple[int, int]): ...
  16. @overload
  17. def __getitem__(self, index: tuple[slice, slice]) -> 'array2d[T]': ...
  18. @overload
  19. def __setitem__(self, index: tuple[int, int], value: T): ...
  20. @overload
  21. def __setitem__(self, index: tuple[slice, slice], value: int | float | str | bool | None | 'array2d[T]'): ...
  22. def __len__(self) -> int: ...
  23. def __eq__(self, other: 'array2d') -> bool: ...
  24. def __ne__(self, other: 'array2d') -> bool: ...
  25. def __repr__(self): ...
  26. def tolist(self) -> list[list[T]]: ...
  27. def map(self, f: Callable[[T], Any]) -> 'array2d': ...
  28. def copy(self) -> 'array2d[T]': ...
  29. def fill_(self, value: T) -> None: ...
  30. def apply_(self, f: Callable[[T], T]) -> None: ...
  31. def copy_(self, other: 'array2d[T] | list[T]') -> None: ...
  32. # algorithms
  33. def count(self, value: T) -> int:
  34. """Counts the number of cells with the given value."""
  35. def count_neighbors(self, value: T, neighborhood: Neighborhood = 'moore') -> 'array2d[int]':
  36. """Counts the number of neighbors with the given value for each cell."""
  37. def find_bounding_rect(self, value: T) -> tuple[int, int, int, int] | None:
  38. """Finds the bounding rectangle of the given value.
  39. Returns a tuple `(x, y, width, height)` or `None` if the value is not found.
  40. """