arrays.c 23 KB

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