array2d.c 53 KB

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