vectors.c 15 KB

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