arrays.c 28 KB

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