mirror of
https://github.com/pocketpy/pocketpy
synced 2025-11-07 12:10:17 +00:00
Compare commits
3 Commits
f4597ed01a
...
d3bc8a53ad
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d3bc8a53ad | ||
|
|
9ff48c7dba | ||
|
|
e1fe3cd4dc |
@ -15,7 +15,7 @@ class array2d[T]:
|
||||
@property
|
||||
def numel(self) -> int: ...
|
||||
|
||||
def __new__(cls, n_cols: int, n_rows: int, default: Callable[[vec2i], T] = None): ...
|
||||
def __new__(cls, n_cols: int, n_rows: int, default: T | Callable[[vec2i], T] | None = None): ...
|
||||
def __eq__(self, other: object) -> array2d[bool]: ... # type: ignore
|
||||
def __ne__(self, other: object) -> array2d[bool]: ... # type: ignore
|
||||
def __repr__(self) -> str: ...
|
||||
@ -26,10 +26,8 @@ class array2d[T]:
|
||||
@overload
|
||||
def is_valid(self, pos: vec2i) -> bool: ...
|
||||
|
||||
@overload
|
||||
def get[R](self, col: int, row: int, default: R) -> T | R: ...
|
||||
@overload
|
||||
def get[R](self, pos: vec2i, default: R) -> T | R: ...
|
||||
def get[R](self, col: int, row: int, default: R = None) -> T | R:
|
||||
"""Gets the value at the given position. If the position is out of bounds, return the default value."""
|
||||
|
||||
@overload
|
||||
def __getitem__(self, index: tuple[int, int]) -> T: ...
|
||||
|
||||
@ -118,24 +118,17 @@ static bool array2d_is_valid(int argc, py_Ref argv) {
|
||||
static bool array2d_get(int argc, py_Ref argv) {
|
||||
py_Ref default_;
|
||||
c11_array2d* self = py_touserdata(argv);
|
||||
int col, row;
|
||||
PY_CHECK_ARG_TYPE(1, tp_int);
|
||||
PY_CHECK_ARG_TYPE(2, tp_int);
|
||||
if(argc == 3) {
|
||||
// get[R](self, pos: vec2i, default: R) -> T | R
|
||||
PY_CHECK_ARG_TYPE(1, tp_vec2i);
|
||||
c11_vec2i pos = py_tovec2i(py_arg(1));
|
||||
col = pos.x;
|
||||
row = pos.y;
|
||||
default_ = py_arg(2);
|
||||
default_ = py_None();
|
||||
} else if(argc == 4) {
|
||||
// get(self, col: int, row: int, default: T) -> T
|
||||
PY_CHECK_ARG_TYPE(1, tp_int);
|
||||
PY_CHECK_ARG_TYPE(2, tp_int);
|
||||
col = py_toint(py_arg(1));
|
||||
row = py_toint(py_arg(2));
|
||||
default_ = py_arg(3);
|
||||
} else {
|
||||
return TypeError("get() expected 3 or 4 arguments");
|
||||
return TypeError("get() expected 2 or 3 arguments");
|
||||
}
|
||||
int col = py_toint(py_arg(1));
|
||||
int row = py_toint(py_arg(2));
|
||||
if(py_array2d_is_valid(self, col, row)) {
|
||||
py_assign(py_retval(), py_array2d__get(self, col, row));
|
||||
} else {
|
||||
|
||||
@ -30,9 +30,9 @@ assert not a.is_valid(0, -1) and not a.is_valid(vec2i(0, -1))
|
||||
|
||||
# test get
|
||||
assert a.get(0, 0, -1) == (0, 0)
|
||||
assert a.get(vec2i(1, 3), -1) == (1, 3)
|
||||
assert a.get(2, 0, None) is None
|
||||
assert a.get(vec2i(0, 4), 'S') == 'S'
|
||||
assert a.get(1, 3) == (1, 3)
|
||||
assert a.get(2, 0) is None
|
||||
assert a.get(0, 4, 'S') == 'S'
|
||||
|
||||
# test __getitem__
|
||||
assert a[0, 0] == (0, 0)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user