array2d.pyi 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. from typing import Callable, Literal, overload, Iterator
  2. from linalg import vec2i
  3. Neighborhood = Literal['Moore', 'von Neumann']
  4. class array2d_like[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 shape(self) -> vec2i: ...
  15. @property
  16. def numel(self) -> int: ...
  17. @overload
  18. def is_valid(self, col: int, row: int) -> bool: ...
  19. @overload
  20. def is_valid(self, pos: vec2i) -> bool: ...
  21. def get[R](self, col: int, row: int, default: R = None) -> T | R:
  22. """Get the value at the given position.
  23. If the position is out of bounds, return the default value.
  24. """
  25. def render(self) -> str: ...
  26. def all(self: array2d_like[bool]) -> bool: ...
  27. def any(self: array2d_like[bool]) -> bool: ...
  28. def map[R](self, f: Callable[[T], R]) -> array2d[R]: ...
  29. def apply(self, f: Callable[[T], T]) -> None: ...
  30. def zip_with[R, U](self, other: array2d_like[U], f: Callable[[T, U], R]) -> array2d[R]: ...
  31. def copy(self) -> 'array2d[T]': ...
  32. def tolist(self) -> list[list[T]]: ...
  33. def __eq__(self, other: object) -> array2d[bool]: ... # type: ignore
  34. def __ne__(self, other: object) -> array2d[bool]: ... # type: ignore
  35. def __iter__(self) -> Iterator[tuple[vec2i, T]]: ...
  36. def __repr__(self) -> str: ...
  37. @overload
  38. def __getitem__(self, index: vec2i) -> T: ...
  39. @overload
  40. def __getitem__(self, index: tuple[int, int]) -> T: ...
  41. @overload
  42. def __getitem__(self, index: tuple[slice, slice]) -> array2d_view[T]: ...
  43. @overload
  44. def __getitem__(self, index: tuple[slice, int] | tuple[int, slice]) -> array2d_view[T]: ...
  45. @overload
  46. def __getitem__(self, mask: array2d_like[bool]) -> list[T]: ...
  47. @overload
  48. def __setitem__(self, index: vec2i, value: T): ...
  49. @overload
  50. def __setitem__(self, index: tuple[int, int], value: T): ...
  51. @overload
  52. def __setitem__(self, index: tuple[slice, slice], value: T | 'array2d_like[T]'): ...
  53. @overload
  54. def __setitem__(self, index: tuple[slice, int] | tuple[int, slice], value: T | 'array2d_like[T]'): ...
  55. @overload
  56. def __setitem__(self, mask: array2d_like[bool], value: T): ...
  57. # algorithms
  58. def count(self, value: T) -> int:
  59. """Count the number of cells with the given value."""
  60. def count_neighbors(self, value: T, neighborhood: Neighborhood) -> array2d[int]:
  61. """Count the number of neighbors with the given value for each cell."""
  62. def get_bounding_rect(self, value: T) -> tuple[int, int, int, int]:
  63. """Get the bounding rectangle of the given value.
  64. Returns a tuple `(x, y, width, height)` or raise `ValueError` if the value is not found.
  65. """
  66. def convolve(self: array2d_like[int], kernel: array2d_like[int], padding: int) -> array2d[int]:
  67. """Convolve the array with the given kernel."""
  68. def get_connected_components(self, value: T, neighborhood: Neighborhood) -> tuple[array2d[int], int]:
  69. """Get connected components of the grid.
  70. Returns the `visited` array and the number of connected components,
  71. where `0` means unvisited, and non-zero means the index of the connected component.
  72. """
  73. class array2d_view[T](array2d_like[T]):
  74. @property
  75. def origin(self) -> vec2i: ...
  76. class array2d[T](array2d_like[T]):
  77. def __new__(
  78. cls,
  79. n_cols: int,
  80. n_rows: int,
  81. default: T | Callable[[vec2i], T] | None = None
  82. ): ...
  83. @staticmethod
  84. def fromlist(data: list[list[T]]) -> array2d[T]: ...
  85. class chunked_array2d[T, TContext]:
  86. def __new__(
  87. cls,
  88. chunk_size: int,
  89. default: T = None,
  90. context_builder: Callable[[vec2i], TContext] | None = None,
  91. ): ...
  92. @property
  93. def chunk_size(self) -> int: ...
  94. def __getitem__(self, index: vec2i) -> T: ...
  95. def __setitem__(self, index: vec2i, value: T): ...
  96. def __delitem__(self, index: vec2i): ...
  97. def __iter__(self) -> Iterator[tuple[vec2i, TContext]]: ...
  98. def __len__(self) -> int: ...
  99. def clear(self) -> None: ...
  100. def world_to_chunk(self, world_pos: vec2i) -> tuple[vec2i, vec2i]:
  101. """Convert world position to chunk position and local position."""
  102. def add_chunk(self, chunk_pos: vec2i) -> TContext: ...
  103. def remove_chunk(self, chunk_pos: vec2i) -> bool: ...
  104. def move_chunk(self, src_chunk_pos: vec2i, dst_chunk_pos: vec2i) -> bool: ...
  105. def get_context(self, chunk_pos: vec2i) -> TContext | None: ...
  106. def view(self) -> array2d_view[T]: ...
  107. def view_rect(self, pos: vec2i, width: int, height: int) -> array2d_view[T]: ...
  108. def view_chunk(self, chunk_pos: vec2i) -> array2d_view[T]: ...
  109. def view_chunks(self, chunk_pos: vec2i, width: int, height: int) -> array2d_view[T]: ...