arrays.c 28 KB

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