vectors.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /* Copyright 1995-1996,1998-2001,2006,2008-2012,2014,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 "array-handle.h"
  19. #include "bdw-gc.h"
  20. #include "boolean.h"
  21. #include "eq.h"
  22. #include "gsubr.h"
  23. #include "list.h"
  24. #include "numbers.h"
  25. #include "pairs.h"
  26. #include "vectors.h"
  27. #include "generalized-vectors.h"
  28. #define VECTOR_MAX_LENGTH (SCM_T_BITS_MAX >> 8)
  29. #define SCM_VALIDATE_MUTABLE_VECTOR(pos, v) \
  30. do { \
  31. SCM_ASSERT (SCM_I_IS_MUTABLE_VECTOR (v), v, pos, FUNC_NAME); \
  32. } while (0)
  33. int
  34. scm_is_vector (SCM obj)
  35. {
  36. return SCM_I_IS_VECTOR (obj);
  37. }
  38. int
  39. scm_is_simple_vector (SCM obj)
  40. {
  41. return SCM_I_IS_VECTOR (obj);
  42. }
  43. const SCM *
  44. scm_vector_elements (SCM vec, scm_t_array_handle *h,
  45. size_t *lenp, ssize_t *incp)
  46. {
  47. scm_array_get_handle (vec, h);
  48. if (1 != scm_array_handle_rank (h))
  49. {
  50. scm_array_handle_release (h);
  51. scm_wrong_type_arg_msg (NULL, 0, vec, "rank 1 array of Scheme values");
  52. }
  53. if (lenp)
  54. {
  55. scm_t_array_dim *dim = scm_array_handle_dims (h);
  56. *lenp = dim->ubnd - dim->lbnd + 1;
  57. *incp = dim->inc;
  58. }
  59. return scm_array_handle_elements (h);
  60. }
  61. SCM *
  62. scm_vector_writable_elements (SCM vec, scm_t_array_handle *h,
  63. size_t *lenp, ssize_t *incp)
  64. {
  65. const SCM *ret = scm_vector_elements (vec, h, lenp, incp);
  66. if (h->writable_elements != h->elements)
  67. scm_wrong_type_arg_msg (NULL, 0, vec, "mutable vector");
  68. return (SCM *) ret;
  69. }
  70. SCM_DEFINE (scm_vector_p, "vector?", 1, 0, 0,
  71. (SCM obj),
  72. "Return @code{#t} if @var{obj} is a vector, otherwise return\n"
  73. "@code{#f}.")
  74. #define FUNC_NAME s_scm_vector_p
  75. {
  76. return scm_from_bool (scm_is_vector (obj));
  77. }
  78. #undef FUNC_NAME
  79. SCM_DEFINE (scm_vector_length, "vector-length", 1, 0, 0,
  80. (SCM v),
  81. "Returns the number of elements in @var{vector} as an exact integer.")
  82. #define FUNC_NAME s_scm_vector_length
  83. {
  84. return scm_from_size_t (scm_c_vector_length (v));
  85. }
  86. #undef FUNC_NAME
  87. size_t
  88. scm_c_vector_length (SCM v)
  89. #define FUNC_NAME s_scm_vector_length
  90. {
  91. SCM_VALIDATE_VECTOR (1, v);
  92. return SCM_I_VECTOR_LENGTH (v);
  93. }
  94. #undef FUNC_NAME
  95. SCM_REGISTER_PROC (s_list_to_vector, "list->vector", 1, 0, 0, scm_vector);
  96. /*
  97. "Return a newly created vector initialized to the elements of"
  98. "the list @var{list}.\n\n"
  99. "@lisp\n"
  100. "(vector->list '#(dah dah didah)) @result{} (dah dah didah)\n"
  101. "(list->vector '(dididit dah)) @result{} #(dididit dah)\n"
  102. "@end lisp")
  103. */
  104. SCM_DEFINE (scm_vector, "vector", 0, 0, 1,
  105. (SCM l),
  106. "@deffnx {Scheme Procedure} list->vector l\n"
  107. "Return a newly allocated vector composed of the\n"
  108. "given arguments. Analogous to @code{list}.\n"
  109. "\n"
  110. "@lisp\n"
  111. "(vector 'a 'b 'c) @result{} #(a b c)\n"
  112. "@end lisp")
  113. #define FUNC_NAME s_scm_vector
  114. {
  115. SCM res;
  116. SCM *data;
  117. long i, len;
  118. SCM_VALIDATE_LIST_COPYLEN (1, l, len);
  119. res = scm_c_make_vector (len, SCM_UNSPECIFIED);
  120. data = SCM_I_VECTOR_WELTS (res);
  121. i = 0;
  122. while (scm_is_pair (l) && i < len)
  123. {
  124. data[i] = SCM_CAR (l);
  125. l = SCM_CDR (l);
  126. i += 1;
  127. }
  128. return res;
  129. }
  130. #undef FUNC_NAME
  131. SCM_DEFINE (scm_vector_ref, "vector-ref", 2, 0, 0,
  132. (SCM vector, SCM k),
  133. "@var{k} must be a valid index of @var{vector}.\n"
  134. "@samp{Vector-ref} returns the contents of element @var{k} of\n"
  135. "@var{vector}.\n\n"
  136. "@lisp\n"
  137. "(vector-ref '#(1 1 2 3 5 8 13 21) 5) @result{} 8\n"
  138. "(vector-ref '#(1 1 2 3 5 8 13 21)\n"
  139. " (let ((i (round (* 2 (acos -1)))))\n"
  140. " (if (inexact? i)\n"
  141. " (inexact->exact i)\n"
  142. " i))) @result{} 13\n"
  143. "@end lisp")
  144. #define FUNC_NAME s_scm_vector_ref
  145. {
  146. return scm_c_vector_ref (vector, scm_to_size_t (k));
  147. }
  148. #undef FUNC_NAME
  149. SCM
  150. scm_c_vector_ref (SCM v, size_t k)
  151. #define FUNC_NAME s_scm_vector_ref
  152. {
  153. SCM_VALIDATE_VECTOR (1, v);
  154. if (k >= SCM_I_VECTOR_LENGTH (v))
  155. scm_out_of_range (NULL, scm_from_size_t (k));
  156. return SCM_SIMPLE_VECTOR_REF (v, k);
  157. }
  158. #undef FUNC_NAME
  159. SCM_DEFINE (scm_vector_set_x, "vector-set!", 3, 0, 0,
  160. (SCM vector, SCM k, SCM obj),
  161. "@var{k} must be a valid index of @var{vector}.\n"
  162. "@code{Vector-set!} stores @var{obj} in element @var{k} of @var{vector}.\n"
  163. "The value returned by @samp{vector-set!} is unspecified.\n"
  164. "@lisp\n"
  165. "(let ((vec (vector 0 '(2 2 2 2) \"Anna\")))\n"
  166. " (vector-set! vec 1 '(\"Sue\" \"Sue\"))\n"
  167. " vec) @result{} #(0 (\"Sue\" \"Sue\") \"Anna\")\n"
  168. "(vector-set! '#(0 1 2) 1 \"doe\") @result{} @emph{error} ; constant vector\n"
  169. "@end lisp")
  170. #define FUNC_NAME s_scm_vector_set_x
  171. {
  172. scm_c_vector_set_x (vector, scm_to_size_t (k), obj);
  173. return SCM_UNSPECIFIED;
  174. }
  175. #undef FUNC_NAME
  176. void
  177. scm_c_vector_set_x (SCM v, size_t k, SCM obj)
  178. #define FUNC_NAME s_scm_vector_set_x
  179. {
  180. SCM_VALIDATE_MUTABLE_VECTOR (1, v);
  181. if (k >= SCM_I_VECTOR_LENGTH (v))
  182. scm_out_of_range (NULL, scm_from_size_t (k));
  183. SCM_SIMPLE_VECTOR_SET (v, k, obj);
  184. }
  185. #undef FUNC_NAME
  186. SCM_DEFINE (scm_make_vector, "make-vector", 1, 1, 0,
  187. (SCM k, SCM fill),
  188. "Return a newly allocated vector of @var{k} elements. If a\n"
  189. "second argument is given, then each position is initialized to\n"
  190. "@var{fill}. Otherwise the initial contents of each position is\n"
  191. "unspecified.")
  192. #define FUNC_NAME s_scm_make_vector
  193. {
  194. size_t l = scm_to_unsigned_integer (k, 0, VECTOR_MAX_LENGTH);
  195. if (SCM_UNBNDP (fill))
  196. fill = SCM_UNSPECIFIED;
  197. return scm_c_make_vector (l, fill);
  198. }
  199. #undef FUNC_NAME
  200. SCM
  201. scm_c_make_vector (size_t k, SCM fill)
  202. #define FUNC_NAME s_scm_make_vector
  203. {
  204. SCM vector;
  205. unsigned long int j;
  206. SCM_ASSERT_RANGE (1, scm_from_size_t (k), k <= VECTOR_MAX_LENGTH);
  207. vector = scm_words ((k << 12) | scm_tc11_vector, k + 1);
  208. for (j = 0; j < k; ++j)
  209. SCM_SIMPLE_VECTOR_SET (vector, j, fill);
  210. return vector;
  211. }
  212. #undef FUNC_NAME
  213. SCM_DEFINE (scm_vector_copy, "vector-copy", 1, 0, 0,
  214. (SCM vec),
  215. "Return a copy of @var{vec}.")
  216. #define FUNC_NAME s_scm_vector_copy
  217. {
  218. scm_t_array_handle handle;
  219. size_t i, len;
  220. ssize_t inc;
  221. const SCM *src;
  222. SCM result, *dst;
  223. src = scm_vector_elements (vec, &handle, &len, &inc);
  224. result = scm_c_make_vector (len, SCM_UNDEFINED);
  225. dst = SCM_I_VECTOR_WELTS (result);
  226. for (i = 0; i < len; i++, src += inc)
  227. dst[i] = *src;
  228. scm_array_handle_release (&handle);
  229. return result;
  230. }
  231. #undef FUNC_NAME
  232. SCM_DEFINE (scm_vector_to_list, "vector->list", 1, 0, 0,
  233. (SCM v),
  234. "Return a newly allocated list composed of the elements of @var{v}.\n"
  235. "\n"
  236. "@lisp\n"
  237. "(vector->list '#(dah dah didah)) @result{} (dah dah didah)\n"
  238. "(list->vector '(dididit dah)) @result{} #(dididit dah)\n"
  239. "@end lisp")
  240. #define FUNC_NAME s_scm_vector_to_list
  241. {
  242. SCM res = SCM_EOL;
  243. const SCM *data;
  244. scm_t_array_handle handle;
  245. size_t i, count, len;
  246. ssize_t inc;
  247. data = scm_vector_elements (v, &handle, &len, &inc);
  248. for (i = (len - 1) * inc, count = 0;
  249. count < len;
  250. i -= inc, count++)
  251. res = scm_cons (data[i], res);
  252. scm_array_handle_release (&handle);
  253. return res;
  254. }
  255. #undef FUNC_NAME
  256. SCM_DEFINE (scm_vector_fill_x, "vector-fill!", 2, 0, 0,
  257. (SCM v, SCM fill),
  258. "Store @var{fill} in every position of @var{vector}. The value\n"
  259. "returned by @code{vector-fill!} is unspecified.")
  260. #define FUNC_NAME s_scm_vector_fill_x
  261. {
  262. scm_t_array_handle handle;
  263. SCM *data;
  264. size_t i, len;
  265. ssize_t inc;
  266. data = scm_vector_writable_elements (v, &handle, &len, &inc);
  267. for (i = 0; i < len; i += inc)
  268. data[i] = fill;
  269. scm_array_handle_release (&handle);
  270. return SCM_UNSPECIFIED;
  271. }
  272. #undef FUNC_NAME
  273. SCM
  274. scm_i_vector_equal_p (SCM x, SCM y)
  275. {
  276. long i;
  277. for (i = SCM_I_VECTOR_LENGTH (x) - 1; i >= 0; i--)
  278. if (scm_is_false (scm_equal_p (SCM_I_VECTOR_ELTS (x)[i],
  279. SCM_I_VECTOR_ELTS (y)[i])))
  280. return SCM_BOOL_F;
  281. return SCM_BOOL_T;
  282. }
  283. SCM_DEFINE (scm_vector_move_left_x, "vector-move-left!", 5, 0, 0,
  284. (SCM vec1, SCM start1, SCM end1, SCM vec2, SCM start2),
  285. "Copy elements from @var{vec1}, positions @var{start1} to @var{end1},\n"
  286. "to @var{vec2} starting at position @var{start2}. @var{start1} and\n"
  287. "@var{start2} are inclusive indices; @var{end1} is exclusive.\n\n"
  288. "@code{vector-move-left!} copies elements in leftmost order.\n"
  289. "Therefore, in the case where @var{vec1} and @var{vec2} refer to the\n"
  290. "same vector, @code{vector-move-left!} is usually appropriate when\n"
  291. "@var{start1} is greater than @var{start2}.")
  292. #define FUNC_NAME s_scm_vector_move_left_x
  293. {
  294. scm_t_array_handle handle1, handle2;
  295. const SCM *elts1;
  296. SCM *elts2;
  297. size_t len1, len2;
  298. ssize_t inc1, inc2;
  299. size_t i, j, e;
  300. elts1 = scm_vector_elements (vec1, &handle1, &len1, &inc1);
  301. elts2 = scm_vector_writable_elements (vec2, &handle2, &len2, &inc2);
  302. i = scm_to_unsigned_integer (start1, 0, len1);
  303. e = scm_to_unsigned_integer (end1, i, len1);
  304. SCM_ASSERT_RANGE (SCM_ARG3, end1, (e-i) <= len2);
  305. j = scm_to_unsigned_integer (start2, 0, len2);
  306. SCM_ASSERT_RANGE (SCM_ARG5, start2, j <= len2 - (e - i));
  307. i *= inc1;
  308. e *= inc1;
  309. j *= inc2;
  310. for (; i < e; i += inc1, j += inc2)
  311. elts2[j] = elts1[i];
  312. scm_array_handle_release (&handle2);
  313. scm_array_handle_release (&handle1);
  314. return SCM_UNSPECIFIED;
  315. }
  316. #undef FUNC_NAME
  317. SCM_DEFINE (scm_vector_move_right_x, "vector-move-right!", 5, 0, 0,
  318. (SCM vec1, SCM start1, SCM end1, SCM vec2, SCM start2),
  319. "Copy elements from @var{vec1}, positions @var{start1} to @var{end1},\n"
  320. "to @var{vec2} starting at position @var{start2}. @var{start1} and\n"
  321. "@var{start2} are inclusive indices; @var{end1} is exclusive.\n\n"
  322. "@code{vector-move-right!} copies elements in rightmost order.\n"
  323. "Therefore, in the case where @var{vec1} and @var{vec2} refer to the\n"
  324. "same vector, @code{vector-move-right!} is usually appropriate when\n"
  325. "@var{start1} is less than @var{start2}.")
  326. #define FUNC_NAME s_scm_vector_move_right_x
  327. {
  328. scm_t_array_handle handle1, handle2;
  329. const SCM *elts1;
  330. SCM *elts2;
  331. size_t len1, len2;
  332. ssize_t inc1, inc2;
  333. size_t i, j, e;
  334. elts1 = scm_vector_elements (vec1, &handle1, &len1, &inc1);
  335. elts2 = scm_vector_writable_elements (vec2, &handle2, &len2, &inc2);
  336. i = scm_to_unsigned_integer (start1, 0, len1);
  337. e = scm_to_unsigned_integer (end1, i, len1);
  338. SCM_ASSERT_RANGE (SCM_ARG3, end1, (e-i) <= len2);
  339. j = scm_to_unsigned_integer (start2, 0, len2);
  340. SCM_ASSERT_RANGE (SCM_ARG5, start2, j <= len2 - (e - i));
  341. j += (e - i);
  342. i *= inc1;
  343. e *= inc1;
  344. j *= inc2;
  345. while (i < e)
  346. {
  347. e -= inc1;
  348. j -= inc2;
  349. elts2[j] = elts1[e];
  350. }
  351. scm_array_handle_release (&handle2);
  352. scm_array_handle_release (&handle1);
  353. return SCM_UNSPECIFIED;
  354. }
  355. #undef FUNC_NAME
  356. SCM_VECTOR_IMPLEMENTATION (SCM_ARRAY_ELEMENT_TYPE_SCM, scm_make_vector)
  357. void
  358. scm_init_vectors ()
  359. {
  360. #include "vectors.x"
  361. }