vectors.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /* Copyright (C) 1995, 1996, 1998, 1999, 2000, 2002 Free Software Foundation, Inc.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2, or (at your option)
  6. * any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; see the file COPYING. If not, write to
  15. * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. * Boston, MA 02111-1307 USA
  17. *
  18. * As a special exception, the Free Software Foundation gives permission
  19. * for additional uses of the text contained in its release of GUILE.
  20. *
  21. * The exception is that, if you link the GUILE library with other files
  22. * to produce an executable, this does not by itself cause the
  23. * resulting executable to be covered by the GNU General Public License.
  24. * Your use of that executable is in no way restricted on account of
  25. * linking the GUILE library code into it.
  26. *
  27. * This exception does not however invalidate any other reasons why
  28. * the executable file might be covered by the GNU General Public License.
  29. *
  30. * This exception applies only to the code released by the
  31. * Free Software Foundation under the name GUILE. If you copy
  32. * code from other Free Software Foundation releases into a copy of
  33. * GUILE, as the General Public License permits, the exception does
  34. * not apply to the code that you add in this way. To avoid misleading
  35. * anyone as to the status of such modified files, you must delete
  36. * this exception notice from them.
  37. *
  38. * If you write modifications of your own for GUILE, it is your choice
  39. * whether to permit this exception to apply to your modifications.
  40. * If you do not wish that, delete this exception notice. */
  41. #include <stdio.h>
  42. #include "libguile/_scm.h"
  43. #include "libguile/eq.h"
  44. #include "libguile/root.h"
  45. #include "libguile/strings.h"
  46. #include "libguile/validate.h"
  47. #include "libguile/vectors.h"
  48. #include "libguile/unif.h"
  49. /*
  50. * This complicates things too much if allowed on any array.
  51. * C code can safely call it on arrays known to be used in a single
  52. * threaded manner.
  53. *
  54. * SCM_REGISTER_PROC(s_vector_set_length_x, "vector-set-length!", 2, 0, 0, scm_vector_set_length_x);
  55. */
  56. static char s_vector_set_length_x[] = "vector-set-length!";
  57. SCM
  58. scm_vector_set_length_x (SCM vect, SCM len)
  59. {
  60. long l;
  61. scm_sizet siz;
  62. scm_sizet sz;
  63. l = SCM_INUM (len);
  64. SCM_ASRTGO (SCM_NIMP (vect), badarg1);
  65. #ifdef HAVE_ARRAYS
  66. if (SCM_TYP7 (vect) == scm_tc7_bvect)
  67. {
  68. l = (l + SCM_LONG_BIT - 1) / SCM_LONG_BIT;
  69. }
  70. sz = scm_uniform_element_size (vect);
  71. if (sz == 0)
  72. #endif
  73. switch (SCM_TYP7 (vect))
  74. {
  75. default:
  76. badarg1: scm_wta (vect, (char *) SCM_ARG1, s_vector_set_length_x);
  77. case scm_tc7_string:
  78. SCM_ASRTGO (!SCM_EQ_P (vect, scm_nullstr), badarg1);
  79. sz = sizeof (char);
  80. l++;
  81. break;
  82. case scm_tc7_vector:
  83. case scm_tc7_wvect:
  84. SCM_ASRTGO (!SCM_EQ_P (vect, scm_nullvect), badarg1);
  85. sz = sizeof (SCM);
  86. break;
  87. }
  88. SCM_ASSERT (SCM_INUMP (len), len, SCM_ARG2, s_vector_set_length_x);
  89. if (!l)
  90. l = 1L;
  91. siz = l * sz;
  92. if (siz != l * sz)
  93. scm_wta (SCM_MAKINUM (l * sz), (char *) SCM_NALLOC, s_vector_set_length_x);
  94. SCM_REDEFER_INTS;
  95. SCM_SETCHARS (vect,
  96. ((char *)
  97. scm_must_realloc (SCM_CHARS (vect),
  98. (long) SCM_LENGTH (vect) * sz,
  99. (long) siz,
  100. s_vector_set_length_x)));
  101. if (SCM_VECTORP (vect))
  102. {
  103. sz = SCM_LENGTH (vect);
  104. while (l > sz)
  105. SCM_VELTS (vect)[--l] = SCM_UNSPECIFIED;
  106. }
  107. else if (SCM_STRINGP (vect))
  108. SCM_CHARS (vect)[l - 1] = 0;
  109. SCM_SETLENGTH (vect, SCM_INUM (len), SCM_TYP7 (vect));
  110. SCM_REALLOW_INTS;
  111. return vect;
  112. }
  113. SCM_DEFINE (scm_vector_p, "vector?", 1, 0, 0,
  114. (SCM obj),
  115. "Returns @t{#t} if @var{obj} is a vector, otherwise returns @t{#f}. (r5rs)")
  116. #define FUNC_NAME s_scm_vector_p
  117. {
  118. if (SCM_IMP (obj))
  119. return SCM_BOOL_F;
  120. return SCM_BOOL (SCM_VECTORP (obj));
  121. }
  122. #undef FUNC_NAME
  123. SCM_GPROC (s_vector_length, "vector-length", 1, 0, 0, scm_vector_length, g_vector_length);
  124. /* Returns the number of elements in @var{vector} as an exact integer. (r5rs) */
  125. SCM
  126. scm_vector_length (SCM v)
  127. {
  128. SCM_GASSERT1 (SCM_VECTORP(v),
  129. g_vector_length, v, SCM_ARG1, s_vector_length);
  130. return SCM_MAKINUM (SCM_LENGTH (v));
  131. }
  132. SCM_REGISTER_PROC (s_list_to_vector, "list->vector", 1, 0, 0, scm_vector);
  133. /*
  134. "@samp{List->vector} returns a newly\n"
  135. "created vector initialized to the elements of the list @var{list}.\n\n"
  136. "@format\n"
  137. "@t{(vector->list '#(dah dah didah))\n"
  138. "=> (dah dah didah)\n"
  139. "list->vector '(dididit dah))\n"
  140. "=> #(dididit dah)\n"
  141. "}\n"
  142. "@end format")
  143. */
  144. SCM_DEFINE (scm_vector, "vector", 0, 0, 1,
  145. (SCM l),
  146. "Returns a newly allocated vector whose elements contain the given\n"
  147. "arguments. Analogous to @samp{list}. (r5rs)\n\n"
  148. "@format\n"
  149. "@t{(vector 'a 'b 'c) ==> #(a b c) }\n"
  150. "@end format")
  151. #define FUNC_NAME s_scm_vector
  152. {
  153. SCM res;
  154. register SCM *data;
  155. int i;
  156. SCM_VALIDATE_LIST_COPYLEN (1,l,i);
  157. res = scm_make_vector (SCM_MAKINUM (i), SCM_UNSPECIFIED);
  158. data = SCM_VELTS (res);
  159. for(; i && SCM_NIMP(l); --i, l = SCM_CDR (l))
  160. *data++ = SCM_CAR (l);
  161. return res;
  162. }
  163. #undef FUNC_NAME
  164. SCM_GPROC (s_vector_ref, "vector-ref", 2, 0, 0, scm_vector_ref, g_vector_ref);
  165. /*
  166. "@var{k} must be a valid index of @var{vector}.\n"
  167. "@samp{Vector-ref} returns the contents of element @var{k} of\n"
  168. "@var{vector}.\n\n"
  169. "@format\n"
  170. "@t{(vector-ref '#(1 1 2 3 5 8 13 21)\n"
  171. " 5)\n"
  172. " ==> 8\n"
  173. "(vector-ref '#(1 1 2 3 5 8 13 21)\n"
  174. " (let ((i (round (* 2 (acos -1)))))\n"
  175. " (if (inexact? i)\n"
  176. " (inexact->exact i)\n"
  177. " i))) \n"
  178. " ==> 13\n"
  179. "}\n"
  180. "@end format"
  181. */
  182. SCM
  183. scm_vector_ref (SCM v, SCM k)
  184. {
  185. SCM_GASSERT2 (SCM_VECTORP (v),
  186. g_vector_ref, v, k, SCM_ARG1, s_vector_ref);
  187. SCM_GASSERT2 (SCM_INUMP (k),
  188. g_vector_ref, v, k, SCM_ARG2, s_vector_ref);
  189. SCM_ASSERT (SCM_INUM (k) < SCM_LENGTH (v) && SCM_INUM (k) >= 0,
  190. k, SCM_OUTOFRANGE, s_vector_ref);
  191. return SCM_VELTS (v)[(long) SCM_INUM (k)];
  192. }
  193. SCM_GPROC (s_vector_set_x, "vector-set!", 3, 0, 0, scm_vector_set_x, g_vector_set_x);
  194. /* (r5rs)
  195. @var{k} must be a valid index of @var{vector}.
  196. @samp{Vector-set!} stores @var{obj} in element @var{k} of @var{vector}.
  197. The value returned by @samp{vector-set!} is unspecified.
  198. @c <!>
  199. @format
  200. @t{(let ((vec (vector 0 '(2 2 2 2) "Anna")))
  201. (vector-set! vec 1 '("Sue" "Sue"))
  202. vec)
  203. ==> #(0 ("Sue" "Sue") "Anna")
  204. (vector-set! '#(0 1 2) 1 "doe")
  205. ==> @emph{error} ; constant vector
  206. }
  207. @end format
  208. */
  209. SCM
  210. scm_vector_set_x (SCM v, SCM k, SCM obj)
  211. {
  212. SCM_GASSERTn (SCM_VECTORP (v),
  213. g_vector_set_x, SCM_LIST3 (v, k, obj),
  214. SCM_ARG1, s_vector_set_x);
  215. SCM_GASSERTn (SCM_INUMP (k),
  216. g_vector_set_x, SCM_LIST3 (v, k, obj),
  217. SCM_ARG2, s_vector_set_x);
  218. SCM_ASSERT ((SCM_INUM (k) < SCM_LENGTH (v)) && (SCM_INUM (k) >= 0),
  219. k, SCM_OUTOFRANGE, s_vector_set_x);
  220. SCM_VELTS(v)[(long) SCM_INUM(k)] = obj;
  221. return SCM_UNSPECIFIED;
  222. }
  223. SCM_DEFINE (scm_make_vector, "make-vector", 1, 1, 0,
  224. (SCM k, SCM fill),
  225. "Returns a newly allocated vector of @var{k} elements. If a second\n"
  226. "argument is given, then each element is initialized to @var{fill}.\n"
  227. "Otherwise the initial contents of each element is unspecified. (r5rs)")
  228. #define FUNC_NAME s_scm_make_vector
  229. {
  230. SCM v;
  231. register long i;
  232. register long j;
  233. register SCM *velts;
  234. SCM_VALIDATE_INUM_MIN (1,k,0);
  235. if (SCM_UNBNDP(fill))
  236. fill = SCM_UNSPECIFIED;
  237. i = SCM_INUM(k);
  238. SCM_NEWCELL(v);
  239. SCM_DEFER_INTS;
  240. SCM_SETCHARS(v, scm_must_malloc(i?(long)(i*sizeof(SCM)):1L, FUNC_NAME));
  241. SCM_SETLENGTH(v, i, scm_tc7_vector);
  242. velts = SCM_VELTS(v);
  243. j = 0;
  244. while(--i >= j) (velts)[i] = fill;
  245. SCM_ALLOW_INTS;
  246. return v;
  247. }
  248. #undef FUNC_NAME
  249. SCM_DEFINE (scm_vector_to_list, "vector->list", 1, 0, 0,
  250. (SCM v),
  251. "@samp{Vector->list} returns a newly allocated list of the objects contained\n"
  252. "in the elements of @var{vector}. (r5rs)\n\n"
  253. "@format\n"
  254. "@t{(vector->list '#(dah dah didah))\n"
  255. "=> (dah dah didah)\n"
  256. "list->vector '(dididit dah))\n"
  257. "=> #(dididit dah)\n"
  258. "}\n"
  259. "@end format")
  260. #define FUNC_NAME s_scm_vector_to_list
  261. {
  262. SCM res = SCM_EOL;
  263. long i;
  264. SCM *data;
  265. SCM_VALIDATE_VECTOR (1,v);
  266. data = SCM_VELTS(v);
  267. for(i = SCM_LENGTH(v)-1;i >= 0;i--) res = scm_cons(data[i], res);
  268. return res;
  269. }
  270. #undef FUNC_NAME
  271. SCM_DEFINE (scm_vector_fill_x, "vector-fill!", 2, 0, 0,
  272. (SCM v, SCM fill_x),
  273. "Stores @var{fill} in every element of @var{vector}.\n"
  274. "The value returned by @samp{vector-fill!} is unspecified. (r5rs)")
  275. #define FUNC_NAME s_scm_vector_fill_x
  276. {
  277. register long i;
  278. register SCM *data;
  279. SCM_VALIDATE_VECTOR (1,v);
  280. data = SCM_VELTS(v);
  281. for(i = SCM_LENGTH(v) - 1; i >= 0; i--)
  282. data[i] = fill_x;
  283. return SCM_UNSPECIFIED;
  284. }
  285. #undef FUNC_NAME
  286. SCM
  287. scm_vector_equal_p(SCM x, SCM y)
  288. {
  289. long i;
  290. for(i = SCM_LENGTH(x)-1;i >= 0;i--)
  291. if (SCM_FALSEP(scm_equal_p(SCM_VELTS(x)[i], SCM_VELTS(y)[i])))
  292. return SCM_BOOL_F;
  293. return SCM_BOOL_T;
  294. }
  295. SCM_DEFINE (scm_vector_move_left_x, "vector-move-left!", 5, 0, 0,
  296. (SCM vec1, SCM start1, SCM end1, SCM vec2, SCM start2),
  297. "Vector version of @code{substring-move-left!}.")
  298. #define FUNC_NAME s_scm_vector_move_left_x
  299. {
  300. long i;
  301. long j;
  302. long e;
  303. SCM_VALIDATE_VECTOR (1,vec1);
  304. SCM_VALIDATE_INUM_COPY (2,start1,i);
  305. SCM_VALIDATE_INUM_COPY (3,end1,e);
  306. SCM_VALIDATE_VECTOR (4,vec2);
  307. SCM_VALIDATE_INUM_COPY (5,start2,j);
  308. SCM_ASSERT (i <= SCM_LENGTH (vec1) && i >= 0, start1, SCM_OUTOFRANGE, FUNC_NAME);
  309. SCM_ASSERT (j <= SCM_LENGTH (vec2) && j >= 0, start2, SCM_OUTOFRANGE, FUNC_NAME);
  310. SCM_ASSERT (e <= SCM_LENGTH (vec1) && e >= 0, end1, SCM_OUTOFRANGE, FUNC_NAME);
  311. SCM_ASSERT (e-i+j <= SCM_LENGTH (vec2), start2, SCM_OUTOFRANGE, FUNC_NAME);
  312. while (i<e) SCM_VELTS (vec2)[j++] = SCM_VELTS (vec1)[i++];
  313. return SCM_UNSPECIFIED;
  314. }
  315. #undef FUNC_NAME
  316. SCM_DEFINE (scm_vector_move_right_x, "vector-move-right!", 5, 0, 0,
  317. (SCM vec1, SCM start1, SCM end1, SCM vec2, SCM start2),
  318. "Vector version of @code{substring-move-right!}.")
  319. #define FUNC_NAME s_scm_vector_move_right_x
  320. {
  321. long i;
  322. long j;
  323. long e;
  324. SCM_VALIDATE_VECTOR (1,vec1);
  325. SCM_VALIDATE_INUM_COPY (2,start1,i);
  326. SCM_VALIDATE_INUM_COPY (3,end1,e);
  327. SCM_VALIDATE_VECTOR (4,vec2);
  328. SCM_VALIDATE_INUM_COPY (5,start2,j);
  329. SCM_ASSERT (i <= SCM_LENGTH (vec1) && i >= 0, start1, SCM_OUTOFRANGE, FUNC_NAME);
  330. SCM_ASSERT (j <= SCM_LENGTH (vec2) && j >= 0, start2, SCM_OUTOFRANGE, FUNC_NAME);
  331. SCM_ASSERT (e <= SCM_LENGTH (vec1) && e >= 0, end1, SCM_OUTOFRANGE, FUNC_NAME);
  332. j = e - i + j;
  333. SCM_ASSERT (j <= SCM_LENGTH (vec2), start2, SCM_OUTOFRANGE, FUNC_NAME);
  334. while (i < e)
  335. SCM_VELTS (vec2)[--j] = SCM_VELTS (vec1)[--e];
  336. return SCM_UNSPECIFIED;
  337. }
  338. #undef FUNC_NAME
  339. void
  340. scm_init_vectors ()
  341. {
  342. #include "libguile/vectors.x"
  343. /*
  344. scm_make_subr (s_resizuve, scm_tc7_subr_2, scm_vector_set_length_x); */
  345. }
  346. /*
  347. Local Variables:
  348. c-file-style: "gnu"
  349. End:
  350. */