generalized-arrays.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /* Copyright (C) 1995,1996,1997,1998,2000,2001,2002,2003,2004, 2005, 2006, 2009, 2010, 2013, 2014 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/array-handle.h"
  27. #include "libguile/generalized-arrays.h"
  28. SCM_INTERNAL SCM scm_i_array_ref (SCM v,
  29. SCM idx0, SCM idx1, SCM idxN);
  30. SCM_INTERNAL SCM scm_i_array_set_x (SCM v, SCM obj,
  31. SCM idx0, SCM idx1, SCM idxN);
  32. int
  33. scm_is_array (SCM obj)
  34. {
  35. if (!SCM_HEAP_OBJECT_P (obj))
  36. return 0;
  37. switch (SCM_TYP7 (obj))
  38. {
  39. case scm_tc7_string:
  40. case scm_tc7_vector:
  41. case scm_tc7_bitvector:
  42. case scm_tc7_bytevector:
  43. case scm_tc7_array:
  44. return 1;
  45. default:
  46. return 0;
  47. }
  48. }
  49. SCM_DEFINE (scm_array_p_2, "array?", 1, 0, 0,
  50. (SCM obj),
  51. "Return @code{#t} if the @var{obj} is an array, and @code{#f} if\n"
  52. "not.")
  53. #define FUNC_NAME s_scm_array_p_2
  54. {
  55. return scm_from_bool (scm_is_array (obj));
  56. }
  57. #undef FUNC_NAME
  58. /* The array type predicate, with an extra argument kept for backward
  59. compatibility. Note that we can't use `SCM_DEFINE' directly because there
  60. would be an argument count mismatch that would be caught by
  61. `snarf-check-and-output-texi.scm'. */
  62. SCM
  63. scm_array_p (SCM obj, SCM unused)
  64. {
  65. return scm_array_p_2 (obj);
  66. }
  67. int
  68. scm_is_typed_array (SCM obj, SCM type)
  69. {
  70. int ret = 0;
  71. if (scm_is_array (obj))
  72. {
  73. scm_t_array_handle h;
  74. scm_array_get_handle (obj, &h);
  75. ret = scm_is_eq (scm_array_handle_element_type (&h), type);
  76. scm_array_handle_release (&h);
  77. }
  78. return ret;
  79. }
  80. SCM_DEFINE (scm_typed_array_p, "typed-array?", 2, 0, 0,
  81. (SCM obj, SCM type),
  82. "Return @code{#t} if the @var{obj} is an array of type\n"
  83. "@var{type}, and @code{#f} if not.")
  84. #define FUNC_NAME s_scm_typed_array_p
  85. {
  86. return scm_from_bool (scm_is_typed_array (obj, type));
  87. }
  88. #undef FUNC_NAME
  89. size_t
  90. scm_c_array_rank (SCM array)
  91. {
  92. scm_t_array_handle handle;
  93. size_t res;
  94. scm_array_get_handle (array, &handle);
  95. res = scm_array_handle_rank (&handle);
  96. scm_array_handle_release (&handle);
  97. return res;
  98. }
  99. SCM_DEFINE (scm_array_rank, "array-rank", 1, 0, 0,
  100. (SCM array),
  101. "Return the number of dimensions of the array @var{array.}\n")
  102. #define FUNC_NAME s_scm_array_rank
  103. {
  104. return scm_from_size_t (scm_c_array_rank (array));
  105. }
  106. #undef FUNC_NAME
  107. size_t
  108. scm_c_array_length (SCM array)
  109. {
  110. scm_t_array_handle handle;
  111. size_t res;
  112. scm_array_get_handle (array, &handle);
  113. if (scm_array_handle_rank (&handle) < 1)
  114. {
  115. scm_array_handle_release (&handle);
  116. scm_wrong_type_arg_msg (NULL, 0, array, "array of nonzero rank");
  117. }
  118. res = handle.dims[0].ubnd - handle.dims[0].lbnd + 1;
  119. scm_array_handle_release (&handle);
  120. return res;
  121. }
  122. SCM_DEFINE (scm_array_length, "array-length", 1, 0, 0,
  123. (SCM array),
  124. "Return the length of an array: its first dimension.\n"
  125. "It is an error to ask for the length of an array of rank 0.")
  126. #define FUNC_NAME s_scm_array_length
  127. {
  128. return scm_from_size_t (scm_c_array_length (array));
  129. }
  130. #undef FUNC_NAME
  131. SCM_DEFINE (scm_array_dimensions, "array-dimensions", 1, 0, 0,
  132. (SCM ra),
  133. "@code{array-dimensions} is similar to @code{array-shape} but replaces\n"
  134. "elements with a @code{0} minimum with one greater than the maximum. So:\n"
  135. "@lisp\n"
  136. "(array-dimensions (make-array 'foo '(-1 3) 5)) @result{} ((-1 3) 5)\n"
  137. "@end lisp")
  138. #define FUNC_NAME s_scm_array_dimensions
  139. {
  140. scm_t_array_handle handle;
  141. scm_t_array_dim *s;
  142. SCM res = SCM_EOL;
  143. size_t k;
  144. scm_array_get_handle (ra, &handle);
  145. s = scm_array_handle_dims (&handle);
  146. k = scm_array_handle_rank (&handle);
  147. while (k--)
  148. res = scm_cons (s[k].lbnd
  149. ? scm_cons2 (scm_from_ssize_t (s[k].lbnd),
  150. scm_from_ssize_t (s[k].ubnd),
  151. SCM_EOL)
  152. : scm_from_ssize_t (1 + s[k].ubnd),
  153. res);
  154. scm_array_handle_release (&handle);
  155. return res;
  156. }
  157. #undef FUNC_NAME
  158. SCM_DEFINE (scm_array_type, "array-type", 1, 0, 0,
  159. (SCM ra),
  160. "")
  161. #define FUNC_NAME s_scm_array_type
  162. {
  163. scm_t_array_handle h;
  164. SCM type;
  165. scm_array_get_handle (ra, &h);
  166. type = scm_array_handle_element_type (&h);
  167. scm_array_handle_release (&h);
  168. return type;
  169. }
  170. #undef FUNC_NAME
  171. SCM_DEFINE (scm_array_type_code,
  172. "array-type-code", 1, 0, 0,
  173. (SCM array),
  174. "Return the type of the elements in @var{array},\n"
  175. "as an integer code.")
  176. #define FUNC_NAME s_scm_array_type_code
  177. {
  178. scm_t_array_handle h;
  179. scm_t_array_element_type element_type;
  180. scm_array_get_handle (array, &h);
  181. element_type = h.element_type;
  182. scm_array_handle_release (&h);
  183. return scm_from_uint16 (element_type);
  184. }
  185. #undef FUNC_NAME
  186. SCM_DEFINE (scm_array_in_bounds_p, "array-in-bounds?", 1, 0, 1,
  187. (SCM ra, SCM args),
  188. "Return @code{#t} if its arguments would be acceptable to\n"
  189. "@code{array-ref}.")
  190. #define FUNC_NAME s_scm_array_in_bounds_p
  191. {
  192. SCM res = SCM_BOOL_T;
  193. size_t k, ndim;
  194. scm_t_array_dim *s;
  195. scm_t_array_handle handle;
  196. SCM_VALIDATE_REST_ARGUMENT (args);
  197. scm_array_get_handle (ra, &handle);
  198. s = scm_array_handle_dims (&handle);
  199. ndim = scm_array_handle_rank (&handle);
  200. for (k = 0; k < ndim; k++)
  201. {
  202. long ind;
  203. if (!scm_is_pair (args))
  204. SCM_WRONG_NUM_ARGS ();
  205. ind = scm_to_long (SCM_CAR (args));
  206. args = SCM_CDR (args);
  207. if (ind < s[k].lbnd || ind > s[k].ubnd)
  208. {
  209. res = SCM_BOOL_F;
  210. /* We do not stop the checking after finding a violation
  211. since we want to validate the type-correctness and
  212. number of arguments in any case.
  213. */
  214. }
  215. }
  216. scm_array_handle_release (&handle);
  217. return res;
  218. }
  219. #undef FUNC_NAME
  220. SCM
  221. scm_c_array_ref_1 (SCM array, ssize_t idx0)
  222. {
  223. scm_t_array_handle handle;
  224. SCM res;
  225. scm_array_get_handle (array, &handle);
  226. res = scm_array_handle_ref (&handle, scm_array_handle_pos_1 (&handle, idx0));
  227. scm_array_handle_release (&handle);
  228. return res;
  229. }
  230. SCM
  231. scm_c_array_ref_2 (SCM array, ssize_t idx0, ssize_t idx1)
  232. {
  233. scm_t_array_handle handle;
  234. SCM res;
  235. scm_array_get_handle (array, &handle);
  236. res = scm_array_handle_ref (&handle, scm_array_handle_pos_2 (&handle, idx0, idx1));
  237. scm_array_handle_release (&handle);
  238. return res;
  239. }
  240. SCM
  241. scm_array_ref (SCM v, SCM args)
  242. {
  243. scm_t_array_handle handle;
  244. SCM res;
  245. scm_array_get_handle (v, &handle);
  246. res = scm_array_handle_ref (&handle, scm_array_handle_pos (&handle, args));
  247. scm_array_handle_release (&handle);
  248. return res;
  249. }
  250. void
  251. scm_c_array_set_1_x (SCM array, SCM obj, ssize_t idx0)
  252. {
  253. scm_t_array_handle handle;
  254. scm_array_get_handle (array, &handle);
  255. scm_array_handle_set (&handle, scm_array_handle_pos_1 (&handle, idx0),
  256. obj);
  257. scm_array_handle_release (&handle);
  258. }
  259. void
  260. scm_c_array_set_2_x (SCM array, SCM obj, ssize_t idx0, ssize_t idx1)
  261. {
  262. scm_t_array_handle handle;
  263. scm_array_get_handle (array, &handle);
  264. scm_array_handle_set (&handle, scm_array_handle_pos_2 (&handle, idx0, idx1),
  265. obj);
  266. scm_array_handle_release (&handle);
  267. }
  268. SCM
  269. scm_array_set_x (SCM v, SCM obj, SCM args)
  270. {
  271. scm_t_array_handle handle;
  272. scm_array_get_handle (v, &handle);
  273. scm_array_handle_set (&handle, scm_array_handle_pos (&handle, args), obj);
  274. scm_array_handle_release (&handle);
  275. return SCM_UNSPECIFIED;
  276. }
  277. SCM_DEFINE (scm_i_array_ref, "array-ref", 1, 2, 1,
  278. (SCM v, SCM idx0, SCM idx1, SCM idxN),
  279. "Return the element at the @code{(idx0, idx1, idxN...)}\n"
  280. "position in array @var{v}.")
  281. #define FUNC_NAME s_scm_i_array_ref
  282. {
  283. if (SCM_UNBNDP (idx0))
  284. return scm_array_ref (v, SCM_EOL);
  285. else if (SCM_UNBNDP (idx1))
  286. return scm_c_array_ref_1 (v, scm_to_ssize_t (idx0));
  287. else if (scm_is_null (idxN))
  288. return scm_c_array_ref_2 (v, scm_to_ssize_t (idx0), scm_to_ssize_t (idx1));
  289. else
  290. return scm_array_ref (v, scm_cons (idx0, scm_cons (idx1, idxN)));
  291. }
  292. #undef FUNC_NAME
  293. SCM_DEFINE (scm_i_array_set_x, "array-set!", 2, 2, 1,
  294. (SCM v, SCM obj, SCM idx0, SCM idx1, SCM idxN),
  295. "Set the element at the @code{(idx0, idx1, idxN...)} position\n"
  296. "in the array @var{v} to @var{obj}. The value returned by\n"
  297. "@code{array-set!} is unspecified.")
  298. #define FUNC_NAME s_scm_i_array_set_x
  299. {
  300. if (SCM_UNBNDP (idx0))
  301. scm_array_set_x (v, obj, SCM_EOL);
  302. else if (SCM_UNBNDP (idx1))
  303. scm_c_array_set_1_x (v, obj, scm_to_ssize_t (idx0));
  304. else if (scm_is_null (idxN))
  305. scm_c_array_set_2_x (v, obj, scm_to_ssize_t (idx0), scm_to_ssize_t (idx1));
  306. else
  307. scm_array_set_x (v, obj, scm_cons (idx0, scm_cons (idx1, idxN)));
  308. return SCM_UNSPECIFIED;
  309. }
  310. #undef FUNC_NAME
  311. static SCM
  312. array_to_list (scm_t_array_handle *h, size_t dim, unsigned long pos)
  313. {
  314. if (dim == scm_array_handle_rank (h))
  315. return scm_array_handle_ref (h, pos);
  316. else
  317. {
  318. SCM res = SCM_EOL;
  319. long inc;
  320. size_t i;
  321. i = h->dims[dim].ubnd - h->dims[dim].lbnd + 1;
  322. inc = h->dims[dim].inc;
  323. pos += (i - 1) * inc;
  324. for (; i > 0; i--, pos -= inc)
  325. res = scm_cons (array_to_list (h, dim + 1, pos), res);
  326. return res;
  327. }
  328. }
  329. SCM_DEFINE (scm_array_to_list, "array->list", 1, 0, 0,
  330. (SCM array),
  331. "Return a list representation of @var{array}.\n\n"
  332. "It is easiest to specify the behavior of this function by\n"
  333. "example:\n"
  334. "@example\n"
  335. "(array->list #0(a)) @result{} 1\n"
  336. "(array->list #1(a b)) @result{} (a b)\n"
  337. "(array->list #2((aa ab) (ba bb)) @result{} ((aa ab) (ba bb))\n"
  338. "@end example\n")
  339. #define FUNC_NAME s_scm_array_to_list
  340. {
  341. scm_t_array_handle h;
  342. SCM res;
  343. scm_array_get_handle (array, &h);
  344. res = array_to_list (&h, 0, 0);
  345. scm_array_handle_release (&h);
  346. return res;
  347. }
  348. #undef FUNC_NAME
  349. void
  350. scm_init_generalized_arrays ()
  351. {
  352. #include "libguile/generalized-arrays.x"
  353. }
  354. /*
  355. Local Variables:
  356. c-file-style: "gnu"
  357. End:
  358. */