add array2d.render

This commit is contained in:
blueloveTH 2024-10-01 12:53:33 +08:00
parent 9c7fdceda3
commit b6caac01de
2 changed files with 21 additions and 0 deletions

View File

@ -52,6 +52,7 @@ class array2d(Generic[T]):
def copy_(self, other: 'array2d[T] | list[T]') -> None: ... def copy_(self, other: 'array2d[T] | list[T]') -> None: ...
def tolist(self) -> list[list[T]]: ... def tolist(self) -> list[list[T]]: ...
def render(self) -> str: ...
# algorithms # algorithms
def count(self, value: T) -> int: def count(self, value: T) -> int:

View File

@ -316,6 +316,25 @@ static bool array2d_tolist(int argc, py_Ref argv) {
return true; return true;
} }
static bool array2d_render(int argc, py_Ref argv){
PY_CHECK_ARGC(1);
c11_sbuf buf;
c11_sbuf__ctor(&buf);
c11_array2d* self = py_touserdata(argv);
for(int j = 0; j < self->n_rows; j++) {
for(int i = 0; i < self->n_cols; i++) {
py_Ref item = py_array2d__get(self, i, j);
if(!py_str(item)) return false;
c11_sbuf__write_sv(&buf, py_tosv(py_retval()));
}
if(j < self->n_rows - 1){
c11_sbuf__write_char(&buf, '\n');
}
}
c11_sbuf__py_submit(&buf, py_retval());
return true;
}
static bool array2d_count(int argc, py_Ref argv) { static bool array2d_count(int argc, py_Ref argv) {
// def count(self, value: T) -> int: ... // def count(self, value: T) -> int: ...
PY_CHECK_ARGC(2); PY_CHECK_ARGC(2);
@ -586,6 +605,7 @@ void pk__add_module_array2d() {
py_bindmethod(array2d, "copy_", array2d_copy_); py_bindmethod(array2d, "copy_", array2d_copy_);
py_bindmethod(array2d, "tolist", array2d_tolist); py_bindmethod(array2d, "tolist", array2d_tolist);
py_bindmethod(array2d, "render", array2d_render);
py_bindmethod(array2d, "count", array2d_count); py_bindmethod(array2d, "count", array2d_count);
py_bindmethod(array2d, "find_bounding_rect", array2d_find_bounding_rect); py_bindmethod(array2d, "find_bounding_rect", array2d_find_bounding_rect);
py_bindmethod(array2d, "count_neighbors", array2d_count_neighbors); py_bindmethod(array2d, "count_neighbors", array2d_count_neighbors);