array2d.pyi 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. from typing import Callable, Any, Generic, TypeVar, Literal, overload, Iterator
  2. T = TypeVar('T')
  3. Neighborhood = Literal['Moore', 'von Neumann']
  4. class array2d(Generic[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__(self, n_cols: int, n_rows: int, default=None): ...
  16. def __len__(self) -> int: ...
  17. def __eq__(self, other: 'array2d') -> bool: ...
  18. def __ne__(self, other: 'array2d') -> bool: ...
  19. def __repr__(self) -> str: ...
  20. def __iter__(self) -> Iterator[tuple[int, int, T]]: ...
  21. def is_valid(self, col: int, row: int) -> bool: ...
  22. def get(self, col: int, row: int, default=None) -> T | None:
  23. """Returns the value at the given position or the default value if out of bounds."""
  24. def unsafe_get(self, col: int, row: int) -> T:
  25. """Returns the value at the given position without bounds checking."""
  26. def unsafe_set(self, col: int, row: int, value: T):
  27. """Sets the value at the given position without bounds checking."""
  28. @overload
  29. def __getitem__(self, index: tuple[int, int]) -> T: ...
  30. @overload
  31. def __getitem__(self, index: tuple[slice, slice]) -> 'array2d[T]': ...
  32. @overload
  33. def __setitem__(self, index: tuple[int, int], value: T): ...
  34. @overload
  35. def __setitem__(self, index: tuple[slice, slice], value: int | float | str | bool | None | 'array2d[T]'): ...
  36. def map(self, f: Callable[[T], Any]) -> 'array2d': ...
  37. def copy(self) -> 'array2d[T]': ...
  38. def fill_(self, value: T) -> None: ...
  39. def apply_(self, f: Callable[[T], T]) -> None: ...
  40. def copy_(self, other: 'array2d[T] | list[T]') -> None: ...
  41. def tolist(self) -> list[list[T]]: ...
  42. # algorithms
  43. def count(self, value: T) -> int:
  44. """Counts the number of cells with the given value."""
  45. def count_neighbors(self, value: T, neighborhood: Neighborhood) -> 'array2d[int]':
  46. """Counts the number of neighbors with the given value for each cell."""
  47. def find_bounding_rect(self, value: T) -> tuple[int, int, int, int] | None:
  48. """Finds the bounding rectangle of the given value.
  49. Returns a tuple `(x, y, width, height)` or `None` if the value is not found.
  50. """