array-map.c 26 KB

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