array2d.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333
  1. #include "pocketpy/interpreter/array2d.h"
  2. #include "pocketpy/interpreter/vm.h"
  3. #include "pocketpy/pocketpy.h"
  4. #include <limits.h>
  5. static bool c11_array2d_like_is_valid(c11_array2d_like* self, unsigned int col, unsigned int row) {
  6. return col < self->n_cols && row < self->n_rows;
  7. }
  8. static py_Ref c11_array2d__get(c11_array2d* self, int col, int row) {
  9. return self->data + row * self->header.n_cols + col;
  10. }
  11. static bool c11_array2d__set(c11_array2d* self, int col, int row, py_Ref value) {
  12. self->data[row * self->header.n_cols + col] = *value;
  13. return true;
  14. }
  15. c11_array2d* py_newarray2d(py_OutRef out, int n_cols, int n_rows) {
  16. int numel = n_cols * n_rows;
  17. c11_array2d* ud = py_newobject(out, tp_array2d, numel, sizeof(c11_array2d));
  18. ud->header.n_cols = n_cols;
  19. ud->header.n_rows = n_rows;
  20. ud->header.numel = numel;
  21. ud->header.f_get = (py_Ref(*)(c11_array2d_like*, int, int))c11_array2d__get;
  22. ud->header.f_set = (bool (*)(c11_array2d_like*, int, int, py_Ref))c11_array2d__set;
  23. ud->data = py_getslot(out, 0);
  24. return ud;
  25. }
  26. /* array2d_like bindings */
  27. static bool array2d_like_n_cols(int argc, py_Ref argv) {
  28. PY_CHECK_ARGC(1);
  29. c11_array2d_like* self = py_touserdata(argv);
  30. py_newint(py_retval(), self->n_cols);
  31. return true;
  32. }
  33. static bool array2d_like_n_rows(int argc, py_Ref argv) {
  34. PY_CHECK_ARGC(1);
  35. c11_array2d_like* self = py_touserdata(argv);
  36. py_newint(py_retval(), self->n_rows);
  37. return true;
  38. }
  39. static bool array2d_like_shape(int argc, py_Ref argv) {
  40. PY_CHECK_ARGC(1);
  41. c11_array2d_like* self = py_touserdata(argv);
  42. c11_vec2i shape;
  43. shape.x = self->n_cols;
  44. shape.y = self->n_rows;
  45. py_newvec2i(py_retval(), shape);
  46. return true;
  47. }
  48. static bool array2d_like_numel(int argc, py_Ref argv) {
  49. PY_CHECK_ARGC(1);
  50. c11_array2d_like* self = py_touserdata(argv);
  51. py_newint(py_retval(), self->numel);
  52. return true;
  53. }
  54. static bool array2d_like_is_valid(int argc, py_Ref argv) {
  55. c11_array2d_like* self = py_touserdata(argv);
  56. int col, row;
  57. if(argc == 2) {
  58. PY_CHECK_ARG_TYPE(1, tp_vec2i);
  59. c11_vec2i pos = py_tovec2i(py_arg(1));
  60. col = pos.x;
  61. row = pos.y;
  62. } else if(argc == 3) {
  63. PY_CHECK_ARG_TYPE(1, tp_int);
  64. PY_CHECK_ARG_TYPE(2, tp_int);
  65. col = py_toint(py_arg(1));
  66. row = py_toint(py_arg(2));
  67. } else {
  68. return TypeError("is_valid() expected 2 or 3 arguments");
  69. }
  70. py_newbool(py_retval(), c11_array2d_like_is_valid(self, col, row));
  71. return true;
  72. }
  73. static bool array2d_like_get(int argc, py_Ref argv) {
  74. PY_CHECK_ARG_TYPE(1, tp_int);
  75. PY_CHECK_ARG_TYPE(2, tp_int);
  76. py_Ref default_;
  77. c11_array2d_like* self = py_touserdata(argv);
  78. if(argc == 3) {
  79. default_ = py_None();
  80. } else if(argc == 4) {
  81. default_ = py_arg(3);
  82. } else {
  83. return TypeError("get() expected 2 or 3 arguments");
  84. }
  85. int col = py_toint(py_arg(1));
  86. int row = py_toint(py_arg(2));
  87. if(c11_array2d_like_is_valid(self, col, row)) {
  88. py_assign(py_retval(), self->f_get(self, col, row));
  89. } else {
  90. py_assign(py_retval(), default_);
  91. }
  92. return true;
  93. }
  94. static bool array2d_like_render(int argc, py_Ref argv) {
  95. PY_CHECK_ARGC(1);
  96. c11_sbuf buf;
  97. c11_sbuf__ctor(&buf);
  98. c11_array2d_like* self = py_touserdata(argv);
  99. for(int j = 0; j < self->n_rows; j++) {
  100. for(int i = 0; i < self->n_cols; i++) {
  101. py_Ref item = self->f_get(self, i, j);
  102. if(!py_str(item)) return false;
  103. c11_sbuf__write_sv(&buf, py_tosv(py_retval()));
  104. }
  105. if(j < self->n_rows - 1) c11_sbuf__write_char(&buf, '\n');
  106. }
  107. c11_sbuf__py_submit(&buf, py_retval());
  108. return true;
  109. }
  110. static bool array2d_like_all(int argc, py_Ref argv) {
  111. PY_CHECK_ARGC(1);
  112. c11_array2d_like* self = py_touserdata(argv);
  113. for(int j = 0; j < self->n_rows; j++) {
  114. for(int i = 0; i < self->n_cols; i++) {
  115. py_Ref item = self->f_get(self, i, j);
  116. if(!py_checkbool(item)) return false;
  117. if(!py_tobool(item)) {
  118. py_newbool(py_retval(), false);
  119. return true;
  120. }
  121. }
  122. }
  123. py_newbool(py_retval(), true);
  124. return true;
  125. }
  126. static bool array2d_like_any(int argc, py_Ref argv) {
  127. PY_CHECK_ARGC(1);
  128. c11_array2d_like* self = py_touserdata(argv);
  129. for(int j = 0; j < self->n_rows; j++) {
  130. for(int i = 0; i < self->n_cols; i++) {
  131. py_Ref item = self->f_get(self, i, j);
  132. if(!py_checkbool(item)) return false;
  133. if(py_tobool(item)) {
  134. py_newbool(py_retval(), true);
  135. return true;
  136. }
  137. }
  138. }
  139. py_newbool(py_retval(), false);
  140. return true;
  141. }
  142. static bool array2d_like_map(int argc, py_Ref argv) {
  143. // def map(self, f: Callable[[T], Any]) -> 'array2d': ...
  144. PY_CHECK_ARGC(2);
  145. c11_array2d_like* self = py_touserdata(argv);
  146. py_Ref f = py_arg(1);
  147. c11_array2d* res = py_newarray2d(py_pushtmp(), self->n_cols, self->n_rows);
  148. for(int j = 0; j < self->n_rows; j++) {
  149. for(int i = 0; i < self->n_cols; i++) {
  150. py_Ref item = self->f_get(self, i, j);
  151. if(!py_call(f, 1, item)) return false;
  152. res->data[j * self->n_cols + i] = *py_retval();
  153. }
  154. }
  155. py_assign(py_retval(), py_peek(-1));
  156. py_pop();
  157. return true;
  158. }
  159. static bool array2d_like_apply(int argc, py_Ref argv) {
  160. // def apply_(self, f: Callable[[T], T]) -> None: ...
  161. PY_CHECK_ARGC(2);
  162. c11_array2d_like* self = py_touserdata(argv);
  163. py_Ref f = py_arg(1);
  164. for(int j = 0; j < self->n_rows; j++) {
  165. for(int i = 0; i < self->n_cols; i++) {
  166. py_Ref item = self->f_get(self, i, j);
  167. if(!py_call(f, 1, item)) return false;
  168. bool ok = self->f_set(self, i, j, py_retval());
  169. if(!ok) return false;
  170. }
  171. }
  172. py_newnone(py_retval());
  173. return true;
  174. }
  175. static bool _check_same_shape(int colA, int rowA, int colB, int rowB) {
  176. if(colA != colB || rowA != rowB) {
  177. const char* fmt = "expected the same shape: (%d, %d) != (%d, %d)";
  178. return ValueError(fmt, colA, rowA, colB, rowB);
  179. }
  180. return true;
  181. }
  182. static bool _array2d_like_check_same_shape(c11_array2d_like* self, c11_array2d_like* other) {
  183. return _check_same_shape(self->n_cols, self->n_rows, other->n_cols, other->n_rows);
  184. }
  185. static bool _array2d_like_broadcasted_zip_with(int argc, py_Ref argv, py_Name op, py_Name rop) {
  186. PY_CHECK_ARGC(2);
  187. c11_array2d_like* self = py_touserdata(argv);
  188. c11_array2d_like* other;
  189. if(py_isinstance(py_arg(1), tp_array2d_like)) {
  190. other = py_touserdata(py_arg(1));
  191. if(!_array2d_like_check_same_shape(self, other)) return false;
  192. } else {
  193. other = NULL;
  194. }
  195. c11_array2d* res = py_newarray2d(py_pushtmp(), self->n_cols, self->n_rows);
  196. for(int j = 0; j < self->n_rows; j++) {
  197. for(int i = 0; i < self->n_cols; i++) {
  198. py_Ref lhs = self->f_get(self, i, j);
  199. py_Ref rhs;
  200. if(other != NULL) {
  201. rhs = other->f_get(other, i, j);
  202. } else {
  203. rhs = py_arg(1); // broadcast
  204. }
  205. if(!py_binaryop(lhs, rhs, op, rop)) return false;
  206. c11_array2d__set(res, i, j, py_retval());
  207. }
  208. }
  209. py_assign(py_retval(), py_peek(-1));
  210. py_pop();
  211. return true;
  212. }
  213. static bool array2d_like_zip_with(int argc, py_Ref argv) {
  214. PY_CHECK_ARGC(3);
  215. c11_array2d_like* self = py_touserdata(argv);
  216. if(!py_checkinstance(py_arg(1), tp_array2d_like)) return false;
  217. c11_array2d_like* other = py_touserdata(py_arg(1));
  218. py_Ref f = py_arg(2);
  219. if(!_array2d_like_check_same_shape(self, other)) return false;
  220. c11_array2d* res = py_newarray2d(py_pushtmp(), self->n_cols, self->n_rows);
  221. for(int j = 0; j < self->n_rows; j++) {
  222. for(int i = 0; i < self->n_cols; i++) {
  223. py_push(f);
  224. py_pushnil();
  225. py_push(self->f_get(self, i, j));
  226. py_push(other->f_get(other, i, j));
  227. if(!py_vectorcall(2, 0)) return false;
  228. c11_array2d__set(res, i, j, py_retval());
  229. }
  230. }
  231. py_assign(py_retval(), py_peek(-1));
  232. py_pop();
  233. return true;
  234. }
  235. #define DEF_ARRAY2D_LIKE__MAGIC_ZIP_WITH(name, op, rop) \
  236. static bool array2d_like##name(int argc, py_Ref argv) { \
  237. return _array2d_like_broadcasted_zip_with(argc, argv, op, rop); \
  238. }
  239. DEF_ARRAY2D_LIKE__MAGIC_ZIP_WITH(__le__, __le__, __ge__)
  240. DEF_ARRAY2D_LIKE__MAGIC_ZIP_WITH(__lt__, __lt__, __gt__)
  241. DEF_ARRAY2D_LIKE__MAGIC_ZIP_WITH(__ge__, __ge__, __le__)
  242. DEF_ARRAY2D_LIKE__MAGIC_ZIP_WITH(__gt__, __gt__, __lt__)
  243. DEF_ARRAY2D_LIKE__MAGIC_ZIP_WITH(__eq__, __eq__, __eq__)
  244. DEF_ARRAY2D_LIKE__MAGIC_ZIP_WITH(__ne__, __ne__, __ne__)
  245. DEF_ARRAY2D_LIKE__MAGIC_ZIP_WITH(__add__, __add__, __radd__)
  246. DEF_ARRAY2D_LIKE__MAGIC_ZIP_WITH(__sub__, __sub__, __rsub__)
  247. DEF_ARRAY2D_LIKE__MAGIC_ZIP_WITH(__mul__, __mul__, __rmul__)
  248. DEF_ARRAY2D_LIKE__MAGIC_ZIP_WITH(__truediv__, __truediv__, __rtruediv__)
  249. DEF_ARRAY2D_LIKE__MAGIC_ZIP_WITH(__floordiv__, __floordiv__, __rfloordiv__)
  250. DEF_ARRAY2D_LIKE__MAGIC_ZIP_WITH(__mod__, __mod__, __rmod__)
  251. DEF_ARRAY2D_LIKE__MAGIC_ZIP_WITH(__pow__, __pow__, __rpow__)
  252. DEF_ARRAY2D_LIKE__MAGIC_ZIP_WITH(__and__, __and__, 0)
  253. DEF_ARRAY2D_LIKE__MAGIC_ZIP_WITH(__or__, __or__, 0)
  254. DEF_ARRAY2D_LIKE__MAGIC_ZIP_WITH(__xor__, __xor__, 0)
  255. #undef DEF_ARRAY2D_LIKE__MAGIC_ZIP_WITH
  256. static bool array2d_like__invert__(int argc, py_Ref argv) {
  257. PY_CHECK_ARGC(1);
  258. c11_array2d_like* self = py_touserdata(argv);
  259. c11_array2d* res = py_newarray2d(py_pushtmp(), self->n_cols, self->n_rows);
  260. for(int j = 0; j < self->n_rows; j++) {
  261. for(int i = 0; i < self->n_cols; i++) {
  262. py_Ref item = self->f_get(self, i, j);
  263. if(!pk_callmagic(__invert__, 1, item)) return false;
  264. c11_array2d__set(res, i, j, py_retval());
  265. }
  266. }
  267. py_assign(py_retval(), py_peek(-1));
  268. py_pop();
  269. return true;
  270. }
  271. static bool array2d_like_copy(int argc, py_Ref argv) {
  272. // def copy(self) -> 'array2d': ...
  273. PY_CHECK_ARGC(1);
  274. c11_array2d_like* self = py_touserdata(argv);
  275. c11_array2d* res = py_newarray2d(py_retval(), self->n_cols, self->n_rows);
  276. for(int j = 0; j < self->n_rows; j++) {
  277. for(int i = 0; i < self->n_cols; i++) {
  278. py_Ref item = self->f_get(self, i, j);
  279. res->data[j * self->n_cols + i] = *item;
  280. }
  281. }
  282. return true;
  283. }
  284. static bool array2d_like_tolist(int argc, py_Ref argv) {
  285. PY_CHECK_ARGC(1);
  286. c11_array2d_like* self = py_touserdata(argv);
  287. py_newlistn(py_retval(), self->n_rows);
  288. for(int j = 0; j < self->n_rows; j++) {
  289. py_Ref row_j = py_list_getitem(py_retval(), j);
  290. py_newlistn(row_j, self->n_cols);
  291. for(int i = 0; i < self->n_cols; i++) {
  292. py_Ref item = self->f_get(self, i, j);
  293. py_list_setitem(row_j, i, item);
  294. }
  295. }
  296. return true;
  297. }
  298. static bool array2d_like__iter__(int argc, py_Ref argv) {
  299. PY_CHECK_ARGC(1);
  300. c11_array2d_like* self = py_touserdata(argv);
  301. c11_array2d_like_iterator* ud =
  302. py_newobject(py_retval(), tp_array2d_like_iterator, 1, sizeof(c11_array2d_like_iterator));
  303. py_setslot(py_retval(), 0, argv); // keep the array alive
  304. ud->array = self;
  305. ud->j = 0;
  306. ud->i = 0;
  307. return true;
  308. }
  309. static bool array2d_like__repr__(int argc, py_Ref argv) {
  310. PY_CHECK_ARGC(1);
  311. c11_array2d_like* self = py_touserdata(argv);
  312. char buf[256];
  313. snprintf(buf,
  314. sizeof(buf),
  315. "%s(%d, %d)",
  316. py_tpname(py_typeof(argv)),
  317. self->n_cols,
  318. self->n_rows);
  319. py_newstr(py_retval(), buf);
  320. return true;
  321. }
  322. #define HANDLE_SLICE() \
  323. int start_col, stop_col, step_col; \
  324. int start_row, stop_row, step_row; \
  325. if(!pk__parse_int_slice(x, self->n_cols, &start_col, &stop_col, &step_col)) return false; \
  326. if(!pk__parse_int_slice(y, self->n_rows, &start_row, &stop_row, &step_row)) return false; \
  327. if(step_col != 1 || step_row != 1) return ValueError("slice step must be 1"); \
  328. int slice_width = stop_col - start_col; \
  329. int slice_height = stop_row - start_row;
  330. static bool _array2d_like_IndexError(c11_array2d_like* self, int col, int row) {
  331. return IndexError("(%d, %d) is not a valid index of array2d_like(%d, %d)",
  332. col,
  333. row,
  334. self->n_cols,
  335. self->n_rows);
  336. }
  337. static py_Ref c11_array2d_view__get(c11_array2d_view* self, int col, int row) {
  338. return self->f_get(self->ctx, col + self->origin.x, row + self->origin.y);
  339. }
  340. static bool c11_array2d_view__set(c11_array2d_view* self, int col, int row, py_Ref value) {
  341. return self->f_set(self->ctx, col + self->origin.x, row + self->origin.y, value);
  342. }
  343. static c11_array2d_view* _array2d_view__new(py_OutRef out,
  344. py_Ref keepalive,
  345. int start_col,
  346. int start_row,
  347. int width,
  348. int height) {
  349. c11_array2d_view* res = py_newobject(out, tp_array2d_view, 1, sizeof(c11_array2d_view));
  350. if(width <= 0 || height <= 0) {
  351. ValueError("width and height must be positive");
  352. return NULL;
  353. }
  354. res->header.n_cols = width;
  355. res->header.n_rows = height;
  356. res->header.numel = width * height;
  357. res->header.f_get = (py_Ref(*)(c11_array2d_like*, int, int))c11_array2d_view__get;
  358. res->header.f_set = (bool (*)(c11_array2d_like*, int, int, py_Ref))c11_array2d_view__set;
  359. res->origin.x = start_col;
  360. res->origin.y = start_row;
  361. py_setslot(out, 0, keepalive);
  362. return res;
  363. }
  364. static bool _array2d_view(py_OutRef out,
  365. py_Ref keepalive,
  366. c11_array2d_like* array,
  367. int start_col,
  368. int start_row,
  369. int width,
  370. int height) {
  371. c11_array2d_view* res = _array2d_view__new(out, keepalive, start_col, start_row, width, height);
  372. if(res == NULL) return false;
  373. res->ctx = array;
  374. res->f_get = (py_Ref(*)(void*, int, int))array->f_get;
  375. res->f_set = (bool (*)(void*, int, int, py_Ref))array->f_set;
  376. return true;
  377. }
  378. static bool _chunked_array2d_view(py_OutRef out,
  379. py_Ref keepalive,
  380. c11_chunked_array2d* array,
  381. int start_col,
  382. int start_row,
  383. int width,
  384. int height) {
  385. c11_array2d_view* res = _array2d_view__new(out, keepalive, start_col, start_row, width, height);
  386. if(res == NULL) return false;
  387. res->ctx = array;
  388. res->f_get = (py_Ref(*)(void*, int, int))c11_chunked_array2d__get;
  389. res->f_set = (bool (*)(void*, int, int, py_Ref))c11_chunked_array2d__set;
  390. return true;
  391. }
  392. static bool array2d_like__getitem__(int argc, py_Ref argv) {
  393. PY_CHECK_ARGC(2);
  394. c11_array2d_like* self = py_touserdata(argv);
  395. if(argv[1].type == tp_vec2i) {
  396. c11_vec2i pos = py_tovec2i(&argv[1]);
  397. if(c11_array2d_like_is_valid(self, pos.x, pos.y)) {
  398. py_assign(py_retval(), self->f_get(self, pos.x, pos.y));
  399. return true;
  400. }
  401. return _array2d_like_IndexError(self, pos.x, pos.y);
  402. }
  403. if(py_isinstance(&argv[1], tp_array2d_like)) {
  404. c11_array2d_like* mask = py_touserdata(&argv[1]);
  405. if(!_array2d_like_check_same_shape(self, mask)) return false;
  406. py_newlist(py_retval());
  407. for(int j = 0; j < self->n_rows; j++) {
  408. for(int i = 0; i < self->n_cols; i++) {
  409. py_Ref item = self->f_get(self, i, j);
  410. py_Ref cond = mask->f_get(mask, i, j);
  411. if(!py_checkbool(cond)) return false;
  412. if(py_tobool(cond)) py_list_append(py_retval(), item);
  413. }
  414. }
  415. return true;
  416. }
  417. PY_CHECK_ARG_TYPE(1, tp_tuple);
  418. if(py_tuple_len(&argv[1]) != 2) return TypeError("expected a tuple of 2 elements");
  419. py_Ref x = py_tuple_getitem(&argv[1], 0);
  420. py_Ref y = py_tuple_getitem(&argv[1], 1);
  421. if(py_isint(x) && py_isint(y)) {
  422. int col = py_toint(x);
  423. int row = py_toint(y);
  424. if(c11_array2d_like_is_valid(self, col, row)) {
  425. py_assign(py_retval(), self->f_get(self, col, row));
  426. return true;
  427. }
  428. return _array2d_like_IndexError(self, col, row);
  429. }
  430. bool _1 = py_istype(x, tp_slice) && py_istype(y, tp_slice);
  431. bool _2 = py_istype(x, tp_int) && py_istype(y, tp_slice);
  432. bool _3 = py_istype(x, tp_slice) && py_istype(y, tp_int);
  433. if(_1 || _2 || _3) {
  434. HANDLE_SLICE();
  435. return _array2d_view(py_retval(),
  436. argv,
  437. self,
  438. start_col,
  439. start_row,
  440. slice_width,
  441. slice_height);
  442. }
  443. return TypeError("expected tuple[int, int] or tuple[slice, slice]");
  444. }
  445. static bool array2d_like__setitem__(int argc, py_Ref argv) {
  446. PY_CHECK_ARGC(3);
  447. c11_array2d_like* self = py_touserdata(argv);
  448. py_Ref value = &argv[2];
  449. if(argv[1].type == tp_vec2i) {
  450. c11_vec2i pos = py_tovec2i(&argv[1]);
  451. if(c11_array2d_like_is_valid(self, pos.x, pos.y)) {
  452. bool ok = self->f_set(self, pos.x, pos.y, value);
  453. if(!ok) return false;
  454. py_newnone(py_retval());
  455. return true;
  456. }
  457. return _array2d_like_IndexError(self, pos.x, pos.y);
  458. }
  459. if(py_isinstance(&argv[1], tp_array2d_like)) {
  460. c11_array2d_like* mask = py_touserdata(&argv[1]);
  461. if(!_array2d_like_check_same_shape(self, mask)) return false;
  462. for(int j = 0; j < self->n_rows; j++) {
  463. for(int i = 0; i < self->n_cols; i++) {
  464. py_Ref cond = mask->f_get(mask, i, j);
  465. if(!py_checkbool(cond)) return false;
  466. if(py_tobool(cond)) {
  467. bool ok = self->f_set(self, i, j, value);
  468. if(!ok) return false;
  469. }
  470. }
  471. }
  472. py_newnone(py_retval());
  473. return true;
  474. }
  475. PY_CHECK_ARG_TYPE(1, tp_tuple);
  476. if(py_tuple_len(py_arg(1)) != 2) return TypeError("expected a tuple of 2 elements");
  477. py_Ref x = py_tuple_getitem(py_arg(1), 0);
  478. py_Ref y = py_tuple_getitem(py_arg(1), 1);
  479. if(py_isint(x) && py_isint(y)) {
  480. int col = py_toint(x);
  481. int row = py_toint(y);
  482. if(c11_array2d_like_is_valid(self, col, row)) {
  483. bool ok = self->f_set(self, col, row, value);
  484. if(!ok) return false;
  485. py_newnone(py_retval());
  486. return true;
  487. }
  488. return _array2d_like_IndexError(self, col, row);
  489. }
  490. bool _1 = py_istype(x, tp_slice) && py_istype(y, tp_slice);
  491. bool _2 = py_istype(x, tp_int) && py_istype(y, tp_slice);
  492. bool _3 = py_istype(x, tp_slice) && py_istype(y, tp_int);
  493. if(_1 || _2 || _3) {
  494. HANDLE_SLICE();
  495. if(py_isinstance(value, tp_array2d_like)) {
  496. c11_array2d_like* values = py_touserdata(value);
  497. if(!_check_same_shape(slice_width, slice_height, values->n_cols, values->n_rows))
  498. return false;
  499. for(int j = 0; j < slice_height; j++) {
  500. for(int i = 0; i < slice_width; i++) {
  501. py_Ref item = values->f_get(values, i, j);
  502. bool ok = self->f_set(self, start_col + i, start_row + j, item);
  503. if(!ok) return false;
  504. }
  505. }
  506. } else {
  507. for(int j = 0; j < slice_height; j++) {
  508. for(int i = 0; i < slice_width; i++) {
  509. bool ok = self->f_set(self, start_col + i, start_row + j, value);
  510. if(!ok) return false;
  511. }
  512. }
  513. }
  514. py_newnone(py_retval());
  515. return true;
  516. }
  517. return TypeError("expected tuple[int, int] or tuple[slice, slice]");
  518. }
  519. // count(self, value: T) -> int
  520. static bool array2d_like_count(int argc, py_Ref argv) {
  521. PY_CHECK_ARGC(2);
  522. c11_array2d_like* self = py_touserdata(argv);
  523. int count = 0;
  524. for(int j = 0; j < self->n_rows; j++) {
  525. for(int i = 0; i < self->n_cols; i++) {
  526. int code = py_equal(self->f_get(self, i, j), py_arg(1));
  527. if(code == -1) return false;
  528. count += code;
  529. }
  530. }
  531. py_newint(py_retval(), count);
  532. return true;
  533. }
  534. // get_bounding_rect(self, value: T) -> tuple[int, int, int, int]
  535. static bool array2d_like_get_bounding_rect(int argc, py_Ref argv) {
  536. PY_CHECK_ARGC(2);
  537. c11_array2d_like* self = py_touserdata(argv);
  538. py_Ref value = py_arg(1);
  539. int left = self->n_cols;
  540. int top = self->n_rows;
  541. int right = 0;
  542. int bottom = 0;
  543. for(int j = 0; j < self->n_rows; j++) {
  544. for(int i = 0; i < self->n_cols; i++) {
  545. py_Ref item = self->f_get(self, i, j);
  546. int res = py_equal(item, value);
  547. if(res == -1) return false;
  548. if(res == 1) {
  549. left = c11__min(left, i);
  550. top = c11__min(top, j);
  551. right = c11__max(right, i);
  552. bottom = c11__max(bottom, j);
  553. }
  554. }
  555. }
  556. int width = right - left + 1;
  557. int height = bottom - top + 1;
  558. if(width <= 0 || height <= 0) {
  559. return ValueError("value not found");
  560. } else {
  561. py_TValue* data = py_newtuple(py_retval(), 4);
  562. py_newint(&data[0], left);
  563. py_newint(&data[1], top);
  564. py_newint(&data[2], width);
  565. py_newint(&data[3], height);
  566. }
  567. return true;
  568. }
  569. // count_neighbors(self, value: T, neighborhood: Neighborhood) -> array2d[int]
  570. static bool array2d_like_count_neighbors(int argc, py_Ref argv) {
  571. PY_CHECK_ARGC(3);
  572. c11_array2d_like* self = py_touserdata(argv);
  573. c11_array2d* res = py_newarray2d(py_pushtmp(), self->n_cols, self->n_rows);
  574. py_Ref value = py_arg(1);
  575. const char* neighborhood = py_tostr(py_arg(2));
  576. const static c11_vec2i Moore[] = {
  577. {{-1, -1}},
  578. {{0, -1}},
  579. {{1, -1}},
  580. {{-1, 0}},
  581. {{1, 0}},
  582. {{-1, 1}},
  583. {{0, 1}},
  584. {{1, 1}},
  585. };
  586. const static c11_vec2i von_Neumann[] = {
  587. {{0, -1}},
  588. {{-1, 0}},
  589. {{1, 0}},
  590. {{0, 1}},
  591. };
  592. const c11_vec2i* offsets;
  593. int n_offsets;
  594. if(strcmp(neighborhood, "Moore") == 0) {
  595. offsets = Moore;
  596. n_offsets = c11__count_array(Moore);
  597. } else if(strcmp(neighborhood, "von Neumann") == 0) {
  598. offsets = von_Neumann;
  599. n_offsets = c11__count_array(von_Neumann);
  600. } else {
  601. return ValueError("neighborhood must be 'Moore' or 'von Neumann'");
  602. }
  603. for(int j = 0; j < self->n_rows; j++) {
  604. for(int i = 0; i < self->n_cols; i++) {
  605. py_i64 count = 0;
  606. for(int k = 0; k < n_offsets; k++) {
  607. int x = i + offsets[k].x;
  608. int y = j + offsets[k].y;
  609. if(x >= 0 && x < self->n_cols && y >= 0 && y < self->n_rows) {
  610. py_Ref item = self->f_get(self, x, y);
  611. int code = py_equal(item, value);
  612. if(code == -1) return false;
  613. count += code;
  614. }
  615. }
  616. py_newint(c11_array2d__get(res, i, j), count);
  617. }
  618. }
  619. py_assign(py_retval(), py_peek(-1));
  620. py_pop();
  621. return true;
  622. }
  623. // convolve(self: array2d_like[int], kernel: array2d_like[int], padding: int) -> array2d[int]
  624. static bool array2d_like_convolve(int argc, py_Ref argv) {
  625. PY_CHECK_ARGC(3);
  626. if(!py_checkinstance(&argv[1], tp_array2d_like)) return false;
  627. PY_CHECK_ARG_TYPE(2, tp_int);
  628. c11_array2d_like* self = py_touserdata(&argv[0]);
  629. c11_array2d_like* kernel = py_touserdata(&argv[1]);
  630. int padding = py_toint(py_arg(2));
  631. if(kernel->n_cols != kernel->n_rows) return ValueError("kernel must be square");
  632. int ksize = kernel->n_cols;
  633. if(ksize % 2 == 0) return ValueError("kernel size must be odd");
  634. int ksize_half = ksize / 2;
  635. c11_array2d* res = py_newarray2d(py_pushtmp(), self->n_cols, self->n_rows);
  636. for(int j = 0; j < self->n_rows; j++) {
  637. for(int i = 0; i < self->n_cols; i++) {
  638. py_i64 sum = 0;
  639. for(int jj = 0; jj < ksize; jj++) {
  640. for(int ii = 0; ii < ksize; ii++) {
  641. int x = i + ii - ksize_half;
  642. int y = j + jj - ksize_half;
  643. py_i64 _0, _1;
  644. if(x < 0 || x >= self->n_cols || y < 0 || y >= self->n_rows) {
  645. _0 = padding;
  646. } else {
  647. py_Ref item = self->f_get(self, x, y);
  648. if(!py_checkint(item)) return false;
  649. _0 = py_toint(item);
  650. }
  651. py_Ref kitem = kernel->f_get(kernel, ii, jj);
  652. if(!py_checkint(kitem)) return false;
  653. _1 = py_toint(kitem);
  654. sum += _0 * _1;
  655. }
  656. }
  657. py_newint(c11_array2d__get(res, i, j), sum);
  658. }
  659. }
  660. py_assign(py_retval(), py_peek(-1));
  661. py_pop();
  662. return true;
  663. }
  664. #undef HANDLE_SLICE
  665. static void register_array2d_like(py_Ref mod) {
  666. py_Type type = py_newtype("array2d_like", tp_object, mod, NULL);
  667. assert(type == tp_array2d_like);
  668. py_bindproperty(type, "n_cols", array2d_like_n_cols, NULL);
  669. py_bindproperty(type, "n_rows", array2d_like_n_rows, NULL);
  670. py_bindproperty(type, "width", array2d_like_n_cols, NULL);
  671. py_bindproperty(type, "height", array2d_like_n_rows, NULL);
  672. py_bindproperty(type, "shape", array2d_like_shape, NULL);
  673. py_bindproperty(type, "numel", array2d_like_numel, NULL);
  674. py_bindmethod(type, "is_valid", array2d_like_is_valid);
  675. py_bindmethod(type, "get", array2d_like_get);
  676. py_bindmethod(type, "render", array2d_like_render);
  677. py_bindmethod(type, "all", array2d_like_all);
  678. py_bindmethod(type, "any", array2d_like_any);
  679. py_bindmethod(type, "map", array2d_like_map);
  680. py_bindmethod(type, "apply", array2d_like_apply);
  681. py_bindmethod(type, "zip_with", array2d_like_zip_with);
  682. py_bindmethod(type, "copy", array2d_like_copy);
  683. py_bindmethod(type, "tolist", array2d_like_tolist);
  684. py_bindmagic(type, __le__, array2d_like__le__);
  685. py_bindmagic(type, __lt__, array2d_like__lt__);
  686. py_bindmagic(type, __ge__, array2d_like__ge__);
  687. py_bindmagic(type, __gt__, array2d_like__gt__);
  688. py_bindmagic(type, __eq__, array2d_like__eq__);
  689. py_bindmagic(type, __ne__, array2d_like__ne__);
  690. py_bindmagic(type, __add__, array2d_like__add__);
  691. py_bindmagic(type, __sub__, array2d_like__sub__);
  692. py_bindmagic(type, __mul__, array2d_like__mul__);
  693. py_bindmagic(type, __truediv__, array2d_like__truediv__);
  694. py_bindmagic(type, __floordiv__, array2d_like__floordiv__);
  695. py_bindmagic(type, __mod__, array2d_like__mod__);
  696. py_bindmagic(type, __pow__, array2d_like__pow__);
  697. py_bindmagic(type, __and__, array2d_like__and__);
  698. py_bindmagic(type, __or__, array2d_like__or__);
  699. py_bindmagic(type, __xor__, array2d_like__xor__);
  700. py_bindmagic(type, __invert__, array2d_like__invert__);
  701. py_bindmagic(type, __iter__, array2d_like__iter__);
  702. py_bindmagic(type, __repr__, array2d_like__repr__);
  703. py_bindmagic(type, __getitem__, array2d_like__getitem__);
  704. py_bindmagic(type, __setitem__, array2d_like__setitem__);
  705. py_bindmethod(type, "count", array2d_like_count);
  706. py_bindmethod(type, "get_bounding_rect", array2d_like_get_bounding_rect);
  707. py_bindmethod(type, "count_neighbors", array2d_like_count_neighbors);
  708. py_bindmethod(type, "convolve", array2d_like_convolve);
  709. const char* scc =
  710. "\ndef get_connected_components(self, value: T, neighborhood: Neighborhood) -> tuple[array2d[int], int]:\n from collections import deque\n from linalg import vec2i\n\n DIRS = [vec2i.LEFT, vec2i.RIGHT, vec2i.UP, vec2i.DOWN]\n assert neighborhood in ['Moore', 'von Neumann']\n\n if neighborhood == 'Moore':\n DIRS.extend([\n vec2i.LEFT+vec2i.UP,\n vec2i.RIGHT+vec2i.UP,\n vec2i.LEFT+vec2i.DOWN,\n vec2i.RIGHT+vec2i.DOWN\n ])\n\n visited = array2d[int](self.width, self.height, default=0)\n queue = deque()\n count = 0\n for y in range(self.height):\n for x in range(self.width):\n if visited[x, y] or self[x, y] != value:\n continue\n count += 1\n queue.append((x, y))\n visited[x, y] = count\n while queue:\n cx, cy = queue.popleft()\n for dx, dy in DIRS:\n nx, ny = cx+dx, cy+dy\n if self.is_valid(nx, ny) and not visited[nx, ny] and self[nx, ny] == value:\n queue.append((nx, ny))\n visited[nx, ny] = count\n return visited, count\n\narray2d_like.get_connected_components = get_connected_components\ndel get_connected_components\n";
  711. if(!py_exec(scc, "array2d.py", EXEC_MODE, mod)) {
  712. py_printexc();
  713. c11__abort("failed to execute array2d.py");
  714. }
  715. }
  716. static bool array2d_like_iterator__next__(int argc, py_Ref argv) {
  717. PY_CHECK_ARGC(1);
  718. c11_array2d_like_iterator* self = py_touserdata(argv);
  719. if(self->j >= self->array->n_rows) return StopIteration();
  720. py_TValue* data = py_newtuple(py_retval(), 2);
  721. py_newvec2i(&data[0],
  722. (c11_vec2i){
  723. {self->i, self->j}
  724. });
  725. py_assign(&data[1], self->array->f_get(self->array, self->i, self->j));
  726. self->i++;
  727. if(self->i >= self->array->n_cols) {
  728. self->i = 0;
  729. self->j++;
  730. }
  731. return true;
  732. }
  733. static void register_array2d_like_iterator(py_Ref mod) {
  734. py_Type type = py_newtype("array2d_like_iterator", tp_object, mod, NULL);
  735. assert(type == tp_array2d_like_iterator);
  736. py_bindmagic(type, __iter__, pk_wrapper__self);
  737. py_bindmagic(type, __next__, array2d_like_iterator__next__);
  738. }
  739. static bool array2d__new__(int argc, py_Ref argv) {
  740. // __new__(cls, n_cols: int, n_rows: int, default: Callable[[vec2i], T] = None)
  741. py_Ref default_ = py_arg(3);
  742. PY_CHECK_ARG_TYPE(0, tp_type);
  743. PY_CHECK_ARG_TYPE(1, tp_int);
  744. PY_CHECK_ARG_TYPE(2, tp_int);
  745. int n_cols = argv[1]._i64;
  746. int n_rows = argv[2]._i64;
  747. if(n_cols <= 0 || n_rows <= 0) return ValueError("array2d() expected positive dimensions");
  748. c11_array2d* ud = py_newarray2d(py_pushtmp(), n_cols, n_rows);
  749. // setup initial values
  750. if(py_callable(default_)) {
  751. for(int j = 0; j < n_rows; j++) {
  752. for(int i = 0; i < n_cols; i++) {
  753. py_TValue tmp;
  754. py_newvec2i(&tmp,
  755. (c11_vec2i){
  756. {i, j}
  757. });
  758. if(!py_call(default_, 1, &tmp)) return false;
  759. ud->data[j * n_cols + i] = *py_retval();
  760. }
  761. }
  762. } else {
  763. for(int i = 0; i < ud->header.numel; i++) {
  764. ud->data[i] = *default_;
  765. }
  766. }
  767. py_assign(py_retval(), py_peek(-1));
  768. py_pop();
  769. return true;
  770. }
  771. // fromlist(data: list[list[T]]) -> array2d[T]
  772. static bool array2d_fromlist_STATIC(int argc, py_Ref argv) {
  773. PY_CHECK_ARGC(1);
  774. if(!py_checktype(argv, tp_list)) return false;
  775. int n_rows = py_list_len(argv);
  776. if(n_rows == 0) return ValueError("fromlist() expected a non-empty list");
  777. int n_cols = -1;
  778. for(int j = 0; j < n_rows; j++) {
  779. py_Ref row_j = py_list_getitem(argv, j);
  780. if(!py_checktype(row_j, tp_list)) return false;
  781. int n_cols_j = py_list_len(row_j);
  782. if(n_cols == -1) {
  783. if(n_cols_j == 0) return ValueError("fromlist() expected a non-empty list");
  784. n_cols = n_cols_j;
  785. } else if(n_cols != n_cols_j) {
  786. return ValueError("fromlist() expected a list of lists with the same length");
  787. }
  788. }
  789. c11_array2d* res = py_newarray2d(py_retval(), n_cols, n_rows);
  790. for(int j = 0; j < n_rows; j++) {
  791. py_Ref row_j = py_list_getitem(argv, j);
  792. for(int i = 0; i < n_cols; i++) {
  793. c11_array2d__set(res, i, j, py_list_getitem(row_j, i));
  794. }
  795. }
  796. return true;
  797. }
  798. static void register_array2d(py_Ref mod) {
  799. py_Type type = py_newtype("array2d", tp_array2d_like, mod, NULL);
  800. assert(type == tp_array2d);
  801. py_bind(py_tpobject(type),
  802. "__new__(cls, n_cols: int, n_rows: int, default=None)",
  803. array2d__new__);
  804. py_bindstaticmethod(type, "fromlist", array2d_fromlist_STATIC);
  805. }
  806. static bool array2d_view_origin(int argc, py_Ref argv) {
  807. PY_CHECK_ARGC(1);
  808. c11_array2d_view* self = py_touserdata(argv);
  809. py_newvec2i(py_retval(), self->origin);
  810. return true;
  811. }
  812. static void register_array2d_view(py_Ref mod) {
  813. py_Type type = py_newtype("array2d_view", tp_array2d_like, mod, NULL);
  814. assert(type == tp_array2d_view);
  815. py_bindproperty(type, "origin", array2d_view_origin, NULL);
  816. }
  817. /* chunked_array2d */
  818. #define SMALLMAP_T__SOURCE
  819. #define K c11_vec2i
  820. #define V py_TValue*
  821. #define NAME c11_chunked_array2d_chunks
  822. #define less(a, b) (a._i64 < b._i64)
  823. #define equal(a, b) (a._i64 == b._i64)
  824. #include "pocketpy/xmacros/smallmap.h"
  825. #undef SMALLMAP_T__SOURCE
  826. static py_TValue* c11_chunked_array2d__new_chunk(c11_chunked_array2d* self, c11_vec2i pos) {
  827. #ifndef NDEBUG
  828. bool exists = c11_chunked_array2d_chunks__contains(&self->chunks, pos);
  829. assert(!exists);
  830. #endif
  831. int chunk_numel = self->chunk_size * self->chunk_size + 1;
  832. py_TValue* data = PK_MALLOC(sizeof(py_TValue) * chunk_numel);
  833. if(!py_isnone(&self->context_builder)) {
  834. py_newvec2i(&data[0], pos);
  835. bool ok = py_call(&self->context_builder, 1, &data[0]);
  836. if(!ok) return NULL;
  837. data[0] = *py_retval();
  838. } else {
  839. data[0] = *py_None();
  840. }
  841. memset(&data[1], 0, sizeof(py_TValue) * (chunk_numel - 1));
  842. c11_chunked_array2d_chunks__set(&self->chunks, pos, data);
  843. self->last_visited.key = pos;
  844. self->last_visited.value = data;
  845. return data;
  846. }
  847. static void
  848. cpy11__divmod_int_uint(int a, int b_log2, int b_mask, int* restrict q, int* restrict r) {
  849. if(a >= 0) {
  850. *q = a >> b_log2;
  851. *r = a & b_mask;
  852. } else {
  853. *q = -1 - ((-a - 1) >> b_log2);
  854. *r = b_mask - ((-a - 1) & b_mask);
  855. }
  856. }
  857. static void c11_chunked_array2d__world_to_chunk(c11_chunked_array2d* self,
  858. int col,
  859. int row,
  860. c11_vec2i* restrict chunk_pos,
  861. c11_vec2i* restrict local_pos) {
  862. cpy11__divmod_int_uint(col,
  863. self->chunk_size_log2,
  864. self->chunk_size_mask,
  865. &chunk_pos->x,
  866. &local_pos->x);
  867. cpy11__divmod_int_uint(row,
  868. self->chunk_size_log2,
  869. self->chunk_size_mask,
  870. &chunk_pos->y,
  871. &local_pos->y);
  872. }
  873. static py_TValue* c11_chunked_array2d__parse_col_row(c11_chunked_array2d* self,
  874. int col,
  875. int row,
  876. c11_vec2i* restrict chunk_pos,
  877. c11_vec2i* restrict local_pos) {
  878. c11_chunked_array2d__world_to_chunk(self, col, row, chunk_pos, local_pos);
  879. py_TValue* data;
  880. if(self->last_visited.value != NULL && chunk_pos->_i64 == self->last_visited.key._i64) {
  881. data = self->last_visited.value;
  882. } else {
  883. data = c11_chunked_array2d_chunks__get(&self->chunks, *chunk_pos, NULL);
  884. }
  885. if(data != NULL) {
  886. self->last_visited.key = *chunk_pos;
  887. self->last_visited.value = data;
  888. }
  889. return data;
  890. }
  891. py_Ref c11_chunked_array2d__get(c11_chunked_array2d* self, int col, int row) {
  892. c11_vec2i chunk_pos, local_pos;
  893. py_TValue* data = c11_chunked_array2d__parse_col_row(self, col, row, &chunk_pos, &local_pos);
  894. if(data == NULL) return &self->default_T;
  895. py_Ref retval = &data[1 + local_pos.y * self->chunk_size + local_pos.x];
  896. if(py_isnil(retval)) return &self->default_T;
  897. return retval;
  898. }
  899. bool c11_chunked_array2d__set(c11_chunked_array2d* self, int col, int row, py_Ref value) {
  900. c11_vec2i chunk_pos, local_pos;
  901. py_TValue* data = c11_chunked_array2d__parse_col_row(self, col, row, &chunk_pos, &local_pos);
  902. if(data == NULL) {
  903. data = c11_chunked_array2d__new_chunk(self, chunk_pos);
  904. if(data == NULL) return false;
  905. }
  906. data[1 + local_pos.y * self->chunk_size + local_pos.x] = *value;
  907. return true;
  908. }
  909. static void c11_chunked_array2d__del(c11_chunked_array2d* self, int col, int row) {
  910. c11_vec2i chunk_pos, local_pos;
  911. py_TValue* data = c11_chunked_array2d__parse_col_row(self, col, row, &chunk_pos, &local_pos);
  912. if(data != NULL) data[1 + local_pos.y * self->chunk_size + local_pos.x] = *py_NIL();
  913. }
  914. static bool chunked_array2d__new__(int argc, py_Ref argv) {
  915. PY_CHECK_ARGC(4);
  916. PY_CHECK_ARG_TYPE(1, tp_int);
  917. py_Type cls = py_totype(argv);
  918. c11_chunked_array2d* self = py_newobject(py_retval(), cls, 0, sizeof(c11_chunked_array2d));
  919. int chunk_size = py_toint(&argv[1]);
  920. self->default_T = argv[2];
  921. self->context_builder = argv[3];
  922. c11_chunked_array2d_chunks__ctor(&self->chunks);
  923. self->chunk_size = chunk_size;
  924. switch(chunk_size) {
  925. case 2: self->chunk_size_log2 = 1; break;
  926. case 4: self->chunk_size_log2 = 2; break;
  927. case 8: self->chunk_size_log2 = 3; break;
  928. case 16: self->chunk_size_log2 = 4; break;
  929. case 32: self->chunk_size_log2 = 5; break;
  930. case 64: self->chunk_size_log2 = 6; break;
  931. case 128: self->chunk_size_log2 = 7; break;
  932. case 256: self->chunk_size_log2 = 8; break;
  933. case 512: self->chunk_size_log2 = 9; break;
  934. case 1024: self->chunk_size_log2 = 10; break;
  935. case 2048: self->chunk_size_log2 = 11; break;
  936. case 4096: self->chunk_size_log2 = 12; break;
  937. default: return ValueError("invalid chunk_size: %d, not power of 2", chunk_size);
  938. }
  939. self->chunk_size_mask = chunk_size - 1;
  940. memset(&self->last_visited, 0, sizeof(c11_chunked_array2d_chunks_KV));
  941. return true;
  942. }
  943. static bool chunked_array2d_chunk_size(int argc, py_Ref argv) {
  944. PY_CHECK_ARGC(1);
  945. c11_chunked_array2d* self = py_touserdata(argv);
  946. py_newint(py_retval(), self->chunk_size);
  947. return true;
  948. }
  949. static bool chunked_array2d_default(int argc, py_Ref argv) {
  950. PY_CHECK_ARGC(1);
  951. c11_chunked_array2d* self = py_touserdata(argv);
  952. py_assign(py_retval(), &self->default_T);
  953. return true;
  954. }
  955. static bool chunked_array2d_context_builder(int argc, py_Ref argv) {
  956. PY_CHECK_ARGC(1);
  957. c11_chunked_array2d* self = py_touserdata(argv);
  958. py_assign(py_retval(), &self->context_builder);
  959. return true;
  960. }
  961. static bool chunked_array2d__getitem__(int argc, py_Ref argv) {
  962. PY_CHECK_ARGC(2);
  963. PY_CHECK_ARG_TYPE(1, tp_vec2i);
  964. c11_chunked_array2d* self = py_touserdata(argv);
  965. c11_vec2i pos = py_tovec2i(&argv[1]);
  966. py_Ref res = c11_chunked_array2d__get(self, pos.x, pos.y);
  967. py_assign(py_retval(), res);
  968. return true;
  969. }
  970. static bool chunked_array2d__setitem__(int argc, py_Ref argv) {
  971. PY_CHECK_ARGC(3);
  972. PY_CHECK_ARG_TYPE(1, tp_vec2i);
  973. c11_chunked_array2d* self = py_touserdata(argv);
  974. c11_vec2i pos = py_tovec2i(&argv[1]);
  975. bool ok = c11_chunked_array2d__set(self, pos.x, pos.y, &argv[2]);
  976. if(!ok) return false;
  977. py_newnone(py_retval());
  978. return true;
  979. }
  980. static bool chunked_array2d__delitem__(int argc, py_Ref argv) {
  981. PY_CHECK_ARGC(2);
  982. PY_CHECK_ARG_TYPE(1, tp_vec2i);
  983. c11_chunked_array2d* self = py_touserdata(argv);
  984. c11_vec2i pos = py_tovec2i(&argv[1]);
  985. c11_chunked_array2d__del(self, pos.x, pos.y);
  986. py_newnone(py_retval());
  987. return true;
  988. }
  989. static bool chunked_array2d__iter__(int argc, py_Ref argv) {
  990. PY_CHECK_ARGC(1);
  991. c11_chunked_array2d* self = py_touserdata(argv);
  992. py_Ref data = py_newtuple(py_pushtmp(), self->chunks.length);
  993. for(int i = 0; i < self->chunks.length; i++) {
  994. c11_chunked_array2d_chunks_KV* kv =
  995. c11__at(c11_chunked_array2d_chunks_KV, &self->chunks, i);
  996. py_Ref p = py_newtuple(&data[i], 2);
  997. py_newvec2i(&p[0], kv->key); // pos
  998. p[1] = kv->value[0]; // context
  999. }
  1000. bool ok = py_iter(py_peek(-1));
  1001. if(!ok) return false;
  1002. py_pop();
  1003. return true;
  1004. }
  1005. static bool chunked_array2d__len__(int argc, py_Ref argv) {
  1006. PY_CHECK_ARGC(1);
  1007. c11_chunked_array2d* self = py_touserdata(argv);
  1008. py_newint(py_retval(), self->chunks.length);
  1009. return true;
  1010. }
  1011. static bool chunked_array2d_clear(int argc, py_Ref argv) {
  1012. PY_CHECK_ARGC(1);
  1013. c11_chunked_array2d* self = py_touserdata(argv);
  1014. c11_chunked_array2d_chunks__clear(&self->chunks);
  1015. self->last_visited.value = NULL;
  1016. py_newnone(py_retval());
  1017. return true;
  1018. }
  1019. static bool chunked_array2d_copy(int argc, py_Ref argv) {
  1020. PY_CHECK_ARGC(1);
  1021. c11_chunked_array2d* self = py_touserdata(argv);
  1022. c11_chunked_array2d* res =
  1023. py_newobject(py_retval(), tp_chunked_array2d, 0, sizeof(c11_chunked_array2d));
  1024. // copy basic data
  1025. memcpy(res, self, sizeof(c11_chunked_array2d));
  1026. // invalidate last_visited cache
  1027. self->last_visited.value = NULL;
  1028. // copy chunks
  1029. memset(&res->chunks, 0, sizeof(c11_chunked_array2d_chunks));
  1030. c11_chunked_array2d_chunks__ctor(&res->chunks);
  1031. c11_vector__reserve(&res->chunks, self->chunks.capacity);
  1032. for(int i = 0; i < self->chunks.length; i++) {
  1033. c11_chunked_array2d_chunks_KV* kv =
  1034. c11__at(c11_chunked_array2d_chunks_KV, &self->chunks, i);
  1035. int chunk_numel = self->chunk_size * self->chunk_size + 1;
  1036. py_TValue* data = PK_MALLOC(sizeof(py_TValue) * chunk_numel);
  1037. memcpy(data, kv->value, sizeof(py_TValue) * chunk_numel);
  1038. // construct new KV
  1039. c11_chunked_array2d_chunks_KV new_kv;
  1040. new_kv.key = kv->key;
  1041. new_kv.value = data;
  1042. c11_vector__push(c11_chunked_array2d_chunks_KV, &res->chunks, new_kv);
  1043. }
  1044. return true;
  1045. }
  1046. static bool chunked_array2d_world_to_chunk(int argc, py_Ref argv) {
  1047. PY_CHECK_ARGC(2);
  1048. PY_CHECK_ARG_TYPE(1, tp_vec2i);
  1049. c11_chunked_array2d* self = py_touserdata(argv);
  1050. c11_vec2i pos = py_tovec2i(&argv[1]);
  1051. c11_vec2i chunk_pos, local_pos;
  1052. c11_chunked_array2d__world_to_chunk(self, pos.x, pos.y, &chunk_pos, &local_pos);
  1053. py_TValue* p = py_newtuple(py_retval(), 2);
  1054. py_newvec2i(&p[0], chunk_pos);
  1055. py_newvec2i(&p[1], local_pos);
  1056. return true;
  1057. }
  1058. static bool chunked_array2d_add_chunk(int argc, py_Ref argv) {
  1059. PY_CHECK_ARGC(2);
  1060. PY_CHECK_ARG_TYPE(1, tp_vec2i);
  1061. c11_chunked_array2d* self = py_touserdata(argv);
  1062. c11_vec2i pos = py_tovec2i(&argv[1]);
  1063. py_TValue* data = c11_chunked_array2d__new_chunk(self, pos);
  1064. if(data == NULL) return false;
  1065. py_assign(py_retval(), &data[0]); // context
  1066. return true;
  1067. }
  1068. static bool chunked_array2d_remove_chunk(int argc, py_Ref argv) {
  1069. PY_CHECK_ARGC(2);
  1070. PY_CHECK_ARG_TYPE(1, tp_vec2i);
  1071. c11_chunked_array2d* self = py_touserdata(argv);
  1072. c11_vec2i pos = py_tovec2i(&argv[1]);
  1073. bool ok = c11_chunked_array2d_chunks__del(&self->chunks, pos);
  1074. self->last_visited.value = NULL;
  1075. py_newbool(py_retval(), ok);
  1076. return true;
  1077. }
  1078. static bool chunked_array2d_move_chunk(int argc, py_Ref argv) {
  1079. PY_CHECK_ARGC(3);
  1080. PY_CHECK_ARG_TYPE(1, tp_vec2i);
  1081. PY_CHECK_ARG_TYPE(2, tp_vec2i);
  1082. c11_chunked_array2d* self = py_touserdata(argv);
  1083. c11_vec2i src = py_tovec2i(&argv[1]);
  1084. c11_vec2i dst = py_tovec2i(&argv[2]);
  1085. py_TValue* src_data = c11_chunked_array2d_chunks__get(&self->chunks, src, NULL);
  1086. py_TValue* dst_data = c11_chunked_array2d_chunks__get(&self->chunks, dst, NULL);
  1087. if(src_data == NULL || dst_data != NULL) {
  1088. py_newbool(py_retval(), false);
  1089. return true;
  1090. }
  1091. c11_chunked_array2d_chunks__del(&self->chunks, src);
  1092. c11_chunked_array2d_chunks__set(&self->chunks, dst, src_data);
  1093. self->last_visited.value = NULL;
  1094. py_newbool(py_retval(), true);
  1095. return true;
  1096. }
  1097. static bool chunked_array2d_get_context(int argc, py_Ref argv) {
  1098. PY_CHECK_ARGC(2);
  1099. PY_CHECK_ARG_TYPE(1, tp_vec2i);
  1100. c11_chunked_array2d* self = py_touserdata(argv);
  1101. c11_vec2i pos = py_tovec2i(&argv[1]);
  1102. py_TValue* data = c11_chunked_array2d_chunks__get(&self->chunks, pos, NULL);
  1103. if(data == NULL) {
  1104. py_newnone(py_retval());
  1105. } else {
  1106. py_assign(py_retval(), &data[0]);
  1107. }
  1108. return true;
  1109. }
  1110. void c11_chunked_array2d__dtor(c11_chunked_array2d* self) {
  1111. c11__foreach(c11_chunked_array2d_chunks_KV, &self->chunks, p_kv) PK_FREE(p_kv->value);
  1112. c11_chunked_array2d_chunks__dtor(&self->chunks);
  1113. }
  1114. static void c11_chunked_array2d__mark(void* ud) {
  1115. c11_chunked_array2d* self = ud;
  1116. pk__mark_value(&self->default_T);
  1117. pk__mark_value(&self->context_builder);
  1118. int chunk_numel = self->chunk_size * self->chunk_size + 1;
  1119. for(int i = 0; i < self->chunks.length; i++) {
  1120. py_TValue* data = c11__getitem(c11_chunked_array2d_chunks_KV, &self->chunks, i).value;
  1121. for(int j = 0; j < chunk_numel; j++) {
  1122. pk__mark_value(data + j);
  1123. }
  1124. }
  1125. }
  1126. static bool chunked_array2d_view(int argc, py_Ref argv) {
  1127. PY_CHECK_ARGC(1);
  1128. c11_chunked_array2d* self = py_touserdata(&argv[0]);
  1129. if(self->chunks.length == 0) { return ValueError("chunked_array2d is empty"); }
  1130. int min_chunk_x = INT_MAX;
  1131. int min_chunk_y = INT_MAX;
  1132. int max_chunk_x = INT_MIN;
  1133. int max_chunk_y = INT_MIN;
  1134. for(int i = 0; i < self->chunks.length; i++) {
  1135. c11_vec2i chunk_pos = c11__getitem(c11_chunked_array2d_chunks_KV, &self->chunks, i).key;
  1136. min_chunk_x = c11__min(min_chunk_x, chunk_pos.x);
  1137. min_chunk_y = c11__min(min_chunk_y, chunk_pos.y);
  1138. max_chunk_x = c11__max(max_chunk_x, chunk_pos.x);
  1139. max_chunk_y = c11__max(max_chunk_y, chunk_pos.y);
  1140. }
  1141. int start_col = min_chunk_x * self->chunk_size;
  1142. int start_row = min_chunk_y * self->chunk_size;
  1143. int width = (max_chunk_x - min_chunk_x + 1) * self->chunk_size;
  1144. int height = (max_chunk_y - min_chunk_y + 1) * self->chunk_size;
  1145. return _chunked_array2d_view(py_retval(), argv, self, start_col, start_row, width, height);
  1146. }
  1147. static bool chunked_array2d_view_rect(int argc, py_Ref argv) {
  1148. PY_CHECK_ARGC(4);
  1149. PY_CHECK_ARG_TYPE(1, tp_vec2i);
  1150. PY_CHECK_ARG_TYPE(2, tp_int);
  1151. PY_CHECK_ARG_TYPE(3, tp_int);
  1152. c11_chunked_array2d* self = py_touserdata(&argv[0]);
  1153. c11_vec2i pos = py_tovec2i(&argv[1]);
  1154. int width = py_toint(&argv[2]);
  1155. int height = py_toint(&argv[3]);
  1156. return _chunked_array2d_view(py_retval(), argv, self, pos.x, pos.y, width, height);
  1157. }
  1158. static bool chunked_array2d_view_chunk(int argc, py_Ref argv) {
  1159. PY_CHECK_ARGC(2);
  1160. PY_CHECK_ARG_TYPE(1, tp_vec2i);
  1161. c11_chunked_array2d* self = py_touserdata(&argv[0]);
  1162. c11_vec2i chunk_pos = py_tovec2i(&argv[1]);
  1163. int start_col = chunk_pos.x * self->chunk_size;
  1164. int start_row = chunk_pos.y * self->chunk_size;
  1165. return _chunked_array2d_view(py_retval(),
  1166. argv,
  1167. self,
  1168. start_col,
  1169. start_row,
  1170. self->chunk_size,
  1171. self->chunk_size);
  1172. }
  1173. static bool chunked_array2d_view_chunks(int argc, py_Ref argv) {
  1174. PY_CHECK_ARGC(4);
  1175. PY_CHECK_ARG_TYPE(1, tp_vec2i);
  1176. PY_CHECK_ARG_TYPE(2, tp_int);
  1177. PY_CHECK_ARG_TYPE(3, tp_int);
  1178. c11_chunked_array2d* self = py_touserdata(&argv[0]);
  1179. c11_vec2i chunk_pos = py_tovec2i(&argv[1]);
  1180. int width = py_toint(&argv[2]) * self->chunk_size;
  1181. int height = py_toint(&argv[3]) * self->chunk_size;
  1182. int start_col = chunk_pos.x * self->chunk_size;
  1183. int start_row = chunk_pos.y * self->chunk_size;
  1184. return _chunked_array2d_view(py_retval(), argv, self, start_col, start_row, width, height);
  1185. }
  1186. static void register_chunked_array2d(py_Ref mod) {
  1187. py_Type type =
  1188. py_newtype("chunked_array2d", tp_object, mod, (py_Dtor)c11_chunked_array2d__dtor);
  1189. pk__tp_set_marker(type, c11_chunked_array2d__mark);
  1190. assert(type == tp_chunked_array2d);
  1191. py_bind(py_tpobject(type),
  1192. "__new__(cls, chunk_size, default=None, context_builder=None)",
  1193. chunked_array2d__new__);
  1194. py_bindproperty(type, "chunk_size", chunked_array2d_chunk_size, NULL);
  1195. py_bindproperty(type, "default", chunked_array2d_default, NULL);
  1196. py_bindproperty(type, "context_builder", chunked_array2d_context_builder, NULL);
  1197. py_bindmagic(type, __getitem__, chunked_array2d__getitem__);
  1198. py_bindmagic(type, __setitem__, chunked_array2d__setitem__);
  1199. py_bindmagic(type, __delitem__, chunked_array2d__delitem__);
  1200. py_bindmagic(type, __iter__, chunked_array2d__iter__);
  1201. py_bindmagic(type, __len__, chunked_array2d__len__);
  1202. py_bindmethod(type, "clear", chunked_array2d_clear);
  1203. py_bindmethod(type, "copy", chunked_array2d_copy);
  1204. py_bindmethod(type, "world_to_chunk", chunked_array2d_world_to_chunk);
  1205. py_bindmethod(type, "add_chunk", chunked_array2d_add_chunk);
  1206. py_bindmethod(type, "remove_chunk", chunked_array2d_remove_chunk);
  1207. py_bindmethod(type, "move_chunk", chunked_array2d_move_chunk);
  1208. py_bindmethod(type, "get_context", chunked_array2d_get_context);
  1209. py_bindmethod(type, "view", chunked_array2d_view);
  1210. py_bindmethod(type, "view_rect", chunked_array2d_view_rect);
  1211. py_bindmethod(type, "view_chunk", chunked_array2d_view_chunk);
  1212. py_bindmethod(type, "view_chunks", chunked_array2d_view_chunks);
  1213. }
  1214. void pk__add_module_array2d() {
  1215. py_GlobalRef mod = py_newmodule("array2d");
  1216. register_array2d_like(mod);
  1217. register_array2d_like_iterator(mod);
  1218. register_array2d(mod);
  1219. register_array2d_view(mod);
  1220. register_chunked_array2d(mod);
  1221. }