array-map.c 27 KB

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