arrays.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  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/smob.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. scm_t_bits scm_i_tc16_array;
  49. #define SCM_SET_ARRAY_CONTIGUOUS_FLAG(x) \
  50. (SCM_SET_SMOB_FLAGS ((x), SCM_SMOB_FLAGS (x) | SCM_I_ARRAY_FLAG_CONTIGUOUS))
  51. #define SCM_CLR_ARRAY_CONTIGUOUS_FLAG(x) \
  52. (SCM_SET_SMOB_FLAGS ((x), SCM_SMOB_FLAGS (x) & ~SCM_I_ARRAY_FLAG_CONTIGUOUS))
  53. SCM_DEFINE (scm_shared_array_root, "shared-array-root", 1, 0, 0,
  54. (SCM ra),
  55. "Return the root vector of a shared array.")
  56. #define FUNC_NAME s_scm_shared_array_root
  57. {
  58. if (SCM_I_ARRAYP (ra))
  59. return SCM_I_ARRAY_V (ra);
  60. else if (scm_is_generalized_vector (ra))
  61. return ra;
  62. scm_wrong_type_arg_msg (FUNC_NAME, SCM_ARG1, ra, "array");
  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. SCM_NEWSMOB(ra, ((scm_t_bits) ndim << 17) + scm_i_tc16_array,
  101. 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, sp;
  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. if (scm_to_long (spec) < 0)
  126. scm_misc_error (NULL, s_bad_spec, SCM_EOL);
  127. s->lbnd = 0;
  128. s->ubnd = scm_to_long (spec) - 1;
  129. s->inc = 1;
  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_long (SCM_CAR (spec));
  136. sp = SCM_CDR (spec);
  137. if (!scm_is_pair (sp)
  138. || !scm_is_integer (SCM_CAR (sp))
  139. || !scm_is_null (SCM_CDR (sp)))
  140. scm_misc_error (NULL, s_bad_spec, SCM_EOL);
  141. s->ubnd = scm_to_long (SCM_CAR (sp));
  142. s->inc = 1;
  143. }
  144. }
  145. return ra;
  146. }
  147. SCM_DEFINE (scm_make_typed_array, "make-typed-array", 2, 0, 1,
  148. (SCM type, SCM fill, SCM bounds),
  149. "Create and return an array of type @var{type}.")
  150. #define FUNC_NAME s_scm_make_typed_array
  151. {
  152. size_t k, rlen = 1;
  153. scm_t_array_dim *s;
  154. SCM ra;
  155. ra = scm_i_shap2ra (bounds);
  156. SCM_SET_ARRAY_CONTIGUOUS_FLAG (ra);
  157. s = SCM_I_ARRAY_DIMS (ra);
  158. k = SCM_I_ARRAY_NDIM (ra);
  159. while (k--)
  160. {
  161. s[k].inc = rlen;
  162. SCM_ASSERT_RANGE (1, bounds, s[k].lbnd <= s[k].ubnd + 1);
  163. rlen = (s[k].ubnd - s[k].lbnd + 1) * s[k].inc;
  164. }
  165. if (scm_is_eq (fill, SCM_UNSPECIFIED))
  166. fill = SCM_UNDEFINED;
  167. SCM_I_ARRAY_V (ra) =
  168. scm_make_generalized_vector (type, scm_from_size_t (rlen), fill);
  169. if (1 == SCM_I_ARRAY_NDIM (ra) && 0 == SCM_I_ARRAY_BASE (ra))
  170. if (s->ubnd < s->lbnd || (0 == s->lbnd && 1 == s->inc))
  171. return SCM_I_ARRAY_V (ra);
  172. return ra;
  173. }
  174. #undef FUNC_NAME
  175. SCM
  176. scm_from_contiguous_typed_array (SCM type, SCM bounds, const void *bytes,
  177. size_t byte_len)
  178. #define FUNC_NAME "scm_from_contiguous_typed_array"
  179. {
  180. size_t k, rlen = 1;
  181. scm_t_array_dim *s;
  182. SCM ra;
  183. scm_t_array_handle h;
  184. void *elts;
  185. size_t sz;
  186. ra = scm_i_shap2ra (bounds);
  187. SCM_SET_ARRAY_CONTIGUOUS_FLAG (ra);
  188. s = SCM_I_ARRAY_DIMS (ra);
  189. k = SCM_I_ARRAY_NDIM (ra);
  190. while (k--)
  191. {
  192. s[k].inc = rlen;
  193. SCM_ASSERT_RANGE (1, bounds, s[k].lbnd <= s[k].ubnd + 1);
  194. rlen = (s[k].ubnd - s[k].lbnd + 1) * s[k].inc;
  195. }
  196. SCM_I_ARRAY_V (ra) =
  197. scm_make_generalized_vector (type, scm_from_size_t (rlen), SCM_UNDEFINED);
  198. scm_array_get_handle (ra, &h);
  199. elts = h.writable_elements;
  200. sz = scm_array_handle_uniform_element_bit_size (&h);
  201. scm_array_handle_release (&h);
  202. if (sz >= 8 && ((sz % 8) == 0))
  203. {
  204. if (byte_len % (sz / 8))
  205. SCM_MISC_ERROR ("byte length not a multiple of the unit size", SCM_EOL);
  206. if (byte_len / (sz / 8) != rlen)
  207. SCM_MISC_ERROR ("byte length and dimensions do not match", SCM_EOL);
  208. }
  209. else if (sz < 8)
  210. {
  211. /* byte_len ?= ceil (rlen * sz / 8) */
  212. if (byte_len != (rlen * sz + 7) / 8)
  213. SCM_MISC_ERROR ("byte length and dimensions do not match", SCM_EOL);
  214. }
  215. else
  216. /* an internal guile error, really */
  217. SCM_MISC_ERROR ("uniform elements larger than 8 bits must fill whole bytes", SCM_EOL);
  218. memcpy (elts, bytes, byte_len);
  219. if (1 == SCM_I_ARRAY_NDIM (ra) && 0 == SCM_I_ARRAY_BASE (ra))
  220. if (s->ubnd < s->lbnd || (0 == s->lbnd && 1 == s->inc))
  221. return SCM_I_ARRAY_V (ra);
  222. return ra;
  223. }
  224. #undef FUNC_NAME
  225. SCM
  226. scm_from_contiguous_array (SCM bounds, const SCM *elts, size_t len)
  227. #define FUNC_NAME "scm_from_contiguous_array"
  228. {
  229. size_t k, rlen = 1;
  230. scm_t_array_dim *s;
  231. SCM ra;
  232. scm_t_array_handle h;
  233. ra = scm_i_shap2ra (bounds);
  234. SCM_SET_ARRAY_CONTIGUOUS_FLAG (ra);
  235. s = SCM_I_ARRAY_DIMS (ra);
  236. k = SCM_I_ARRAY_NDIM (ra);
  237. while (k--)
  238. {
  239. s[k].inc = rlen;
  240. SCM_ASSERT_RANGE (1, bounds, s[k].lbnd <= s[k].ubnd + 1);
  241. rlen = (s[k].ubnd - s[k].lbnd + 1) * s[k].inc;
  242. }
  243. if (rlen != len)
  244. SCM_MISC_ERROR ("element length and dimensions do not match", SCM_EOL);
  245. SCM_I_ARRAY_V (ra) = scm_c_make_vector (rlen, SCM_UNDEFINED);
  246. scm_array_get_handle (ra, &h);
  247. memcpy (h.writable_elements, elts, rlen * sizeof(SCM));
  248. scm_array_handle_release (&h);
  249. if (1 == SCM_I_ARRAY_NDIM (ra) && 0 == SCM_I_ARRAY_BASE (ra))
  250. if (s->ubnd < s->lbnd || (0 == s->lbnd && 1 == s->inc))
  251. return SCM_I_ARRAY_V (ra);
  252. return ra;
  253. }
  254. #undef FUNC_NAME
  255. SCM_DEFINE (scm_make_array, "make-array", 1, 0, 1,
  256. (SCM fill, SCM bounds),
  257. "Create and return an array.")
  258. #define FUNC_NAME s_scm_make_array
  259. {
  260. return scm_make_typed_array (SCM_BOOL_T, fill, bounds);
  261. }
  262. #undef FUNC_NAME
  263. static void
  264. scm_i_ra_set_contp (SCM ra)
  265. {
  266. size_t k = SCM_I_ARRAY_NDIM (ra);
  267. if (k)
  268. {
  269. long inc = SCM_I_ARRAY_DIMS (ra)[k - 1].inc;
  270. while (k--)
  271. {
  272. if (inc != SCM_I_ARRAY_DIMS (ra)[k].inc)
  273. {
  274. SCM_CLR_ARRAY_CONTIGUOUS_FLAG (ra);
  275. return;
  276. }
  277. inc *= (SCM_I_ARRAY_DIMS (ra)[k].ubnd
  278. - SCM_I_ARRAY_DIMS (ra)[k].lbnd + 1);
  279. }
  280. }
  281. SCM_SET_ARRAY_CONTIGUOUS_FLAG (ra);
  282. }
  283. SCM_DEFINE (scm_make_shared_array, "make-shared-array", 2, 0, 1,
  284. (SCM oldra, SCM mapfunc, SCM dims),
  285. "@code{make-shared-array} can be used to create shared subarrays of other\n"
  286. "arrays. The @var{mapper} is a function that translates coordinates in\n"
  287. "the new array into coordinates in the old array. A @var{mapper} must be\n"
  288. "linear, and its range must stay within the bounds of the old array, but\n"
  289. "it can be 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{array}, but with\n"
  395. "dimensions arranged in a different order. There must be one\n"
  396. "@var{dim} argument for each dimension of @var{array}.\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{array}. 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{array}.\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_NIMP (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{array} may be @dfn{unrolled} into a one dimensional shared array\n"
  492. "without changing their order (last subscript changing fastest), then\n"
  493. "@code{array-contents} returns that shared array, otherwise it returns\n"
  494. "@code{#f}. All arrays made by @var{make-array} and\n"
  495. "@var{make-uniform-array} may be unrolled, some arrays made by\n"
  496. "@var{make-shared-array} may not be.\n\n"
  497. "If the optional argument @var{strict} is provided, a shared array will\n"
  498. "be returned only if its elements are stored internally contiguous in\n"
  499. "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 ('(', 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 (' ', port);
  661. }
  662. scm_putc (')', port);
  663. }
  664. return 1;
  665. }
  666. /* Print an array.
  667. */
  668. static 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 ('#', 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 ('@', port);
  695. scm_intprint (h.dims[i].lbnd, 10, port);
  696. }
  697. if (print_lens)
  698. {
  699. scm_putc (':', 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 ('(', port);
  720. scm_i_print_array_dimension (&h, 0, 0, port, pstate);
  721. scm_putc (')', port);
  722. return 1;
  723. }
  724. else
  725. return scm_i_print_array_dimension (&h, 0, 0, port, pstate);
  726. }
  727. /* Read an array. This function can also read vectors and uniform
  728. vectors. Also, the conflict between '#f' and '#f32' and '#f64' is
  729. handled here.
  730. C is the first character read after the '#'.
  731. */
  732. static SCM
  733. tag_to_type (const char *tag, SCM port)
  734. {
  735. if (*tag == '\0')
  736. return SCM_BOOL_T;
  737. else
  738. return scm_from_locale_symbol (tag);
  739. }
  740. static int
  741. read_decimal_integer (SCM port, int c, ssize_t *resp)
  742. {
  743. ssize_t sign = 1;
  744. ssize_t res = 0;
  745. int got_it = 0;
  746. if (c == '-')
  747. {
  748. sign = -1;
  749. c = scm_getc (port);
  750. }
  751. while ('0' <= c && c <= '9')
  752. {
  753. res = 10*res + c-'0';
  754. got_it = 1;
  755. c = scm_getc (port);
  756. }
  757. if (got_it)
  758. *resp = sign * res;
  759. return c;
  760. }
  761. SCM
  762. scm_i_read_array (SCM port, int c)
  763. {
  764. ssize_t rank;
  765. char tag[80];
  766. int tag_len;
  767. SCM shape = SCM_BOOL_F, elements;
  768. /* XXX - shortcut for ordinary vectors. Shouldn't be necessary but
  769. the array code can not deal with zero-length dimensions yet, and
  770. we want to allow zero-length vectors, of course.
  771. */
  772. if (c == '(')
  773. {
  774. scm_ungetc (c, port);
  775. return scm_vector (scm_read (port));
  776. }
  777. /* Disambiguate between '#f' and uniform floating point vectors.
  778. */
  779. if (c == 'f')
  780. {
  781. c = scm_getc (port);
  782. if (c != '3' && c != '6')
  783. {
  784. if (c != EOF)
  785. scm_ungetc (c, port);
  786. return SCM_BOOL_F;
  787. }
  788. rank = 1;
  789. tag[0] = 'f';
  790. tag_len = 1;
  791. goto continue_reading_tag;
  792. }
  793. /* Read rank.
  794. */
  795. rank = 1;
  796. c = read_decimal_integer (port, c, &rank);
  797. if (rank < 0)
  798. scm_i_input_error (NULL, port, "array rank must be non-negative",
  799. SCM_EOL);
  800. /* Read tag.
  801. */
  802. tag_len = 0;
  803. continue_reading_tag:
  804. while (c != EOF && c != '(' && c != '@' && c != ':' && tag_len < 80)
  805. {
  806. tag[tag_len++] = c;
  807. c = scm_getc (port);
  808. }
  809. tag[tag_len] = '\0';
  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 (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 (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 (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_to_type (tag, port), 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_SMOB_TYPE_BITS (scm_i_tc16_array),
  898. SCM_SMOB_TYPE_MASK,
  899. array_handle_ref, array_handle_set,
  900. array_get_handle)
  901. void
  902. scm_init_arrays ()
  903. {
  904. scm_i_tc16_array = scm_make_smob_type ("array", 0);
  905. scm_set_smob_print (scm_i_tc16_array, scm_i_print_array);
  906. scm_set_smob_equalp (scm_i_tc16_array, scm_array_equal_p);
  907. scm_add_feature ("array");
  908. #include "libguile/arrays.x"
  909. }
  910. /*
  911. Local Variables:
  912. c-file-style: "gnu"
  913. End:
  914. */