generalized-arrays.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /* Copyright (C) 1995,1996,1997,1998,2000,2001,2002,2003,2004, 2005, 2006, 2009, 2010 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. int
  29. scm_is_array (SCM obj)
  30. {
  31. return scm_i_array_implementation_for_obj (obj) ? 1 : 0;
  32. }
  33. SCM_DEFINE (scm_array_p_2, "array?", 1, 0, 0,
  34. (SCM obj),
  35. "Return @code{#t} if the @var{obj} is an array, and @code{#f} if\n"
  36. "not.")
  37. #define FUNC_NAME s_scm_array_p_2
  38. {
  39. return scm_from_bool (scm_is_array (obj));
  40. }
  41. #undef FUNC_NAME
  42. /* The array type predicate, with an extra argument kept for backward
  43. compatibility. Note that we can't use `SCM_DEFINE' directly because there
  44. would be an argument count mismatch that would be caught by
  45. `snarf-check-and-output-texi.scm'. */
  46. SCM
  47. scm_array_p (SCM obj, SCM unused)
  48. {
  49. return scm_array_p_2 (obj);
  50. }
  51. int
  52. scm_is_typed_array (SCM obj, SCM type)
  53. {
  54. int ret = 0;
  55. if (scm_i_array_implementation_for_obj (obj))
  56. {
  57. scm_t_array_handle h;
  58. scm_array_get_handle (obj, &h);
  59. ret = scm_is_eq (scm_array_handle_element_type (&h), type);
  60. scm_array_handle_release (&h);
  61. }
  62. return ret;
  63. }
  64. SCM_DEFINE (scm_typed_array_p, "typed-array?", 2, 0, 0,
  65. (SCM obj, SCM type),
  66. "Return @code{#t} if the @var{obj} is an array of type\n"
  67. "@var{type}, and @code{#f} if not.")
  68. #define FUNC_NAME s_scm_typed_array_p
  69. {
  70. return scm_from_bool (scm_is_typed_array (obj, type));
  71. }
  72. #undef FUNC_NAME
  73. size_t
  74. scm_c_array_rank (SCM array)
  75. {
  76. scm_t_array_handle handle;
  77. size_t res;
  78. scm_array_get_handle (array, &handle);
  79. res = scm_array_handle_rank (&handle);
  80. scm_array_handle_release (&handle);
  81. return res;
  82. }
  83. SCM_DEFINE (scm_array_rank, "array-rank", 1, 0, 0,
  84. (SCM array),
  85. "Return the number of dimensions of the array @var{array.}\n")
  86. #define FUNC_NAME s_scm_array_rank
  87. {
  88. return scm_from_size_t (scm_c_array_rank (array));
  89. }
  90. #undef FUNC_NAME
  91. SCM_DEFINE (scm_array_dimensions, "array-dimensions", 1, 0, 0,
  92. (SCM ra),
  93. "@code{array-dimensions} is similar to @code{array-shape} but replaces\n"
  94. "elements with a @code{0} minimum with one greater than the maximum. So:\n"
  95. "@lisp\n"
  96. "(array-dimensions (make-array 'foo '(-1 3) 5)) @result{} ((-1 3) 5)\n"
  97. "@end lisp")
  98. #define FUNC_NAME s_scm_array_dimensions
  99. {
  100. scm_t_array_handle handle;
  101. scm_t_array_dim *s;
  102. SCM res = SCM_EOL;
  103. size_t k;
  104. scm_array_get_handle (ra, &handle);
  105. s = scm_array_handle_dims (&handle);
  106. k = scm_array_handle_rank (&handle);
  107. while (k--)
  108. res = scm_cons (s[k].lbnd
  109. ? scm_cons2 (scm_from_ssize_t (s[k].lbnd),
  110. scm_from_ssize_t (s[k].ubnd),
  111. SCM_EOL)
  112. : scm_from_ssize_t (1 + s[k].ubnd),
  113. res);
  114. scm_array_handle_release (&handle);
  115. return res;
  116. }
  117. #undef FUNC_NAME
  118. SCM_DEFINE (scm_array_type, "array-type", 1, 0, 0,
  119. (SCM ra),
  120. "")
  121. #define FUNC_NAME s_scm_array_type
  122. {
  123. scm_t_array_handle h;
  124. SCM type;
  125. scm_array_get_handle (ra, &h);
  126. type = scm_array_handle_element_type (&h);
  127. scm_array_handle_release (&h);
  128. return type;
  129. }
  130. #undef FUNC_NAME
  131. SCM_DEFINE (scm_array_in_bounds_p, "array-in-bounds?", 1, 0, 1,
  132. (SCM ra, SCM args),
  133. "Return @code{#t} if its arguments would be acceptable to\n"
  134. "@code{array-ref}.")
  135. #define FUNC_NAME s_scm_array_in_bounds_p
  136. {
  137. SCM res = SCM_BOOL_T;
  138. size_t k, ndim;
  139. scm_t_array_dim *s;
  140. scm_t_array_handle handle;
  141. SCM_VALIDATE_REST_ARGUMENT (args);
  142. scm_array_get_handle (ra, &handle);
  143. s = scm_array_handle_dims (&handle);
  144. ndim = scm_array_handle_rank (&handle);
  145. for (k = 0; k < ndim; k++)
  146. {
  147. long ind;
  148. if (!scm_is_pair (args))
  149. SCM_WRONG_NUM_ARGS ();
  150. ind = scm_to_long (SCM_CAR (args));
  151. args = SCM_CDR (args);
  152. if (ind < s[k].lbnd || ind > s[k].ubnd)
  153. {
  154. res = SCM_BOOL_F;
  155. /* We do not stop the checking after finding a violation
  156. since we want to validate the type-correctness and
  157. number of arguments in any case.
  158. */
  159. }
  160. }
  161. scm_array_handle_release (&handle);
  162. return res;
  163. }
  164. #undef FUNC_NAME
  165. SCM_DEFINE (scm_array_ref, "array-ref", 1, 0, 1,
  166. (SCM v, SCM args),
  167. "Return the element at the @code{(index1, index2)} element in\n"
  168. "array @var{v}.")
  169. #define FUNC_NAME s_scm_array_ref
  170. {
  171. scm_t_array_handle handle;
  172. SCM res;
  173. scm_array_get_handle (v, &handle);
  174. res = scm_array_handle_ref (&handle, scm_array_handle_pos (&handle, args));
  175. scm_array_handle_release (&handle);
  176. return res;
  177. }
  178. #undef FUNC_NAME
  179. SCM_DEFINE (scm_array_set_x, "array-set!", 2, 0, 1,
  180. (SCM v, SCM obj, SCM args),
  181. "Set the element at the @code{(index1, index2)} element in array\n"
  182. "@var{v} to @var{obj}. The value returned by @code{array-set!}\n"
  183. "is unspecified.")
  184. #define FUNC_NAME s_scm_array_set_x
  185. {
  186. scm_t_array_handle handle;
  187. scm_array_get_handle (v, &handle);
  188. scm_array_handle_set (&handle, scm_array_handle_pos (&handle, args), obj);
  189. scm_array_handle_release (&handle);
  190. return SCM_UNSPECIFIED;
  191. }
  192. #undef FUNC_NAME
  193. static SCM
  194. array_to_list (scm_t_array_handle *h, size_t dim, unsigned long pos)
  195. {
  196. if (dim == scm_array_handle_rank (h))
  197. return scm_array_handle_ref (h, pos);
  198. else
  199. {
  200. SCM res = SCM_EOL;
  201. long inc;
  202. size_t i;
  203. i = h->dims[dim].ubnd - h->dims[dim].lbnd + 1;
  204. inc = h->dims[dim].inc;
  205. pos += (i - 1) * inc;
  206. for (; i > 0; i--, pos -= inc)
  207. res = scm_cons (array_to_list (h, dim + 1, pos), res);
  208. return res;
  209. }
  210. }
  211. SCM_DEFINE (scm_array_to_list, "array->list", 1, 0, 0,
  212. (SCM array),
  213. "Return a list representation of @var{array}.\n\n"
  214. "It is easiest to specify the behavior of this function by\n"
  215. "example:\n"
  216. "@example\n"
  217. "(array->list #0(a)) @result{} 1\n"
  218. "(array->list #1(a b)) @result{} (a b)\n"
  219. "(array->list #2((aa ab) (ba bb)) @result{} ((aa ab) (ba bb))\n"
  220. "@end example\n")
  221. #define FUNC_NAME s_scm_array_to_list
  222. {
  223. scm_t_array_handle h;
  224. SCM res;
  225. scm_array_get_handle (array, &h);
  226. res = array_to_list (&h, 0, 0);
  227. scm_array_handle_release (&h);
  228. return res;
  229. }
  230. #undef FUNC_NAME
  231. void
  232. scm_init_generalized_arrays ()
  233. {
  234. #include "libguile/generalized-arrays.x"
  235. }
  236. /*
  237. Local Variables:
  238. c-file-style: "gnu"
  239. End:
  240. */