arrays.c 24 KB

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