arrays.c 23 KB

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