1 /* Copyright (C) 1995,1996,1998,1999,2000,2001, 2006, 2008, 2009 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 License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
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.
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
25 #include "libguile/_scm.h"
26 #include "libguile/eq.h"
27 #include "libguile/root.h"
28 #include "libguile/strings.h"
29 #include "libguile/lang.h"
31 #include "libguile/validate.h"
32 #include "libguile/vectors.h"
33 #include "libguile/unif.h"
34 #include "libguile/bytevectors.h"
35 #include "libguile/ramap.h"
36 #include "libguile/srfi-4.h"
37 #include "libguile/strings.h"
38 #include "libguile/srfi-13.h"
39 #include "libguile/dynwind.h"
40 #include "libguile/deprecation.h"
44 #define VECTOR_MAX_LENGTH (SCM_T_BITS_MAX >> 8)
47 scm_is_vector (SCM obj
)
49 if (SCM_I_IS_VECTOR (obj
))
51 if (SCM_I_ARRAYP (obj
) && SCM_I_ARRAY_NDIM (obj
) == 1)
53 SCM v
= SCM_I_ARRAY_V (obj
);
54 return SCM_I_IS_VECTOR (v
);
60 scm_is_simple_vector (SCM obj
)
62 return SCM_I_IS_VECTOR (obj
);
66 scm_vector_elements (SCM vec
, scm_t_array_handle
*h
,
67 size_t *lenp
, ssize_t
*incp
)
69 scm_generalized_vector_get_handle (vec
, h
);
72 scm_t_array_dim
*dim
= scm_array_handle_dims (h
);
73 *lenp
= dim
->ubnd
- dim
->lbnd
+ 1;
76 return scm_array_handle_elements (h
);
80 scm_vector_writable_elements (SCM vec
, scm_t_array_handle
*h
,
81 size_t *lenp
, ssize_t
*incp
)
83 scm_generalized_vector_get_handle (vec
, h
);
86 scm_t_array_dim
*dim
= scm_array_handle_dims (h
);
87 *lenp
= dim
->ubnd
- dim
->lbnd
+ 1;
90 return scm_array_handle_writable_elements (h
);
93 SCM_DEFINE (scm_vector_p
, "vector?", 1, 0, 0,
95 "Return @code{#t} if @var{obj} is a vector, otherwise return\n"
97 #define FUNC_NAME s_scm_vector_p
99 return scm_from_bool (scm_is_vector (obj
));
103 SCM_GPROC (s_vector_length
, "vector-length", 1, 0, 0, scm_vector_length
, g_vector_length
);
104 /* Returns the number of elements in @var{vector} as an exact integer. */
106 scm_vector_length (SCM v
)
108 if (SCM_I_IS_VECTOR (v
))
109 return scm_from_size_t (SCM_I_VECTOR_LENGTH (v
));
110 else if (SCM_I_ARRAYP (v
) && SCM_I_ARRAY_NDIM (v
) == 1)
112 scm_t_array_dim
*dim
= SCM_I_ARRAY_DIMS (v
);
113 return scm_from_size_t (dim
->ubnd
- dim
->lbnd
+ 1);
116 SCM_WTA_DISPATCH_1 (g_vector_length
, v
, 1, NULL
);
120 scm_c_vector_length (SCM v
)
122 if (SCM_I_IS_VECTOR (v
))
123 return SCM_I_VECTOR_LENGTH (v
);
125 return scm_to_size_t (scm_vector_length (v
));
128 SCM_REGISTER_PROC (s_list_to_vector
, "list->vector", 1, 0, 0, scm_vector
);
130 "Return a newly created vector initialized to the elements of"
131 "the list @var{list}.\n\n"
133 "(vector->list '#(dah dah didah)) @result{} (dah dah didah)\n"
134 "(list->vector '(dididit dah)) @result{} #(dididit dah)\n"
137 SCM_DEFINE (scm_vector
, "vector", 0, 0, 1,
139 "@deffnx {Scheme Procedure} list->vector l\n"
140 "Return a newly allocated vector composed of the\n"
141 "given arguments. Analogous to @code{list}.\n"
144 "(vector 'a 'b 'c) @result{} #(a b c)\n"
146 #define FUNC_NAME s_scm_vector
151 scm_t_array_handle handle
;
153 SCM_VALIDATE_LIST_COPYLEN (1, l
, len
);
155 res
= scm_c_make_vector (len
, SCM_UNSPECIFIED
);
156 data
= scm_vector_writable_elements (res
, &handle
, NULL
, NULL
);
158 while (scm_is_pair (l
) && i
< len
)
160 data
[i
] = SCM_CAR (l
);
165 scm_array_handle_release (&handle
);
171 SCM_GPROC (s_vector_ref
, "vector-ref", 2, 0, 0, scm_vector_ref
, g_vector_ref
);
174 "@var{k} must be a valid index of @var{vector}.\n"
175 "@samp{Vector-ref} returns the contents of element @var{k} of\n"
178 "(vector-ref '#(1 1 2 3 5 8 13 21) 5) @result{} 8\n"
179 "(vector-ref '#(1 1 2 3 5 8 13 21)\n"
180 " (let ((i (round (* 2 (acos -1)))))\n"
181 " (if (inexact? i)\n"
182 " (inexact->exact i)\n"
183 " i))) @result{} 13\n"
188 scm_vector_ref (SCM v
, SCM k
)
189 #define FUNC_NAME s_vector_ref
191 return scm_c_vector_ref (v
, scm_to_size_t (k
));
196 scm_c_vector_ref (SCM v
, size_t k
)
198 if (SCM_I_IS_VECTOR (v
))
200 if (k
>= SCM_I_VECTOR_LENGTH (v
))
201 scm_out_of_range (NULL
, scm_from_size_t (k
));
202 return (SCM_I_VECTOR_ELTS(v
))[k
];
204 else if (SCM_I_ARRAYP (v
) && SCM_I_ARRAY_NDIM (v
) == 1)
206 scm_t_array_dim
*dim
= SCM_I_ARRAY_DIMS (v
);
207 SCM vv
= SCM_I_ARRAY_V (v
);
208 if (SCM_I_IS_VECTOR (vv
))
210 if (k
>= dim
->ubnd
- dim
->lbnd
+ 1)
211 scm_out_of_range (NULL
, scm_from_size_t (k
));
212 k
= SCM_I_ARRAY_BASE (v
) + k
*dim
->inc
;
213 return (SCM_I_VECTOR_ELTS (vv
))[k
];
215 scm_wrong_type_arg_msg (NULL
, 0, v
, "non-uniform vector");
218 SCM_WTA_DISPATCH_2 (g_vector_ref
, v
, scm_from_size_t (k
), 2, NULL
);
221 SCM_GPROC (s_vector_set_x
, "vector-set!", 3, 0, 0, scm_vector_set_x
, g_vector_set_x
);
223 /* "@var{k} must be a valid index of @var{vector}.\n"
224 "@code{Vector-set!} stores @var{obj} in element @var{k} of @var{vector}.\n"
225 "The value returned by @samp{vector-set!} is unspecified.\n"
227 "(let ((vec (vector 0 '(2 2 2 2) "Anna")))\n"
228 " (vector-set! vec 1 '("Sue" "Sue"))\n"
229 " vec) @result{} #(0 ("Sue" "Sue") "Anna")\n"
230 "(vector-set! '#(0 1 2) 1 "doe") @result{} @emph{error} ; constant vector\n"
235 scm_vector_set_x (SCM v
, SCM k
, SCM obj
)
236 #define FUNC_NAME s_vector_set_x
238 scm_c_vector_set_x (v
, scm_to_size_t (k
), obj
);
239 return SCM_UNSPECIFIED
;
244 scm_c_vector_set_x (SCM v
, size_t k
, SCM obj
)
246 if (SCM_I_IS_VECTOR (v
))
248 if (k
>= SCM_I_VECTOR_LENGTH (v
))
249 scm_out_of_range (NULL
, scm_from_size_t (k
));
250 (SCM_I_VECTOR_WELTS(v
))[k
] = obj
;
252 else if (SCM_I_ARRAYP (v
) && SCM_I_ARRAY_NDIM (v
) == 1)
254 scm_t_array_dim
*dim
= SCM_I_ARRAY_DIMS (v
);
255 SCM vv
= SCM_I_ARRAY_V (v
);
256 if (SCM_I_IS_VECTOR (vv
))
258 if (k
>= dim
->ubnd
- dim
->lbnd
+ 1)
259 scm_out_of_range (NULL
, scm_from_size_t (k
));
260 k
= SCM_I_ARRAY_BASE (v
) + k
*dim
->inc
;
261 (SCM_I_VECTOR_WELTS (vv
))[k
] = obj
;
264 scm_wrong_type_arg_msg (NULL
, 0, v
, "non-uniform vector");
268 if (SCM_UNPACK (g_vector_set_x
))
269 scm_apply_generic (g_vector_set_x
,
270 scm_list_3 (v
, scm_from_size_t (k
), obj
));
272 scm_wrong_type_arg_msg (NULL
, 0, v
, "vector");
276 SCM_DEFINE (scm_make_vector
, "make-vector", 1, 1, 0,
278 "Return a newly allocated vector of @var{k} elements. If a\n"
279 "second argument is given, then each position is initialized to\n"
280 "@var{fill}. Otherwise the initial contents of each position is\n"
282 #define FUNC_NAME s_scm_make_vector
284 size_t l
= scm_to_unsigned_integer (k
, 0, VECTOR_MAX_LENGTH
);
286 if (SCM_UNBNDP (fill
))
287 fill
= SCM_UNSPECIFIED
;
289 return scm_c_make_vector (l
, fill
);
295 scm_c_make_vector (size_t k
, SCM fill
)
296 #define FUNC_NAME s_scm_make_vector
305 SCM_ASSERT_RANGE (1, scm_from_ulong (k
), k
<= VECTOR_MAX_LENGTH
);
307 base
= scm_gc_malloc (k
* sizeof (SCM
), "vector");
308 for (j
= 0; j
!= k
; ++j
)
314 v
= scm_cell ((k
<< 8) | scm_tc7_vector
, (scm_t_bits
) base
);
315 scm_remember_upto_here_1 (fill
);
321 SCM_DEFINE (scm_vector_copy
, "vector-copy", 1, 0, 0,
323 "Return a copy of @var{vec}.")
324 #define FUNC_NAME s_scm_vector_copy
326 scm_t_array_handle handle
;
332 src
= scm_vector_elements (vec
, &handle
, &len
, &inc
);
333 dst
= scm_gc_malloc (len
* sizeof (SCM
), "vector");
334 for (i
= 0; i
< len
; i
++, src
+= inc
)
336 scm_array_handle_release (&handle
);
338 return scm_cell ((len
<< 8) | scm_tc7_vector
, (scm_t_bits
) dst
);
343 scm_i_vector_free (SCM vec
)
345 scm_gc_free (SCM_I_VECTOR_WELTS (vec
),
346 SCM_I_VECTOR_LENGTH (vec
) * sizeof(SCM
),
350 /* Allocate memory for a weak vector on behalf of the caller. The allocated
351 * vector will be of the given weak vector subtype. It will contain size
352 * elements which are initialized with the 'fill' object, or, if 'fill' is
353 * undefined, with an unspecified object.
356 scm_i_allocate_weak_vector (scm_t_bits type
, SCM size
, SCM fill
)
362 c_size
= scm_to_unsigned_integer (size
, 0, VECTOR_MAX_LENGTH
);
368 if (SCM_UNBNDP (fill
))
369 fill
= SCM_UNSPECIFIED
;
371 base
= scm_gc_malloc (c_size
* sizeof (SCM
), "weak vector");
372 for (j
= 0; j
!= c_size
; ++j
)
378 v
= scm_double_cell ((c_size
<< 8) | scm_tc7_wvect
,
381 SCM_UNPACK (SCM_EOL
));
382 scm_remember_upto_here_1 (fill
);
387 SCM_DEFINE (scm_vector_to_list
, "vector->list", 1, 0, 0,
389 "Return a newly allocated list composed of the elements of @var{v}.\n"
392 "(vector->list '#(dah dah didah)) @result{} (dah dah didah)\n"
393 "(list->vector '(dididit dah)) @result{} #(dididit dah)\n"
395 #define FUNC_NAME s_scm_vector_to_list
399 scm_t_array_handle handle
;
400 size_t i
, count
, len
;
403 data
= scm_vector_elements (v
, &handle
, &len
, &inc
);
404 for (i
= (len
- 1) * inc
, count
= 0;
407 res
= scm_cons (data
[i
], res
);
409 scm_array_handle_release (&handle
);
415 SCM_DEFINE (scm_vector_fill_x
, "vector-fill!", 2, 0, 0,
417 "Store @var{fill} in every position of @var{vector}. The value\n"
418 "returned by @code{vector-fill!} is unspecified.")
419 #define FUNC_NAME s_scm_vector_fill_x
421 scm_t_array_handle handle
;
426 data
= scm_vector_writable_elements (v
, &handle
, &len
, &inc
);
427 for (i
= 0; i
< len
; i
+= inc
)
429 scm_array_handle_release (&handle
);
430 return SCM_UNSPECIFIED
;
436 scm_i_vector_equal_p (SCM x
, SCM y
)
439 for (i
= SCM_I_VECTOR_LENGTH (x
) - 1; i
>= 0; i
--)
440 if (scm_is_false (scm_equal_p (SCM_I_VECTOR_ELTS (x
)[i
],
441 SCM_I_VECTOR_ELTS (y
)[i
])))
447 SCM_DEFINE (scm_vector_move_left_x
, "vector-move-left!", 5, 0, 0,
448 (SCM vec1
, SCM start1
, SCM end1
, SCM vec2
, SCM start2
),
449 "Copy elements from @var{vec1}, positions @var{start1} to @var{end1},\n"
450 "to @var{vec2} starting at position @var{start2}. @var{start1} and\n"
451 "@var{start2} are inclusive indices; @var{end1} is exclusive.\n\n"
452 "@code{vector-move-left!} copies elements in leftmost order.\n"
453 "Therefore, in the case where @var{vec1} and @var{vec2} refer to the\n"
454 "same vector, @code{vector-move-left!} is usually appropriate when\n"
455 "@var{start1} is greater than @var{start2}.")
456 #define FUNC_NAME s_scm_vector_move_left_x
458 scm_t_array_handle handle1
, handle2
;
465 elts1
= scm_vector_elements (vec1
, &handle1
, &len1
, &inc1
);
466 elts2
= scm_vector_writable_elements (vec2
, &handle2
, &len2
, &inc2
);
468 i
= scm_to_unsigned_integer (start1
, 0, len1
);
469 e
= scm_to_unsigned_integer (end1
, i
, len1
);
470 j
= scm_to_unsigned_integer (start2
, 0, len2
- (i
-e
));
475 for (; i
< e
; i
+= inc1
, j
+= inc2
)
478 scm_array_handle_release (&handle2
);
479 scm_array_handle_release (&handle1
);
481 return SCM_UNSPECIFIED
;
485 SCM_DEFINE (scm_vector_move_right_x
, "vector-move-right!", 5, 0, 0,
486 (SCM vec1
, SCM start1
, SCM end1
, SCM vec2
, SCM start2
),
487 "Copy elements from @var{vec1}, positions @var{start1} to @var{end1},\n"
488 "to @var{vec2} starting at position @var{start2}. @var{start1} and\n"
489 "@var{start2} are inclusive indices; @var{end1} is exclusive.\n\n"
490 "@code{vector-move-right!} copies elements in rightmost order.\n"
491 "Therefore, in the case where @var{vec1} and @var{vec2} refer to the\n"
492 "same vector, @code{vector-move-right!} is usually appropriate when\n"
493 "@var{start1} is less than @var{start2}.")
494 #define FUNC_NAME s_scm_vector_move_right_x
496 scm_t_array_handle handle1
, handle2
;
503 elts1
= scm_vector_elements (vec1
, &handle1
, &len1
, &inc1
);
504 elts2
= scm_vector_writable_elements (vec2
, &handle2
, &len2
, &inc2
);
506 i
= scm_to_unsigned_integer (start1
, 0, len1
);
507 e
= scm_to_unsigned_integer (end1
, i
, len1
);
508 j
= scm_to_unsigned_integer (start2
, 0, len2
- (i
-e
));
520 scm_array_handle_release (&handle2
);
521 scm_array_handle_release (&handle1
);
523 return SCM_UNSPECIFIED
;
528 /* Generalized vectors. */
531 scm_is_generalized_vector (SCM obj
)
533 return (scm_is_vector (obj
)
534 || scm_is_string (obj
)
535 || scm_is_bitvector (obj
)
536 || scm_is_uniform_vector (obj
)
537 || scm_is_bytevector (obj
));
540 SCM_DEFINE (scm_generalized_vector_p
, "generalized-vector?", 1, 0, 0,
542 "Return @code{#t} if @var{obj} is a vector, string,\n"
543 "bitvector, or uniform numeric vector.")
544 #define FUNC_NAME s_scm_generalized_vector_p
546 return scm_from_bool (scm_is_generalized_vector (obj
));
551 scm_generalized_vector_get_handle (SCM vec
, scm_t_array_handle
*h
)
553 scm_array_get_handle (vec
, h
);
554 if (scm_array_handle_rank (h
) != 1)
555 scm_wrong_type_arg_msg (NULL
, 0, vec
, "vector");
559 scm_c_generalized_vector_length (SCM v
)
561 if (scm_is_vector (v
))
562 return scm_c_vector_length (v
);
563 else if (scm_is_string (v
))
564 return scm_c_string_length (v
);
565 else if (scm_is_bitvector (v
))
566 return scm_c_bitvector_length (v
);
567 else if (scm_is_uniform_vector (v
))
568 return scm_c_uniform_vector_length (v
);
569 else if (scm_is_bytevector (v
))
570 return scm_c_bytevector_length (v
);
572 scm_wrong_type_arg_msg (NULL
, 0, v
, "generalized vector");
575 SCM_DEFINE (scm_generalized_vector_length
, "generalized-vector-length", 1, 0, 0,
577 "Return the length of the generalized vector @var{v}.")
578 #define FUNC_NAME s_scm_generalized_vector_length
580 return scm_from_size_t (scm_c_generalized_vector_length (v
));
585 scm_c_generalized_vector_ref (SCM v
, size_t idx
)
587 if (scm_is_vector (v
))
588 return scm_c_vector_ref (v
, idx
);
589 else if (scm_is_string (v
))
590 return scm_c_string_ref (v
, idx
);
591 else if (scm_is_bitvector (v
))
592 return scm_c_bitvector_ref (v
, idx
);
593 else if (scm_is_uniform_vector (v
))
594 return scm_c_uniform_vector_ref (v
, idx
);
595 else if (scm_is_bytevector (v
))
596 return scm_from_uint8 (scm_c_bytevector_ref (v
, idx
));
598 scm_wrong_type_arg_msg (NULL
, 0, v
, "generalized vector");
601 SCM_DEFINE (scm_generalized_vector_ref
, "generalized-vector-ref", 2, 0, 0,
603 "Return the element at index @var{idx} of the\n"
604 "generalized vector @var{v}.")
605 #define FUNC_NAME s_scm_generalized_vector_ref
607 return scm_c_generalized_vector_ref (v
, scm_to_size_t (idx
));
612 scm_c_generalized_vector_set_x (SCM v
, size_t idx
, SCM val
)
614 if (scm_is_vector (v
))
615 scm_c_vector_set_x (v
, idx
, val
);
616 else if (scm_is_string (v
))
617 scm_c_string_set_x (v
, idx
, val
);
618 else if (scm_is_bitvector (v
))
619 scm_c_bitvector_set_x (v
, idx
, val
);
620 else if (scm_is_uniform_vector (v
))
621 scm_c_uniform_vector_set_x (v
, idx
, val
);
622 else if (scm_is_bytevector (v
))
623 scm_i_bytevector_generalized_set_x (v
, idx
, val
);
625 scm_wrong_type_arg_msg (NULL
, 0, v
, "generalized vector");
628 SCM_DEFINE (scm_generalized_vector_set_x
, "generalized-vector-set!", 3, 0, 0,
629 (SCM v
, SCM idx
, SCM val
),
630 "Set the element at index @var{idx} of the\n"
631 "generalized vector @var{v} to @var{val}.")
632 #define FUNC_NAME s_scm_generalized_vector_set_x
634 scm_c_generalized_vector_set_x (v
, scm_to_size_t (idx
), val
);
635 return SCM_UNSPECIFIED
;
639 SCM_DEFINE (scm_generalized_vector_to_list
, "generalized-vector->list", 1, 0, 0,
641 "Return a new list whose elements are the elements of the\n"
642 "generalized vector @var{v}.")
643 #define FUNC_NAME s_scm_generalized_vector_to_list
645 if (scm_is_vector (v
))
646 return scm_vector_to_list (v
);
647 else if (scm_is_string (v
))
648 return scm_string_to_list (v
);
649 else if (scm_is_bitvector (v
))
650 return scm_bitvector_to_list (v
);
651 else if (scm_is_uniform_vector (v
))
652 return scm_uniform_vector_to_list (v
);
654 scm_wrong_type_arg_msg (NULL
, 0, v
, "generalized vector");
662 scm_nullvect
= scm_c_make_vector (0, SCM_UNDEFINED
);
664 #include "libguile/vectors.x"