1 /* Copyright (C) 1995,1996,1998,1999,2000,2001 Free Software Foundation, Inc.
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
8 * This library 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 GNU
11 * Lesser General Public License for more details.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "libguile/_scm.h"
22 #include "libguile/eq.h"
23 #include "libguile/root.h"
24 #include "libguile/strings.h"
25 #include "libguile/lang.h"
27 #include "libguile/validate.h"
28 #include "libguile/vectors.h"
29 #include "libguile/unif.h"
30 #include "libguile/srfi-4.h"
31 #include "libguile/strings.h"
32 #include "libguile/srfi-13.h"
36 SCM_DEFINE (scm_vector_p
, "vector?", 1, 0, 0,
38 "Return @code{#t} if @var{obj} is a vector, otherwise return\n"
40 #define FUNC_NAME s_scm_vector_p
42 return scm_from_bool (SCM_VECTORP (obj
));
47 scm_is_vector (SCM obj
)
49 return SCM_VECTORP (obj
);
52 SCM_GPROC (s_vector_length
, "vector-length", 1, 0, 0, scm_vector_length
, g_vector_length
);
53 /* Returns the number of elements in @var{vector} as an exact integer. */
55 scm_vector_length (SCM v
)
57 SCM_GASSERT1 (SCM_VECTORP(v
),
58 g_vector_length
, v
, SCM_ARG1
, s_vector_length
);
59 return scm_from_size_t (SCM_VECTOR_LENGTH (v
));
63 scm_c_vector_length (SCM v
)
66 return SCM_VECTOR_LENGTH (v
);
69 /* Call primitive generic to get an error or maybe dispatch to a
72 return scm_to_size_t (scm_vector_length (v
));
76 SCM_REGISTER_PROC (s_list_to_vector
, "list->vector", 1, 0, 0, scm_vector
);
78 "Return a newly created vector initialized to the elements of"
79 "the list @var{list}.\n\n"
81 "(vector->list '#(dah dah didah)) @result{} (dah dah didah)\n"
82 "(list->vector '(dididit dah)) @result{} #(dididit dah)\n"
85 SCM_DEFINE (scm_vector
, "vector", 0, 0, 1,
87 "@deffnx {Scheme Procedure} list->vector l\n"
88 "Return a newly allocated vector composed of the\n"
89 "given arguments. Analogous to @code{list}.\n"
92 "(vector 'a 'b 'c) @result{} #(a b c)\n"
94 #define FUNC_NAME s_scm_vector
100 /* Dirk:FIXME:: In case of multiple threads, the list might get corrupted
101 while the vector is being created. */
102 SCM_VALIDATE_LIST_COPYLEN (1, l
, i
);
103 res
= scm_c_make_vector (i
, SCM_UNSPECIFIED
);
106 this code doesn't alloc. -- accessing RES is safe.
108 data
= SCM_WRITABLE_VELTS (res
);
109 while (!SCM_NULL_OR_NIL_P (l
))
111 *data
++ = SCM_CAR (l
);
119 SCM_GPROC (s_vector_ref
, "vector-ref", 2, 0, 0, scm_vector_ref
, g_vector_ref
);
122 "@var{k} must be a valid index of @var{vector}.\n"
123 "@samp{Vector-ref} returns the contents of element @var{k} of\n"
126 "(vector-ref '#(1 1 2 3 5 8 13 21) 5) @result{} 8\n"
127 "(vector-ref '#(1 1 2 3 5 8 13 21)\n"
128 " (let ((i (round (* 2 (acos -1)))))\n"
129 " (if (inexact? i)\n"
130 " (inexact->exact i)\n"
131 " i))) @result{} 13\n"
136 scm_vector_ref (SCM v
, SCM k
)
137 #define FUNC_NAME s_vector_ref
139 SCM_GASSERT2 (SCM_VECTORP (v
),
140 g_vector_ref
, v
, k
, SCM_ARG1
, s_vector_ref
);
141 SCM_GASSERT2 (SCM_I_INUMP (k
),
142 g_vector_ref
, v
, k
, SCM_ARG2
, s_vector_ref
);
143 SCM_ASSERT_RANGE (2, k
,
144 SCM_I_INUM (k
) < SCM_VECTOR_LENGTH (v
)
145 && SCM_I_INUM (k
) >= 0);
146 return SCM_VELTS (v
)[(long) SCM_I_INUM (k
)];
151 scm_c_vector_ref (SCM v
, size_t k
)
153 if (SCM_VECTORP (v
) && k
< SCM_VECTOR_LENGTH (v
))
154 return SCM_VECTOR_REF (v
, k
);
157 /* Call primitive generic to get an error or maybe dispatch to a
160 return scm_vector_ref (v
, scm_from_size_t (k
));
164 SCM_GPROC (s_vector_set_x
, "vector-set!", 3, 0, 0, scm_vector_set_x
, g_vector_set_x
);
166 /* "@var{k} must be a valid index of @var{vector}.\n"
167 "@code{Vector-set!} stores @var{obj} in element @var{k} of @var{vector}.\n"
168 "The value returned by @samp{vector-set!} is unspecified.\n"
170 "(let ((vec (vector 0 '(2 2 2 2) "Anna")))\n"
171 " (vector-set! vec 1 '("Sue" "Sue"))\n"
172 " vec) @result{} #(0 ("Sue" "Sue") "Anna")\n"
173 "(vector-set! '#(0 1 2) 1 "doe") @result{} @emph{error} ; constant vector\n"
178 scm_vector_set_x (SCM v
, SCM k
, SCM obj
)
179 #define FUNC_NAME s_vector_set_x
181 SCM_GASSERTn (SCM_VECTORP (v
),
182 g_vector_set_x
, scm_list_3 (v
, k
, obj
),
183 SCM_ARG1
, s_vector_set_x
);
184 SCM_GASSERTn (SCM_I_INUMP (k
),
185 g_vector_set_x
, scm_list_3 (v
, k
, obj
),
186 SCM_ARG2
, s_vector_set_x
);
187 SCM_ASSERT_RANGE (2, k
,
188 SCM_I_INUM (k
) < SCM_VECTOR_LENGTH (v
)
189 && SCM_I_INUM (k
) >= 0);
190 SCM_VECTOR_SET (v
, (long) SCM_I_INUM(k
), obj
);
191 return SCM_UNSPECIFIED
;
196 scm_c_vector_set_x (SCM v
, size_t k
, SCM obj
)
198 if (SCM_VECTORP (v
) && k
< SCM_VECTOR_LENGTH (v
))
200 SCM_VECTOR_SET (v
, k
, obj
);
201 return SCM_UNSPECIFIED
;
205 /* Call primitive generic to get an error or maybe dispatch to a
208 return scm_vector_set_x (v
, scm_from_size_t (k
), obj
);
212 SCM_DEFINE (scm_make_vector
, "make-vector", 1, 1, 0,
214 "Return a newly allocated vector of @var{k} elements. If a\n"
215 "second argument is given, then each position is initialized to\n"
216 "@var{fill}. Otherwise the initial contents of each position is\n"
218 #define FUNC_NAME s_scm_make_vector
220 size_t l
= scm_to_unsigned_integer (k
, 0, SCM_VECTOR_MAX_LENGTH
);
222 if (SCM_UNBNDP (fill
))
223 fill
= SCM_UNSPECIFIED
;
225 return scm_c_make_vector (l
, fill
);
231 scm_c_make_vector (size_t k
, SCM fill
)
232 #define FUNC_NAME s_scm_make_vector
241 SCM_ASSERT_RANGE (1, scm_from_ulong (k
), k
<= SCM_VECTOR_MAX_LENGTH
);
243 base
= scm_gc_malloc (k
* sizeof (scm_t_bits
), "vector");
244 for (j
= 0; j
!= k
; ++j
)
245 base
[j
] = SCM_UNPACK (fill
);
250 v
= scm_cell (SCM_MAKE_VECTOR_TAG (k
, scm_tc7_vector
), (scm_t_bits
) base
);
251 scm_remember_upto_here_1 (fill
);
258 SCM_DEFINE (scm_vector_to_list
, "vector->list", 1, 0, 0,
260 "Return a newly allocated list composed of the elements of @var{v}.\n"
263 "(vector->list '#(dah dah didah)) @result{} (dah dah didah)\n"
264 "(list->vector '(dididit dah)) @result{} #(dididit dah)\n"
266 #define FUNC_NAME s_scm_vector_to_list
271 SCM_VALIDATE_VECTOR (1, v
);
273 for(i
= SCM_VECTOR_LENGTH(v
)-1;i
>= 0;i
--) res
= scm_cons(data
[i
], res
);
279 SCM_DEFINE (scm_vector_fill_x
, "vector-fill!", 2, 0, 0,
281 "Store @var{fill} in every position of @var{vector}. The value\n"
282 "returned by @code{vector-fill!} is unspecified.")
283 #define FUNC_NAME s_scm_vector_fill_x
286 SCM_VALIDATE_VECTOR (1, v
);
288 for(i
= SCM_VECTOR_LENGTH (v
) - 1; i
>= 0; i
--)
289 SCM_VECTOR_SET(v
, i
, fill
);
290 return SCM_UNSPECIFIED
;
296 scm_vector_equal_p(SCM x
, SCM y
)
299 for(i
= SCM_VECTOR_LENGTH (x
) - 1; i
>= 0; i
--)
300 if (scm_is_false (scm_equal_p (SCM_VELTS (x
)[i
], SCM_VELTS (y
)[i
])))
306 SCM_DEFINE (scm_vector_move_left_x
, "vector-move-left!", 5, 0, 0,
307 (SCM vec1
, SCM start1
, SCM end1
, SCM vec2
, SCM start2
),
308 "Copy elements from @var{vec1}, positions @var{start1} to @var{end1},\n"
309 "to @var{vec2} starting at position @var{start2}. @var{start1} and\n"
310 "@var{start2} are inclusive indices; @var{end1} is exclusive.\n\n"
311 "@code{vector-move-left!} copies elements in leftmost order.\n"
312 "Therefore, in the case where @var{vec1} and @var{vec2} refer to the\n"
313 "same vector, @code{vector-move-left!} is usually appropriate when\n"
314 "@var{start1} is greater than @var{start2}.")
315 #define FUNC_NAME s_scm_vector_move_left_x
319 SCM_VALIDATE_VECTOR (1, vec1
);
320 SCM_VALIDATE_VECTOR (4, vec2
);
321 i
= scm_to_unsigned_integer (start1
, 0, SCM_VECTOR_LENGTH(vec1
));
322 e
= scm_to_unsigned_integer (end1
, i
, SCM_VECTOR_LENGTH(vec1
));
323 j
= scm_to_unsigned_integer (start2
, 0, SCM_VECTOR_LENGTH(vec2
)-(i
-e
));
327 SCM_VECTOR_SET (vec2
, j
, SCM_VELTS (vec1
)[i
]);
332 return SCM_UNSPECIFIED
;
336 SCM_DEFINE (scm_vector_move_right_x
, "vector-move-right!", 5, 0, 0,
337 (SCM vec1
, SCM start1
, SCM end1
, SCM vec2
, SCM start2
),
338 "Copy elements from @var{vec1}, positions @var{start1} to @var{end1},\n"
339 "to @var{vec2} starting at position @var{start2}. @var{start1} and\n"
340 "@var{start2} are inclusive indices; @var{end1} is exclusive.\n\n"
341 "@code{vector-move-right!} copies elements in rightmost order.\n"
342 "Therefore, in the case where @var{vec1} and @var{vec2} refer to the\n"
343 "same vector, @code{vector-move-right!} is usually appropriate when\n"
344 "@var{start1} is less than @var{start2}.")
345 #define FUNC_NAME s_scm_vector_move_right_x
349 SCM_VALIDATE_VECTOR (1, vec1
);
350 SCM_VALIDATE_VECTOR (4, vec2
);
351 i
= scm_to_unsigned_integer (start1
, 0, SCM_VECTOR_LENGTH(vec1
));
352 e
= scm_to_unsigned_integer (end1
, i
, SCM_VECTOR_LENGTH(vec1
));
353 j
= scm_to_unsigned_integer (start2
, 0, SCM_VECTOR_LENGTH(vec2
)-(i
-e
));
360 SCM_VECTOR_SET (vec2
, j
, SCM_VELTS (vec1
)[e
]);
363 return SCM_UNSPECIFIED
;
368 /* Generalized vectors. */
371 scm_is_generalized_vector (SCM obj
)
373 return (scm_is_vector (obj
)
374 || scm_is_string (obj
)
375 || scm_is_bitvector (obj
)
376 || scm_is_uniform_vector (obj
));
379 SCM_DEFINE (scm_generalized_vector_p
, "generalized-vector?", 1, 0, 0,
381 "Return @code{#t} if @var{obj} is a vector, string,\n"
382 "bitvector, or uniform numeric vector.")
383 #define FUNC_NAME s_scm_generalized_vector_p
385 return scm_from_bool (scm_is_generalized_vector (obj
));
390 scm_c_generalized_vector_length (SCM v
)
392 if (scm_is_vector (v
))
393 return scm_c_vector_length (v
);
394 else if (scm_is_string (v
))
395 return scm_c_string_length (v
);
396 else if (scm_is_bitvector (v
))
397 return scm_c_bitvector_length (v
);
398 else if (scm_is_uniform_vector (v
))
399 return scm_c_uniform_vector_length (v
);
401 scm_wrong_type_arg_msg (NULL
, 0, v
, "generalized vector");
404 SCM_DEFINE (scm_generalized_vector_length
, "generalized-vector-length", 1, 0, 0,
406 "Return the length of the generalized vector @var{v}.")
407 #define FUNC_NAME s_scm_generalized_vector_length
409 return scm_from_size_t (scm_c_generalized_vector_length (v
));
414 scm_c_generalized_vector_ref (SCM v
, size_t idx
)
416 if (scm_is_vector (v
))
417 return scm_c_vector_ref (v
, idx
);
418 else if (scm_is_string (v
))
419 return scm_c_string_ref (v
, idx
);
420 else if (scm_is_bitvector (v
))
421 return scm_c_bitvector_ref (v
, idx
);
422 else if (scm_is_uniform_vector (v
))
423 return scm_c_uniform_vector_ref (v
, idx
);
425 scm_wrong_type_arg_msg (NULL
, 0, v
, "generalized vector");
428 SCM_DEFINE (scm_generalized_vector_ref
, "generalized-vector-ref", 2, 0, 0,
430 "Return the element at index @var{idx} of the\n"
431 "generalized vector @var{v}.")
432 #define FUNC_NAME s_scm_generalized_vector_ref
434 return scm_c_generalized_vector_ref (v
, scm_to_size_t (idx
));
439 scm_c_generalized_vector_set_x (SCM v
, size_t idx
, SCM val
)
441 if (scm_is_vector (v
))
442 scm_c_vector_set_x (v
, idx
, val
);
443 else if (scm_is_string (v
))
444 scm_c_string_set_x (v
, idx
, val
);
445 else if (scm_is_bitvector (v
))
446 scm_c_bitvector_set_x (v
, idx
, val
);
447 else if (scm_is_uniform_vector (v
))
448 scm_c_uniform_vector_set_x (v
, idx
, val
);
450 scm_wrong_type_arg_msg (NULL
, 0, v
, "generalized vector");
453 SCM_DEFINE (scm_generalized_vector_set_x
, "generalized-vector-set!", 3, 0, 0,
454 (SCM v
, SCM idx
, SCM val
),
455 "Set the element at index @var{idx} of the\n"
456 "generalized vector @var{v} to @var{val}.")
457 #define FUNC_NAME s_scm_generalized_vector_set_x
459 scm_c_generalized_vector_set_x (v
, scm_to_size_t (idx
), val
);
460 return SCM_UNSPECIFIED
;
464 SCM_DEFINE (scm_generalized_vector_to_list
, "generalized-vector->list", 1, 0, 0,
466 "Return a new list whose elements are the elements of the\n"
467 "generalized vector @var{v}.")
468 #define FUNC_NAME s_scm_generalized_vector_to_list
470 if (scm_is_vector (v
))
471 return scm_vector_to_list (v
);
472 else if (scm_is_string (v
))
473 return scm_string_to_list (v
);
474 else if (scm_is_bitvector (v
))
475 return scm_bitvector_to_list (v
);
476 else if (scm_is_uniform_vector (v
))
477 return scm_uniform_vector_to_list (v
);
479 scm_wrong_type_arg_msg (NULL
, 0, v
, "generalized vector");
487 scm_nullvect
= scm_c_make_vector (0, SCM_UNDEFINED
);
489 #include "libguile/vectors.x"