vectors.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /* Copyright (C) 1995,1996,1998,1999,2000,2001, 2006, 2008, 2009, 2010,
  2. * 2011, 2012 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/deprecation.h"
  34. #include "libguile/bdw-gc.h"
  35. #define VECTOR_MAX_LENGTH (SCM_T_BITS_MAX >> 8)
  36. int
  37. scm_is_vector (SCM obj)
  38. {
  39. if (SCM_I_IS_VECTOR (obj))
  40. return 1;
  41. if (SCM_I_ARRAYP (obj) && SCM_I_ARRAY_NDIM (obj) == 1)
  42. {
  43. SCM v = SCM_I_ARRAY_V (obj);
  44. return SCM_I_IS_VECTOR (v);
  45. }
  46. return 0;
  47. }
  48. int
  49. scm_is_simple_vector (SCM obj)
  50. {
  51. return SCM_I_IS_VECTOR (obj);
  52. }
  53. const SCM *
  54. scm_vector_elements (SCM vec, scm_t_array_handle *h,
  55. size_t *lenp, ssize_t *incp)
  56. {
  57. if (SCM_I_WVECTP (vec))
  58. scm_wrong_type_arg_msg (NULL, 0, vec, "non-weak vector");
  59. scm_generalized_vector_get_handle (vec, h);
  60. if (lenp)
  61. {
  62. scm_t_array_dim *dim = scm_array_handle_dims (h);
  63. *lenp = dim->ubnd - dim->lbnd + 1;
  64. *incp = dim->inc;
  65. }
  66. return scm_array_handle_elements (h);
  67. }
  68. SCM *
  69. scm_vector_writable_elements (SCM vec, scm_t_array_handle *h,
  70. size_t *lenp, ssize_t *incp)
  71. {
  72. if (SCM_I_WVECTP (vec))
  73. scm_wrong_type_arg_msg (NULL, 0, vec, "non-weak vector");
  74. scm_generalized_vector_get_handle (vec, h);
  75. if (lenp)
  76. {
  77. scm_t_array_dim *dim = scm_array_handle_dims (h);
  78. *lenp = dim->ubnd - dim->lbnd + 1;
  79. *incp = dim->inc;
  80. }
  81. return scm_array_handle_writable_elements (h);
  82. }
  83. SCM_DEFINE (scm_vector_p, "vector?", 1, 0, 0,
  84. (SCM obj),
  85. "Return @code{#t} if @var{obj} is a vector, otherwise return\n"
  86. "@code{#f}.")
  87. #define FUNC_NAME s_scm_vector_p
  88. {
  89. return scm_from_bool (scm_is_vector (obj));
  90. }
  91. #undef FUNC_NAME
  92. SCM_GPROC (s_vector_length, "vector-length", 1, 0, 0, scm_vector_length, g_vector_length);
  93. /* Returns the number of elements in @var{vector} as an exact integer. */
  94. SCM
  95. scm_vector_length (SCM v)
  96. {
  97. if (SCM_I_IS_VECTOR (v))
  98. return scm_from_size_t (SCM_I_VECTOR_LENGTH (v));
  99. else if (SCM_I_ARRAYP (v) && SCM_I_ARRAY_NDIM (v) == 1)
  100. {
  101. scm_t_array_dim *dim = SCM_I_ARRAY_DIMS (v);
  102. return scm_from_size_t (dim->ubnd - dim->lbnd + 1);
  103. }
  104. else
  105. return scm_wta_dispatch_1 (g_vector_length, v, 1, "vector-length");
  106. }
  107. size_t
  108. scm_c_vector_length (SCM v)
  109. {
  110. if (SCM_I_IS_VECTOR (v))
  111. return SCM_I_VECTOR_LENGTH (v);
  112. else
  113. return scm_to_size_t (scm_vector_length (v));
  114. }
  115. SCM_REGISTER_PROC (s_list_to_vector, "list->vector", 1, 0, 0, scm_vector);
  116. /*
  117. "Return a newly created vector initialized to the elements of"
  118. "the list @var{list}.\n\n"
  119. "@lisp\n"
  120. "(vector->list '#(dah dah didah)) @result{} (dah dah didah)\n"
  121. "(list->vector '(dididit dah)) @result{} #(dididit dah)\n"
  122. "@end lisp")
  123. */
  124. SCM_DEFINE (scm_vector, "vector", 0, 0, 1,
  125. (SCM l),
  126. "@deffnx {Scheme Procedure} list->vector l\n"
  127. "Return a newly allocated vector composed of the\n"
  128. "given arguments. Analogous to @code{list}.\n"
  129. "\n"
  130. "@lisp\n"
  131. "(vector 'a 'b 'c) @result{} #(a b c)\n"
  132. "@end lisp")
  133. #define FUNC_NAME s_scm_vector
  134. {
  135. SCM res;
  136. SCM *data;
  137. long i, len;
  138. scm_t_array_handle handle;
  139. SCM_VALIDATE_LIST_COPYLEN (1, l, len);
  140. res = scm_c_make_vector (len, SCM_UNSPECIFIED);
  141. data = scm_vector_writable_elements (res, &handle, NULL, NULL);
  142. i = 0;
  143. while (scm_is_pair (l) && i < len)
  144. {
  145. data[i] = SCM_CAR (l);
  146. l = SCM_CDR (l);
  147. i += 1;
  148. }
  149. scm_array_handle_release (&handle);
  150. return res;
  151. }
  152. #undef FUNC_NAME
  153. SCM_GPROC (s_vector_ref, "vector-ref", 2, 0, 0, scm_vector_ref, g_vector_ref);
  154. /*
  155. "@var{k} must be a valid index of @var{vector}.\n"
  156. "@samp{Vector-ref} returns the contents of element @var{k} of\n"
  157. "@var{vector}.\n\n"
  158. "@lisp\n"
  159. "(vector-ref '#(1 1 2 3 5 8 13 21) 5) @result{} 8\n"
  160. "(vector-ref '#(1 1 2 3 5 8 13 21)\n"
  161. " (let ((i (round (* 2 (acos -1)))))\n"
  162. " (if (inexact? i)\n"
  163. " (inexact->exact i)\n"
  164. " i))) @result{} 13\n"
  165. "@end lisp"
  166. */
  167. SCM
  168. scm_vector_ref (SCM v, SCM k)
  169. #define FUNC_NAME s_vector_ref
  170. {
  171. return scm_c_vector_ref (v, scm_to_size_t (k));
  172. }
  173. #undef FUNC_NAME
  174. SCM
  175. scm_c_vector_ref (SCM v, size_t k)
  176. {
  177. if (SCM_I_IS_NONWEAK_VECTOR (v))
  178. {
  179. if (k >= SCM_I_VECTOR_LENGTH (v))
  180. scm_out_of_range (NULL, scm_from_size_t (k));
  181. return SCM_SIMPLE_VECTOR_REF (v, k);
  182. }
  183. else if (SCM_I_WVECTP (v))
  184. return scm_c_weak_vector_ref (v, k);
  185. else if (SCM_I_ARRAYP (v) && SCM_I_ARRAY_NDIM (v) == 1)
  186. {
  187. scm_t_array_dim *dim = SCM_I_ARRAY_DIMS (v);
  188. SCM vv = SCM_I_ARRAY_V (v);
  189. k = SCM_I_ARRAY_BASE (v) + k*dim->inc;
  190. if (k >= dim->ubnd - dim->lbnd + 1)
  191. scm_out_of_range (NULL, scm_from_size_t (k));
  192. if (SCM_I_IS_NONWEAK_VECTOR (vv))
  193. return SCM_SIMPLE_VECTOR_REF (vv, k);
  194. else if (SCM_I_WVECTP (vv))
  195. return scm_c_weak_vector_ref (vv, k);
  196. else
  197. scm_wrong_type_arg_msg (NULL, 0, v, "non-uniform vector");
  198. }
  199. else
  200. return scm_wta_dispatch_2 (g_vector_ref, v, scm_from_size_t (k), 2,
  201. "vector-ref");
  202. }
  203. SCM_GPROC (s_vector_set_x, "vector-set!", 3, 0, 0, scm_vector_set_x, g_vector_set_x);
  204. /* "@var{k} must be a valid index of @var{vector}.\n"
  205. "@code{Vector-set!} stores @var{obj} in element @var{k} of @var{vector}.\n"
  206. "The value returned by @samp{vector-set!} is unspecified.\n"
  207. "@lisp\n"
  208. "(let ((vec (vector 0 '(2 2 2 2) "Anna")))\n"
  209. " (vector-set! vec 1 '("Sue" "Sue"))\n"
  210. " vec) @result{} #(0 ("Sue" "Sue") "Anna")\n"
  211. "(vector-set! '#(0 1 2) 1 "doe") @result{} @emph{error} ; constant vector\n"
  212. "@end lisp"
  213. */
  214. SCM
  215. scm_vector_set_x (SCM v, SCM k, SCM obj)
  216. #define FUNC_NAME s_vector_set_x
  217. {
  218. scm_c_vector_set_x (v, scm_to_size_t (k), obj);
  219. return SCM_UNSPECIFIED;
  220. }
  221. #undef FUNC_NAME
  222. void
  223. scm_c_vector_set_x (SCM v, size_t k, SCM obj)
  224. {
  225. if (SCM_I_IS_NONWEAK_VECTOR (v))
  226. {
  227. if (k >= SCM_I_VECTOR_LENGTH (v))
  228. scm_out_of_range (NULL, scm_from_size_t (k));
  229. SCM_SIMPLE_VECTOR_SET (v, k, obj);
  230. }
  231. else if (SCM_I_WVECTP (v))
  232. scm_c_weak_vector_set_x (v, k, obj);
  233. else if (SCM_I_ARRAYP (v) && SCM_I_ARRAY_NDIM (v) == 1)
  234. {
  235. scm_t_array_dim *dim = SCM_I_ARRAY_DIMS (v);
  236. SCM vv = SCM_I_ARRAY_V (v);
  237. k = SCM_I_ARRAY_BASE (v) + k*dim->inc;
  238. if (k >= dim->ubnd - dim->lbnd + 1)
  239. scm_out_of_range (NULL, scm_from_size_t (k));
  240. if (SCM_I_IS_NONWEAK_VECTOR (vv))
  241. SCM_SIMPLE_VECTOR_SET (vv, k, obj);
  242. else if (SCM_I_WVECTP (vv))
  243. scm_c_weak_vector_set_x (vv, k, obj);
  244. else
  245. scm_wrong_type_arg_msg (NULL, 0, v, "non-uniform vector");
  246. }
  247. else
  248. {
  249. if (SCM_UNPACK (g_vector_set_x))
  250. scm_wta_dispatch_n (g_vector_set_x,
  251. scm_list_3 (v, scm_from_size_t (k), obj),
  252. 0,
  253. "vector-set!");
  254. else
  255. scm_wrong_type_arg_msg (NULL, 0, v, "vector");
  256. }
  257. }
  258. SCM_DEFINE (scm_make_vector, "make-vector", 1, 1, 0,
  259. (SCM k, SCM fill),
  260. "Return a newly allocated vector of @var{k} elements. If a\n"
  261. "second argument is given, then each position is initialized to\n"
  262. "@var{fill}. Otherwise the initial contents of each position is\n"
  263. "unspecified.")
  264. #define FUNC_NAME s_scm_make_vector
  265. {
  266. size_t l = scm_to_unsigned_integer (k, 0, VECTOR_MAX_LENGTH);
  267. if (SCM_UNBNDP (fill))
  268. fill = SCM_UNSPECIFIED;
  269. return scm_c_make_vector (l, fill);
  270. }
  271. #undef FUNC_NAME
  272. SCM
  273. scm_c_make_vector (size_t k, SCM fill)
  274. #define FUNC_NAME s_scm_make_vector
  275. {
  276. SCM vector;
  277. unsigned long int j;
  278. SCM_ASSERT_RANGE (1, scm_from_size_t (k), k <= VECTOR_MAX_LENGTH);
  279. vector = scm_words ((k << 8) | scm_tc7_vector, k + 1);
  280. for (j = 0; j < k; ++j)
  281. SCM_SIMPLE_VECTOR_SET (vector, j, fill);
  282. return vector;
  283. }
  284. #undef FUNC_NAME
  285. SCM_DEFINE (scm_vector_copy, "vector-copy", 1, 0, 0,
  286. (SCM vec),
  287. "Return a copy of @var{vec}.")
  288. #define FUNC_NAME s_scm_vector_copy
  289. {
  290. scm_t_array_handle handle;
  291. size_t i, len;
  292. ssize_t inc;
  293. const SCM *src;
  294. SCM result, *dst;
  295. src = scm_vector_elements (vec, &handle, &len, &inc);
  296. result = scm_c_make_vector (len, SCM_UNDEFINED);
  297. dst = SCM_I_VECTOR_WELTS (result);
  298. for (i = 0; i < len; i++, src += inc)
  299. dst[i] = *src;
  300. scm_array_handle_release (&handle);
  301. return result;
  302. }
  303. #undef FUNC_NAME
  304. SCM_DEFINE (scm_vector_to_list, "vector->list", 1, 0, 0,
  305. (SCM v),
  306. "Return a newly allocated list composed of the elements of @var{v}.\n"
  307. "\n"
  308. "@lisp\n"
  309. "(vector->list '#(dah dah didah)) @result{} (dah dah didah)\n"
  310. "(list->vector '(dididit dah)) @result{} #(dididit dah)\n"
  311. "@end lisp")
  312. #define FUNC_NAME s_scm_vector_to_list
  313. {
  314. SCM res = SCM_EOL;
  315. const SCM *data;
  316. scm_t_array_handle handle;
  317. size_t i, count, len;
  318. ssize_t inc;
  319. data = scm_vector_elements (v, &handle, &len, &inc);
  320. for (i = (len - 1) * inc, count = 0;
  321. count < len;
  322. i -= inc, count++)
  323. res = scm_cons (data[i], res);
  324. scm_array_handle_release (&handle);
  325. return res;
  326. }
  327. #undef FUNC_NAME
  328. SCM_DEFINE (scm_vector_fill_x, "vector-fill!", 2, 0, 0,
  329. (SCM v, SCM fill),
  330. "Store @var{fill} in every position of @var{vector}. The value\n"
  331. "returned by @code{vector-fill!} is unspecified.")
  332. #define FUNC_NAME s_scm_vector_fill_x
  333. {
  334. scm_t_array_handle handle;
  335. SCM *data;
  336. size_t i, len;
  337. ssize_t inc;
  338. data = scm_vector_writable_elements (v, &handle, &len, &inc);
  339. for (i = 0; i < len; i += inc)
  340. data[i] = fill;
  341. scm_array_handle_release (&handle);
  342. return SCM_UNSPECIFIED;
  343. }
  344. #undef FUNC_NAME
  345. SCM
  346. scm_i_vector_equal_p (SCM x, SCM y)
  347. {
  348. long i;
  349. for (i = SCM_I_VECTOR_LENGTH (x) - 1; i >= 0; i--)
  350. if (scm_is_false (scm_equal_p (SCM_I_VECTOR_ELTS (x)[i],
  351. SCM_I_VECTOR_ELTS (y)[i])))
  352. return SCM_BOOL_F;
  353. return SCM_BOOL_T;
  354. }
  355. SCM_DEFINE (scm_vector_move_left_x, "vector-move-left!", 5, 0, 0,
  356. (SCM vec1, SCM start1, SCM end1, SCM vec2, SCM start2),
  357. "Copy elements from @var{vec1}, positions @var{start1} to @var{end1},\n"
  358. "to @var{vec2} starting at position @var{start2}. @var{start1} and\n"
  359. "@var{start2} are inclusive indices; @var{end1} is exclusive.\n\n"
  360. "@code{vector-move-left!} copies elements in leftmost order.\n"
  361. "Therefore, in the case where @var{vec1} and @var{vec2} refer to the\n"
  362. "same vector, @code{vector-move-left!} is usually appropriate when\n"
  363. "@var{start1} is greater than @var{start2}.")
  364. #define FUNC_NAME s_scm_vector_move_left_x
  365. {
  366. scm_t_array_handle handle1, handle2;
  367. const SCM *elts1;
  368. SCM *elts2;
  369. size_t len1, len2;
  370. ssize_t inc1, inc2;
  371. size_t i, j, e;
  372. elts1 = scm_vector_elements (vec1, &handle1, &len1, &inc1);
  373. elts2 = scm_vector_writable_elements (vec2, &handle2, &len2, &inc2);
  374. i = scm_to_unsigned_integer (start1, 0, len1);
  375. e = scm_to_unsigned_integer (end1, i, len1);
  376. SCM_ASSERT_RANGE (SCM_ARG3, end1, (e-i) <= len2);
  377. j = scm_to_unsigned_integer (start2, 0, len2);
  378. SCM_ASSERT_RANGE (SCM_ARG5, start2, j <= len2 - (e - i));
  379. i *= inc1;
  380. e *= inc1;
  381. j *= inc2;
  382. for (; i < e; i += inc1, j += inc2)
  383. elts2[j] = elts1[i];
  384. scm_array_handle_release (&handle2);
  385. scm_array_handle_release (&handle1);
  386. return SCM_UNSPECIFIED;
  387. }
  388. #undef FUNC_NAME
  389. SCM_DEFINE (scm_vector_move_right_x, "vector-move-right!", 5, 0, 0,
  390. (SCM vec1, SCM start1, SCM end1, SCM vec2, SCM start2),
  391. "Copy elements from @var{vec1}, positions @var{start1} to @var{end1},\n"
  392. "to @var{vec2} starting at position @var{start2}. @var{start1} and\n"
  393. "@var{start2} are inclusive indices; @var{end1} is exclusive.\n\n"
  394. "@code{vector-move-right!} copies elements in rightmost order.\n"
  395. "Therefore, in the case where @var{vec1} and @var{vec2} refer to the\n"
  396. "same vector, @code{vector-move-right!} is usually appropriate when\n"
  397. "@var{start1} is less than @var{start2}.")
  398. #define FUNC_NAME s_scm_vector_move_right_x
  399. {
  400. scm_t_array_handle handle1, handle2;
  401. const SCM *elts1;
  402. SCM *elts2;
  403. size_t len1, len2;
  404. ssize_t inc1, inc2;
  405. size_t i, j, e;
  406. elts1 = scm_vector_elements (vec1, &handle1, &len1, &inc1);
  407. elts2 = scm_vector_writable_elements (vec2, &handle2, &len2, &inc2);
  408. i = scm_to_unsigned_integer (start1, 0, len1);
  409. e = scm_to_unsigned_integer (end1, i, len1);
  410. SCM_ASSERT_RANGE (SCM_ARG3, end1, (e-i) <= len2);
  411. j = scm_to_unsigned_integer (start2, 0, len2);
  412. SCM_ASSERT_RANGE (SCM_ARG5, start2, j <= len2 - (e - i));
  413. j += (e - i);
  414. i *= inc1;
  415. e *= inc1;
  416. j *= inc2;
  417. while (i < e)
  418. {
  419. e -= inc1;
  420. j -= inc2;
  421. elts2[j] = elts1[e];
  422. }
  423. scm_array_handle_release (&handle2);
  424. scm_array_handle_release (&handle1);
  425. return SCM_UNSPECIFIED;
  426. }
  427. #undef FUNC_NAME
  428. static SCM
  429. vector_handle_ref (scm_t_array_handle *h, size_t idx)
  430. {
  431. if (idx > h->dims[0].ubnd)
  432. scm_out_of_range ("vector-handle-ref", scm_from_size_t (idx));
  433. return ((SCM*)h->elements)[idx];
  434. }
  435. static void
  436. vector_handle_set (scm_t_array_handle *h, size_t idx, SCM val)
  437. {
  438. if (idx > h->dims[0].ubnd)
  439. scm_out_of_range ("vector-handle-set!", scm_from_size_t (idx));
  440. ((SCM*)h->writable_elements)[idx] = val;
  441. }
  442. static void
  443. vector_get_handle (SCM v, scm_t_array_handle *h)
  444. {
  445. h->array = v;
  446. h->ndims = 1;
  447. h->dims = &h->dim0;
  448. h->dim0.lbnd = 0;
  449. h->dim0.ubnd = SCM_I_VECTOR_LENGTH (v) - 1;
  450. h->dim0.inc = 1;
  451. h->element_type = SCM_ARRAY_ELEMENT_TYPE_SCM;
  452. h->elements = h->writable_elements = SCM_I_VECTOR_WELTS (v);
  453. }
  454. /* the & ~2 allows catching scm_tc7_wvect as well. needs changing if you change
  455. tags.h. */
  456. SCM_ARRAY_IMPLEMENTATION (scm_tc7_vector, 0x7f & ~2,
  457. vector_handle_ref, vector_handle_set,
  458. vector_get_handle)
  459. SCM_VECTOR_IMPLEMENTATION (SCM_ARRAY_ELEMENT_TYPE_SCM, scm_make_vector)
  460. void
  461. scm_init_vectors ()
  462. {
  463. #include "libguile/vectors.x"
  464. }
  465. /*
  466. Local Variables:
  467. c-file-style: "gnu"
  468. End:
  469. */