arrays.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364
  1. /* Copyright 1995-1998,2000-2006,2009-2015,2018
  2. Free Software Foundation, Inc.
  3. This file is part of Guile.
  4. Guile is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Guile is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  11. License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with Guile. If not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifdef HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #include <stdio.h>
  19. #include <errno.h>
  20. #include <string.h>
  21. #include <assert.h>
  22. #include "array-map.h"
  23. #include "bitvectors.h"
  24. #include "boolean.h"
  25. #include "bytevectors.h"
  26. #include "chars.h"
  27. #include "dynwind.h"
  28. #include "eq.h"
  29. #include "eval.h"
  30. #include "feature.h"
  31. #include "fports.h"
  32. #include "gsubr.h"
  33. #include "list.h"
  34. #include "modules.h"
  35. #include "numbers.h"
  36. #include "pairs.h"
  37. #include "procs.h"
  38. #include "read.h"
  39. #include "srfi-13.h"
  40. #include "srfi-4.h"
  41. #include "strings.h"
  42. #include "uniform.h"
  43. #include "vectors.h"
  44. #include "verify.h"
  45. #include "arrays.h"
  46. /* ---------------------- */
  47. /* Handling of root types */
  48. /* ---------------------- */
  49. struct scm_t_vector_ctor
  50. {
  51. SCM tag;
  52. SCM (*ctor)(SCM, SCM);
  53. };
  54. #define VECTOR_CTORS_N_STATIC_ALLOC 20
  55. static struct scm_t_vector_ctor vector_ctors[VECTOR_CTORS_N_STATIC_ALLOC];
  56. static int num_vector_ctors_registered = 0;
  57. static void
  58. scm_i_register_vector_constructor (SCM type, SCM (*ctor)(SCM, SCM))
  59. {
  60. if (num_vector_ctors_registered >= VECTOR_CTORS_N_STATIC_ALLOC)
  61. /* need to increase VECTOR_CTORS_N_STATIC_ALLOC, buster */
  62. abort ();
  63. else
  64. {
  65. vector_ctors[num_vector_ctors_registered].tag = type;
  66. vector_ctors[num_vector_ctors_registered].ctor = ctor;
  67. num_vector_ctors_registered++;
  68. }
  69. }
  70. SCM_DEFINE (scm_make_generalized_vector, "make-generalized-vector", 2, 1, 0,
  71. (SCM type, SCM len, SCM fill),
  72. "Make a generalized vector")
  73. #define FUNC_NAME s_scm_make_generalized_vector
  74. {
  75. int i;
  76. for (i = 0; i < num_vector_ctors_registered; i++)
  77. if (scm_is_eq (vector_ctors[i].tag, type))
  78. return vector_ctors[i].ctor(len, fill);
  79. scm_wrong_type_arg_msg (FUNC_NAME, SCM_ARG1, type, "array type");
  80. }
  81. #undef FUNC_NAME
  82. /* ------------------- */
  83. /* Basic array library */
  84. /* ------------------- */
  85. SCM_INTERNAL SCM scm_i_array_ref (SCM v,
  86. SCM idx0, SCM idx1, SCM idxN);
  87. SCM_INTERNAL SCM scm_i_array_set_x (SCM v, SCM obj,
  88. SCM idx0, SCM idx1, SCM idxN);
  89. int
  90. scm_is_array (SCM obj)
  91. {
  92. if (!SCM_HEAP_OBJECT_P (obj))
  93. return 0;
  94. switch (SCM_TYP7 (obj))
  95. {
  96. case scm_tc7_string:
  97. case scm_tc7_vector:
  98. case scm_tc7_bitvector:
  99. case scm_tc7_bytevector:
  100. case scm_tc7_array:
  101. return 1;
  102. default:
  103. return 0;
  104. }
  105. }
  106. SCM_DEFINE (scm_array_p, "array?", 1, 0, 0,
  107. (SCM obj),
  108. "Return @code{#t} if the @var{obj} is an array, and @code{#f} if\n"
  109. "not.")
  110. #define FUNC_NAME s_scm_array_p
  111. {
  112. return scm_from_bool (scm_is_array (obj));
  113. }
  114. #undef FUNC_NAME
  115. int
  116. scm_is_typed_array (SCM obj, SCM type)
  117. {
  118. int ret = 0;
  119. if (scm_is_array (obj))
  120. {
  121. scm_t_array_handle h;
  122. scm_array_get_handle (obj, &h);
  123. ret = scm_is_eq (scm_array_handle_element_type (&h), type);
  124. scm_array_handle_release (&h);
  125. }
  126. return ret;
  127. }
  128. SCM_DEFINE (scm_typed_array_p, "typed-array?", 2, 0, 0,
  129. (SCM obj, SCM type),
  130. "Return @code{#t} if the @var{obj} is an array of type\n"
  131. "@var{type}, and @code{#f} if not.")
  132. #define FUNC_NAME s_scm_typed_array_p
  133. {
  134. return scm_from_bool (scm_is_typed_array (obj, type));
  135. }
  136. #undef FUNC_NAME
  137. size_t
  138. scm_c_array_length (SCM array)
  139. {
  140. scm_t_array_handle handle;
  141. size_t res;
  142. scm_array_get_handle (array, &handle);
  143. if (scm_array_handle_rank (&handle) < 1)
  144. {
  145. scm_array_handle_release (&handle);
  146. scm_wrong_type_arg_msg (NULL, 0, array, "array of nonzero rank");
  147. }
  148. res = handle.dims[0].ubnd - handle.dims[0].lbnd + 1;
  149. scm_array_handle_release (&handle);
  150. return res;
  151. }
  152. SCM_DEFINE (scm_array_length, "array-length", 1, 0, 0,
  153. (SCM array),
  154. "Return the length of an array: its first dimension.\n"
  155. "It is an error to ask for the length of an array of rank 0.")
  156. #define FUNC_NAME s_scm_array_length
  157. {
  158. return scm_from_size_t (scm_c_array_length (array));
  159. }
  160. #undef FUNC_NAME
  161. SCM_DEFINE (scm_array_dimensions, "array-dimensions", 1, 0, 0,
  162. (SCM ra),
  163. "@code{array-dimensions} is similar to @code{array-shape} but replaces\n"
  164. "elements with a @code{0} minimum with one greater than the maximum. So:\n"
  165. "@lisp\n"
  166. "(array-dimensions (make-array 'foo '(-1 3) 5)) @result{} ((-1 3) 5)\n"
  167. "@end lisp")
  168. #define FUNC_NAME s_scm_array_dimensions
  169. {
  170. scm_t_array_handle handle;
  171. scm_t_array_dim *s;
  172. SCM res = SCM_EOL;
  173. size_t k;
  174. scm_array_get_handle (ra, &handle);
  175. s = scm_array_handle_dims (&handle);
  176. k = scm_array_handle_rank (&handle);
  177. while (k--)
  178. res = scm_cons (s[k].lbnd
  179. ? scm_cons2 (scm_from_ssize_t (s[k].lbnd),
  180. scm_from_ssize_t (s[k].ubnd),
  181. SCM_EOL)
  182. : scm_from_ssize_t (1 + s[k].ubnd),
  183. res);
  184. scm_array_handle_release (&handle);
  185. return res;
  186. }
  187. #undef FUNC_NAME
  188. SCM_DEFINE (scm_array_type, "array-type", 1, 0, 0,
  189. (SCM ra),
  190. "")
  191. #define FUNC_NAME s_scm_array_type
  192. {
  193. scm_t_array_handle h;
  194. SCM type;
  195. scm_array_get_handle (ra, &h);
  196. type = scm_array_handle_element_type (&h);
  197. scm_array_handle_release (&h);
  198. return type;
  199. }
  200. #undef FUNC_NAME
  201. SCM_DEFINE (scm_array_type_code,
  202. "array-type-code", 1, 0, 0,
  203. (SCM array),
  204. "Return the type of the elements in @var{array},\n"
  205. "as an integer code.")
  206. #define FUNC_NAME s_scm_array_type_code
  207. {
  208. scm_t_array_handle h;
  209. scm_t_array_element_type element_type;
  210. scm_array_get_handle (array, &h);
  211. element_type = h.element_type;
  212. scm_array_handle_release (&h);
  213. return scm_from_uint16 (element_type);
  214. }
  215. #undef FUNC_NAME
  216. SCM_DEFINE (scm_array_in_bounds_p, "array-in-bounds?", 1, 0, 1,
  217. (SCM ra, SCM args),
  218. "Return @code{#t} if its arguments would be acceptable to\n"
  219. "@code{array-ref}.")
  220. #define FUNC_NAME s_scm_array_in_bounds_p
  221. {
  222. SCM res = SCM_BOOL_T;
  223. size_t k, ndim;
  224. scm_t_array_dim *s;
  225. scm_t_array_handle handle;
  226. SCM_VALIDATE_REST_ARGUMENT (args);
  227. scm_array_get_handle (ra, &handle);
  228. s = scm_array_handle_dims (&handle);
  229. ndim = scm_array_handle_rank (&handle);
  230. for (k = 0; k < ndim; k++)
  231. {
  232. long ind;
  233. if (!scm_is_pair (args))
  234. SCM_WRONG_NUM_ARGS ();
  235. ind = scm_to_long (SCM_CAR (args));
  236. args = SCM_CDR (args);
  237. if (ind < s[k].lbnd || ind > s[k].ubnd)
  238. {
  239. res = SCM_BOOL_F;
  240. /* We do not stop the checking after finding a violation
  241. since we want to validate the type-correctness and
  242. number of arguments in any case.
  243. */
  244. }
  245. }
  246. scm_array_handle_release (&handle);
  247. return res;
  248. }
  249. #undef FUNC_NAME
  250. SCM
  251. scm_c_array_ref_1 (SCM array, ssize_t idx0)
  252. {
  253. scm_t_array_handle handle;
  254. SCM res;
  255. scm_array_get_handle (array, &handle);
  256. res = scm_array_handle_ref (&handle, scm_array_handle_pos_1 (&handle, idx0));
  257. scm_array_handle_release (&handle);
  258. return res;
  259. }
  260. SCM
  261. scm_c_array_ref_2 (SCM array, ssize_t idx0, ssize_t idx1)
  262. {
  263. scm_t_array_handle handle;
  264. SCM res;
  265. scm_array_get_handle (array, &handle);
  266. res = scm_array_handle_ref (&handle, scm_array_handle_pos_2 (&handle, idx0, idx1));
  267. scm_array_handle_release (&handle);
  268. return res;
  269. }
  270. SCM
  271. scm_array_ref (SCM v, SCM args)
  272. {
  273. scm_t_array_handle handle;
  274. SCM res;
  275. scm_array_get_handle (v, &handle);
  276. res = scm_array_handle_ref (&handle, scm_array_handle_pos (&handle, args));
  277. scm_array_handle_release (&handle);
  278. return res;
  279. }
  280. void
  281. scm_c_array_set_1_x (SCM array, SCM obj, ssize_t idx0)
  282. {
  283. scm_t_array_handle handle;
  284. scm_array_get_handle (array, &handle);
  285. scm_array_handle_set (&handle, scm_array_handle_pos_1 (&handle, idx0),
  286. obj);
  287. scm_array_handle_release (&handle);
  288. }
  289. void
  290. scm_c_array_set_2_x (SCM array, SCM obj, ssize_t idx0, ssize_t idx1)
  291. {
  292. scm_t_array_handle handle;
  293. scm_array_get_handle (array, &handle);
  294. scm_array_handle_set (&handle, scm_array_handle_pos_2 (&handle, idx0, idx1),
  295. obj);
  296. scm_array_handle_release (&handle);
  297. }
  298. SCM
  299. scm_array_set_x (SCM v, SCM obj, SCM args)
  300. {
  301. scm_t_array_handle handle;
  302. scm_array_get_handle (v, &handle);
  303. scm_array_handle_set (&handle, scm_array_handle_pos (&handle, args), obj);
  304. scm_array_handle_release (&handle);
  305. return SCM_UNSPECIFIED;
  306. }
  307. SCM_DEFINE (scm_i_array_ref, "array-ref", 1, 2, 1,
  308. (SCM v, SCM idx0, SCM idx1, SCM idxN),
  309. "Return the element at the @code{(idx0, idx1, idxN...)}\n"
  310. "position in array @var{v}.")
  311. #define FUNC_NAME s_scm_i_array_ref
  312. {
  313. if (SCM_UNBNDP (idx0))
  314. return scm_array_ref (v, SCM_EOL);
  315. else if (SCM_UNBNDP (idx1))
  316. return scm_c_array_ref_1 (v, scm_to_ssize_t (idx0));
  317. else if (scm_is_null (idxN))
  318. return scm_c_array_ref_2 (v, scm_to_ssize_t (idx0), scm_to_ssize_t (idx1));
  319. else
  320. return scm_array_ref (v, scm_cons (idx0, scm_cons (idx1, idxN)));
  321. }
  322. #undef FUNC_NAME
  323. SCM_DEFINE (scm_i_array_set_x, "array-set!", 2, 2, 1,
  324. (SCM v, SCM obj, SCM idx0, SCM idx1, SCM idxN),
  325. "Set the element at the @code{(idx0, idx1, idxN...)} position\n"
  326. "in the array @var{v} to @var{obj}. The value returned by\n"
  327. "@code{array-set!} is unspecified.")
  328. #define FUNC_NAME s_scm_i_array_set_x
  329. {
  330. if (SCM_UNBNDP (idx0))
  331. scm_array_set_x (v, obj, SCM_EOL);
  332. else if (SCM_UNBNDP (idx1))
  333. scm_c_array_set_1_x (v, obj, scm_to_ssize_t (idx0));
  334. else if (scm_is_null (idxN))
  335. scm_c_array_set_2_x (v, obj, scm_to_ssize_t (idx0), scm_to_ssize_t (idx1));
  336. else
  337. scm_array_set_x (v, obj, scm_cons (idx0, scm_cons (idx1, idxN)));
  338. return SCM_UNSPECIFIED;
  339. }
  340. #undef FUNC_NAME
  341. static SCM
  342. array_to_list (scm_t_array_handle *h, size_t dim, unsigned long pos)
  343. {
  344. if (dim == scm_array_handle_rank (h))
  345. return scm_array_handle_ref (h, pos);
  346. else
  347. {
  348. SCM res = SCM_EOL;
  349. long inc;
  350. size_t i;
  351. i = h->dims[dim].ubnd - h->dims[dim].lbnd + 1;
  352. inc = h->dims[dim].inc;
  353. pos += (i - 1) * inc;
  354. for (; i > 0; i--, pos -= inc)
  355. res = scm_cons (array_to_list (h, dim + 1, pos), res);
  356. return res;
  357. }
  358. }
  359. SCM_DEFINE (scm_array_to_list, "array->list", 1, 0, 0,
  360. (SCM array),
  361. "Return a list representation of @var{array}.\n\n"
  362. "It is easiest to specify the behavior of this function by\n"
  363. "example:\n"
  364. "@example\n"
  365. "(array->list #0(a)) @result{} 1\n"
  366. "(array->list #1(a b)) @result{} (a b)\n"
  367. "(array->list #2((aa ab) (ba bb)) @result{} ((aa ab) (ba bb))\n"
  368. "@end example\n")
  369. #define FUNC_NAME s_scm_array_to_list
  370. {
  371. scm_t_array_handle h;
  372. SCM res;
  373. scm_array_get_handle (array, &h);
  374. res = array_to_list (&h, 0, 0);
  375. scm_array_handle_release (&h);
  376. return res;
  377. }
  378. #undef FUNC_NAME
  379. size_t
  380. scm_c_array_rank (SCM array)
  381. {
  382. if (SCM_I_ARRAYP (array))
  383. return SCM_I_ARRAY_NDIM (array);
  384. else if (scm_is_array (array))
  385. return 1;
  386. else
  387. scm_wrong_type_arg_msg ("array-rank", SCM_ARG1, array, "array");
  388. }
  389. SCM_DEFINE (scm_array_rank, "array-rank", 1, 0, 0,
  390. (SCM array),
  391. "Return the number of dimensions of the array @var{array.}\n")
  392. #define FUNC_NAME s_scm_array_rank
  393. {
  394. return scm_from_size_t (scm_c_array_rank (array));
  395. }
  396. #undef FUNC_NAME
  397. SCM_DEFINE (scm_shared_array_root, "shared-array-root", 1, 0, 0,
  398. (SCM ra),
  399. "Return the root vector of a shared array.")
  400. #define FUNC_NAME s_scm_shared_array_root
  401. {
  402. if (SCM_I_ARRAYP (ra))
  403. return SCM_I_ARRAY_V (ra);
  404. else if (scm_is_array (ra))
  405. return ra;
  406. else
  407. scm_wrong_type_arg_msg (FUNC_NAME, SCM_ARG1, ra, "array");
  408. }
  409. #undef FUNC_NAME
  410. SCM_DEFINE (scm_shared_array_offset, "shared-array-offset", 1, 0, 0,
  411. (SCM ra),
  412. "Return the root vector index of the first element in the array.")
  413. #define FUNC_NAME s_scm_shared_array_offset
  414. {
  415. if (SCM_I_ARRAYP (ra))
  416. return scm_from_size_t (SCM_I_ARRAY_BASE (ra));
  417. else if (scm_is_array (ra))
  418. return scm_from_size_t (0);
  419. else
  420. scm_wrong_type_arg_msg (FUNC_NAME, SCM_ARG1, ra, "array");
  421. }
  422. #undef FUNC_NAME
  423. SCM_DEFINE (scm_shared_array_increments, "shared-array-increments", 1, 0, 0,
  424. (SCM ra),
  425. "For each dimension, return the distance between elements in the root vector.")
  426. #define FUNC_NAME s_scm_shared_array_increments
  427. {
  428. if (SCM_I_ARRAYP (ra))
  429. {
  430. size_t k = SCM_I_ARRAY_NDIM (ra);
  431. SCM res = SCM_EOL;
  432. scm_t_array_dim *dims = SCM_I_ARRAY_DIMS (ra);
  433. while (k--)
  434. res = scm_cons (scm_from_ssize_t (dims[k].inc), res);
  435. return res;
  436. }
  437. else if (scm_is_array (ra))
  438. return scm_list_1 (scm_from_ssize_t (1));
  439. else
  440. scm_wrong_type_arg_msg (FUNC_NAME, SCM_ARG1, ra, "array");
  441. }
  442. #undef FUNC_NAME
  443. /* FIXME: to avoid this assumption, fix the accessors in arrays.h,
  444. scm_i_make_array, and the array cases in system/vm/assembler.scm. */
  445. verify (sizeof (scm_t_array_dim) == 3*sizeof (scm_t_bits));
  446. /* Matching SCM_I_ARRAY accessors in arrays.h */
  447. SCM
  448. scm_i_make_array (int ndim)
  449. {
  450. SCM ra = scm_words (((scm_t_bits) ndim << 17) + scm_tc7_array, 3 + ndim*3);
  451. SCM_I_ARRAY_SET_V (ra, SCM_BOOL_F);
  452. SCM_I_ARRAY_SET_BASE (ra, 0);
  453. /* dimensions are unset */
  454. return ra;
  455. }
  456. static char s_bad_spec[] = "Bad scm_array dimension";
  457. /* Increments will still need to be set. */
  458. SCM
  459. scm_i_shap2ra (SCM args)
  460. {
  461. scm_t_array_dim *s;
  462. SCM ra, spec;
  463. int ndim = scm_ilength (args);
  464. if (ndim < 0)
  465. scm_misc_error (NULL, s_bad_spec, SCM_EOL);
  466. ra = scm_i_make_array (ndim);
  467. SCM_I_ARRAY_SET_BASE (ra, 0);
  468. s = SCM_I_ARRAY_DIMS (ra);
  469. for (; !scm_is_null (args); s++, args = SCM_CDR (args))
  470. {
  471. spec = SCM_CAR (args);
  472. if (scm_is_integer (spec))
  473. {
  474. s->lbnd = 0;
  475. s->ubnd = scm_to_ssize_t (spec);
  476. if (s->ubnd < 0)
  477. scm_misc_error (NULL, s_bad_spec, SCM_EOL);
  478. --s->ubnd;
  479. }
  480. else
  481. {
  482. if (!scm_is_pair (spec) || !scm_is_integer (SCM_CAR (spec)))
  483. scm_misc_error (NULL, s_bad_spec, SCM_EOL);
  484. s->lbnd = scm_to_ssize_t (SCM_CAR (spec));
  485. spec = SCM_CDR (spec);
  486. if (!scm_is_pair (spec)
  487. || !scm_is_integer (SCM_CAR (spec))
  488. || !scm_is_null (SCM_CDR (spec)))
  489. scm_misc_error (NULL, s_bad_spec, SCM_EOL);
  490. s->ubnd = scm_to_ssize_t (SCM_CAR (spec));
  491. if (s->ubnd - s->lbnd < -1)
  492. scm_misc_error (NULL, s_bad_spec, SCM_EOL);
  493. }
  494. s->inc = 1;
  495. }
  496. return ra;
  497. }
  498. SCM_DEFINE (scm_make_typed_array, "make-typed-array", 2, 0, 1,
  499. (SCM type, SCM fill, SCM bounds),
  500. "Create and return an array of type @var{type}.")
  501. #define FUNC_NAME s_scm_make_typed_array
  502. {
  503. size_t k, rlen = 1;
  504. scm_t_array_dim *s;
  505. SCM ra;
  506. ra = scm_i_shap2ra (bounds);
  507. s = SCM_I_ARRAY_DIMS (ra);
  508. k = SCM_I_ARRAY_NDIM (ra);
  509. while (k--)
  510. {
  511. s[k].inc = rlen;
  512. SCM_ASSERT_RANGE (1, bounds, s[k].lbnd <= s[k].ubnd + 1);
  513. rlen = (s[k].ubnd - s[k].lbnd + 1) * s[k].inc;
  514. }
  515. if (scm_is_eq (fill, SCM_UNSPECIFIED))
  516. fill = SCM_UNDEFINED;
  517. SCM_I_ARRAY_SET_V (ra, scm_make_generalized_vector (type, scm_from_size_t (rlen), fill));
  518. if (1 == SCM_I_ARRAY_NDIM (ra) && 0 == SCM_I_ARRAY_BASE (ra))
  519. if (0 == s->lbnd)
  520. return SCM_I_ARRAY_V (ra);
  521. return ra;
  522. }
  523. #undef FUNC_NAME
  524. SCM_DEFINE (scm_make_array, "make-array", 1, 0, 1,
  525. (SCM fill, SCM bounds),
  526. "Create and return an array.")
  527. #define FUNC_NAME s_scm_make_array
  528. {
  529. return scm_make_typed_array (SCM_BOOL_T, fill, bounds);
  530. }
  531. #undef FUNC_NAME
  532. SCM_DEFINE (scm_make_shared_array, "make-shared-array", 2, 0, 1,
  533. (SCM oldra, SCM mapfunc, SCM dims),
  534. "@code{make-shared-array} can be used to create shared subarrays\n"
  535. "of other arrays. The @var{mapfunc} is a function that\n"
  536. "translates coordinates in the new array into coordinates in the\n"
  537. "old array. A @var{mapfunc} must be linear, and its range must\n"
  538. "stay within the bounds of the old array, but it can be\n"
  539. "otherwise arbitrary. A simple example:\n"
  540. "@lisp\n"
  541. "(define fred (make-array #f 8 8))\n"
  542. "(define freds-diagonal\n"
  543. " (make-shared-array fred (lambda (i) (list i i)) 8))\n"
  544. "(array-set! freds-diagonal 'foo 3)\n"
  545. "(array-ref fred 3 3) @result{} foo\n"
  546. "(define freds-center\n"
  547. " (make-shared-array fred (lambda (i j) (list (+ 3 i) (+ 3 j))) 2 2))\n"
  548. "(array-ref freds-center 0 0) @result{} foo\n"
  549. "@end lisp")
  550. #define FUNC_NAME s_scm_make_shared_array
  551. {
  552. scm_t_array_handle old_handle;
  553. SCM ra;
  554. SCM inds, indptr;
  555. SCM imap;
  556. size_t k;
  557. ssize_t i;
  558. long old_base, old_min, new_min, old_max, new_max;
  559. scm_t_array_dim *s;
  560. SCM_VALIDATE_REST_ARGUMENT (dims);
  561. SCM_VALIDATE_PROC (2, mapfunc);
  562. ra = scm_i_shap2ra (dims);
  563. scm_array_get_handle (oldra, &old_handle);
  564. if (SCM_I_ARRAYP (oldra))
  565. {
  566. SCM_I_ARRAY_SET_V (ra, SCM_I_ARRAY_V (oldra));
  567. old_base = old_min = old_max = SCM_I_ARRAY_BASE (oldra);
  568. s = scm_array_handle_dims (&old_handle);
  569. k = scm_array_handle_rank (&old_handle);
  570. while (k--)
  571. {
  572. if (s[k].inc > 0)
  573. old_max += (s[k].ubnd - s[k].lbnd) * s[k].inc;
  574. else
  575. old_min += (s[k].ubnd - s[k].lbnd) * s[k].inc;
  576. }
  577. }
  578. else
  579. {
  580. SCM_I_ARRAY_SET_V (ra, oldra);
  581. old_base = old_min = 0;
  582. old_max = scm_c_array_length (oldra) - 1;
  583. }
  584. inds = SCM_EOL;
  585. s = SCM_I_ARRAY_DIMS (ra);
  586. for (k = 0; k < SCM_I_ARRAY_NDIM (ra); k++)
  587. {
  588. inds = scm_cons (scm_from_ssize_t (s[k].lbnd), inds);
  589. if (s[k].ubnd < s[k].lbnd)
  590. {
  591. if (1 == SCM_I_ARRAY_NDIM (ra))
  592. ra = scm_make_generalized_vector (scm_array_type (ra),
  593. SCM_INUM0, SCM_UNDEFINED);
  594. else
  595. SCM_I_ARRAY_SET_V (ra, scm_make_generalized_vector (scm_array_type (ra),
  596. SCM_INUM0, SCM_UNDEFINED));
  597. scm_array_handle_release (&old_handle);
  598. return ra;
  599. }
  600. }
  601. imap = scm_apply_0 (mapfunc, scm_reverse (inds));
  602. i = scm_array_handle_pos (&old_handle, imap);
  603. new_min = new_max = i + old_base;
  604. SCM_I_ARRAY_SET_BASE (ra, new_min);
  605. indptr = inds;
  606. k = SCM_I_ARRAY_NDIM (ra);
  607. while (k--)
  608. {
  609. if (s[k].ubnd > s[k].lbnd)
  610. {
  611. SCM_SETCAR (indptr, scm_sum (SCM_CAR (indptr), scm_from_int (1)));
  612. imap = scm_apply_0 (mapfunc, scm_reverse (inds));
  613. s[k].inc = scm_array_handle_pos (&old_handle, imap) - i;
  614. i += s[k].inc;
  615. if (s[k].inc > 0)
  616. new_max += (s[k].ubnd - s[k].lbnd) * s[k].inc;
  617. else
  618. new_min += (s[k].ubnd - s[k].lbnd) * s[k].inc;
  619. }
  620. else
  621. s[k].inc = new_max - new_min + 1; /* contiguous by default */
  622. indptr = SCM_CDR (indptr);
  623. }
  624. scm_array_handle_release (&old_handle);
  625. if (old_min > new_min || old_max < new_max)
  626. SCM_MISC_ERROR ("mapping out of range", SCM_EOL);
  627. if (1 == SCM_I_ARRAY_NDIM (ra) && 0 == SCM_I_ARRAY_BASE (ra))
  628. {
  629. SCM v = SCM_I_ARRAY_V (ra);
  630. size_t length = scm_c_array_length (v);
  631. if (1 == s->inc && 0 == s->lbnd && length == 1 + s->ubnd)
  632. return v;
  633. if (s->ubnd < s->lbnd)
  634. return scm_make_generalized_vector (scm_array_type (ra), SCM_INUM0,
  635. SCM_UNDEFINED);
  636. }
  637. return ra;
  638. }
  639. #undef FUNC_NAME
  640. static void
  641. array_from_pos (scm_t_array_handle *handle, size_t *ndim, size_t *k, SCM *i, ssize_t *pos,
  642. scm_t_array_dim **s, char const * FUNC_NAME, SCM error_args)
  643. {
  644. *s = scm_array_handle_dims (handle);
  645. *k = *ndim = scm_array_handle_rank (handle);
  646. for (; *k>0 && scm_is_pair (*i); --*k, ++*s, *i=scm_cdr (*i))
  647. {
  648. ssize_t ik = scm_to_ssize_t (scm_car (*i));
  649. if (ik<(*s)->lbnd || ik>(*s)->ubnd)
  650. {
  651. scm_array_handle_release (handle);
  652. scm_misc_error (FUNC_NAME, "indices out of range", error_args);
  653. }
  654. *pos += (ik-(*s)->lbnd) * (*s)->inc;
  655. }
  656. }
  657. static void
  658. array_from_get_o (scm_t_array_handle *handle, size_t k, scm_t_array_dim *s, ssize_t pos,
  659. SCM *o)
  660. {
  661. scm_t_array_dim * os;
  662. *o = scm_i_make_array (k);
  663. SCM_I_ARRAY_SET_V (*o, handle->vector);
  664. SCM_I_ARRAY_SET_BASE (*o, pos + handle->base);
  665. os = SCM_I_ARRAY_DIMS (*o);
  666. for (; k>0; --k, ++s, ++os)
  667. {
  668. os->ubnd = s->ubnd;
  669. os->lbnd = s->lbnd;
  670. os->inc = s->inc;
  671. }
  672. }
  673. SCM_DEFINE (scm_array_slice, "array-slice", 1, 0, 1,
  674. (SCM ra, SCM indices),
  675. "Return the array slice @var{ra}[@var{indices} ..., ...]\n"
  676. "The rank of @var{ra} must equal to the number of indices or larger.\n\n"
  677. "See also @code{array-ref}, @code{array-cell-ref}, @code{array-cell-set!}.\n\n"
  678. "@code{array-slice} may return a rank-0 array. For example:\n"
  679. "@lisp\n"
  680. "(array-slice #2((1 2 3) (4 5 6)) 1 1) @result{} #0(5)\n"
  681. "(array-slice #2((1 2 3) (4 5 6)) 1) @result{} #(4 5 6)\n"
  682. "(array-slice #2((1 2 3) (4 5 6))) @result{} #2((1 2 3) (4 5 6))\n"
  683. "(array-slice #0(5) @result{} #0(5).\n"
  684. "@end lisp")
  685. #define FUNC_NAME s_scm_array_slice
  686. {
  687. SCM o, i = indices;
  688. size_t ndim, k;
  689. ssize_t pos = 0;
  690. scm_t_array_handle handle;
  691. scm_t_array_dim *s;
  692. scm_array_get_handle (ra, &handle);
  693. array_from_pos (&handle, &ndim, &k, &i, &pos, &s, FUNC_NAME, scm_list_2 (ra, indices));
  694. if (k==ndim)
  695. o = ra;
  696. else if (scm_is_null (i))
  697. {
  698. array_from_get_o(&handle, k, s, pos, &o);
  699. }
  700. else
  701. {
  702. scm_array_handle_release (&handle);
  703. scm_misc_error(FUNC_NAME, "too many indices", scm_list_2 (ra, indices));
  704. }
  705. scm_array_handle_release (&handle);
  706. return o;
  707. }
  708. #undef FUNC_NAME
  709. SCM_DEFINE (scm_array_cell_ref, "array-cell-ref", 1, 0, 1,
  710. (SCM ra, SCM indices),
  711. "Return the element at the @code{(@var{indices} ...)} position\n"
  712. "in array @var{ra}, or the array slice @var{ra}[@var{indices} ..., ...]\n"
  713. "if the rank of @var{ra} is larger than the number of indices.\n\n"
  714. "See also @code{array-ref}, @code{array-slice}, @code{array-cell-set!}.\n\n"
  715. "@code{array-cell-ref} never returns a rank 0 array. For example:\n"
  716. "@lisp\n"
  717. "(array-cell-ref #2((1 2 3) (4 5 6)) 1 1) @result{} 5\n"
  718. "(array-cell-ref #2((1 2 3) (4 5 6)) 1) @result{} #(4 5 6)\n"
  719. "(array-cell-ref #2((1 2 3) (4 5 6))) @result{} #2((1 2 3) (4 5 6))\n"
  720. "(array-cell-ref #0(5) @result{} 5.\n"
  721. "@end lisp")
  722. #define FUNC_NAME s_scm_array_cell_ref
  723. {
  724. SCM o, i = indices;
  725. size_t ndim, k;
  726. ssize_t pos = 0;
  727. scm_t_array_handle handle;
  728. scm_t_array_dim *s;
  729. scm_array_get_handle (ra, &handle);
  730. array_from_pos (&handle, &ndim, &k, &i, &pos, &s, FUNC_NAME, scm_list_2 (ra, indices));
  731. if (k>0)
  732. {
  733. if (k==ndim)
  734. o = ra;
  735. else
  736. array_from_get_o(&handle, k, s, pos, &o);
  737. }
  738. else if (scm_is_null(i))
  739. o = scm_array_handle_ref (&handle, pos);
  740. else
  741. {
  742. scm_array_handle_release (&handle);
  743. scm_misc_error(FUNC_NAME, "too many indices", scm_list_2 (ra, indices));
  744. }
  745. scm_array_handle_release (&handle);
  746. return o;
  747. }
  748. #undef FUNC_NAME
  749. SCM_DEFINE (scm_array_cell_set_x, "array-cell-set!", 2, 0, 1,
  750. (SCM ra, SCM b, SCM indices),
  751. "Set the array slice @var{ra}[@var{indices} ..., ...] to @var{b}\n."
  752. "Equivalent to @code{(array-copy! @var{b} (apply array-cell-ref @var{ra} @var{indices}))}\n"
  753. "if the number of indices is smaller than the rank of @var{ra}; otherwise\n"
  754. "equivalent to @code{(apply array-set! @var{ra} @var{b} @var{indices})}.\n"
  755. "This function returns the modified array @var{ra}.\n\n"
  756. "See also @code{array-ref}, @code{array-cell-ref}, @code{array-slice}.\n\n"
  757. "For example:\n"
  758. "@lisp\n"
  759. "(define A (list->array 2 '((1 2 3) (4 5 6))))\n"
  760. "(array-cell-set! A #0(99) 1 1) @result{} #2((1 2 3) (4 #0(99) 6))\n"
  761. "(array-cell-set! A 99 1 1) @result{} #2((1 2 3) (4 99 6))\n"
  762. "(array-cell-set! A #(a b c) 0) @result{} #2((a b c) (4 99 6))\n"
  763. "(array-cell-set! A #2((x y z) (9 8 7))) @result{} #2((x y z) (9 8 7))\n\n"
  764. "(define B (make-array 0))\n"
  765. "(array-cell-set! B 15) @result{} #0(15)\n"
  766. "@end lisp")
  767. #define FUNC_NAME s_scm_array_cell_set_x
  768. {
  769. SCM o, i = indices;
  770. size_t ndim, k;
  771. ssize_t pos = 0;
  772. scm_t_array_handle handle;
  773. scm_t_array_dim *s;
  774. scm_array_get_handle (ra, &handle);
  775. array_from_pos (&handle, &ndim, &k, &i, &pos, &s, FUNC_NAME, scm_list_3 (ra, b, indices));
  776. if (k>0)
  777. {
  778. if (k==ndim)
  779. o = ra;
  780. else
  781. array_from_get_o(&handle, k, s, pos, &o);
  782. scm_array_handle_release(&handle);
  783. /* an error is still possible here if o and b don't match. */
  784. /* FIXME copying like this wastes the handle, and the bounds matching
  785. behavior of array-copy! is not strict. */
  786. scm_array_copy_x(b, o);
  787. }
  788. else if (scm_is_null(i))
  789. {
  790. scm_array_handle_set (&handle, pos, b); /* ra may be non-ARRAYP */
  791. scm_array_handle_release (&handle);
  792. }
  793. else
  794. {
  795. scm_array_handle_release (&handle);
  796. scm_misc_error(FUNC_NAME, "too many indices", scm_list_3 (ra, b, indices));
  797. }
  798. return ra;
  799. }
  800. #undef FUNC_NAME
  801. #undef ARRAY_FROM_GET_O
  802. /* args are RA . DIMS */
  803. SCM_DEFINE (scm_transpose_array, "transpose-array", 1, 0, 1,
  804. (SCM ra, SCM args),
  805. "Return an array sharing contents with @var{ra}, but with\n"
  806. "dimensions arranged in a different order. There must be one\n"
  807. "@var{dim} argument for each dimension of @var{ra}.\n"
  808. "@var{dim0}, @var{dim1}, @dots{} should be integers between 0\n"
  809. "and the rank of the array to be returned. Each integer in that\n"
  810. "range must appear at least once in the argument list.\n"
  811. "\n"
  812. "The values of @var{dim0}, @var{dim1}, @dots{} correspond to\n"
  813. "dimensions in the array to be returned, their positions in the\n"
  814. "argument list to dimensions of @var{ra}. Several @var{dim}s\n"
  815. "may have the same value, in which case the returned array will\n"
  816. "have smaller rank than @var{ra}.\n"
  817. "\n"
  818. "@lisp\n"
  819. "(transpose-array '#2((a b) (c d)) 1 0) @result{} #2((a c) (b d))\n"
  820. "(transpose-array '#2((a b) (c d)) 0 0) @result{} #1(a d)\n"
  821. "(transpose-array '#3(((a b c) (d e f)) ((1 2 3) (4 5 6))) 1 1 0) @result{}\n"
  822. " #2((a 4) (b 5) (c 6))\n"
  823. "@end lisp")
  824. #define FUNC_NAME s_scm_transpose_array
  825. {
  826. SCM res, vargs;
  827. scm_t_array_dim *s, *r;
  828. int ndim, i, k;
  829. SCM_VALIDATE_REST_ARGUMENT (args);
  830. SCM_ASSERT (SCM_HEAP_OBJECT_P (ra), ra, SCM_ARG1, FUNC_NAME);
  831. switch (scm_c_array_rank (ra))
  832. {
  833. case 0:
  834. if (!scm_is_null (args))
  835. SCM_WRONG_NUM_ARGS ();
  836. return ra;
  837. case 1:
  838. /* Make sure that we are called with a single zero as
  839. arguments.
  840. */
  841. if (scm_is_null (args) || !scm_is_null (SCM_CDR (args)))
  842. SCM_WRONG_NUM_ARGS ();
  843. SCM_VALIDATE_INT_COPY (SCM_ARG2, SCM_CAR (args), i);
  844. SCM_ASSERT_RANGE (SCM_ARG2, SCM_CAR (args), i == 0);
  845. return ra;
  846. default:
  847. vargs = scm_vector (args);
  848. if (SCM_VECTOR_LENGTH (vargs) != SCM_I_ARRAY_NDIM (ra))
  849. SCM_WRONG_NUM_ARGS ();
  850. ndim = 0;
  851. for (k = 0; k < SCM_I_ARRAY_NDIM (ra); k++)
  852. {
  853. i = scm_to_signed_integer (SCM_VECTOR_REF (vargs, k),
  854. 0, SCM_I_ARRAY_NDIM(ra));
  855. if (ndim < i)
  856. ndim = i;
  857. }
  858. ndim++;
  859. res = scm_i_make_array (ndim);
  860. SCM_I_ARRAY_SET_V (res, SCM_I_ARRAY_V (ra));
  861. SCM_I_ARRAY_SET_BASE (res, SCM_I_ARRAY_BASE (ra));
  862. for (k = ndim; k--;)
  863. {
  864. SCM_I_ARRAY_DIMS (res)[k].lbnd = 0;
  865. SCM_I_ARRAY_DIMS (res)[k].ubnd = -1;
  866. }
  867. for (k = SCM_I_ARRAY_NDIM (ra); k--;)
  868. {
  869. i = scm_to_int (SCM_VECTOR_REF (vargs, k));
  870. s = &(SCM_I_ARRAY_DIMS (ra)[k]);
  871. r = &(SCM_I_ARRAY_DIMS (res)[i]);
  872. if (r->ubnd < r->lbnd)
  873. {
  874. r->lbnd = s->lbnd;
  875. r->ubnd = s->ubnd;
  876. r->inc = s->inc;
  877. ndim--;
  878. }
  879. else
  880. {
  881. if (r->ubnd > s->ubnd)
  882. r->ubnd = s->ubnd;
  883. if (r->lbnd < s->lbnd)
  884. {
  885. SCM_I_ARRAY_SET_BASE (res, SCM_I_ARRAY_BASE (res) + (s->lbnd - r->lbnd) * r->inc);
  886. r->lbnd = s->lbnd;
  887. }
  888. r->inc += s->inc;
  889. }
  890. }
  891. if (ndim > 0)
  892. SCM_MISC_ERROR ("bad argument list", SCM_EOL);
  893. return res;
  894. }
  895. }
  896. #undef FUNC_NAME
  897. /* attempts to unroll an array into a one-dimensional array.
  898. returns the unrolled array or #f if it can't be done. */
  899. /* if strict is true, return #f if returned array
  900. wouldn't have contiguous elements. */
  901. SCM_DEFINE (scm_array_contents, "array-contents", 1, 1, 0,
  902. (SCM ra, SCM strict),
  903. "If @var{ra} may be @dfn{unrolled} into a one dimensional shared\n"
  904. "array without changing their order (last subscript changing\n"
  905. "fastest), then @code{array-contents} returns that shared array,\n"
  906. "otherwise it returns @code{#f}. All arrays made by\n"
  907. "@code{make-array} and @code{make-uniform-array} may be unrolled,\n"
  908. "some arrays made by @code{make-shared-array} may not be. If\n"
  909. "the optional argument @var{strict} is provided, a shared array\n"
  910. "will be returned only if its elements are stored contiguously\n"
  911. "in memory.")
  912. #define FUNC_NAME s_scm_array_contents
  913. {
  914. if (SCM_I_ARRAYP (ra))
  915. {
  916. SCM v;
  917. size_t ndim = SCM_I_ARRAY_NDIM (ra);
  918. scm_t_array_dim *s = SCM_I_ARRAY_DIMS (ra);
  919. size_t k = ndim;
  920. size_t len = 1;
  921. if (k)
  922. {
  923. ssize_t last_inc = s[k - 1].inc;
  924. while (k--)
  925. {
  926. if (len*last_inc != s[k].inc)
  927. return SCM_BOOL_F;
  928. len *= (s[k].ubnd - s[k].lbnd + 1);
  929. }
  930. }
  931. if (!SCM_UNBNDP (strict) && scm_is_true (strict))
  932. {
  933. if (ndim && (1 != s[ndim - 1].inc))
  934. return SCM_BOOL_F;
  935. if (scm_is_bitvector (SCM_I_ARRAY_V (ra))
  936. && (len != scm_c_bitvector_length (SCM_I_ARRAY_V (ra)) ||
  937. SCM_I_ARRAY_BASE (ra) % SCM_LONG_BIT ||
  938. len % SCM_LONG_BIT))
  939. return SCM_BOOL_F;
  940. }
  941. v = SCM_I_ARRAY_V (ra);
  942. if ((len == scm_c_array_length (v)) && (0 == SCM_I_ARRAY_BASE (ra)))
  943. return v;
  944. else
  945. {
  946. SCM sra = scm_i_make_array (1);
  947. SCM_I_ARRAY_DIMS (sra)->lbnd = 0;
  948. SCM_I_ARRAY_DIMS (sra)->ubnd = len - 1;
  949. SCM_I_ARRAY_SET_V (sra, v);
  950. SCM_I_ARRAY_SET_BASE (sra, SCM_I_ARRAY_BASE (ra));
  951. SCM_I_ARRAY_DIMS (sra)->inc = (ndim ? SCM_I_ARRAY_DIMS (ra)[ndim - 1].inc : 1);
  952. return sra;
  953. }
  954. }
  955. else if (scm_is_array (ra))
  956. return ra;
  957. else
  958. scm_wrong_type_arg_msg (NULL, 0, ra, "array");
  959. }
  960. #undef FUNC_NAME
  961. static void
  962. list_to_array (SCM lst, scm_t_array_handle *handle, ssize_t pos, size_t k)
  963. {
  964. if (k == scm_array_handle_rank (handle))
  965. scm_array_handle_set (handle, pos, lst);
  966. else
  967. {
  968. scm_t_array_dim *dim = scm_array_handle_dims (handle) + k;
  969. ssize_t inc = dim->inc;
  970. size_t len = 1 + dim->ubnd - dim->lbnd, n;
  971. char *errmsg = NULL;
  972. n = len;
  973. while (n > 0 && scm_is_pair (lst))
  974. {
  975. list_to_array (SCM_CAR (lst), handle, pos, k + 1);
  976. pos += inc;
  977. lst = SCM_CDR (lst);
  978. n -= 1;
  979. }
  980. if (n != 0)
  981. errmsg = "too few elements for array dimension ~a, need ~a";
  982. if (!scm_is_null (lst))
  983. errmsg = "too many elements for array dimension ~a, want ~a";
  984. if (errmsg)
  985. scm_misc_error (NULL, errmsg, scm_list_2 (scm_from_size_t (k),
  986. scm_from_size_t (len)));
  987. }
  988. }
  989. SCM_DEFINE (scm_list_to_typed_array, "list->typed-array", 3, 0, 0,
  990. (SCM type, SCM shape, SCM lst),
  991. "Return an array of the type @var{type}\n"
  992. "with elements the same as those of @var{lst}.\n"
  993. "\n"
  994. "The argument @var{shape} determines the number of dimensions\n"
  995. "of the array and their shape. It is either an exact integer,\n"
  996. "giving the\n"
  997. "number of dimensions directly, or a list whose length\n"
  998. "specifies the number of dimensions and each element specified\n"
  999. "the lower and optionally the upper bound of the corresponding\n"
  1000. "dimension.\n"
  1001. "When the element is list of two elements, these elements\n"
  1002. "give the lower and upper bounds. When it is an exact\n"
  1003. "integer, it gives only the lower bound.")
  1004. #define FUNC_NAME s_scm_list_to_typed_array
  1005. {
  1006. SCM row;
  1007. SCM ra;
  1008. scm_t_array_handle handle;
  1009. row = lst;
  1010. if (scm_is_integer (shape))
  1011. {
  1012. size_t k = scm_to_size_t (shape);
  1013. shape = SCM_EOL;
  1014. while (k-- > 0)
  1015. {
  1016. shape = scm_cons (scm_length (row), shape);
  1017. if (k > 0 && !scm_is_null (row))
  1018. row = scm_car (row);
  1019. }
  1020. }
  1021. else
  1022. {
  1023. SCM shape_spec = shape;
  1024. shape = SCM_EOL;
  1025. while (1)
  1026. {
  1027. SCM spec = scm_car (shape_spec);
  1028. if (scm_is_pair (spec))
  1029. shape = scm_cons (spec, shape);
  1030. else
  1031. shape = scm_cons (scm_list_2 (spec,
  1032. scm_sum (scm_sum (spec,
  1033. scm_length (row)),
  1034. scm_from_int (-1))),
  1035. shape);
  1036. shape_spec = scm_cdr (shape_spec);
  1037. if (scm_is_pair (shape_spec))
  1038. {
  1039. if (!scm_is_null (row))
  1040. row = scm_car (row);
  1041. }
  1042. else
  1043. break;
  1044. }
  1045. }
  1046. ra = scm_make_typed_array (type, SCM_UNSPECIFIED,
  1047. scm_reverse_x (shape, SCM_EOL));
  1048. scm_array_get_handle (ra, &handle);
  1049. list_to_array (lst, &handle, 0, 0);
  1050. scm_array_handle_release (&handle);
  1051. return ra;
  1052. }
  1053. #undef FUNC_NAME
  1054. SCM_DEFINE (scm_list_to_array, "list->array", 2, 0, 0,
  1055. (SCM ndim, SCM lst),
  1056. "Return an array with elements the same as those of @var{lst}.")
  1057. #define FUNC_NAME s_scm_list_to_array
  1058. {
  1059. return scm_list_to_typed_array (SCM_BOOL_T, ndim, lst);
  1060. }
  1061. #undef FUNC_NAME
  1062. /* Print dimension DIM of ARRAY.
  1063. */
  1064. static int
  1065. scm_i_print_array_dimension (scm_t_array_handle *h, int dim, int pos,
  1066. SCM port, scm_print_state *pstate)
  1067. {
  1068. if (dim == h->ndims)
  1069. scm_iprin1 (scm_array_handle_ref (h, pos), port, pstate);
  1070. else
  1071. {
  1072. ssize_t i;
  1073. scm_putc ('(', port);
  1074. for (i = h->dims[dim].lbnd; i <= h->dims[dim].ubnd;
  1075. i++, pos += h->dims[dim].inc)
  1076. {
  1077. scm_i_print_array_dimension (h, dim+1, pos, port, pstate);
  1078. if (i < h->dims[dim].ubnd)
  1079. scm_putc (' ', port);
  1080. }
  1081. scm_putc (')', port);
  1082. }
  1083. return 1;
  1084. }
  1085. int
  1086. scm_i_print_array (SCM array, SCM port, scm_print_state *pstate)
  1087. {
  1088. scm_t_array_handle h;
  1089. int d;
  1090. scm_call_2 (scm_c_private_ref ("ice-9 arrays", "array-print-prefix"),
  1091. array, port);
  1092. scm_array_get_handle (array, &h);
  1093. if (h.ndims == 0)
  1094. {
  1095. /* Rank zero arrays, which are really just scalars, are printed
  1096. specially. The consequent way would be to print them as
  1097. #0 OBJ
  1098. where OBJ is the printed representation of the scalar, but we
  1099. print them instead as
  1100. #0(OBJ)
  1101. to make them look less strange.
  1102. Just printing them as
  1103. OBJ
  1104. would be correct in a way as well, but zero rank arrays are
  1105. not really the same as Scheme values since they are boxed and
  1106. can be modified with array-set!, say.
  1107. */
  1108. scm_putc ('(', port);
  1109. scm_i_print_array_dimension (&h, 0, 0, port, pstate);
  1110. scm_putc (')', port);
  1111. d = 1;
  1112. }
  1113. else
  1114. d = scm_i_print_array_dimension (&h, 0, 0, port, pstate);
  1115. scm_array_handle_release (&h);
  1116. return d;
  1117. }
  1118. // -----------------------------------------------
  1119. // other functions
  1120. // -----------------------------------------------
  1121. SCM_DEFINE (scm_uniform_array_to_bytevector, "uniform-array->bytevector",
  1122. 1, 0, 0, (SCM array),
  1123. "Return a newly allocated bytevector whose contents\n"
  1124. "will be copied from the uniform array @var{array}.")
  1125. #define FUNC_NAME s_scm_uniform_array_to_bytevector
  1126. {
  1127. SCM contents, ret;
  1128. size_t len, sz, byte_len;
  1129. scm_t_array_handle h;
  1130. const void *elts;
  1131. contents = scm_array_contents (array, SCM_BOOL_T);
  1132. if (scm_is_false (contents))
  1133. scm_wrong_type_arg_msg (FUNC_NAME, 0, array, "uniform contiguous array");
  1134. scm_array_get_handle (contents, &h);
  1135. assert (h.base == 0);
  1136. elts = h.elements;
  1137. len = h.dims->inc * (h.dims->ubnd - h.dims->lbnd + 1);
  1138. sz = scm_array_handle_uniform_element_bit_size (&h);
  1139. if (sz >= 8 && ((sz % 8) == 0))
  1140. byte_len = len * (sz / 8);
  1141. else if (sz < 8)
  1142. /* Elements of sub-byte size (bitvectors) are addressed in 32-bit
  1143. units. */
  1144. byte_len = ((len * sz + 31) / 32) * 4;
  1145. else
  1146. /* an internal guile error, really */
  1147. SCM_MISC_ERROR ("uniform elements larger than 8 bits must fill whole bytes", SCM_EOL);
  1148. ret = scm_c_make_bytevector (byte_len);
  1149. if (byte_len != 0)
  1150. /* Empty arrays may have elements == NULL. We must avoid passing
  1151. NULL to memcpy, even if the length is zero, to avoid undefined
  1152. behavior. */
  1153. memcpy (SCM_BYTEVECTOR_CONTENTS (ret), elts, byte_len);
  1154. scm_array_handle_release (&h);
  1155. return ret;
  1156. }
  1157. #undef FUNC_NAME
  1158. /* ---------------------- */
  1159. /* Init hook */
  1160. /* ---------------------- */
  1161. #define SCM_VECTOR_IMPLEMENTATION(type, ctor) \
  1162. SCM_SNARF_INIT (scm_i_register_vector_constructor \
  1163. (scm_i_array_element_types[type], ctor))
  1164. SCM_VECTOR_IMPLEMENTATION (SCM_ARRAY_ELEMENT_TYPE_SCM, scm_make_vector)
  1165. SCM_VECTOR_IMPLEMENTATION (SCM_ARRAY_ELEMENT_TYPE_BIT, scm_make_bitvector)
  1166. SCM_VECTOR_IMPLEMENTATION (SCM_ARRAY_ELEMENT_TYPE_CHAR, scm_make_string)
  1167. SCM_VECTOR_IMPLEMENTATION (SCM_ARRAY_ELEMENT_TYPE_VU8, scm_make_bytevector)
  1168. void
  1169. scm_init_arrays ()
  1170. {
  1171. #define REGISTER(tag, TAG) \
  1172. scm_i_register_vector_constructor \
  1173. (scm_i_array_element_types[SCM_ARRAY_ELEMENT_TYPE_##TAG], \
  1174. scm_make_##tag##vector)
  1175. REGISTER (u8, U8);
  1176. REGISTER (s8, S8);
  1177. REGISTER (u16, U16);
  1178. REGISTER (s16, S16);
  1179. REGISTER (u32, U32);
  1180. REGISTER (s32, S32);
  1181. REGISTER (u64, U64);
  1182. REGISTER (s64, S64);
  1183. REGISTER (f32, F32);
  1184. REGISTER (f64, F64);
  1185. REGISTER (c32, C32);
  1186. REGISTER (c64, C64);
  1187. scm_add_feature ("array");
  1188. #include "arrays.x"
  1189. }