array2d.pyi 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 copy(self) -> 'array2d[T]': ...
  31. def tolist(self) -> list[list[T]]: ...
  32. def __eq__(self, other: object) -> array2d[bool]: ... # type: ignore
  33. def __ne__(self, other: object) -> array2d[bool]: ... # type: ignore
  34. def __iter__(self) -> Iterator[tuple[vec2i, T]]: ...
  35. def __repr__(self) -> str: ...
  36. @overload
  37. def __getitem__(self, index: vec2i) -> T: ...
  38. @overload
  39. def __getitem__(self, index: tuple[int, int]) -> T: ...
  40. @overload
  41. def __getitem__(self, index: tuple[slice, slice]) -> array2d_view[T]: ...
  42. @overload
  43. def __getitem__(self, mask: array2d_like[bool]) -> list[T]: ...
  44. @overload
  45. def __setitem__(self, index: vec2i, value: T): ...
  46. @overload
  47. def __setitem__(self, index: tuple[int, int], value: T): ...
  48. @overload
  49. def __setitem__(self, index: tuple[slice, slice], value: T | 'array2d_like[T]'): ...
  50. @overload
  51. def __setitem__(self, mask: array2d_like[bool], value: T): ...
  52. # algorithms
  53. def count(self, value: T) -> int:
  54. """Count the number of cells with the given value."""
  55. def count_neighbors(self, value: T, neighborhood: Neighborhood) -> array2d[int]:
  56. """Count the number of neighbors with the given value for each cell."""
  57. def get_bounding_rect(self, value: T) -> tuple[int, int, int, int]:
  58. """Get the bounding rectangle of the given value.
  59. Returns a tuple `(x, y, width, height)` or raise `ValueError` if the value is not found.
  60. """
  61. def convolve(self: array2d_like[int], kernel: array2d_like[int], padding: int) -> array2d[int]:
  62. """Convolve the array with the given kernel."""
  63. def get_connected_components(self, value: T, neighborhood: Neighborhood) -> tuple[array2d[int], int]:
  64. """Get connected components of the grid.
  65. Returns the `visited` array and the number of connected components,
  66. where `0` means unvisited, and non-zero means the index of the connected component.
  67. """
  68. class array2d_view[T](array2d_like[T]):
  69. @property
  70. def origin(self) -> vec2i: ...
  71. class array2d[T](array2d_like[T]):
  72. def __new__(
  73. cls,
  74. n_cols: int,
  75. n_rows: int,
  76. default: T | Callable[[vec2i], T] | None = None
  77. ): ...
  78. @staticmethod
  79. def fromlist(data: list[list[T]]) -> array2d[T]: ...
  80. class chunked_array2d[T, TContext]:
  81. def __init__(
  82. self,
  83. chunk_size: int,
  84. default: T = None,
  85. context_builder: Callable[[vec2i], TContext] | None = None,
  86. ): ...
  87. @property
  88. def chunk_size(self) -> int: ...
  89. def __getitem__(self, index: vec2i) -> T: ...
  90. def __setitem__(self, index: vec2i, value: T): ...
  91. def __delitem__(self, index: vec2i): ...
  92. def __iter__(self) -> Iterator[tuple[vec2i, TContext]]: ...
  93. def clear(self) -> None: ...
  94. def world_to_chunk(self, world_pos: vec2i) -> tuple[vec2i, vec2i]:
  95. """Convert world position to chunk position and local position."""
  96. def add_chunk(self, chunk_pos: vec2i) -> TContext: ...
  97. def remove_chunk(self, chunk_pos: vec2i) -> bool: ...
  98. def get_context(self, chunk_pos: vec2i) -> TContext | None: ...
  99. def view(self) -> array2d_view[T]: ...
  100. def view_rect(self, pos: vec2i, width: int, height: int) -> array2d_view[T]: ...
  101. def view_chunk(self, chunk_pos: vec2i) -> array2d_view[T]: ...
  102. def view_chunks(self, chunk_pos: vec2i, width: int, height: int) -> array2d_view[T]: ...