array2d.pyi 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. from typing import Callable, Literal, overload, Iterator, Self
  2. from vmath import vec2i, color32
  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, returns the default value.
  24. """
  25. def index(self, value: T) -> vec2i:
  26. """Get the position of the first occurrence of the given value.
  27. Raises `ValueError` if the value is not found.
  28. """
  29. def render(self) -> str: ...
  30. def render_with_color(self, fg: array2d_like[color32 | None], bg: array2d_like[color32 | None]) -> str: ...
  31. def all(self: array2d_like[bool]) -> bool: ...
  32. def any(self: array2d_like[bool]) -> bool: ...
  33. def map[R](self, f: Callable[[T], R]) -> array2d[R]: ...
  34. def apply(self, f: Callable[[T], T]) -> None: ...
  35. def zip_with[R, U](self, other: array2d_like[U], f: Callable[[T, U], R]) -> array2d[R]: ...
  36. def copy(self) -> 'array2d[T]': ...
  37. def tolist(self) -> list[list[T]]: ...
  38. def __le__(self, other: T | array2d_like[T]) -> array2d[bool]: ...
  39. def __lt__(self, other: T | array2d_like[T]) -> array2d[bool]: ...
  40. def __ge__(self, other: T | array2d_like[T]) -> array2d[bool]: ...
  41. def __gt__(self, other: T | array2d_like[T]) -> array2d[bool]: ...
  42. def __eq__(self, other: T | array2d_like[T]) -> array2d[bool]: ... # type: ignore
  43. def __ne__(self, other: T | array2d_like[T]) -> array2d[bool]: ... # type: ignore
  44. def __add__(self, other: T | array2d_like[T]) -> array2d[T]: ...
  45. def __sub__(self, other: T | array2d_like[T]) -> array2d[T]: ...
  46. def __mul__(self, other: T | array2d_like[T]) -> array2d[T]: ...
  47. def __truediv__(self, other: T | array2d_like[T]) -> array2d[T]: ...
  48. def __floordiv__(self, other: T | array2d_like[T]) -> array2d[T]: ...
  49. def __mod__(self, other: T | array2d_like[T]) -> array2d[T]: ...
  50. def __pow__(self, other: T | array2d_like[T]) -> array2d[T]: ...
  51. def __and__(self, other: T | array2d_like[T]) -> array2d[T]: ...
  52. def __or__(self, other: T | array2d_like[T]) -> array2d[T]: ...
  53. def __xor__(self, other: T | array2d_like[T]) -> array2d[T]: ...
  54. def __invert__(self) -> array2d[T]: ...
  55. def __iter__(self) -> Iterator[tuple[vec2i, T]]: ...
  56. def __repr__(self) -> str: ...
  57. @overload
  58. def __getitem__(self, index: vec2i) -> T: ...
  59. @overload
  60. def __getitem__(self, index: tuple[int, int]) -> T: ...
  61. @overload
  62. def __getitem__(self, index: tuple[slice, slice]) -> array2d_view[T]: ...
  63. @overload
  64. def __getitem__(self, index: tuple[slice, int] | tuple[int, slice]) -> array2d_view[T]: ...
  65. @overload
  66. def __getitem__(self, mask: array2d_like[bool]) -> list[T]: ...
  67. @overload
  68. def __setitem__(self, index: vec2i, value: T): ...
  69. @overload
  70. def __setitem__(self, index: tuple[int, int], value: T): ...
  71. @overload
  72. def __setitem__(self, index: tuple[slice, slice], value: T | 'array2d_like[T]'): ...
  73. @overload
  74. def __setitem__(self, index: tuple[slice, int] | tuple[int, slice], value: T | 'array2d_like[T]'): ...
  75. @overload
  76. def __setitem__(self, mask: array2d_like[bool], value: T): ...
  77. # algorithms
  78. def count(self, value: T) -> int:
  79. """Count the number of cells with the given value."""
  80. def count_neighbors(self, value: T, neighborhood: Neighborhood) -> array2d[int]:
  81. """Count the number of neighbors with the given value for each cell."""
  82. def get_bounding_rect(self, value: T) -> tuple[int, int, int, int]:
  83. """Get the bounding rectangle of the given value.
  84. Returns a tuple `(x, y, width, height)` or raise `ValueError` if the value is not found.
  85. """
  86. def convolve(self: array2d_like[int], kernel: array2d_like[int], padding: int) -> array2d[int]:
  87. """Convolve the array with the given kernel."""
  88. def get_connected_components(self, value: T, neighborhood: Neighborhood) -> tuple[array2d[int], int]:
  89. """Get connected components of the grid via BFS algorithm.
  90. Returns the `visited` array and the number of connected components,
  91. where `0` means unvisited, and non-zero means the index of the connected component.
  92. """
  93. class array2d_view[T](array2d_like[T]):
  94. @property
  95. def origin(self) -> vec2i: ...
  96. class array2d[T](array2d_like[T]):
  97. def __new__(
  98. cls,
  99. n_cols: int,
  100. n_rows: int,
  101. default: T | Callable[[vec2i], T] | None = None
  102. ): ...
  103. @staticmethod
  104. def fromlist(data: list[list[T]]) -> array2d[T]: ...
  105. class chunked_array2d[T, TContext]:
  106. def __new__(
  107. cls,
  108. chunk_size: int,
  109. default: T = None,
  110. context_builder: Callable[[vec2i], TContext] | None = None,
  111. ): ...
  112. @property
  113. def chunk_size(self) -> int: ...
  114. @property
  115. def default(self) -> T: ...
  116. @property
  117. def context_builder(self) -> Callable[[vec2i], TContext] | None: ...
  118. def __getitem__(self, index: vec2i) -> T: ...
  119. def __setitem__(self, index: vec2i, value: T): ...
  120. def __delitem__(self, index: vec2i): ...
  121. def __iter__(self) -> Iterator[tuple[vec2i, TContext]]: ...
  122. def __len__(self) -> int: ...
  123. def clear(self) -> None: ...
  124. def copy(self) -> Self: ...
  125. def world_to_chunk(self, world_pos: vec2i) -> tuple[vec2i, vec2i]:
  126. """Converts world position to chunk position and local position."""
  127. def add_chunk(self, chunk_pos: vec2i) -> TContext: ...
  128. def remove_chunk(self, chunk_pos: vec2i) -> bool: ...
  129. def move_chunk(self, src_chunk_pos: vec2i, dst_chunk_pos: vec2i) -> bool: ...
  130. def get_context(self, chunk_pos: vec2i) -> TContext | None: ...
  131. def view(self) -> array2d_view[T]: ...
  132. def view_rect(self, pos: vec2i, width: int, height: int) -> array2d_view[T]: ...
  133. def view_chunk(self, chunk_pos: vec2i) -> array2d_view[T]: ...
  134. def view_chunks(self, chunk_pos: vec2i, width: int, height: int) -> array2d_view[T]: ...