mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-19 19:10:17 +00:00
...
This commit is contained in:
parent
cfac4cbbca
commit
3a2a9c4e13
@ -70,11 +70,5 @@ class array2d(Generic[T]):
|
||||
Returns a tuple `(x, y, width, height)` or raise `ValueError` if the value is not found.
|
||||
"""
|
||||
|
||||
def find_one(self, condition: Callable[[T], bool]) -> vec2i:
|
||||
"""Finds the position of the first cell that satisfies the condition.
|
||||
|
||||
Returns a `vec2i` or raise `ValueError` if no cell satisfies the condition.
|
||||
"""
|
||||
|
||||
def convolve(self: array2d[int], kernel: 'array2d[int]', padding: int) -> 'array2d[int]':
|
||||
"""Convolves the array with the given kernel."""
|
||||
|
@ -568,28 +568,6 @@ static bool array2d__setitem__(int argc, py_Ref argv) {
|
||||
}
|
||||
}
|
||||
|
||||
// find_one(self, condition: Callable[[T], bool]) -> vec2i
|
||||
static bool array2d_find_one(int argc, py_Ref argv) {
|
||||
PY_CHECK_ARGC(2);
|
||||
c11_array2d* self = py_touserdata(argv);
|
||||
py_Ref condition = py_arg(1);
|
||||
for(int j = 0; j < self->n_rows; j++) {
|
||||
for(int i = 0; i < self->n_cols; i++) {
|
||||
bool ok = py_call(condition, 1, py_array2d__get(self, i, j));
|
||||
if(!ok) return false;
|
||||
if(!py_isbool(py_retval())) return TypeError("condition must return a bool");
|
||||
if(py_tobool(py_retval())) {
|
||||
py_newvec2i(py_retval(),
|
||||
(c11_vec2i){
|
||||
{i, j}
|
||||
});
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return ValueError("condition not met");
|
||||
}
|
||||
|
||||
static bool _array2d_is_all_ints(c11_array2d* self) {
|
||||
for(int i = 0; i < self->numel; i++) {
|
||||
if(!py_isint(self->data + i)) return false;
|
||||
@ -684,7 +662,6 @@ void pk__add_module_array2d() {
|
||||
py_bindmethod(array2d, "count", array2d_count);
|
||||
py_bindmethod(array2d, "find_bounding_rect", array2d_find_bounding_rect);
|
||||
py_bindmethod(array2d, "count_neighbors", array2d_count_neighbors);
|
||||
py_bindmethod(array2d, "find_one", array2d_find_one);
|
||||
py_bindmethod(array2d, "convolve", array2d_convolve);
|
||||
}
|
||||
|
||||
|
@ -96,16 +96,6 @@ assert x == d and x is not d
|
||||
x.copy_([1, 2, 3, 4, 5, 6, 7, 8])
|
||||
assert x.tolist() == [[1, 2], [3, 4], [5, 6], [7, 8]]
|
||||
|
||||
# test find_one
|
||||
a = array2d(3, 3, default=0)
|
||||
a[1, 1] = 1
|
||||
assert a.find_one(lambda x: x == 1) == vec2i(1, 1)
|
||||
try:
|
||||
a.find_one(lambda x: x == 2)
|
||||
exit(1)
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
# test alive_neighbors
|
||||
a = array2d[int](3, 3, default=0)
|
||||
a[1, 1] = 1
|
||||
|
Loading…
x
Reference in New Issue
Block a user