arrays.c 28 KB

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