generalized-arrays.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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_length (SCM array)
  91. {
  92. scm_t_array_handle handle;
  93. size_t res;
  94. scm_array_get_handle (array, &handle);
  95. if (scm_array_handle_rank (&handle) < 1)
  96. {
  97. scm_array_handle_release (&handle);
  98. scm_wrong_type_arg_msg (NULL, 0, array, "array of nonzero rank");
  99. }
  100. res = handle.dims[0].ubnd - handle.dims[0].lbnd + 1;
  101. scm_array_handle_release (&handle);
  102. return res;
  103. }
  104. SCM_DEFINE (scm_array_length, "array-length", 1, 0, 0,
  105. (SCM array),
  106. "Return the length of an array: its first dimension.\n"
  107. "It is an error to ask for the length of an array of rank 0.")
  108. #define FUNC_NAME s_scm_array_length
  109. {
  110. return scm_from_size_t (scm_c_array_length (array));
  111. }
  112. #undef FUNC_NAME
  113. SCM_DEFINE (scm_array_dimensions, "array-dimensions", 1, 0, 0,
  114. (SCM ra),
  115. "@code{array-dimensions} is similar to @code{array-shape} but replaces\n"
  116. "elements with a @code{0} minimum with one greater than the maximum. So:\n"
  117. "@lisp\n"
  118. "(array-dimensions (make-array 'foo '(-1 3) 5)) @result{} ((-1 3) 5)\n"
  119. "@end lisp")
  120. #define FUNC_NAME s_scm_array_dimensions
  121. {
  122. scm_t_array_handle handle;
  123. scm_t_array_dim *s;
  124. SCM res = SCM_EOL;
  125. size_t k;
  126. scm_array_get_handle (ra, &handle);
  127. s = scm_array_handle_dims (&handle);
  128. k = scm_array_handle_rank (&handle);
  129. while (k--)
  130. res = scm_cons (s[k].lbnd
  131. ? scm_cons2 (scm_from_ssize_t (s[k].lbnd),
  132. scm_from_ssize_t (s[k].ubnd),
  133. SCM_EOL)
  134. : scm_from_ssize_t (1 + s[k].ubnd),
  135. res);
  136. scm_array_handle_release (&handle);
  137. return res;
  138. }
  139. #undef FUNC_NAME
  140. SCM_DEFINE (scm_array_type, "array-type", 1, 0, 0,
  141. (SCM ra),
  142. "")
  143. #define FUNC_NAME s_scm_array_type
  144. {
  145. scm_t_array_handle h;
  146. SCM type;
  147. scm_array_get_handle (ra, &h);
  148. type = scm_array_handle_element_type (&h);
  149. scm_array_handle_release (&h);
  150. return type;
  151. }
  152. #undef FUNC_NAME
  153. SCM_DEFINE (scm_array_type_code,
  154. "array-type-code", 1, 0, 0,
  155. (SCM array),
  156. "Return the type of the elements in @var{array},\n"
  157. "as an integer code.")
  158. #define FUNC_NAME s_scm_array_type_code
  159. {
  160. scm_t_array_handle h;
  161. scm_t_array_element_type element_type;
  162. scm_array_get_handle (array, &h);
  163. element_type = h.element_type;
  164. scm_array_handle_release (&h);
  165. return scm_from_uint16 (element_type);
  166. }
  167. #undef FUNC_NAME
  168. SCM_DEFINE (scm_array_in_bounds_p, "array-in-bounds?", 1, 0, 1,
  169. (SCM ra, SCM args),
  170. "Return @code{#t} if its arguments would be acceptable to\n"
  171. "@code{array-ref}.")
  172. #define FUNC_NAME s_scm_array_in_bounds_p
  173. {
  174. SCM res = SCM_BOOL_T;
  175. size_t k, ndim;
  176. scm_t_array_dim *s;
  177. scm_t_array_handle handle;
  178. SCM_VALIDATE_REST_ARGUMENT (args);
  179. scm_array_get_handle (ra, &handle);
  180. s = scm_array_handle_dims (&handle);
  181. ndim = scm_array_handle_rank (&handle);
  182. for (k = 0; k < ndim; k++)
  183. {
  184. long ind;
  185. if (!scm_is_pair (args))
  186. SCM_WRONG_NUM_ARGS ();
  187. ind = scm_to_long (SCM_CAR (args));
  188. args = SCM_CDR (args);
  189. if (ind < s[k].lbnd || ind > s[k].ubnd)
  190. {
  191. res = SCM_BOOL_F;
  192. /* We do not stop the checking after finding a violation
  193. since we want to validate the type-correctness and
  194. number of arguments in any case.
  195. */
  196. }
  197. }
  198. scm_array_handle_release (&handle);
  199. return res;
  200. }
  201. #undef FUNC_NAME
  202. SCM
  203. scm_c_array_ref_1 (SCM array, ssize_t idx0)
  204. {
  205. scm_t_array_handle handle;
  206. SCM res;
  207. scm_array_get_handle (array, &handle);
  208. res = scm_array_handle_ref (&handle, scm_array_handle_pos_1 (&handle, idx0));
  209. scm_array_handle_release (&handle);
  210. return res;
  211. }
  212. SCM
  213. scm_c_array_ref_2 (SCM array, ssize_t idx0, ssize_t idx1)
  214. {
  215. scm_t_array_handle handle;
  216. SCM res;
  217. scm_array_get_handle (array, &handle);
  218. res = scm_array_handle_ref (&handle, scm_array_handle_pos_2 (&handle, idx0, idx1));
  219. scm_array_handle_release (&handle);
  220. return res;
  221. }
  222. SCM
  223. scm_array_ref (SCM v, SCM args)
  224. {
  225. scm_t_array_handle handle;
  226. SCM res;
  227. scm_array_get_handle (v, &handle);
  228. res = scm_array_handle_ref (&handle, scm_array_handle_pos (&handle, args));
  229. scm_array_handle_release (&handle);
  230. return res;
  231. }
  232. void
  233. scm_c_array_set_1_x (SCM array, SCM obj, ssize_t idx0)
  234. {
  235. scm_t_array_handle handle;
  236. scm_array_get_handle (array, &handle);
  237. scm_array_handle_set (&handle, scm_array_handle_pos_1 (&handle, idx0),
  238. obj);
  239. scm_array_handle_release (&handle);
  240. }
  241. void
  242. scm_c_array_set_2_x (SCM array, SCM obj, ssize_t idx0, ssize_t idx1)
  243. {
  244. scm_t_array_handle handle;
  245. scm_array_get_handle (array, &handle);
  246. scm_array_handle_set (&handle, scm_array_handle_pos_2 (&handle, idx0, idx1),
  247. obj);
  248. scm_array_handle_release (&handle);
  249. }
  250. SCM
  251. scm_array_set_x (SCM v, SCM obj, SCM args)
  252. {
  253. scm_t_array_handle handle;
  254. scm_array_get_handle (v, &handle);
  255. scm_array_handle_set (&handle, scm_array_handle_pos (&handle, args), obj);
  256. scm_array_handle_release (&handle);
  257. return SCM_UNSPECIFIED;
  258. }
  259. SCM_DEFINE (scm_i_array_ref, "array-ref", 1, 2, 1,
  260. (SCM v, SCM idx0, SCM idx1, SCM idxN),
  261. "Return the element at the @code{(idx0, idx1, idxN...)}\n"
  262. "position in array @var{v}.")
  263. #define FUNC_NAME s_scm_i_array_ref
  264. {
  265. if (SCM_UNBNDP (idx0))
  266. return scm_array_ref (v, SCM_EOL);
  267. else if (SCM_UNBNDP (idx1))
  268. return scm_c_array_ref_1 (v, scm_to_ssize_t (idx0));
  269. else if (scm_is_null (idxN))
  270. return scm_c_array_ref_2 (v, scm_to_ssize_t (idx0), scm_to_ssize_t (idx1));
  271. else
  272. return scm_array_ref (v, scm_cons (idx0, scm_cons (idx1, idxN)));
  273. }
  274. #undef FUNC_NAME
  275. SCM_DEFINE (scm_i_array_set_x, "array-set!", 2, 2, 1,
  276. (SCM v, SCM obj, SCM idx0, SCM idx1, SCM idxN),
  277. "Set the element at the @code{(idx0, idx1, idxN...)} position\n"
  278. "in the array @var{v} to @var{obj}. The value returned by\n"
  279. "@code{array-set!} is unspecified.")
  280. #define FUNC_NAME s_scm_i_array_set_x
  281. {
  282. if (SCM_UNBNDP (idx0))
  283. scm_array_set_x (v, obj, SCM_EOL);
  284. else if (SCM_UNBNDP (idx1))
  285. scm_c_array_set_1_x (v, obj, scm_to_ssize_t (idx0));
  286. else if (scm_is_null (idxN))
  287. scm_c_array_set_2_x (v, obj, scm_to_ssize_t (idx0), scm_to_ssize_t (idx1));
  288. else
  289. scm_array_set_x (v, obj, scm_cons (idx0, scm_cons (idx1, idxN)));
  290. return SCM_UNSPECIFIED;
  291. }
  292. #undef FUNC_NAME
  293. static SCM
  294. array_to_list (scm_t_array_handle *h, size_t dim, unsigned long pos)
  295. {
  296. if (dim == scm_array_handle_rank (h))
  297. return scm_array_handle_ref (h, pos);
  298. else
  299. {
  300. SCM res = SCM_EOL;
  301. long inc;
  302. size_t i;
  303. i = h->dims[dim].ubnd - h->dims[dim].lbnd + 1;
  304. inc = h->dims[dim].inc;
  305. pos += (i - 1) * inc;
  306. for (; i > 0; i--, pos -= inc)
  307. res = scm_cons (array_to_list (h, dim + 1, pos), res);
  308. return res;
  309. }
  310. }
  311. SCM_DEFINE (scm_array_to_list, "array->list", 1, 0, 0,
  312. (SCM array),
  313. "Return a list representation of @var{array}.\n\n"
  314. "It is easiest to specify the behavior of this function by\n"
  315. "example:\n"
  316. "@example\n"
  317. "(array->list #0(a)) @result{} 1\n"
  318. "(array->list #1(a b)) @result{} (a b)\n"
  319. "(array->list #2((aa ab) (ba bb)) @result{} ((aa ab) (ba bb))\n"
  320. "@end example\n")
  321. #define FUNC_NAME s_scm_array_to_list
  322. {
  323. scm_t_array_handle h;
  324. SCM res;
  325. scm_array_get_handle (array, &h);
  326. res = array_to_list (&h, 0, 0);
  327. scm_array_handle_release (&h);
  328. return res;
  329. }
  330. #undef FUNC_NAME
  331. void
  332. scm_init_generalized_arrays ()
  333. {
  334. #include "libguile/generalized-arrays.x"
  335. }
  336. /*
  337. Local Variables:
  338. c-file-style: "gnu"
  339. End:
  340. */