array-map.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  1. /* Copyright 1996,1998,2000-2001,2004-2006,2008-2015,2018-2019
  2. Free Software Foundation, Inc.
  3. This file is part of Guile.
  4. Guile is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Guile is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  11. License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with Guile. If not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifdef HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #include <assert.h>
  19. #include <string.h>
  20. #include "arrays.h"
  21. #include "boolean.h"
  22. #include "chars.h"
  23. #include "eq.h"
  24. #include "eval.h"
  25. #include "feature.h"
  26. #include "gsubr.h"
  27. #include "list.h"
  28. #include "numbers.h"
  29. #include "pairs.h"
  30. #include "procs.h"
  31. #include "smob.h"
  32. #include "srfi-4.h"
  33. #include "strings.h"
  34. #include "symbols.h"
  35. #include "vectors.h"
  36. #include "array-map.h"
  37. /* The WHAT argument for `scm_gc_malloc ()' et al. */
  38. static const char vi_gc_hint[] = "array-indices";
  39. static SCM
  40. make1array (SCM v, ssize_t inc)
  41. {
  42. SCM a = scm_i_make_array (1);
  43. SCM_I_ARRAY_SET_BASE (a, 0);
  44. SCM_I_ARRAY_DIMS (a)->lbnd = 0;
  45. SCM_I_ARRAY_DIMS (a)->ubnd = scm_c_array_length (v) - 1;
  46. SCM_I_ARRAY_DIMS (a)->inc = inc;
  47. SCM_I_ARRAY_SET_V (a, v);
  48. return a;
  49. }
  50. /* Linear index of not-unrolled index set. */
  51. static size_t
  52. cindk (SCM ra, ssize_t *ve, int kend)
  53. {
  54. if (SCM_I_ARRAYP (ra))
  55. {
  56. int k;
  57. size_t i = SCM_I_ARRAY_BASE (ra);
  58. for (k = 0; k < kend; ++k)
  59. i += (ve[k] - SCM_I_ARRAY_DIMS (ra)[k].lbnd) * SCM_I_ARRAY_DIMS (ra)[k].inc;
  60. return i;
  61. }
  62. else
  63. return 0; /* this is BASE */
  64. }
  65. /* array mapper: apply cproc to each dimension of the given arrays?.
  66. int (*cproc) (); procedure to call on unrolled arrays?
  67. cproc (dest, source list) or
  68. cproc (dest, data, source list).
  69. SCM data; data to give to cproc or unbound.
  70. SCM ra0; destination array.
  71. SCM lra; list of source arrays.
  72. const char *what; caller, for error reporting. */
  73. #define LBND(ra, k) SCM_I_ARRAY_DIMS (ra)[k].lbnd
  74. #define UBND(ra, k) SCM_I_ARRAY_DIMS (ra)[k].ubnd
  75. #define MAX(A, B) ((A) >= (B) ? (A) : (B))
  76. /* scm_ramapc() always calls cproc with rank-1 arrays created by
  77. make1array. cproc (rafe, ramap, rafill, racp) can assume that the
  78. dims[0].lbnd of these arrays is always 0. */
  79. int
  80. scm_ramapc (void *cproc_ptr, SCM data, SCM ra0, SCM lra, const char *what)
  81. {
  82. int (*cproc) () = cproc_ptr;
  83. SCM z, va0, lva, *plva;
  84. int k, kmax, kroll;
  85. ssize_t *vi, inc;
  86. size_t len;
  87. /* Prepare reference argument. */
  88. if (SCM_I_ARRAYP (ra0))
  89. {
  90. kmax = SCM_I_ARRAY_NDIM (ra0)-1;
  91. inc = kmax < 0 ? 0 : SCM_I_ARRAY_DIMS (ra0)[kmax].inc;
  92. va0 = make1array (SCM_I_ARRAY_V (ra0), inc);
  93. /* Find unroll depth */
  94. for (kroll = MAX (0, kmax); kroll > 0; --kroll)
  95. {
  96. inc *= (UBND (ra0, kroll) - LBND (ra0, kroll) + 1);
  97. if (inc != SCM_I_ARRAY_DIMS (ra0)[kroll-1].inc)
  98. break;
  99. }
  100. }
  101. else
  102. {
  103. kroll = kmax = 0;
  104. va0 = ra0 = make1array (ra0, 1);
  105. }
  106. /* Prepare rest arguments. */
  107. lva = SCM_EOL;
  108. plva = &lva;
  109. for (z = lra; !scm_is_null (z); z = SCM_CDR (z))
  110. {
  111. SCM va1, ra1 = SCM_CAR (z);
  112. if (SCM_I_ARRAYP (ra1))
  113. {
  114. if (kmax != SCM_I_ARRAY_NDIM (ra1) - 1)
  115. scm_misc_error (what, "array shape mismatch: ~S", scm_list_1 (ra0));
  116. inc = kmax < 0 ? 0 : SCM_I_ARRAY_DIMS (ra1)[kmax].inc;
  117. va1 = make1array (SCM_I_ARRAY_V (ra1), inc);
  118. /* Check unroll depth. */
  119. for (k = kmax; k > kroll; --k)
  120. {
  121. ssize_t l0 = LBND (ra0, k), u0 = UBND (ra0, k);
  122. if (l0 < LBND (ra1, k) || u0 > UBND (ra1, k))
  123. scm_misc_error (what, "array shape mismatch: ~S", scm_list_1 (ra0));
  124. inc *= (u0 - l0 + 1);
  125. if (inc != SCM_I_ARRAY_DIMS (ra1)[k-1].inc)
  126. {
  127. kroll = k;
  128. break;
  129. }
  130. }
  131. /* Check matching of not-unrolled axes. */
  132. for (; k>=0; --k)
  133. if (LBND (ra0, k) < LBND (ra1, k) || UBND (ra0, k) > UBND (ra1, k))
  134. scm_misc_error (what, "array shape mismatch: ~S", scm_list_1 (ra0));
  135. }
  136. else
  137. {
  138. if (kmax != 0)
  139. scm_misc_error (what, "array shape mismatch: ~S", scm_list_1 (ra0));
  140. va1 = make1array (ra1, 1);
  141. if (LBND (ra0, 0) < 0 /* LBND (va1, 0) */ || UBND (ra0, 0) > UBND (va1, 0))
  142. scm_misc_error (what, "array shape mismatch: ~S", scm_list_1 (ra0));
  143. }
  144. *plva = scm_cons (va1, SCM_EOL);
  145. plva = SCM_CDRLOC (*plva);
  146. }
  147. /* Check emptiness of not-unrolled axes. */
  148. for (k = 0; k < kroll; ++k)
  149. if (0 == (UBND (ra0, k) - LBND (ra0, k) + 1))
  150. return 1;
  151. /* Set unrolled size. */
  152. for (len = 1; k <= kmax; ++k)
  153. len *= (UBND (ra0, k) - LBND (ra0, k) + 1);
  154. UBND (va0, 0) = len - 1;
  155. for (z = lva; !scm_is_null (z); z = SCM_CDR (z))
  156. UBND (SCM_CAR (z), 0) = len - 1;
  157. /* Set starting indices and go. */
  158. vi = scm_gc_malloc_pointerless (sizeof(ssize_t) * kroll, vi_gc_hint);
  159. for (k = 0; k < kroll; ++k)
  160. vi[k] = LBND (ra0, k);
  161. do
  162. {
  163. if (k == kroll)
  164. {
  165. SCM y = lra;
  166. SCM_I_ARRAY_SET_BASE (va0, cindk (ra0, vi, kroll));
  167. for (z = lva; !scm_is_null (z); z = SCM_CDR (z), y = SCM_CDR (y))
  168. SCM_I_ARRAY_SET_BASE (SCM_CAR (z), cindk (SCM_CAR (y), vi, kroll));
  169. if (! (SCM_UNBNDP (data) ? cproc (va0, lva) : cproc (va0, data, lva)))
  170. return 0;
  171. --k;
  172. }
  173. else if (vi[k] < UBND (ra0, k))
  174. {
  175. ++vi[k];
  176. ++k;
  177. }
  178. else
  179. {
  180. vi[k] = LBND (ra0, k) - 1;
  181. --k;
  182. }
  183. }
  184. while (k >= 0);
  185. return 1;
  186. }
  187. #undef UBND
  188. #undef LBND
  189. static int
  190. rafill (SCM dst, SCM fill)
  191. {
  192. size_t n = SCM_I_ARRAY_DIMS (dst)->ubnd + 1;
  193. size_t i = SCM_I_ARRAY_BASE (dst);
  194. ssize_t inc = SCM_I_ARRAY_DIMS (dst)->inc;
  195. scm_t_array_handle h;
  196. dst = SCM_I_ARRAY_V (dst);
  197. scm_array_get_handle (dst, &h);
  198. for (; n-- > 0; i += inc)
  199. h.vset (h.vector, i, fill);
  200. scm_array_handle_release (&h);
  201. return 1;
  202. }
  203. SCM_DEFINE (scm_array_fill_x, "array-fill!", 2, 0, 0,
  204. (SCM ra, SCM fill),
  205. "Store @var{fill} in every element of array @var{ra}. The value\n"
  206. "returned is unspecified.")
  207. #define FUNC_NAME s_scm_array_fill_x
  208. {
  209. scm_ramapc (rafill, fill, ra, SCM_EOL, FUNC_NAME);
  210. return SCM_UNSPECIFIED;
  211. }
  212. #undef FUNC_NAME
  213. static int
  214. racp (SCM src, SCM dst)
  215. {
  216. size_t i_s, i_d, n;
  217. ssize_t inc_s, inc_d;
  218. scm_t_array_handle h_s, h_d;
  219. dst = SCM_CAR (dst);
  220. i_s = SCM_I_ARRAY_BASE (src);
  221. i_d = SCM_I_ARRAY_BASE (dst);
  222. n = (SCM_I_ARRAY_DIMS (src)->ubnd + 1);
  223. inc_s = SCM_I_ARRAY_DIMS (src)->inc;
  224. inc_d = SCM_I_ARRAY_DIMS (dst)->inc;
  225. src = SCM_I_ARRAY_V (src);
  226. dst = SCM_I_ARRAY_V (dst);
  227. scm_array_get_handle (src, &h_s);
  228. scm_array_get_handle (dst, &h_d);
  229. if (h_s.element_type == SCM_ARRAY_ELEMENT_TYPE_SCM
  230. && h_d.element_type == SCM_ARRAY_ELEMENT_TYPE_SCM)
  231. {
  232. SCM const * el_s = h_s.elements;
  233. SCM * el_d = h_d.writable_elements;
  234. if (!el_d && n>0)
  235. scm_wrong_type_arg_msg ("array-copy!", SCM_ARG2, dst, "mutable array");
  236. for (; n-- > 0; i_s += inc_s, i_d += inc_d)
  237. el_d[i_d] = el_s[i_s];
  238. }
  239. else
  240. for (; n-- > 0; i_s += inc_s, i_d += inc_d)
  241. h_d.vset (h_d.vector, i_d, h_s.vref (h_s.vector, i_s));
  242. scm_array_handle_release (&h_d);
  243. scm_array_handle_release (&h_s);
  244. return 1;
  245. }
  246. SCM_REGISTER_PROC(s_array_copy_in_order_x, "array-copy-in-order!", 2, 0, 0, scm_array_copy_x);
  247. SCM_DEFINE (scm_array_copy_x, "array-copy!", 2, 0, 0,
  248. (SCM src, SCM dst),
  249. "@deffnx {Scheme Procedure} array-copy-in-order! src dst\n"
  250. "Copy every element from vector or array @var{src} to the\n"
  251. "corresponding element of @var{dst}. @var{dst} must have the\n"
  252. "same rank as @var{src}, and be at least as large in each\n"
  253. "dimension. The order is unspecified.")
  254. #define FUNC_NAME s_scm_array_copy_x
  255. {
  256. scm_ramapc (racp, SCM_UNDEFINED, src, scm_cons (dst, SCM_EOL), FUNC_NAME);
  257. return SCM_UNSPECIFIED;
  258. }
  259. #undef FUNC_NAME
  260. static int
  261. ramap (SCM ra0, SCM proc, SCM ras)
  262. {
  263. size_t i0 = SCM_I_ARRAY_BASE (ra0);
  264. ssize_t inc0 = SCM_I_ARRAY_DIMS (ra0)->inc;
  265. size_t n = SCM_I_ARRAY_DIMS (ra0)->ubnd + 1;
  266. scm_t_array_handle h0;
  267. ra0 = SCM_I_ARRAY_V (ra0);
  268. scm_array_get_handle (ra0, &h0);
  269. if (scm_is_null (ras))
  270. for (; n--; i0 += inc0)
  271. h0.vset (h0.vector, i0, scm_call_0 (proc));
  272. else
  273. {
  274. SCM ra1 = SCM_CAR (ras);
  275. size_t i1 = SCM_I_ARRAY_BASE (ra1);
  276. ssize_t inc1 = SCM_I_ARRAY_DIMS (ra1)->inc;
  277. scm_t_array_handle h1;
  278. ra1 = SCM_I_ARRAY_V (ra1);
  279. scm_array_get_handle (ra1, &h1);
  280. ras = SCM_CDR (ras);
  281. if (scm_is_null (ras))
  282. for (; n--; i0 += inc0, i1 += inc1)
  283. h0.vset (h0.vector, i0, scm_call_1 (proc, h1.vref (h1.vector, i1)));
  284. else
  285. {
  286. SCM ra2 = SCM_CAR (ras);
  287. size_t i2 = SCM_I_ARRAY_BASE (ra2);
  288. ssize_t inc2 = SCM_I_ARRAY_DIMS (ra2)->inc;
  289. scm_t_array_handle h2;
  290. ra2 = SCM_I_ARRAY_V (ra2);
  291. scm_array_get_handle (ra2, &h2);
  292. ras = SCM_CDR (ras);
  293. if (scm_is_null (ras))
  294. for (; n--; i0 += inc0, i1 += inc1, i2 += inc2)
  295. h0.vset (h0.vector, i0, scm_call_2 (proc, h1.vref (h1.vector, i1), h2.vref (h2.vector, i2)));
  296. else
  297. {
  298. scm_t_array_handle *hs;
  299. size_t restn = scm_ilength (ras);
  300. SCM args = SCM_EOL;
  301. SCM *p = &args;
  302. SCM **sa = scm_gc_malloc (sizeof(SCM *) * restn, vi_gc_hint);
  303. size_t k;
  304. ssize_t i;
  305. for (k = 0; k < restn; ++k)
  306. {
  307. *p = scm_cons (SCM_UNSPECIFIED, SCM_EOL);
  308. sa[k] = SCM_CARLOC (*p);
  309. p = SCM_CDRLOC (*p);
  310. }
  311. hs = scm_gc_malloc (sizeof(scm_t_array_handle) * restn, vi_gc_hint);
  312. for (k = 0; k < restn; ++k, ras = scm_cdr (ras))
  313. scm_array_get_handle (scm_car (ras), hs+k);
  314. for (i = 0; n--; i0 += inc0, i1 += inc1, i2 += inc2, ++i)
  315. {
  316. for (k = 0; k < restn; ++k)
  317. *(sa[k]) = scm_array_handle_ref (hs+k, i*hs[k].dims[0].inc);
  318. h0.vset (h0.vector, i0, scm_apply_2 (proc, h1.vref (h1.vector, i1), h2.vref (h2.vector, i2), args));
  319. }
  320. for (k = 0; k < restn; ++k)
  321. scm_array_handle_release (hs+k);
  322. }
  323. scm_array_handle_release (&h2);
  324. }
  325. scm_array_handle_release (&h1);
  326. }
  327. scm_array_handle_release (&h0);
  328. return 1;
  329. }
  330. SCM_REGISTER_PROC(s_array_map_in_order_x, "array-map-in-order!", 2, 0, 1, scm_array_map_x);
  331. SCM_SYMBOL (sym_b, "b");
  332. SCM_DEFINE (scm_array_map_x, "array-map!", 2, 0, 1,
  333. (SCM ra0, SCM proc, SCM lra),
  334. "@deffnx {Scheme Procedure} array-map-in-order! ra0 proc . lra\n"
  335. "@var{array1}, @dots{} must have the same number of dimensions\n"
  336. "as @var{ra0} and have a range for each index which includes the\n"
  337. "range for the corresponding index in @var{ra0}. @var{proc} is\n"
  338. "applied to each tuple of elements of @var{array1}, @dots{} and\n"
  339. "the result is stored as the corresponding element in @var{ra0}.\n"
  340. "The value returned is unspecified. The order of application is\n"
  341. "unspecified.")
  342. #define FUNC_NAME s_scm_array_map_x
  343. {
  344. SCM_VALIDATE_PROC (2, proc);
  345. SCM_VALIDATE_REST_ARGUMENT (lra);
  346. scm_ramapc (ramap, proc, ra0, lra, FUNC_NAME);
  347. return SCM_UNSPECIFIED;
  348. }
  349. #undef FUNC_NAME
  350. static int
  351. rafe (SCM ra0, SCM proc, SCM ras)
  352. {
  353. size_t i0 = SCM_I_ARRAY_BASE (ra0);
  354. ssize_t inc0 = SCM_I_ARRAY_DIMS (ra0)->inc;
  355. size_t n = SCM_I_ARRAY_DIMS (ra0)->ubnd + 1;
  356. scm_t_array_handle h0;
  357. ra0 = SCM_I_ARRAY_V (ra0);
  358. scm_array_get_handle (ra0, &h0);
  359. if (scm_is_null (ras))
  360. for (; n--; i0 += inc0)
  361. scm_call_1 (proc, h0.vref (h0.vector, i0));
  362. else
  363. {
  364. scm_t_array_handle *hs;
  365. size_t restn = scm_ilength (ras);
  366. SCM args = SCM_EOL;
  367. SCM *p = &args;
  368. SCM **sa = scm_gc_malloc (sizeof(SCM *) * restn, vi_gc_hint);
  369. for (size_t k = 0; k < restn; ++k)
  370. {
  371. *p = scm_cons (SCM_UNSPECIFIED, SCM_EOL);
  372. sa[k] = SCM_CARLOC (*p);
  373. p = SCM_CDRLOC (*p);
  374. }
  375. hs = scm_gc_malloc (sizeof(scm_t_array_handle) * restn, vi_gc_hint);
  376. for (size_t k = 0; k < restn; ++k, ras = scm_cdr (ras))
  377. scm_array_get_handle (scm_car (ras), hs+k);
  378. for (ssize_t i = 0; n--; i0 += inc0, ++i)
  379. {
  380. for (size_t k = 0; k < restn; ++k)
  381. *(sa[k]) = scm_array_handle_ref (hs+k, i*hs[k].dims[0].inc);
  382. scm_apply_1 (proc, h0.vref (h0.vector, i0), args);
  383. }
  384. for (size_t k = 0; k < restn; ++k)
  385. scm_array_handle_release (hs+k);
  386. }
  387. scm_array_handle_release (&h0);
  388. return 1;
  389. }
  390. SCM_DEFINE (scm_array_for_each, "array-for-each", 2, 0, 1,
  391. (SCM proc, SCM ra0, SCM lra),
  392. "Apply @var{proc} to each tuple of elements of @var{ra0} @dots{}\n"
  393. "in row-major order. The value returned is unspecified.")
  394. #define FUNC_NAME s_scm_array_for_each
  395. {
  396. SCM_VALIDATE_PROC (1, proc);
  397. SCM_VALIDATE_REST_ARGUMENT (lra);
  398. scm_ramapc (rafe, proc, ra0, lra, FUNC_NAME);
  399. return SCM_UNSPECIFIED;
  400. }
  401. #undef FUNC_NAME
  402. static void
  403. array_index_map_1 (SCM ra, SCM proc)
  404. {
  405. scm_t_array_handle h;
  406. ssize_t i, inc;
  407. size_t p;
  408. scm_array_get_handle (ra, &h);
  409. inc = h.dims[0].inc;
  410. for (i = h.dims[0].lbnd, p = h.base; i <= h.dims[0].ubnd; ++i, p += inc)
  411. h.vset (h.vector, p, scm_call_1 (proc, scm_from_ssize_t (i)));
  412. scm_array_handle_release (&h);
  413. }
  414. /* Here we assume that the array is a scm_tc7_array, as that is the only
  415. kind of array in Guile that supports rank > 1. */
  416. static void
  417. array_index_map_n (SCM ra, SCM proc)
  418. {
  419. scm_t_array_handle h;
  420. int k, kmax = SCM_I_ARRAY_NDIM (ra) - 1;
  421. SCM args = SCM_EOL;
  422. SCM *p = &args;
  423. ssize_t *vi = scm_gc_malloc_pointerless (sizeof(ssize_t) * (kmax + 1), vi_gc_hint);
  424. SCM **si = scm_gc_malloc_pointerless (sizeof(SCM *) * (kmax + 1), vi_gc_hint);
  425. for (k = 0; k <= kmax; k++)
  426. {
  427. vi[k] = SCM_I_ARRAY_DIMS (ra)[k].lbnd;
  428. if (vi[k] > SCM_I_ARRAY_DIMS (ra)[k].ubnd)
  429. return;
  430. *p = scm_cons (scm_from_ssize_t (vi[k]), SCM_EOL);
  431. si[k] = SCM_CARLOC (*p);
  432. p = SCM_CDRLOC (*p);
  433. }
  434. scm_array_get_handle (ra, &h);
  435. k = kmax;
  436. do
  437. {
  438. if (k == kmax)
  439. {
  440. size_t i;
  441. vi[kmax] = SCM_I_ARRAY_DIMS (ra)[kmax].lbnd;
  442. i = cindk (ra, vi, kmax+1);
  443. for (; vi[kmax] <= SCM_I_ARRAY_DIMS (ra)[kmax].ubnd; ++vi[kmax])
  444. {
  445. *(si[kmax]) = scm_from_ssize_t (vi[kmax]);
  446. h.vset (h.vector, i, scm_apply_0 (proc, args));
  447. i += SCM_I_ARRAY_DIMS (ra)[kmax].inc;
  448. }
  449. k--;
  450. }
  451. else if (vi[k] < SCM_I_ARRAY_DIMS (ra)[k].ubnd)
  452. {
  453. *(si[k]) = scm_from_ssize_t (++vi[k]);
  454. k++;
  455. }
  456. else
  457. {
  458. vi[k] = SCM_I_ARRAY_DIMS (ra)[k].lbnd - 1;
  459. k--;
  460. }
  461. }
  462. while (k >= 0);
  463. scm_array_handle_release (&h);
  464. }
  465. SCM_DEFINE (scm_array_index_map_x, "array-index-map!", 2, 0, 0,
  466. (SCM ra, SCM proc),
  467. "Apply @var{proc} to the indices of each element of @var{ra} in\n"
  468. "turn, storing the result in the corresponding element. The value\n"
  469. "returned and the order of application are unspecified.\n\n"
  470. "One can implement @var{array-indexes} as\n"
  471. "@lisp\n"
  472. "(define (array-indexes array)\n"
  473. " (let ((ra (apply make-array #f (array-shape array))))\n"
  474. " (array-index-map! ra (lambda x x))\n"
  475. " ra))\n"
  476. "@end lisp\n"
  477. "Another example:\n"
  478. "@lisp\n"
  479. "(define (apl:index-generator n)\n"
  480. " (let ((v (make-uniform-vector n 1)))\n"
  481. " (array-index-map! v (lambda (i) i))\n"
  482. " v))\n"
  483. "@end lisp")
  484. #define FUNC_NAME s_scm_array_index_map_x
  485. {
  486. SCM_VALIDATE_PROC (2, proc);
  487. switch (scm_c_array_rank (ra))
  488. {
  489. case 0:
  490. scm_array_set_x (ra, scm_call_0 (proc), SCM_EOL);
  491. break;
  492. case 1:
  493. array_index_map_1 (ra, proc);
  494. break;
  495. default:
  496. array_index_map_n (ra, proc);
  497. break;
  498. }
  499. return SCM_UNSPECIFIED;
  500. }
  501. #undef FUNC_NAME
  502. static int
  503. array_compare (scm_t_array_handle *hx, scm_t_array_handle *hy,
  504. size_t dim, unsigned long posx, unsigned long posy)
  505. {
  506. if (dim == scm_array_handle_rank (hx))
  507. return scm_is_true (scm_equal_p (scm_array_handle_ref (hx, posx),
  508. scm_array_handle_ref (hy, posy)));
  509. else
  510. {
  511. long incx, incy;
  512. size_t i;
  513. if (hx->dims[dim].lbnd != hy->dims[dim].lbnd
  514. || hx->dims[dim].ubnd != hy->dims[dim].ubnd)
  515. return 0;
  516. i = hx->dims[dim].ubnd - hx->dims[dim].lbnd + 1;
  517. incx = hx->dims[dim].inc;
  518. incy = hy->dims[dim].inc;
  519. posx += (i - 1) * incx;
  520. posy += (i - 1) * incy;
  521. for (; i > 0; i--, posx -= incx, posy -= incy)
  522. if (!array_compare (hx, hy, dim + 1, posx, posy))
  523. return 0;
  524. return 1;
  525. }
  526. }
  527. SCM
  528. scm_array_equal_p (SCM x, SCM y)
  529. {
  530. scm_t_array_handle hx, hy;
  531. SCM res;
  532. scm_array_get_handle (x, &hx);
  533. scm_array_get_handle (y, &hy);
  534. scm_t_array_element_type t1 = hx.element_type;
  535. scm_t_array_element_type t2 = hy.element_type;
  536. /* R6RS and Guile mostly use #vu8(...) as the literal syntax for
  537. bytevectors, but R7RS uses #u8. To allow R7RS users to re-use the
  538. various routines implemented on bytevectors which return vu8-tagged
  539. values and to also be able to do (equal? #u8(1 2 3) (bytevector 1 2
  540. 3)), we allow equality comparisons between vu8 and u8. */
  541. if (t1 == SCM_ARRAY_ELEMENT_TYPE_VU8)
  542. t1 = SCM_ARRAY_ELEMENT_TYPE_U8;
  543. if (t2 == SCM_ARRAY_ELEMENT_TYPE_VU8)
  544. t2 = SCM_ARRAY_ELEMENT_TYPE_U8;
  545. res = scm_from_bool (hx.ndims == hy.ndims && t1 == t2);
  546. if (scm_is_true (res))
  547. res = scm_from_bool (array_compare (&hx, &hy, 0, 0, 0));
  548. scm_array_handle_release (&hy);
  549. scm_array_handle_release (&hx);
  550. return res;
  551. }
  552. static SCM scm_i_array_equal_p (SCM, SCM, SCM);
  553. SCM_DEFINE (scm_i_array_equal_p, "array-equal?", 0, 2, 1,
  554. (SCM ra0, SCM ra1, SCM rest),
  555. "Return @code{#t} iff all arguments are arrays with the same\n"
  556. "shape, the same type, and have corresponding elements which are\n"
  557. "either @code{equal?} or @code{array-equal?}. This function\n"
  558. "differs from @code{equal?} in that all arguments must be arrays.")
  559. #define FUNC_NAME s_scm_i_array_equal_p
  560. {
  561. if (SCM_UNBNDP (ra0) || SCM_UNBNDP (ra1))
  562. return SCM_BOOL_T;
  563. while (!scm_is_null (rest))
  564. {
  565. if (scm_is_false (scm_array_equal_p (ra0, ra1)))
  566. return SCM_BOOL_F;
  567. ra0 = ra1;
  568. ra1 = scm_car (rest);
  569. rest = scm_cdr (rest);
  570. }
  571. return scm_array_equal_p (ra0, ra1);
  572. }
  573. #undef FUNC_NAME
  574. /* Copy array descriptor with different base. */
  575. SCM
  576. scm_i_array_rebase (SCM a, size_t base)
  577. {
  578. size_t ndim = SCM_I_ARRAY_NDIM (a);
  579. SCM b = scm_words (((scm_t_bits) ndim << 17) + scm_tc7_array, 3 + ndim*3);
  580. SCM_I_ARRAY_SET_V (b, SCM_I_ARRAY_V (a));
  581. /* FIXME do check base */
  582. SCM_I_ARRAY_SET_BASE (b, base);
  583. memcpy (SCM_I_ARRAY_DIMS (b), SCM_I_ARRAY_DIMS (a), sizeof (scm_t_array_dim)*ndim);
  584. return b;
  585. }
  586. static inline size_t padtoptr(size_t d) { return (d + (sizeof (void *) - 1)) & ~(sizeof (void *) - 1); }
  587. SCM_DEFINE (scm_array_slice_for_each, "array-slice-for-each", 2, 0, 1,
  588. (SCM frame_rank, SCM op, SCM args),
  589. "Apply @var{op} to each of the cells of rank rank(@var{arg})-@var{frame_rank}\n"
  590. "of the arrays @var{args}, in unspecified order. The first\n"
  591. "@var{frame_rank} dimensions of each @var{arg} must match.\n"
  592. "Rank-0 cells are passed as rank-0 arrays.\n\n"
  593. "The value returned is unspecified.\n\n"
  594. "For example:\n"
  595. "@lisp\n"
  596. ";; Sort the rows of rank-2 array A.\n\n"
  597. "(array-slice-for-each 1 (lambda (x) (sort! x <)) a)\n"
  598. "\n"
  599. ";; Compute the arguments of the (x y) vectors in the rows of rank-2\n"
  600. ";; array XYS and store them in rank-1 array ANGLES. Inside OP,\n"
  601. ";; XY is a rank-1 (2-1) array, and ANGLE is a rank-0 (1-1) array.\n\n"
  602. "(array-slice-for-each 1 \n"
  603. " (lambda (xy angle)\n"
  604. " (array-set! angle (atan (array-ref xy 1) (array-ref xy 0))))\n"
  605. " xys angles)\n"
  606. "@end lisp")
  607. #define FUNC_NAME s_scm_array_slice_for_each
  608. {
  609. SCM xargs = args;
  610. int const N = scm_ilength (args);
  611. int const frank = scm_to_int (frame_rank);
  612. int ocd;
  613. ssize_t step;
  614. SCM dargs_ = SCM_EOL;
  615. char const * msg;
  616. scm_t_array_dim * ais;
  617. int n, k;
  618. ssize_t z;
  619. /* to be allocated inside the pool */
  620. scm_t_array_handle * ah;
  621. SCM * args_;
  622. scm_t_array_dim ** as;
  623. int * rank;
  624. ssize_t * s;
  625. SCM * ai;
  626. SCM ** dargs;
  627. ssize_t * i;
  628. int * order;
  629. size_t * base;
  630. /* size the pool */
  631. char * pool;
  632. char * pool0;
  633. size_t pool_size = 0;
  634. pool_size += padtoptr(N*sizeof (scm_t_array_handle));
  635. pool_size += padtoptr(N*sizeof (SCM));
  636. pool_size += padtoptr(N*sizeof (scm_t_array_dim *));
  637. pool_size += padtoptr(N*sizeof (int));
  638. pool_size += padtoptr(frank*sizeof (ssize_t));
  639. pool_size += padtoptr(N*sizeof (SCM));
  640. pool_size += padtoptr(N*sizeof (SCM *));
  641. pool_size += padtoptr(frank*sizeof (ssize_t));
  642. pool_size += padtoptr(frank*sizeof (int));
  643. pool_size += padtoptr(N*sizeof (size_t));
  644. pool = scm_gc_malloc (pool_size, "pool");
  645. /* place the items in the pool */
  646. #define AFIC_ALLOC_ADVANCE(pool, count, type, name) \
  647. name = (void *)pool; \
  648. pool += padtoptr(count*sizeof (type));
  649. pool0 = pool;
  650. AFIC_ALLOC_ADVANCE (pool, N, scm_t_array_handle, ah);
  651. AFIC_ALLOC_ADVANCE (pool, N, SCM, args_);
  652. AFIC_ALLOC_ADVANCE (pool, N, scm_t_array_dim *, as);
  653. AFIC_ALLOC_ADVANCE (pool, N, int, rank);
  654. AFIC_ALLOC_ADVANCE (pool, frank, ssize_t, s);
  655. AFIC_ALLOC_ADVANCE (pool, N, SCM, ai);
  656. AFIC_ALLOC_ADVANCE (pool, N, SCM *, dargs);
  657. AFIC_ALLOC_ADVANCE (pool, frank, ssize_t, i);
  658. AFIC_ALLOC_ADVANCE (pool, frank, int, order);
  659. AFIC_ALLOC_ADVANCE (pool, N, size_t, base);
  660. assert((pool0+pool_size==pool) && "internal error");
  661. #undef AFIC_ALLOC_ADVANCE
  662. for (n=0, xargs=args; scm_is_pair(xargs); xargs=scm_cdr(xargs), ++n)
  663. {
  664. args_[n] = scm_car(xargs);
  665. scm_array_get_handle(args_[n], ah+n);
  666. as[n] = scm_array_handle_dims(ah+n);
  667. rank[n] = scm_array_handle_rank(ah+n);
  668. }
  669. /* checks */
  670. msg = NULL;
  671. if (frank<0)
  672. msg = "bad frame rank ~S, ~S";
  673. else
  674. {
  675. for (n=0; n!=N; ++n)
  676. {
  677. if (rank[n]<frank)
  678. {
  679. msg = "frame too large for arguments: ~S, ~S";
  680. goto check_msg;
  681. }
  682. for (k=0; k!=frank; ++k)
  683. {
  684. if (as[0][k].lbnd!=as[n][k].lbnd || as[0][k].ubnd!=as[n][k].ubnd)
  685. {
  686. msg = "mismatched frames: ~S, ~S";
  687. goto check_msg;
  688. }
  689. s[k] = as[n][k].ubnd - as[n][k].lbnd + 1;
  690. /* this check is needed if the array cannot be entirely */
  691. /* unrolled, because the unrolled subloop will be run before */
  692. /* checking the dimensions of the frame. */
  693. if (s[k]==0)
  694. goto end;
  695. }
  696. }
  697. }
  698. check_msg: ;
  699. if (msg!=NULL)
  700. {
  701. for (n=0; n!=N; ++n)
  702. scm_array_handle_release(ah+n);
  703. scm_misc_error("array-slice-for-each", msg, scm_cons(frame_rank, args));
  704. }
  705. /* prepare moving cells. */
  706. for (n=0; n!=N; ++n)
  707. {
  708. ai[n] = scm_i_make_array(rank[n]-frank);
  709. SCM_I_ARRAY_SET_V (ai[n], scm_shared_array_root(args_[n]));
  710. /* FIXME scm_array_handle_base (ah+n) should be in Guile */
  711. SCM_I_ARRAY_SET_BASE (ai[n], ah[n].base);
  712. ais = SCM_I_ARRAY_DIMS(ai[n]);
  713. for (k=frank; k!=rank[n]; ++k)
  714. {
  715. ais[k-frank] = as[n][k];
  716. }
  717. }
  718. /* prepare rest list for callee. */
  719. {
  720. SCM *p = &dargs_;
  721. for (n=0; n<N; ++n)
  722. {
  723. *p = scm_cons (SCM_UNSPECIFIED, SCM_EOL);
  724. dargs[n] = SCM_CARLOC (*p);
  725. p = SCM_CDRLOC (*p);
  726. }
  727. }
  728. /* special case for rank 0. */
  729. if (frank==0)
  730. {
  731. for (n=0; n<N; ++n)
  732. *dargs[n] = ai[n];
  733. scm_apply_0(op, dargs_);
  734. for (n=0; n<N; ++n)
  735. scm_array_handle_release(ah+n);
  736. return SCM_UNSPECIFIED;
  737. }
  738. /* FIXME determine best looping order. */
  739. for (k=0; k!=frank; ++k)
  740. {
  741. i[k] = 0;
  742. order[k] = frank-1-k;
  743. }
  744. /* find outermost compact dim. */
  745. step = s[order[0]];
  746. ocd = 1;
  747. for (; ocd<frank; step *= s[order[ocd]], ++ocd)
  748. for (n=0; n!=N; ++n)
  749. if (step*as[n][order[0]].inc!=as[n][order[ocd]].inc)
  750. goto ocd_reached;
  751. ocd_reached: ;
  752. /* rank loop. */
  753. for (n=0; n!=N; ++n)
  754. base[n] = SCM_I_ARRAY_BASE(ai[n]);
  755. for (;;)
  756. {
  757. /* unrolled loop. */
  758. for (z=0; z!=step; ++z)
  759. {
  760. /* we are forced to create fresh array descriptors for each */
  761. /* call since we don't know whether the callee will keep them, */
  762. /* and Guile offers no way to copy the descriptor (since */
  763. /* descriptors are immutable). Yet another reason why this */
  764. /* should be in Scheme. */
  765. for (n=0; n<N; ++n)
  766. {
  767. *dargs[n] = scm_i_array_rebase(ai[n], base[n]);
  768. base[n] += as[n][order[0]].inc;
  769. }
  770. scm_apply_0(op, dargs_);
  771. }
  772. for (n=0; n<N; ++n)
  773. base[n] -= step*as[n][order[0]].inc;
  774. for (k=ocd; ; ++k)
  775. {
  776. if (k==frank)
  777. goto end;
  778. else if (i[order[k]]<s[order[k]]-1)
  779. {
  780. ++i[order[k]];
  781. for (n=0; n<N; ++n)
  782. base[n] += as[n][order[k]].inc;
  783. break;
  784. }
  785. else
  786. {
  787. i[order[k]] = 0;
  788. for (n=0; n<N; ++n)
  789. base[n] += as[n][order[k]].inc*(1-s[order[k]]);
  790. }
  791. }
  792. }
  793. end:;
  794. for (n=0; n<N; ++n)
  795. scm_array_handle_release(ah+n);
  796. return SCM_UNSPECIFIED;
  797. }
  798. #undef FUNC_NAME
  799. SCM_DEFINE (scm_array_slice_for_each_in_order, "array-slice-for-each-in-order", 2, 0, 1,
  800. (SCM frank, SCM op, SCM a),
  801. "Same as array-slice-for-each, but visit the cells sequentially\n"
  802. "and in row-major order.\n")
  803. #define FUNC_NAME s_scm_array_slice_for_each_in_order
  804. {
  805. return scm_array_slice_for_each (frank, op, a);
  806. }
  807. #undef FUNC_NAME
  808. void
  809. scm_init_array_map (void)
  810. {
  811. #include "array-map.x"
  812. scm_add_feature (s_scm_array_for_each);
  813. }