array2d.c 54 KB

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