arrays.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  1. /* Copyright 1995-1998,2000-2006,2009-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 <stdio.h>
  19. #include <errno.h>
  20. #include <string.h>
  21. #include "array-map.h"
  22. #include "bitvectors.h"
  23. #include "boolean.h"
  24. #include "bytevectors.h"
  25. #include "chars.h"
  26. #include "dynwind.h"
  27. #include "eq.h"
  28. #include "eval.h"
  29. #include "feature.h"
  30. #include "fports.h"
  31. #include "generalized-arrays.h"
  32. #include "generalized-vectors.h"
  33. #include "gsubr.h"
  34. #include "list.h"
  35. #include "modules.h"
  36. #include "numbers.h"
  37. #include "pairs.h"
  38. #include "procs.h"
  39. #include "read.h"
  40. #include "srfi-13.h"
  41. #include "srfi-4.h"
  42. #include "strings.h"
  43. #include "uniform.h"
  44. #include "vectors.h"
  45. #include "verify.h"
  46. #include "arrays.h"
  47. size_t
  48. scm_c_array_rank (SCM array)
  49. {
  50. if (SCM_I_ARRAYP (array))
  51. return SCM_I_ARRAY_NDIM (array);
  52. else if (scm_is_array (array))
  53. return 1;
  54. else
  55. scm_wrong_type_arg_msg ("array-rank", SCM_ARG1, array, "array");
  56. }
  57. SCM_DEFINE (scm_array_rank, "array-rank", 1, 0, 0,
  58. (SCM array),
  59. "Return the number of dimensions of the array @var{array.}\n")
  60. #define FUNC_NAME s_scm_array_rank
  61. {
  62. return scm_from_size_t (scm_c_array_rank (array));
  63. }
  64. #undef FUNC_NAME
  65. SCM_DEFINE (scm_shared_array_root, "shared-array-root", 1, 0, 0,
  66. (SCM ra),
  67. "Return the root vector of a shared array.")
  68. #define FUNC_NAME s_scm_shared_array_root
  69. {
  70. if (SCM_I_ARRAYP (ra))
  71. return SCM_I_ARRAY_V (ra);
  72. else if (scm_is_array (ra))
  73. return ra;
  74. else
  75. scm_wrong_type_arg_msg (FUNC_NAME, SCM_ARG1, ra, "array");
  76. }
  77. #undef FUNC_NAME
  78. SCM_DEFINE (scm_shared_array_offset, "shared-array-offset", 1, 0, 0,
  79. (SCM ra),
  80. "Return the root vector index of the first element in the array.")
  81. #define FUNC_NAME s_scm_shared_array_offset
  82. {
  83. if (SCM_I_ARRAYP (ra))
  84. return scm_from_size_t (SCM_I_ARRAY_BASE (ra));
  85. else if (scm_is_array (ra))
  86. return scm_from_size_t (0);
  87. else
  88. scm_wrong_type_arg_msg (FUNC_NAME, SCM_ARG1, ra, "array");
  89. }
  90. #undef FUNC_NAME
  91. SCM_DEFINE (scm_shared_array_increments, "shared-array-increments", 1, 0, 0,
  92. (SCM ra),
  93. "For each dimension, return the distance between elements in the root vector.")
  94. #define FUNC_NAME s_scm_shared_array_increments
  95. {
  96. if (SCM_I_ARRAYP (ra))
  97. {
  98. size_t k = SCM_I_ARRAY_NDIM (ra);
  99. SCM res = SCM_EOL;
  100. scm_t_array_dim *dims = SCM_I_ARRAY_DIMS (ra);
  101. while (k--)
  102. res = scm_cons (scm_from_ssize_t (dims[k].inc), res);
  103. return res;
  104. }
  105. else if (scm_is_array (ra))
  106. return scm_list_1 (scm_from_ssize_t (1));
  107. else
  108. scm_wrong_type_arg_msg (FUNC_NAME, SCM_ARG1, ra, "array");
  109. }
  110. #undef FUNC_NAME
  111. /* FIXME: to avoid this assumption, fix the accessors in arrays.h,
  112. scm_i_make_array, and the array cases in system/vm/assembler.scm. */
  113. verify (sizeof (scm_t_array_dim) == 3*sizeof (scm_t_bits));
  114. /* Matching SCM_I_ARRAY accessors in arrays.h */
  115. SCM
  116. scm_i_make_array (int ndim)
  117. {
  118. SCM ra = scm_words (((scm_t_bits) ndim << 17) + scm_tc11_array, 3 + ndim*3);
  119. SCM_I_ARRAY_SET_V (ra, SCM_BOOL_F);
  120. SCM_I_ARRAY_SET_BASE (ra, 0);
  121. /* dimensions are unset */
  122. return ra;
  123. }
  124. static char s_bad_spec[] = "Bad scm_array dimension";
  125. /* Increments will still need to be set. */
  126. SCM
  127. scm_i_shap2ra (SCM args)
  128. {
  129. scm_t_array_dim *s;
  130. SCM ra, spec;
  131. int ndim = scm_ilength (args);
  132. if (ndim < 0)
  133. scm_misc_error (NULL, s_bad_spec, SCM_EOL);
  134. ra = scm_i_make_array (ndim);
  135. SCM_I_ARRAY_SET_BASE (ra, 0);
  136. s = SCM_I_ARRAY_DIMS (ra);
  137. for (; !scm_is_null (args); s++, args = SCM_CDR (args))
  138. {
  139. spec = SCM_CAR (args);
  140. if (scm_is_integer (spec))
  141. {
  142. s->lbnd = 0;
  143. s->ubnd = scm_to_ssize_t (spec);
  144. if (s->ubnd < 0)
  145. scm_misc_error (NULL, s_bad_spec, SCM_EOL);
  146. --s->ubnd;
  147. }
  148. else
  149. {
  150. if (!scm_is_pair (spec) || !scm_is_integer (SCM_CAR (spec)))
  151. scm_misc_error (NULL, s_bad_spec, SCM_EOL);
  152. s->lbnd = scm_to_ssize_t (SCM_CAR (spec));
  153. spec = SCM_CDR (spec);
  154. if (!scm_is_pair (spec)
  155. || !scm_is_integer (SCM_CAR (spec))
  156. || !scm_is_null (SCM_CDR (spec)))
  157. scm_misc_error (NULL, s_bad_spec, SCM_EOL);
  158. s->ubnd = scm_to_ssize_t (SCM_CAR (spec));
  159. if (s->ubnd - s->lbnd < -1)
  160. scm_misc_error (NULL, s_bad_spec, SCM_EOL);
  161. }
  162. s->inc = 1;
  163. }
  164. return ra;
  165. }
  166. SCM_DEFINE (scm_make_typed_array, "make-typed-array", 2, 0, 1,
  167. (SCM type, SCM fill, SCM bounds),
  168. "Create and return an array of type @var{type}.")
  169. #define FUNC_NAME s_scm_make_typed_array
  170. {
  171. size_t k, rlen = 1;
  172. scm_t_array_dim *s;
  173. SCM ra;
  174. ra = scm_i_shap2ra (bounds);
  175. SCM_SET_ARRAY_CONTIGUOUS_FLAG (ra);
  176. s = SCM_I_ARRAY_DIMS (ra);
  177. k = SCM_I_ARRAY_NDIM (ra);
  178. while (k--)
  179. {
  180. s[k].inc = rlen;
  181. SCM_ASSERT_RANGE (1, bounds, s[k].lbnd <= s[k].ubnd + 1);
  182. rlen = (s[k].ubnd - s[k].lbnd + 1) * s[k].inc;
  183. }
  184. if (scm_is_eq (fill, SCM_UNSPECIFIED))
  185. fill = SCM_UNDEFINED;
  186. SCM_I_ARRAY_SET_V (ra, scm_make_generalized_vector (type, scm_from_size_t (rlen), fill));
  187. if (1 == SCM_I_ARRAY_NDIM (ra) && 0 == SCM_I_ARRAY_BASE (ra))
  188. if (0 == s->lbnd)
  189. return SCM_I_ARRAY_V (ra);
  190. return ra;
  191. }
  192. #undef FUNC_NAME
  193. SCM
  194. scm_from_contiguous_typed_array (SCM type, SCM bounds, const void *bytes,
  195. size_t byte_len)
  196. #define FUNC_NAME "scm_from_contiguous_typed_array"
  197. {
  198. size_t k, rlen = 1;
  199. scm_t_array_dim *s;
  200. SCM ra;
  201. scm_t_array_handle h;
  202. void *elts;
  203. size_t sz;
  204. ra = scm_i_shap2ra (bounds);
  205. SCM_SET_ARRAY_CONTIGUOUS_FLAG (ra);
  206. s = SCM_I_ARRAY_DIMS (ra);
  207. k = SCM_I_ARRAY_NDIM (ra);
  208. while (k--)
  209. {
  210. s[k].inc = rlen;
  211. SCM_ASSERT_RANGE (1, bounds, s[k].lbnd <= s[k].ubnd + 1);
  212. rlen = (s[k].ubnd - s[k].lbnd + 1) * s[k].inc;
  213. }
  214. SCM_I_ARRAY_SET_V (ra, scm_make_generalized_vector (type, scm_from_size_t (rlen), SCM_UNDEFINED));
  215. scm_array_get_handle (ra, &h);
  216. elts = h.writable_elements;
  217. sz = scm_array_handle_uniform_element_bit_size (&h);
  218. scm_array_handle_release (&h);
  219. if (sz >= 8 && ((sz % 8) == 0))
  220. {
  221. if (byte_len % (sz / 8))
  222. SCM_MISC_ERROR ("byte length not a multiple of the unit size", SCM_EOL);
  223. if (byte_len / (sz / 8) != rlen)
  224. SCM_MISC_ERROR ("byte length and dimensions do not match", SCM_EOL);
  225. }
  226. else if (sz < 8)
  227. {
  228. /* Elements of sub-byte size (bitvectors) are addressed in 32-bit
  229. units. */
  230. if (byte_len != ((rlen * sz + 31) / 32) * 4)
  231. SCM_MISC_ERROR ("byte length and dimensions do not match", SCM_EOL);
  232. }
  233. else
  234. /* an internal guile error, really */
  235. SCM_MISC_ERROR ("uniform elements larger than 8 bits must fill whole bytes", SCM_EOL);
  236. memcpy (elts, bytes, byte_len);
  237. if (1 == SCM_I_ARRAY_NDIM (ra) && 0 == SCM_I_ARRAY_BASE (ra))
  238. if (0 == s->lbnd)
  239. return SCM_I_ARRAY_V (ra);
  240. return ra;
  241. }
  242. #undef FUNC_NAME
  243. SCM_DEFINE (scm_make_array, "make-array", 1, 0, 1,
  244. (SCM fill, SCM bounds),
  245. "Create and return an array.")
  246. #define FUNC_NAME s_scm_make_array
  247. {
  248. return scm_make_typed_array (SCM_BOOL_T, fill, bounds);
  249. }
  250. #undef FUNC_NAME
  251. /* see scm_from_contiguous_array */
  252. static void
  253. scm_i_ra_set_contp (SCM ra)
  254. {
  255. size_t k = SCM_I_ARRAY_NDIM (ra);
  256. if (k)
  257. {
  258. ssize_t inc = SCM_I_ARRAY_DIMS (ra)[k - 1].inc;
  259. while (k--)
  260. {
  261. if (inc != SCM_I_ARRAY_DIMS (ra)[k].inc)
  262. {
  263. SCM_CLR_ARRAY_CONTIGUOUS_FLAG (ra);
  264. return;
  265. }
  266. inc *= (SCM_I_ARRAY_DIMS (ra)[k].ubnd
  267. - SCM_I_ARRAY_DIMS (ra)[k].lbnd + 1);
  268. }
  269. }
  270. SCM_SET_ARRAY_CONTIGUOUS_FLAG (ra);
  271. }
  272. SCM_DEFINE (scm_make_shared_array, "make-shared-array", 2, 0, 1,
  273. (SCM oldra, SCM mapfunc, SCM dims),
  274. "@code{make-shared-array} can be used to create shared subarrays\n"
  275. "of other arrays. The @var{mapfunc} is a function that\n"
  276. "translates coordinates in the new array into coordinates in the\n"
  277. "old array. A @var{mapfunc} must be linear, and its range must\n"
  278. "stay within the bounds of the old array, but it can be\n"
  279. "otherwise arbitrary. A simple example:\n"
  280. "@lisp\n"
  281. "(define fred (make-array #f 8 8))\n"
  282. "(define freds-diagonal\n"
  283. " (make-shared-array fred (lambda (i) (list i i)) 8))\n"
  284. "(array-set! freds-diagonal 'foo 3)\n"
  285. "(array-ref fred 3 3) @result{} foo\n"
  286. "(define freds-center\n"
  287. " (make-shared-array fred (lambda (i j) (list (+ 3 i) (+ 3 j))) 2 2))\n"
  288. "(array-ref freds-center 0 0) @result{} foo\n"
  289. "@end lisp")
  290. #define FUNC_NAME s_scm_make_shared_array
  291. {
  292. scm_t_array_handle old_handle;
  293. SCM ra;
  294. SCM inds, indptr;
  295. SCM imap;
  296. size_t k;
  297. ssize_t i;
  298. long old_base, old_min, new_min, old_max, new_max;
  299. scm_t_array_dim *s;
  300. SCM_VALIDATE_REST_ARGUMENT (dims);
  301. SCM_VALIDATE_PROC (2, mapfunc);
  302. ra = scm_i_shap2ra (dims);
  303. scm_array_get_handle (oldra, &old_handle);
  304. if (SCM_I_ARRAYP (oldra))
  305. {
  306. SCM_I_ARRAY_SET_V (ra, SCM_I_ARRAY_V (oldra));
  307. old_base = old_min = old_max = SCM_I_ARRAY_BASE (oldra);
  308. s = scm_array_handle_dims (&old_handle);
  309. k = scm_array_handle_rank (&old_handle);
  310. while (k--)
  311. {
  312. if (s[k].inc > 0)
  313. old_max += (s[k].ubnd - s[k].lbnd) * s[k].inc;
  314. else
  315. old_min += (s[k].ubnd - s[k].lbnd) * s[k].inc;
  316. }
  317. }
  318. else
  319. {
  320. SCM_I_ARRAY_SET_V (ra, oldra);
  321. old_base = old_min = 0;
  322. old_max = scm_c_array_length (oldra) - 1;
  323. }
  324. inds = SCM_EOL;
  325. s = SCM_I_ARRAY_DIMS (ra);
  326. for (k = 0; k < SCM_I_ARRAY_NDIM (ra); k++)
  327. {
  328. inds = scm_cons (scm_from_ssize_t (s[k].lbnd), inds);
  329. if (s[k].ubnd < s[k].lbnd)
  330. {
  331. if (1 == SCM_I_ARRAY_NDIM (ra))
  332. ra = scm_make_generalized_vector (scm_array_type (ra),
  333. SCM_INUM0, SCM_UNDEFINED);
  334. else
  335. SCM_I_ARRAY_SET_V (ra, scm_make_generalized_vector (scm_array_type (ra),
  336. SCM_INUM0, SCM_UNDEFINED));
  337. scm_array_handle_release (&old_handle);
  338. return ra;
  339. }
  340. }
  341. imap = scm_apply_0 (mapfunc, scm_reverse (inds));
  342. i = scm_array_handle_pos (&old_handle, imap);
  343. new_min = new_max = i + old_base;
  344. SCM_I_ARRAY_SET_BASE (ra, new_min);
  345. indptr = inds;
  346. k = SCM_I_ARRAY_NDIM (ra);
  347. while (k--)
  348. {
  349. if (s[k].ubnd > s[k].lbnd)
  350. {
  351. SCM_SETCAR (indptr, scm_sum (SCM_CAR (indptr), scm_from_int (1)));
  352. imap = scm_apply_0 (mapfunc, scm_reverse (inds));
  353. s[k].inc = scm_array_handle_pos (&old_handle, imap) - i;
  354. i += s[k].inc;
  355. if (s[k].inc > 0)
  356. new_max += (s[k].ubnd - s[k].lbnd) * s[k].inc;
  357. else
  358. new_min += (s[k].ubnd - s[k].lbnd) * s[k].inc;
  359. }
  360. else
  361. s[k].inc = new_max - new_min + 1; /* contiguous by default */
  362. indptr = SCM_CDR (indptr);
  363. }
  364. scm_array_handle_release (&old_handle);
  365. if (old_min > new_min || old_max < new_max)
  366. SCM_MISC_ERROR ("mapping out of range", SCM_EOL);
  367. if (1 == SCM_I_ARRAY_NDIM (ra) && 0 == SCM_I_ARRAY_BASE (ra))
  368. {
  369. SCM v = SCM_I_ARRAY_V (ra);
  370. size_t length = scm_c_array_length (v);
  371. if (1 == s->inc && 0 == s->lbnd && length == 1 + s->ubnd)
  372. return v;
  373. if (s->ubnd < s->lbnd)
  374. return scm_make_generalized_vector (scm_array_type (ra), SCM_INUM0,
  375. SCM_UNDEFINED);
  376. }
  377. scm_i_ra_set_contp (ra);
  378. return ra;
  379. }
  380. #undef FUNC_NAME
  381. static void
  382. array_from_pos (scm_t_array_handle *handle, size_t *ndim, size_t *k, SCM *i, ssize_t *pos,
  383. scm_t_array_dim **s, char const * FUNC_NAME, SCM error_args)
  384. {
  385. *s = scm_array_handle_dims (handle);
  386. *k = *ndim = scm_array_handle_rank (handle);
  387. for (; *k>0 && scm_is_pair (*i); --*k, ++*s, *i=scm_cdr (*i))
  388. {
  389. ssize_t ik = scm_to_ssize_t (scm_car (*i));
  390. if (ik<(*s)->lbnd || ik>(*s)->ubnd)
  391. {
  392. scm_array_handle_release (handle);
  393. scm_misc_error (FUNC_NAME, "indices out of range", error_args);
  394. }
  395. *pos += (ik-(*s)->lbnd) * (*s)->inc;
  396. }
  397. }
  398. static void
  399. array_from_get_o (scm_t_array_handle *handle, size_t k, scm_t_array_dim *s, ssize_t pos,
  400. SCM *o)
  401. {
  402. scm_t_array_dim * os;
  403. *o = scm_i_make_array (k);
  404. SCM_I_ARRAY_SET_V (*o, handle->vector);
  405. SCM_I_ARRAY_SET_BASE (*o, pos + handle->base);
  406. os = SCM_I_ARRAY_DIMS (*o);
  407. for (; k>0; --k, ++s, ++os)
  408. {
  409. os->ubnd = s->ubnd;
  410. os->lbnd = s->lbnd;
  411. os->inc = s->inc;
  412. }
  413. }
  414. SCM_DEFINE (scm_array_slice, "array-slice", 1, 0, 1,
  415. (SCM ra, SCM indices),
  416. "Return the array slice @var{ra}[@var{indices} ..., ...]\n"
  417. "The rank of @var{ra} must equal to the number of indices or larger.\n\n"
  418. "See also @code{array-ref}, @code{array-cell-ref}, @code{array-cell-set!}.\n\n"
  419. "@code{array-slice} may return a rank-0 array. For example:\n"
  420. "@lisp\n"
  421. "(array-slice #2((1 2 3) (4 5 6)) 1 1) @result{} #0(5)\n"
  422. "(array-slice #2((1 2 3) (4 5 6)) 1) @result{} #(4 5 6)\n"
  423. "(array-slice #2((1 2 3) (4 5 6))) @result{} #2((1 2 3) (4 5 6))\n"
  424. "(array-slice #0(5) @result{} #0(5).\n"
  425. "@end lisp")
  426. #define FUNC_NAME s_scm_array_slice
  427. {
  428. SCM o, i = indices;
  429. size_t ndim, k;
  430. ssize_t pos = 0;
  431. scm_t_array_handle handle;
  432. scm_t_array_dim *s;
  433. scm_array_get_handle (ra, &handle);
  434. array_from_pos (&handle, &ndim, &k, &i, &pos, &s, FUNC_NAME, scm_list_2 (ra, indices));
  435. if (k==ndim)
  436. o = ra;
  437. else if (scm_is_null (i))
  438. {
  439. array_from_get_o(&handle, k, s, pos, &o);
  440. }
  441. else
  442. {
  443. scm_array_handle_release (&handle);
  444. scm_misc_error(FUNC_NAME, "too many indices", scm_list_2 (ra, indices));
  445. }
  446. scm_array_handle_release (&handle);
  447. return o;
  448. }
  449. #undef FUNC_NAME
  450. SCM_DEFINE (scm_array_cell_ref, "array-cell-ref", 1, 0, 1,
  451. (SCM ra, SCM indices),
  452. "Return the element at the @code{(@var{indices} ...)} position\n"
  453. "in array @var{ra}, or the array slice @var{ra}[@var{indices} ..., ...]\n"
  454. "if the rank of @var{ra} is larger than the number of indices.\n\n"
  455. "See also @code{array-ref}, @code{array-slice}, @code{array-cell-set!}.\n\n"
  456. "@code{array-cell-ref} never returns a rank 0 array. For example:\n"
  457. "@lisp\n"
  458. "(array-cell-ref #2((1 2 3) (4 5 6)) 1 1) @result{} 5\n"
  459. "(array-cell-ref #2((1 2 3) (4 5 6)) 1) @result{} #(4 5 6)\n"
  460. "(array-cell-ref #2((1 2 3) (4 5 6))) @result{} #2((1 2 3) (4 5 6))\n"
  461. "(array-cell-ref #0(5) @result{} 5.\n"
  462. "@end lisp")
  463. #define FUNC_NAME s_scm_array_cell_ref
  464. {
  465. SCM o, i = indices;
  466. size_t ndim, k;
  467. ssize_t pos = 0;
  468. scm_t_array_handle handle;
  469. scm_t_array_dim *s;
  470. scm_array_get_handle (ra, &handle);
  471. array_from_pos (&handle, &ndim, &k, &i, &pos, &s, FUNC_NAME, scm_list_2 (ra, indices));
  472. if (k>0)
  473. {
  474. if (k==ndim)
  475. o = ra;
  476. else
  477. array_from_get_o(&handle, k, s, pos, &o);
  478. }
  479. else if (scm_is_null(i))
  480. o = scm_array_handle_ref (&handle, pos);
  481. else
  482. {
  483. scm_array_handle_release (&handle);
  484. scm_misc_error(FUNC_NAME, "too many indices", scm_list_2 (ra, indices));
  485. }
  486. scm_array_handle_release (&handle);
  487. return o;
  488. }
  489. #undef FUNC_NAME
  490. SCM_DEFINE (scm_array_cell_set_x, "array-cell-set!", 2, 0, 1,
  491. (SCM ra, SCM b, SCM indices),
  492. "Set the array slice @var{ra}[@var{indices} ..., ...] to @var{b}\n."
  493. "Equivalent to @code{(array-copy! @var{b} (apply array-cell-ref @var{ra} @var{indices}))}\n"
  494. "if the number of indices is smaller than the rank of @var{ra}; otherwise\n"
  495. "equivalent to @code{(apply array-set! @var{ra} @var{b} @var{indices})}.\n"
  496. "This function returns the modified array @var{ra}.\n\n"
  497. "See also @code{array-ref}, @code{array-cell-ref}, @code{array-slice}.\n\n"
  498. "For example:\n"
  499. "@lisp\n"
  500. "(define A (list->array 2 '((1 2 3) (4 5 6))))\n"
  501. "(array-cell-set! A #0(99) 1 1) @result{} #2((1 2 3) (4 #0(99) 6))\n"
  502. "(array-cell-set! A 99 1 1) @result{} #2((1 2 3) (4 99 6))\n"
  503. "(array-cell-set! A #(a b c) 0) @result{} #2((a b c) (4 99 6))\n"
  504. "(array-cell-set! A #2((x y z) (9 8 7))) @result{} #2((x y z) (9 8 7))\n\n"
  505. "(define B (make-array 0))\n"
  506. "(array-cell-set! B 15) @result{} #0(15)\n"
  507. "@end lisp")
  508. #define FUNC_NAME s_scm_array_cell_set_x
  509. {
  510. SCM o, i = indices;
  511. size_t ndim, k;
  512. ssize_t pos = 0;
  513. scm_t_array_handle handle;
  514. scm_t_array_dim *s;
  515. scm_array_get_handle (ra, &handle);
  516. array_from_pos (&handle, &ndim, &k, &i, &pos, &s, FUNC_NAME, scm_list_3 (ra, b, indices));
  517. if (k>0)
  518. {
  519. if (k==ndim)
  520. o = ra;
  521. else
  522. array_from_get_o(&handle, k, s, pos, &o);
  523. scm_array_handle_release(&handle);
  524. /* an error is still possible here if o and b don't match. */
  525. /* FIXME copying like this wastes the handle, and the bounds matching
  526. behavior of array-copy! is not strict. */
  527. scm_array_copy_x(b, o);
  528. }
  529. else if (scm_is_null(i))
  530. {
  531. scm_array_handle_set (&handle, pos, b); /* ra may be non-ARRAYP */
  532. scm_array_handle_release (&handle);
  533. }
  534. else
  535. {
  536. scm_array_handle_release (&handle);
  537. scm_misc_error(FUNC_NAME, "too many indices", scm_list_3 (ra, b, indices));
  538. }
  539. return ra;
  540. }
  541. #undef FUNC_NAME
  542. #undef ARRAY_FROM_GET_O
  543. /* args are RA . DIMS */
  544. SCM_DEFINE (scm_transpose_array, "transpose-array", 1, 0, 1,
  545. (SCM ra, SCM args),
  546. "Return an array sharing contents with @var{ra}, but with\n"
  547. "dimensions arranged in a different order. There must be one\n"
  548. "@var{dim} argument for each dimension of @var{ra}.\n"
  549. "@var{dim0}, @var{dim1}, @dots{} should be integers between 0\n"
  550. "and the rank of the array to be returned. Each integer in that\n"
  551. "range must appear at least once in the argument list.\n"
  552. "\n"
  553. "The values of @var{dim0}, @var{dim1}, @dots{} correspond to\n"
  554. "dimensions in the array to be returned, their positions in the\n"
  555. "argument list to dimensions of @var{ra}. Several @var{dim}s\n"
  556. "may have the same value, in which case the returned array will\n"
  557. "have smaller rank than @var{ra}.\n"
  558. "\n"
  559. "@lisp\n"
  560. "(transpose-array '#2((a b) (c d)) 1 0) @result{} #2((a c) (b d))\n"
  561. "(transpose-array '#2((a b) (c d)) 0 0) @result{} #1(a d)\n"
  562. "(transpose-array '#3(((a b c) (d e f)) ((1 2 3) (4 5 6))) 1 1 0) @result{}\n"
  563. " #2((a 4) (b 5) (c 6))\n"
  564. "@end lisp")
  565. #define FUNC_NAME s_scm_transpose_array
  566. {
  567. SCM res, vargs;
  568. scm_t_array_dim *s, *r;
  569. int ndim, i, k;
  570. SCM_VALIDATE_REST_ARGUMENT (args);
  571. SCM_ASSERT (SCM_HEAP_OBJECT_P (ra), ra, SCM_ARG1, FUNC_NAME);
  572. switch (scm_c_array_rank (ra))
  573. {
  574. case 0:
  575. if (!scm_is_null (args))
  576. SCM_WRONG_NUM_ARGS ();
  577. return ra;
  578. case 1:
  579. /* Make sure that we are called with a single zero as
  580. arguments.
  581. */
  582. if (scm_is_null (args) || !scm_is_null (SCM_CDR (args)))
  583. SCM_WRONG_NUM_ARGS ();
  584. SCM_VALIDATE_INT_COPY (SCM_ARG2, SCM_CAR (args), i);
  585. SCM_ASSERT_RANGE (SCM_ARG2, SCM_CAR (args), i == 0);
  586. return ra;
  587. default:
  588. vargs = scm_vector (args);
  589. if (SCM_SIMPLE_VECTOR_LENGTH (vargs) != SCM_I_ARRAY_NDIM (ra))
  590. SCM_WRONG_NUM_ARGS ();
  591. ndim = 0;
  592. for (k = 0; k < SCM_I_ARRAY_NDIM (ra); k++)
  593. {
  594. i = scm_to_signed_integer (SCM_SIMPLE_VECTOR_REF (vargs, k),
  595. 0, SCM_I_ARRAY_NDIM(ra));
  596. if (ndim < i)
  597. ndim = i;
  598. }
  599. ndim++;
  600. res = scm_i_make_array (ndim);
  601. SCM_I_ARRAY_SET_V (res, SCM_I_ARRAY_V (ra));
  602. SCM_I_ARRAY_SET_BASE (res, SCM_I_ARRAY_BASE (ra));
  603. for (k = ndim; k--;)
  604. {
  605. SCM_I_ARRAY_DIMS (res)[k].lbnd = 0;
  606. SCM_I_ARRAY_DIMS (res)[k].ubnd = -1;
  607. }
  608. for (k = SCM_I_ARRAY_NDIM (ra); k--;)
  609. {
  610. i = scm_to_int (SCM_SIMPLE_VECTOR_REF (vargs, k));
  611. s = &(SCM_I_ARRAY_DIMS (ra)[k]);
  612. r = &(SCM_I_ARRAY_DIMS (res)[i]);
  613. if (r->ubnd < r->lbnd)
  614. {
  615. r->lbnd = s->lbnd;
  616. r->ubnd = s->ubnd;
  617. r->inc = s->inc;
  618. ndim--;
  619. }
  620. else
  621. {
  622. if (r->ubnd > s->ubnd)
  623. r->ubnd = s->ubnd;
  624. if (r->lbnd < s->lbnd)
  625. {
  626. SCM_I_ARRAY_SET_BASE (res, SCM_I_ARRAY_BASE (res) + (s->lbnd - r->lbnd) * r->inc);
  627. r->lbnd = s->lbnd;
  628. }
  629. r->inc += s->inc;
  630. }
  631. }
  632. if (ndim > 0)
  633. SCM_MISC_ERROR ("bad argument list", SCM_EOL);
  634. scm_i_ra_set_contp (res);
  635. return res;
  636. }
  637. }
  638. #undef FUNC_NAME
  639. /* attempts to unroll an array into a one-dimensional array.
  640. returns the unrolled array or #f if it can't be done. */
  641. /* if strict is true, return #f if returned array
  642. wouldn't have contiguous elements. */
  643. SCM_DEFINE (scm_array_contents, "array-contents", 1, 1, 0,
  644. (SCM ra, SCM strict),
  645. "If @var{ra} may be @dfn{unrolled} into a one dimensional shared\n"
  646. "array without changing their order (last subscript changing\n"
  647. "fastest), then @code{array-contents} returns that shared array,\n"
  648. "otherwise it returns @code{#f}. All arrays made by\n"
  649. "@code{make-array} and @code{make-uniform-array} may be unrolled,\n"
  650. "some arrays made by @code{make-shared-array} may not be. If\n"
  651. "the optional argument @var{strict} is provided, a shared array\n"
  652. "will be returned only if its elements are stored contiguously\n"
  653. "in memory.")
  654. #define FUNC_NAME s_scm_array_contents
  655. {
  656. if (SCM_I_ARRAYP (ra))
  657. {
  658. SCM v;
  659. size_t ndim = SCM_I_ARRAY_NDIM (ra);
  660. scm_t_array_dim *s = SCM_I_ARRAY_DIMS (ra);
  661. size_t k = ndim;
  662. size_t len = 1;
  663. if (k)
  664. {
  665. ssize_t last_inc = s[k - 1].inc;
  666. while (k--)
  667. {
  668. if (len*last_inc != s[k].inc)
  669. return SCM_BOOL_F;
  670. len *= (s[k].ubnd - s[k].lbnd + 1);
  671. }
  672. }
  673. if (!SCM_UNBNDP (strict) && scm_is_true (strict))
  674. {
  675. if (ndim && (1 != s[ndim - 1].inc))
  676. return SCM_BOOL_F;
  677. if (scm_is_bitvector (SCM_I_ARRAY_V (ra))
  678. && (len != scm_c_bitvector_length (SCM_I_ARRAY_V (ra)) ||
  679. SCM_I_ARRAY_BASE (ra) % SCM_LONG_BIT ||
  680. len % SCM_LONG_BIT))
  681. return SCM_BOOL_F;
  682. }
  683. v = SCM_I_ARRAY_V (ra);
  684. if ((len == scm_c_array_length (v)) && (0 == SCM_I_ARRAY_BASE (ra)))
  685. return v;
  686. else
  687. {
  688. SCM sra = scm_i_make_array (1);
  689. SCM_I_ARRAY_DIMS (sra)->lbnd = 0;
  690. SCM_I_ARRAY_DIMS (sra)->ubnd = len - 1;
  691. SCM_I_ARRAY_SET_V (sra, v);
  692. SCM_I_ARRAY_SET_BASE (sra, SCM_I_ARRAY_BASE (ra));
  693. SCM_I_ARRAY_DIMS (sra)->inc = (ndim ? SCM_I_ARRAY_DIMS (ra)[ndim - 1].inc : 1);
  694. return sra;
  695. }
  696. }
  697. else if (scm_is_array (ra))
  698. return ra;
  699. else
  700. scm_wrong_type_arg_msg (NULL, 0, ra, "array");
  701. }
  702. #undef FUNC_NAME
  703. static void
  704. list_to_array (SCM lst, scm_t_array_handle *handle, ssize_t pos, size_t k)
  705. {
  706. if (k == scm_array_handle_rank (handle))
  707. scm_array_handle_set (handle, pos, lst);
  708. else
  709. {
  710. scm_t_array_dim *dim = scm_array_handle_dims (handle) + k;
  711. ssize_t inc = dim->inc;
  712. size_t len = 1 + dim->ubnd - dim->lbnd, n;
  713. char *errmsg = NULL;
  714. n = len;
  715. while (n > 0 && scm_is_pair (lst))
  716. {
  717. list_to_array (SCM_CAR (lst), handle, pos, k + 1);
  718. pos += inc;
  719. lst = SCM_CDR (lst);
  720. n -= 1;
  721. }
  722. if (n != 0)
  723. errmsg = "too few elements for array dimension ~a, need ~a";
  724. if (!scm_is_null (lst))
  725. errmsg = "too many elements for array dimension ~a, want ~a";
  726. if (errmsg)
  727. scm_misc_error (NULL, errmsg, scm_list_2 (scm_from_size_t (k),
  728. scm_from_size_t (len)));
  729. }
  730. }
  731. SCM_DEFINE (scm_list_to_typed_array, "list->typed-array", 3, 0, 0,
  732. (SCM type, SCM shape, SCM lst),
  733. "Return an array of the type @var{type}\n"
  734. "with elements the same as those of @var{lst}.\n"
  735. "\n"
  736. "The argument @var{shape} determines the number of dimensions\n"
  737. "of the array and their shape. It is either an exact integer,\n"
  738. "giving the\n"
  739. "number of dimensions directly, or a list whose length\n"
  740. "specifies the number of dimensions and each element specified\n"
  741. "the lower and optionally the upper bound of the corresponding\n"
  742. "dimension.\n"
  743. "When the element is list of two elements, these elements\n"
  744. "give the lower and upper bounds. When it is an exact\n"
  745. "integer, it gives only the lower bound.")
  746. #define FUNC_NAME s_scm_list_to_typed_array
  747. {
  748. SCM row;
  749. SCM ra;
  750. scm_t_array_handle handle;
  751. row = lst;
  752. if (scm_is_integer (shape))
  753. {
  754. size_t k = scm_to_size_t (shape);
  755. shape = SCM_EOL;
  756. while (k-- > 0)
  757. {
  758. shape = scm_cons (scm_length (row), shape);
  759. if (k > 0 && !scm_is_null (row))
  760. row = scm_car (row);
  761. }
  762. }
  763. else
  764. {
  765. SCM shape_spec = shape;
  766. shape = SCM_EOL;
  767. while (1)
  768. {
  769. SCM spec = scm_car (shape_spec);
  770. if (scm_is_pair (spec))
  771. shape = scm_cons (spec, shape);
  772. else
  773. shape = scm_cons (scm_list_2 (spec,
  774. scm_sum (scm_sum (spec,
  775. scm_length (row)),
  776. scm_from_int (-1))),
  777. shape);
  778. shape_spec = scm_cdr (shape_spec);
  779. if (scm_is_pair (shape_spec))
  780. {
  781. if (!scm_is_null (row))
  782. row = scm_car (row);
  783. }
  784. else
  785. break;
  786. }
  787. }
  788. ra = scm_make_typed_array (type, SCM_UNSPECIFIED,
  789. scm_reverse_x (shape, SCM_EOL));
  790. scm_array_get_handle (ra, &handle);
  791. list_to_array (lst, &handle, 0, 0);
  792. scm_array_handle_release (&handle);
  793. return ra;
  794. }
  795. #undef FUNC_NAME
  796. SCM_DEFINE (scm_list_to_array, "list->array", 2, 0, 0,
  797. (SCM ndim, SCM lst),
  798. "Return an array with elements the same as those of @var{lst}.")
  799. #define FUNC_NAME s_scm_list_to_array
  800. {
  801. return scm_list_to_typed_array (SCM_BOOL_T, ndim, lst);
  802. }
  803. #undef FUNC_NAME
  804. /* Print dimension DIM of ARRAY.
  805. */
  806. static int
  807. scm_i_print_array_dimension (scm_t_array_handle *h, int dim, int pos,
  808. SCM port, scm_print_state *pstate)
  809. {
  810. if (dim == h->ndims)
  811. scm_iprin1 (scm_array_handle_ref (h, pos), port, pstate);
  812. else
  813. {
  814. ssize_t i;
  815. scm_putc ('(', port);
  816. for (i = h->dims[dim].lbnd; i <= h->dims[dim].ubnd;
  817. i++, pos += h->dims[dim].inc)
  818. {
  819. scm_i_print_array_dimension (h, dim+1, pos, port, pstate);
  820. if (i < h->dims[dim].ubnd)
  821. scm_putc (' ', port);
  822. }
  823. scm_putc (')', port);
  824. }
  825. return 1;
  826. }
  827. int
  828. scm_i_print_array (SCM array, SCM port, scm_print_state *pstate)
  829. {
  830. scm_t_array_handle h;
  831. int d;
  832. scm_call_2 (scm_c_private_ref ("ice-9 arrays", "array-print-prefix"),
  833. array, port);
  834. scm_array_get_handle (array, &h);
  835. if (h.ndims == 0)
  836. {
  837. /* Rank zero arrays, which are really just scalars, are printed
  838. specially. The consequent way would be to print them as
  839. #0 OBJ
  840. where OBJ is the printed representation of the scalar, but we
  841. print them instead as
  842. #0(OBJ)
  843. to make them look less strange.
  844. Just printing them as
  845. OBJ
  846. would be correct in a way as well, but zero rank arrays are
  847. not really the same as Scheme values since they are boxed and
  848. can be modified with array-set!, say.
  849. */
  850. scm_putc ('(', port);
  851. scm_i_print_array_dimension (&h, 0, 0, port, pstate);
  852. scm_putc (')', port);
  853. d = 1;
  854. }
  855. else
  856. d = scm_i_print_array_dimension (&h, 0, 0, port, pstate);
  857. scm_array_handle_release (&h);
  858. return d;
  859. }
  860. void
  861. scm_init_arrays ()
  862. {
  863. scm_add_feature ("array");
  864. #include "arrays.x"
  865. }