vectors.c 12 KB

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