fix names

This commit is contained in:
blueloveTH 2024-04-21 21:04:42 +08:00
parent d2baf1b145
commit d634ec9829
3 changed files with 8 additions and 8 deletions

View File

@ -2,7 +2,7 @@ from typing import Callable, Any, Generic, TypeVar, Literal, overload
T = TypeVar('T') T = TypeVar('T')
Neighborhood = Literal['moore', 'von_neumann'] Neighborhood = Literal['Moore', 'von Neumann']
class array2d(Generic[T]): class array2d(Generic[T]):
def __init__(self, n_cols: int, n_rows: int, default=None): ... def __init__(self, n_cols: int, n_rows: int, default=None): ...
@ -44,7 +44,7 @@ class array2d(Generic[T]):
def count(self, value: T) -> int: def count(self, value: T) -> int:
"""Counts the number of cells with the given value.""" """Counts the number of cells with the given value."""
def count_neighbors(self, value: T, neighborhood: Neighborhood = 'moore') -> 'array2d[int]': def count_neighbors(self, value: T, neighborhood: Neighborhood = 'Moore') -> 'array2d[int]':
"""Counts the number of neighbors with the given value for each cell.""" """Counts the number of neighbors with the given value for each cell."""
def find_bounding_rect(self, value: T) -> tuple[int, int, int, int] | None: def find_bounding_rect(self, value: T) -> tuple[int, int, int, int] | None:

View File

@ -264,14 +264,14 @@ struct Array2d{
return vm->True; return vm->True;
}); });
vm->bind(type, "count_neighbors(self, value, neighborhood='moore') -> array2d[int]", [](VM* vm, ArgsView args){ vm->bind(type, "count_neighbors(self, value, neighborhood='Moore') -> array2d[int]", [](VM* vm, ArgsView args){
Array2d& self = PK_OBJ_GET(Array2d, args[0]); Array2d& self = PK_OBJ_GET(Array2d, args[0]);
PyObject* new_array_obj = vm->heap.gcnew<Array2d>(Array2d::_type(vm)); PyObject* new_array_obj = vm->heap.gcnew<Array2d>(Array2d::_type(vm));
Array2d& new_array = PK_OBJ_GET(Array2d, new_array_obj); Array2d& new_array = PK_OBJ_GET(Array2d, new_array_obj);
new_array.init(self.n_cols, self.n_rows); new_array.init(self.n_cols, self.n_rows);
PyObject* value = args[1]; PyObject* value = args[1];
const Str& neighborhood = CAST(Str&, args[2]); const Str& neighborhood = CAST(Str&, args[2]);
if(neighborhood == "moore"){ if(neighborhood == "Moore"){
for(int j = 0; j < new_array.n_rows; j++){ for(int j = 0; j < new_array.n_rows; j++){
for(int i = 0; i < new_array.n_cols; i++){ for(int i = 0; i < new_array.n_cols; i++){
int count = 0; int count = 0;
@ -298,7 +298,7 @@ struct Array2d{
} }
} }
}else{ }else{
vm->ValueError("neighborhood must be 'moore' or 'von_neumann'"); vm->ValueError("neighborhood must be 'Moore' or 'von Neumann'");
} }
return new_array_obj; return new_array_obj;
}); });

View File

@ -108,7 +108,7 @@ assert A().get(0, 0, default=2) == 0
# test alive_neighbors # test alive_neighbors
a = array2d(3, 3, default=0) a = array2d(3, 3, default=0)
a[1, 1] = 1 a[1, 1] = 1
""" moore von_neumann """ Moore von Neumann
0 0 0 1 1 1 0 1 0 0 0 0 1 1 1 0 1 0
0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 1 0 1
0 0 0 1 1 1 0 1 0 0 0 0 1 1 1 0 1 0
@ -118,8 +118,8 @@ moore_result[1, 1] = 0
von_neumann_result = array2d(3, 3, default=0) von_neumann_result = array2d(3, 3, default=0)
von_neumann_result[0, 1] = von_neumann_result[1, 0] = von_neumann_result[1, 2] = von_neumann_result[2, 1] = 1 von_neumann_result[0, 1] = von_neumann_result[1, 0] = von_neumann_result[1, 2] = von_neumann_result[2, 1] = 1
a.count_neighbors(0, 'moore') == moore_result a.count_neighbors(0, 'Moore') == moore_result
a.count_neighbors(0, 'von_neumann') == von_neumann_result a.count_neighbors(0, 'von Neumann') == von_neumann_result
# test slice get # test slice get
a = array2d(5, 5, default=0) a = array2d(5, 5, default=0)