array-map.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  1. /* Copyright (C) 1996,1998,2000,2001,2004,2005, 2006, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public License
  5. * as published by the Free Software Foundation; either version 3 of
  6. * the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301 USA
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include "libguile/_scm.h"
  22. #include "libguile/strings.h"
  23. #include "libguile/arrays.h"
  24. #include "libguile/smob.h"
  25. #include "libguile/chars.h"
  26. #include "libguile/eq.h"
  27. #include "libguile/eval.h"
  28. #include "libguile/feature.h"
  29. #include "libguile/root.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/generalized-vectors.h"
  35. #include "libguile/validate.h"
  36. #include "libguile/array-map.h"
  37. /* The WHAT argument for `scm_gc_malloc ()' et al. */
  38. static const char indices_gc_hint[] = "array-indices";
  39. #define GVREF scm_c_generalized_vector_ref
  40. #define GVSET scm_c_generalized_vector_set_x
  41. static unsigned long
  42. cind (SCM ra, long *ve)
  43. {
  44. unsigned long i;
  45. int k;
  46. if (!SCM_I_ARRAYP (ra))
  47. return *ve;
  48. i = SCM_I_ARRAY_BASE (ra);
  49. for (k = 0; k < SCM_I_ARRAY_NDIM (ra); k++)
  50. i += (ve[k] - SCM_I_ARRAY_DIMS (ra)[k].lbnd) * SCM_I_ARRAY_DIMS (ra)[k].inc;
  51. return i;
  52. }
  53. /* Checker for scm_array mapping functions:
  54. return values: 4 --> shapes, increments, and bases are the same;
  55. 3 --> shapes and increments are the same;
  56. 2 --> shapes are the same;
  57. 1 --> ras are at least as big as ra0;
  58. 0 --> no match.
  59. */
  60. int
  61. scm_ra_matchp (SCM ra0, SCM ras)
  62. {
  63. SCM ra1;
  64. scm_t_array_dim dims;
  65. scm_t_array_dim *s0 = &dims;
  66. scm_t_array_dim *s1;
  67. unsigned long bas0 = 0;
  68. int i, ndim = 1;
  69. int exact = 2 /* 4 */ ; /* Don't care about values >2 (yet?) */
  70. if (scm_is_generalized_vector (ra0))
  71. {
  72. s0->lbnd = 0;
  73. s0->inc = 1;
  74. s0->ubnd = scm_c_generalized_vector_length (ra0) - 1;
  75. }
  76. else if (SCM_I_ARRAYP (ra0))
  77. {
  78. ndim = SCM_I_ARRAY_NDIM (ra0);
  79. s0 = SCM_I_ARRAY_DIMS (ra0);
  80. bas0 = SCM_I_ARRAY_BASE (ra0);
  81. }
  82. else
  83. return 0;
  84. while (scm_is_pair (ras))
  85. {
  86. ra1 = SCM_CAR (ras);
  87. if (scm_is_generalized_vector (ra1))
  88. {
  89. size_t length;
  90. if (1 != ndim)
  91. return 0;
  92. length = scm_c_generalized_vector_length (ra1);
  93. switch (exact)
  94. {
  95. case 4:
  96. if (0 != bas0)
  97. exact = 3;
  98. case 3:
  99. if (1 != s0->inc)
  100. exact = 2;
  101. case 2:
  102. if ((0 == s0->lbnd) && (s0->ubnd == length - 1))
  103. break;
  104. exact = 1;
  105. case 1:
  106. if (s0->lbnd < 0 || s0->ubnd >= length)
  107. return 0;
  108. }
  109. }
  110. else if (SCM_I_ARRAYP (ra1) && ndim == SCM_I_ARRAY_NDIM (ra1))
  111. {
  112. s1 = SCM_I_ARRAY_DIMS (ra1);
  113. if (bas0 != SCM_I_ARRAY_BASE (ra1))
  114. exact = 3;
  115. for (i = 0; i < ndim; i++)
  116. switch (exact)
  117. {
  118. case 4:
  119. case 3:
  120. if (s0[i].inc != s1[i].inc)
  121. exact = 2;
  122. case 2:
  123. if (s0[i].lbnd == s1[i].lbnd && s0[i].ubnd == s1[i].ubnd)
  124. break;
  125. exact = 1;
  126. default:
  127. if (s0[i].lbnd < s1[i].lbnd || s0[i].ubnd > s1[i].ubnd)
  128. return (s0[i].lbnd <= s0[i].ubnd ? 0 : 1);
  129. }
  130. }
  131. else
  132. return 0;
  133. ras = SCM_CDR (ras);
  134. }
  135. return exact;
  136. }
  137. /* array mapper: apply cproc to each dimension of the given arrays?.
  138. int (*cproc) (); procedure to call on unrolled arrays?
  139. cproc (dest, source list) or
  140. cproc (dest, data, source list).
  141. SCM data; data to give to cproc or unbound.
  142. SCM ra0; destination array.
  143. SCM lra; list of source arrays.
  144. const char *what; caller, for error reporting. */
  145. int
  146. scm_ramapc (void *cproc_ptr, SCM data, SCM ra0, SCM lra, const char *what)
  147. {
  148. SCM z;
  149. SCM vra0, ra1, vra1;
  150. SCM lvra, *plvra;
  151. long *vinds;
  152. int k, kmax;
  153. int (*cproc) ();
  154. cproc = cproc_ptr;
  155. switch (scm_ra_matchp (ra0, lra))
  156. {
  157. default:
  158. case 0:
  159. scm_misc_error (what, "array shape mismatch: ~S", scm_list_1 (ra0));
  160. case 2:
  161. case 3:
  162. case 4: /* Try unrolling arrays */
  163. kmax = (SCM_I_ARRAYP (ra0) ? SCM_I_ARRAY_NDIM (ra0) - 1 : 0);
  164. if (kmax < 0)
  165. goto gencase;
  166. vra0 = scm_array_contents (ra0, SCM_UNDEFINED);
  167. if (SCM_IMP (vra0)) goto gencase;
  168. if (!SCM_I_ARRAYP (vra0))
  169. {
  170. size_t length = scm_c_generalized_vector_length (vra0);
  171. vra1 = scm_i_make_array (1);
  172. SCM_I_ARRAY_BASE (vra1) = 0;
  173. SCM_I_ARRAY_DIMS (vra1)->lbnd = 0;
  174. SCM_I_ARRAY_DIMS (vra1)->ubnd = length - 1;
  175. SCM_I_ARRAY_DIMS (vra1)->inc = 1;
  176. SCM_I_ARRAY_V (vra1) = vra0;
  177. vra0 = vra1;
  178. }
  179. lvra = SCM_EOL;
  180. plvra = &lvra;
  181. for (z = lra; scm_is_pair (z); z = SCM_CDR (z))
  182. {
  183. ra1 = SCM_CAR (z);
  184. vra1 = scm_i_make_array (1);
  185. SCM_I_ARRAY_DIMS (vra1)->lbnd = SCM_I_ARRAY_DIMS (vra0)->lbnd;
  186. SCM_I_ARRAY_DIMS (vra1)->ubnd = SCM_I_ARRAY_DIMS (vra0)->ubnd;
  187. if (!SCM_I_ARRAYP (ra1))
  188. {
  189. SCM_I_ARRAY_BASE (vra1) = 0;
  190. SCM_I_ARRAY_DIMS (vra1)->inc = 1;
  191. SCM_I_ARRAY_V (vra1) = ra1;
  192. }
  193. else if (!SCM_I_ARRAY_CONTP (ra1))
  194. goto gencase;
  195. else
  196. {
  197. SCM_I_ARRAY_BASE (vra1) = SCM_I_ARRAY_BASE (ra1);
  198. SCM_I_ARRAY_DIMS (vra1)->inc = SCM_I_ARRAY_DIMS (ra1)[kmax].inc;
  199. SCM_I_ARRAY_V (vra1) = SCM_I_ARRAY_V (ra1);
  200. }
  201. *plvra = scm_cons (vra1, SCM_EOL);
  202. plvra = SCM_CDRLOC (*plvra);
  203. }
  204. return (SCM_UNBNDP (data) ? cproc(vra0, lvra) : cproc(vra0, data, lvra));
  205. case 1:
  206. gencase: /* Have to loop over all dimensions. */
  207. vra0 = scm_i_make_array (1);
  208. if (SCM_I_ARRAYP (ra0))
  209. {
  210. kmax = SCM_I_ARRAY_NDIM (ra0) - 1;
  211. if (kmax < 0)
  212. {
  213. SCM_I_ARRAY_DIMS (vra0)->lbnd = 0;
  214. SCM_I_ARRAY_DIMS (vra0)->ubnd = 0;
  215. SCM_I_ARRAY_DIMS (vra0)->inc = 1;
  216. }
  217. else
  218. {
  219. SCM_I_ARRAY_DIMS (vra0)->lbnd = SCM_I_ARRAY_DIMS (ra0)[kmax].lbnd;
  220. SCM_I_ARRAY_DIMS (vra0)->ubnd = SCM_I_ARRAY_DIMS (ra0)[kmax].ubnd;
  221. SCM_I_ARRAY_DIMS (vra0)->inc = SCM_I_ARRAY_DIMS (ra0)[kmax].inc;
  222. }
  223. SCM_I_ARRAY_BASE (vra0) = SCM_I_ARRAY_BASE (ra0);
  224. SCM_I_ARRAY_V (vra0) = SCM_I_ARRAY_V (ra0);
  225. }
  226. else
  227. {
  228. size_t length = scm_c_generalized_vector_length (ra0);
  229. kmax = 0;
  230. SCM_I_ARRAY_DIMS (vra0)->lbnd = 0;
  231. SCM_I_ARRAY_DIMS (vra0)->ubnd = length - 1;
  232. SCM_I_ARRAY_DIMS (vra0)->inc = 1;
  233. SCM_I_ARRAY_BASE (vra0) = 0;
  234. SCM_I_ARRAY_V (vra0) = ra0;
  235. ra0 = vra0;
  236. }
  237. lvra = SCM_EOL;
  238. plvra = &lvra;
  239. for (z = lra; scm_is_pair (z); z = SCM_CDR (z))
  240. {
  241. ra1 = SCM_CAR (z);
  242. vra1 = scm_i_make_array (1);
  243. SCM_I_ARRAY_DIMS (vra1)->lbnd = SCM_I_ARRAY_DIMS (vra0)->lbnd;
  244. SCM_I_ARRAY_DIMS (vra1)->ubnd = SCM_I_ARRAY_DIMS (vra0)->ubnd;
  245. if (SCM_I_ARRAYP (ra1))
  246. {
  247. if (kmax >= 0)
  248. SCM_I_ARRAY_DIMS (vra1)->inc = SCM_I_ARRAY_DIMS (ra1)[kmax].inc;
  249. SCM_I_ARRAY_V (vra1) = SCM_I_ARRAY_V (ra1);
  250. }
  251. else
  252. {
  253. SCM_I_ARRAY_DIMS (vra1)->inc = 1;
  254. SCM_I_ARRAY_V (vra1) = ra1;
  255. }
  256. *plvra = scm_cons (vra1, SCM_EOL);
  257. plvra = SCM_CDRLOC (*plvra);
  258. }
  259. vinds = scm_gc_malloc_pointerless (sizeof(long) * SCM_I_ARRAY_NDIM (ra0),
  260. indices_gc_hint);
  261. for (k = 0; k <= kmax; k++)
  262. vinds[k] = SCM_I_ARRAY_DIMS (ra0)[k].lbnd;
  263. k = kmax;
  264. do
  265. {
  266. if (k == kmax)
  267. {
  268. SCM y = lra;
  269. SCM_I_ARRAY_BASE (vra0) = cind (ra0, vinds);
  270. for (z = lvra; scm_is_pair (z); z = SCM_CDR (z), y = SCM_CDR (y))
  271. SCM_I_ARRAY_BASE (SCM_CAR (z)) = cind (SCM_CAR (y), vinds);
  272. if (0 == (SCM_UNBNDP (data) ? cproc(vra0, lvra) : cproc(vra0, data, lvra)))
  273. return 0;
  274. k--;
  275. continue;
  276. }
  277. if (vinds[k] < SCM_I_ARRAY_DIMS (ra0)[k].ubnd)
  278. {
  279. vinds[k]++;
  280. k++;
  281. continue;
  282. }
  283. vinds[k] = SCM_I_ARRAY_DIMS (ra0)[k].lbnd - 1;
  284. k--;
  285. }
  286. while (k >= 0);
  287. return 1;
  288. }
  289. }
  290. SCM_DEFINE (scm_array_fill_x, "array-fill!", 2, 0, 0,
  291. (SCM ra, SCM fill),
  292. "Store @var{fill} in every element of @var{array}. The value returned\n"
  293. "is unspecified.")
  294. #define FUNC_NAME s_scm_array_fill_x
  295. {
  296. scm_ramapc (scm_array_fill_int, fill, ra, SCM_EOL, FUNC_NAME);
  297. return SCM_UNSPECIFIED;
  298. }
  299. #undef FUNC_NAME
  300. /* to be used as cproc in scm_ramapc to fill an array dimension with
  301. "fill". */
  302. int
  303. scm_array_fill_int (SCM ra, SCM fill, SCM ignore SCM_UNUSED)
  304. #define FUNC_NAME s_scm_array_fill_x
  305. {
  306. unsigned long i;
  307. unsigned long n = SCM_I_ARRAY_DIMS (ra)->ubnd - SCM_I_ARRAY_DIMS (ra)->lbnd + 1;
  308. long inc = SCM_I_ARRAY_DIMS (ra)->inc;
  309. unsigned long base = SCM_I_ARRAY_BASE (ra);
  310. ra = SCM_I_ARRAY_V (ra);
  311. for (i = base; n--; i += inc)
  312. GVSET (ra, i, fill);
  313. return 1;
  314. }
  315. #undef FUNC_NAME
  316. static int
  317. racp (SCM src, SCM dst)
  318. {
  319. long n = (SCM_I_ARRAY_DIMS (src)->ubnd - SCM_I_ARRAY_DIMS (src)->lbnd + 1);
  320. long inc_d, inc_s = SCM_I_ARRAY_DIMS (src)->inc;
  321. unsigned long i_d, i_s = SCM_I_ARRAY_BASE (src);
  322. dst = SCM_CAR (dst);
  323. inc_d = SCM_I_ARRAY_DIMS (dst)->inc;
  324. i_d = SCM_I_ARRAY_BASE (dst);
  325. src = SCM_I_ARRAY_V (src);
  326. dst = SCM_I_ARRAY_V (dst);
  327. for (; n-- > 0; i_s += inc_s, i_d += inc_d)
  328. GVSET (dst, i_d, GVREF (src, i_s));
  329. return 1;
  330. }
  331. SCM_REGISTER_PROC(s_array_copy_in_order_x, "array-copy-in-order!", 2, 0, 0, scm_array_copy_x);
  332. SCM_DEFINE (scm_array_copy_x, "array-copy!", 2, 0, 0,
  333. (SCM src, SCM dst),
  334. "@deffnx {Scheme Procedure} array-copy-in-order! src dst\n"
  335. "Copy every element from vector or array @var{source} to the\n"
  336. "corresponding element of @var{destination}. @var{destination} must have\n"
  337. "the same rank as @var{source}, and be at least as large in each\n"
  338. "dimension. The order is unspecified.")
  339. #define FUNC_NAME s_scm_array_copy_x
  340. {
  341. scm_ramapc (racp, SCM_UNDEFINED, src, scm_cons (dst, SCM_EOL), FUNC_NAME);
  342. return SCM_UNSPECIFIED;
  343. }
  344. #undef FUNC_NAME
  345. /* Functions callable by ARRAY-MAP! */
  346. int
  347. scm_ra_eqp (SCM ra0, SCM ras)
  348. {
  349. SCM ra1 = SCM_CAR (ras), ra2 = SCM_CAR (SCM_CDR (ras));
  350. scm_t_array_handle ra0_handle;
  351. scm_t_array_dim *ra0_dims;
  352. size_t n;
  353. ssize_t inc0;
  354. size_t i0 = 0;
  355. unsigned long i1 = SCM_I_ARRAY_BASE (ra1), i2 = SCM_I_ARRAY_BASE (ra2);
  356. long inc1 = SCM_I_ARRAY_DIMS (ra1)->inc;
  357. long inc2 = SCM_I_ARRAY_DIMS (ra1)->inc;
  358. ra1 = SCM_I_ARRAY_V (ra1);
  359. ra2 = SCM_I_ARRAY_V (ra2);
  360. scm_array_get_handle (ra0, &ra0_handle);
  361. ra0_dims = scm_array_handle_dims (&ra0_handle);
  362. n = ra0_dims[0].ubnd - ra0_dims[0].lbnd + 1;
  363. inc0 = ra0_dims[0].inc;
  364. {
  365. for (; n-- > 0; i0 += inc0, i1 += inc1, i2 += inc2)
  366. if (scm_is_true (scm_array_handle_ref (&ra0_handle, i0)))
  367. if (!scm_is_eq (GVREF (ra1, i1), GVREF (ra2, i2)))
  368. scm_array_handle_set (&ra0_handle, i0, SCM_BOOL_F);
  369. }
  370. scm_array_handle_release (&ra0_handle);
  371. return 1;
  372. }
  373. /* opt 0 means <, nonzero means >= */
  374. static int
  375. ra_compare (SCM ra0, SCM ra1, SCM ra2, int opt)
  376. {
  377. scm_t_array_handle ra0_handle;
  378. scm_t_array_dim *ra0_dims;
  379. size_t n;
  380. ssize_t inc0;
  381. size_t i0 = 0;
  382. unsigned long i1 = SCM_I_ARRAY_BASE (ra1), i2 = SCM_I_ARRAY_BASE (ra2);
  383. long inc1 = SCM_I_ARRAY_DIMS (ra1)->inc;
  384. long inc2 = SCM_I_ARRAY_DIMS (ra1)->inc;
  385. ra1 = SCM_I_ARRAY_V (ra1);
  386. ra2 = SCM_I_ARRAY_V (ra2);
  387. scm_array_get_handle (ra0, &ra0_handle);
  388. ra0_dims = scm_array_handle_dims (&ra0_handle);
  389. n = ra0_dims[0].ubnd - ra0_dims[0].lbnd + 1;
  390. inc0 = ra0_dims[0].inc;
  391. {
  392. for (; n-- > 0; i0 += inc0, i1 += inc1, i2 += inc2)
  393. if (scm_is_true (scm_array_handle_ref (&ra0_handle, i0)))
  394. if (opt ?
  395. scm_is_true (scm_less_p (GVREF (ra1, i1), GVREF (ra2, i2))) :
  396. scm_is_false (scm_less_p (GVREF (ra1, i1), GVREF (ra2, i2))))
  397. scm_array_handle_set (&ra0_handle, i0, SCM_BOOL_F);
  398. }
  399. scm_array_handle_release (&ra0_handle);
  400. return 1;
  401. }
  402. int
  403. scm_ra_lessp (SCM ra0, SCM ras)
  404. {
  405. return ra_compare (ra0, SCM_CAR (ras), SCM_CAR (SCM_CDR (ras)), 0);
  406. }
  407. int
  408. scm_ra_leqp (SCM ra0, SCM ras)
  409. {
  410. return ra_compare (ra0, SCM_CAR (SCM_CDR (ras)), SCM_CAR (ras), 1);
  411. }
  412. int
  413. scm_ra_grp (SCM ra0, SCM ras)
  414. {
  415. return ra_compare (ra0, SCM_CAR (SCM_CDR (ras)), SCM_CAR (ras), 0);
  416. }
  417. int
  418. scm_ra_greqp (SCM ra0, SCM ras)
  419. {
  420. return ra_compare (ra0, SCM_CAR (ras), SCM_CAR (SCM_CDR (ras)), 1);
  421. }
  422. int
  423. scm_ra_sum (SCM ra0, SCM ras)
  424. {
  425. long n = SCM_I_ARRAY_DIMS (ra0)->ubnd - SCM_I_ARRAY_DIMS (ra0)->lbnd + 1;
  426. unsigned long i0 = SCM_I_ARRAY_BASE (ra0);
  427. long inc0 = SCM_I_ARRAY_DIMS (ra0)->inc;
  428. ra0 = SCM_I_ARRAY_V (ra0);
  429. if (!scm_is_null(ras))
  430. {
  431. SCM ra1 = SCM_CAR (ras);
  432. unsigned long i1 = SCM_I_ARRAY_BASE (ra1);
  433. long inc1 = SCM_I_ARRAY_DIMS (ra1)->inc;
  434. ra1 = SCM_I_ARRAY_V (ra1);
  435. switch (SCM_TYP7 (ra0) == SCM_TYP7 (ra1) ? SCM_TYP7 (ra0) : 0)
  436. {
  437. default:
  438. {
  439. for (; n-- > 0; i0 += inc0, i1 += inc1)
  440. GVSET (ra0, i0, scm_sum (GVREF(ra0, i0), GVREF(ra1, i1)));
  441. break;
  442. }
  443. }
  444. }
  445. return 1;
  446. }
  447. int
  448. scm_ra_difference (SCM ra0, SCM ras)
  449. {
  450. long n = SCM_I_ARRAY_DIMS (ra0)->ubnd - SCM_I_ARRAY_DIMS (ra0)->lbnd + 1;
  451. unsigned long i0 = SCM_I_ARRAY_BASE (ra0);
  452. long inc0 = SCM_I_ARRAY_DIMS (ra0)->inc;
  453. ra0 = SCM_I_ARRAY_V (ra0);
  454. if (scm_is_null (ras))
  455. {
  456. switch (SCM_TYP7 (ra0))
  457. {
  458. default:
  459. {
  460. for (; n-- > 0; i0 += inc0)
  461. GVSET (ra0, i0, scm_difference (GVREF(ra0, i0), SCM_UNDEFINED));
  462. break;
  463. }
  464. }
  465. }
  466. else
  467. {
  468. SCM ra1 = SCM_CAR (ras);
  469. unsigned long i1 = SCM_I_ARRAY_BASE (ra1);
  470. long inc1 = SCM_I_ARRAY_DIMS (ra1)->inc;
  471. ra1 = SCM_I_ARRAY_V (ra1);
  472. switch (SCM_TYP7 (ra0) == SCM_TYP7 (ra1) ? SCM_TYP7 (ra0) : 0)
  473. {
  474. default:
  475. {
  476. for (; n-- > 0; i0 += inc0, i1 += inc1)
  477. GVSET (ra0, i0, scm_difference (GVREF (ra0, i0),
  478. GVREF (ra1, i1)));
  479. break;
  480. }
  481. }
  482. }
  483. return 1;
  484. }
  485. int
  486. scm_ra_product (SCM ra0, SCM ras)
  487. {
  488. long n = SCM_I_ARRAY_DIMS (ra0)->ubnd - SCM_I_ARRAY_DIMS (ra0)->lbnd + 1;
  489. unsigned long i0 = SCM_I_ARRAY_BASE (ra0);
  490. long inc0 = SCM_I_ARRAY_DIMS (ra0)->inc;
  491. ra0 = SCM_I_ARRAY_V (ra0);
  492. if (!scm_is_null (ras))
  493. {
  494. SCM ra1 = SCM_CAR (ras);
  495. unsigned long i1 = SCM_I_ARRAY_BASE (ra1);
  496. long inc1 = SCM_I_ARRAY_DIMS (ra1)->inc;
  497. ra1 = SCM_I_ARRAY_V (ra1);
  498. switch (SCM_TYP7 (ra0) == SCM_TYP7 (ra1) ? SCM_TYP7 (ra0) : 0)
  499. {
  500. default:
  501. {
  502. for (; n-- > 0; i0 += inc0, i1 += inc1)
  503. GVSET (ra0, i0, scm_product (GVREF (ra0, i0),
  504. GVREF (ra1, i1)));
  505. }
  506. }
  507. }
  508. return 1;
  509. }
  510. int
  511. scm_ra_divide (SCM ra0, SCM ras)
  512. {
  513. long n = SCM_I_ARRAY_DIMS (ra0)->ubnd - SCM_I_ARRAY_DIMS (ra0)->lbnd + 1;
  514. unsigned long i0 = SCM_I_ARRAY_BASE (ra0);
  515. long inc0 = SCM_I_ARRAY_DIMS (ra0)->inc;
  516. ra0 = SCM_I_ARRAY_V (ra0);
  517. if (scm_is_null (ras))
  518. {
  519. switch (SCM_TYP7 (ra0))
  520. {
  521. default:
  522. {
  523. for (; n-- > 0; i0 += inc0)
  524. GVSET (ra0, i0, scm_divide (GVREF (ra0, i0), SCM_UNDEFINED));
  525. break;
  526. }
  527. }
  528. }
  529. else
  530. {
  531. SCM ra1 = SCM_CAR (ras);
  532. unsigned long i1 = SCM_I_ARRAY_BASE (ra1);
  533. long inc1 = SCM_I_ARRAY_DIMS (ra1)->inc;
  534. ra1 = SCM_I_ARRAY_V (ra1);
  535. switch (SCM_TYP7 (ra0) == SCM_TYP7 (ra1) ? SCM_TYP7 (ra0) : 0)
  536. {
  537. default:
  538. {
  539. for (; n-- > 0; i0 += inc0, i1 += inc1)
  540. {
  541. SCM res = scm_divide (GVREF (ra0, i0),
  542. GVREF (ra1, i1));
  543. GVSET (ra0, i0, res);
  544. }
  545. break;
  546. }
  547. }
  548. }
  549. return 1;
  550. }
  551. int
  552. scm_array_identity (SCM dst, SCM src)
  553. {
  554. return racp (SCM_CAR (src), scm_cons (dst, SCM_EOL));
  555. }
  556. static int
  557. ramap (SCM ra0, SCM proc, SCM ras)
  558. {
  559. long i = SCM_I_ARRAY_DIMS (ra0)->lbnd;
  560. long inc = SCM_I_ARRAY_DIMS (ra0)->inc;
  561. long n = SCM_I_ARRAY_DIMS (ra0)->ubnd;
  562. long base = SCM_I_ARRAY_BASE (ra0) - i * inc;
  563. ra0 = SCM_I_ARRAY_V (ra0);
  564. if (scm_is_null (ras))
  565. for (; i <= n; i++)
  566. GVSET (ra0, i*inc+base, scm_call_0 (proc));
  567. else
  568. {
  569. SCM ra1 = SCM_CAR (ras);
  570. SCM args;
  571. unsigned long k, i1 = SCM_I_ARRAY_BASE (ra1);
  572. long inc1 = SCM_I_ARRAY_DIMS (ra1)->inc;
  573. ra1 = SCM_I_ARRAY_V (ra1);
  574. ras = scm_vector (SCM_CDR (ras));
  575. for (; i <= n; i++, i1 += inc1)
  576. {
  577. args = SCM_EOL;
  578. for (k = scm_c_vector_length (ras); k--;)
  579. args = scm_cons (GVREF (scm_c_vector_ref (ras, k), i), args);
  580. args = scm_cons (GVREF (ra1, i1), args);
  581. GVSET (ra0, i*inc+base, scm_apply_0 (proc, args));
  582. }
  583. }
  584. return 1;
  585. }
  586. SCM_REGISTER_PROC(s_array_map_in_order_x, "array-map-in-order!", 2, 0, 1, scm_array_map_x);
  587. SCM_SYMBOL (sym_b, "b");
  588. SCM_DEFINE (scm_array_map_x, "array-map!", 2, 0, 1,
  589. (SCM ra0, SCM proc, SCM lra),
  590. "@deffnx {Scheme Procedure} array-map-in-order! ra0 proc . lra\n"
  591. "@var{array1}, @dots{} must have the same number of dimensions as\n"
  592. "@var{array0} and have a range for each index which includes the range\n"
  593. "for the corresponding index in @var{array0}. @var{proc} is applied to\n"
  594. "each tuple of elements of @var{array1} @dots{} and the result is stored\n"
  595. "as the corresponding element in @var{array0}. The value returned is\n"
  596. "unspecified. The order of application is unspecified.")
  597. #define FUNC_NAME s_scm_array_map_x
  598. {
  599. SCM_VALIDATE_PROC (2, proc);
  600. SCM_VALIDATE_REST_ARGUMENT (lra);
  601. scm_ramapc (ramap, proc, ra0, lra, FUNC_NAME);
  602. return SCM_UNSPECIFIED;
  603. }
  604. #undef FUNC_NAME
  605. static int
  606. rafe (SCM ra0, SCM proc, SCM ras)
  607. {
  608. long i = SCM_I_ARRAY_DIMS (ra0)->lbnd;
  609. unsigned long i0 = SCM_I_ARRAY_BASE (ra0);
  610. long inc0 = SCM_I_ARRAY_DIMS (ra0)->inc;
  611. long n = SCM_I_ARRAY_DIMS (ra0)->ubnd;
  612. ra0 = SCM_I_ARRAY_V (ra0);
  613. if (scm_is_null (ras))
  614. for (; i <= n; i++, i0 += inc0)
  615. scm_call_1 (proc, GVREF (ra0, i0));
  616. else
  617. {
  618. SCM ra1 = SCM_CAR (ras);
  619. SCM args;
  620. unsigned long k, i1 = SCM_I_ARRAY_BASE (ra1);
  621. long inc1 = SCM_I_ARRAY_DIMS (ra1)->inc;
  622. ra1 = SCM_I_ARRAY_V (ra1);
  623. ras = scm_vector (SCM_CDR (ras));
  624. for (; i <= n; i++, i0 += inc0, i1 += inc1)
  625. {
  626. args = SCM_EOL;
  627. for (k = scm_c_vector_length (ras); k--;)
  628. args = scm_cons (GVREF (scm_c_vector_ref (ras, k), i), args);
  629. args = scm_cons2 (GVREF (ra0, i0), GVREF (ra1, i1), args);
  630. scm_apply_0 (proc, args);
  631. }
  632. }
  633. return 1;
  634. }
  635. SCM_DEFINE (scm_array_for_each, "array-for-each", 2, 0, 1,
  636. (SCM proc, SCM ra0, SCM lra),
  637. "Apply @var{proc} to each tuple of elements of @var{array0} @dots{}\n"
  638. "in row-major order. The value returned is unspecified.")
  639. #define FUNC_NAME s_scm_array_for_each
  640. {
  641. SCM_VALIDATE_PROC (1, proc);
  642. SCM_VALIDATE_REST_ARGUMENT (lra);
  643. scm_ramapc (rafe, proc, ra0, lra, FUNC_NAME);
  644. return SCM_UNSPECIFIED;
  645. }
  646. #undef FUNC_NAME
  647. SCM_DEFINE (scm_array_index_map_x, "array-index-map!", 2, 0, 0,
  648. (SCM ra, SCM proc),
  649. "Apply @var{proc} to the indices of each element of @var{array} in\n"
  650. "turn, storing the result in the corresponding element. The value\n"
  651. "returned and the order of application are unspecified.\n\n"
  652. "One can implement @var{array-indexes} as\n"
  653. "@lisp\n"
  654. "(define (array-indexes array)\n"
  655. " (let ((ra (apply make-array #f (array-shape array))))\n"
  656. " (array-index-map! ra (lambda x x))\n"
  657. " ra))\n"
  658. "@end lisp\n"
  659. "Another example:\n"
  660. "@lisp\n"
  661. "(define (apl:index-generator n)\n"
  662. " (let ((v (make-uniform-vector n 1)))\n"
  663. " (array-index-map! v (lambda (i) i))\n"
  664. " v))\n"
  665. "@end lisp")
  666. #define FUNC_NAME s_scm_array_index_map_x
  667. {
  668. unsigned long i;
  669. SCM_VALIDATE_PROC (2, proc);
  670. if (SCM_I_ARRAYP (ra))
  671. {
  672. SCM args = SCM_EOL;
  673. int j, k, kmax = SCM_I_ARRAY_NDIM (ra) - 1;
  674. long *vinds;
  675. if (kmax < 0)
  676. return scm_array_set_x (ra, scm_call_0 (proc), SCM_EOL);
  677. vinds = scm_gc_malloc_pointerless (sizeof(long) * SCM_I_ARRAY_NDIM (ra),
  678. indices_gc_hint);
  679. for (k = 0; k <= kmax; k++)
  680. vinds[k] = SCM_I_ARRAY_DIMS (ra)[k].lbnd;
  681. k = kmax;
  682. do
  683. {
  684. if (k == kmax)
  685. {
  686. vinds[k] = SCM_I_ARRAY_DIMS (ra)[k].lbnd;
  687. i = cind (ra, vinds);
  688. for (; vinds[k] <= SCM_I_ARRAY_DIMS (ra)[k].ubnd; vinds[k]++)
  689. {
  690. for (j = kmax + 1, args = SCM_EOL; j--;)
  691. args = scm_cons (scm_from_long (vinds[j]), args);
  692. GVSET (SCM_I_ARRAY_V (ra), i, scm_apply_0 (proc, args));
  693. i += SCM_I_ARRAY_DIMS (ra)[k].inc;
  694. }
  695. k--;
  696. continue;
  697. }
  698. if (vinds[k] < SCM_I_ARRAY_DIMS (ra)[k].ubnd)
  699. {
  700. vinds[k]++;
  701. k++;
  702. continue;
  703. }
  704. vinds[k] = SCM_I_ARRAY_DIMS (ra)[k].lbnd - 1;
  705. k--;
  706. }
  707. while (k >= 0);
  708. return SCM_UNSPECIFIED;
  709. }
  710. else if (scm_is_generalized_vector (ra))
  711. {
  712. size_t length = scm_c_generalized_vector_length (ra);
  713. for (i = 0; i < length; i++)
  714. GVSET (ra, i, scm_call_1 (proc, scm_from_ulong (i)));
  715. return SCM_UNSPECIFIED;
  716. }
  717. else
  718. scm_wrong_type_arg_msg (NULL, 0, ra, "array");
  719. }
  720. #undef FUNC_NAME
  721. static int
  722. array_compare (scm_t_array_handle *hx, scm_t_array_handle *hy,
  723. size_t dim, unsigned long posx, unsigned long posy)
  724. {
  725. if (dim == scm_array_handle_rank (hx))
  726. return scm_is_true (scm_equal_p (scm_array_handle_ref (hx, posx),
  727. scm_array_handle_ref (hy, posy)));
  728. else
  729. {
  730. long incx, incy;
  731. size_t i;
  732. if (hx->dims[dim].lbnd != hy->dims[dim].lbnd
  733. || hx->dims[dim].ubnd != hy->dims[dim].ubnd)
  734. return 0;
  735. i = hx->dims[dim].ubnd - hx->dims[dim].lbnd + 1;
  736. incx = hx->dims[dim].inc;
  737. incy = hy->dims[dim].inc;
  738. posx += (i - 1) * incx;
  739. posy += (i - 1) * incy;
  740. for (; i > 0; i--, posx -= incx, posy -= incy)
  741. if (!array_compare (hx, hy, dim + 1, posx, posy))
  742. return 0;
  743. return 1;
  744. }
  745. }
  746. SCM
  747. scm_array_equal_p (SCM x, SCM y)
  748. {
  749. scm_t_array_handle hx, hy;
  750. SCM res;
  751. scm_array_get_handle (x, &hx);
  752. scm_array_get_handle (y, &hy);
  753. res = scm_from_bool (hx.ndims == hy.ndims
  754. && hx.element_type == hy.element_type);
  755. if (scm_is_true (res))
  756. res = scm_from_bool (array_compare (&hx, &hy, 0, 0, 0));
  757. scm_array_handle_release (&hy);
  758. scm_array_handle_release (&hx);
  759. return res;
  760. }
  761. static SCM scm_i_array_equal_p (SCM, SCM, SCM);
  762. SCM_DEFINE (scm_i_array_equal_p, "array-equal?", 0, 2, 1,
  763. (SCM ra0, SCM ra1, SCM rest),
  764. "Return @code{#t} iff all arguments are arrays with the same\n"
  765. "shape, the same type, and have corresponding elements which are\n"
  766. "either @code{equal?} or @code{array-equal?}. This function\n"
  767. "differs from @code{equal?} in that all arguments must be arrays.")
  768. #define FUNC_NAME s_scm_i_array_equal_p
  769. {
  770. if (SCM_UNBNDP (ra0) || SCM_UNBNDP (ra1))
  771. return SCM_BOOL_T;
  772. while (!scm_is_null (rest))
  773. { if (scm_is_false (scm_array_equal_p (ra0, ra1)))
  774. return SCM_BOOL_F;
  775. ra0 = ra1;
  776. ra1 = scm_car (rest);
  777. rest = scm_cdr (rest);
  778. }
  779. return scm_array_equal_p (ra0, ra1);
  780. }
  781. #undef FUNC_NAME
  782. void
  783. scm_init_array_map (void)
  784. {
  785. scm_smobs[SCM_TC2SMOBNUM (scm_i_tc16_array)].equalp = scm_array_equal_p;
  786. #include "libguile/array-map.x"
  787. scm_add_feature (s_scm_array_for_each);
  788. }
  789. /*
  790. Local Variables:
  791. c-file-style: "gnu"
  792. End:
  793. */