generalized-arrays.c 10 KB

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