1 /* Copyright (C) 1995,1996,1998,1999,2000,2001, 2006, 2008 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "libguile/_scm.h"
25 #include "libguile/eq.h"
26 #include "libguile/root.h"
27 #include "libguile/strings.h"
28 #include "libguile/lang.h"
30 #include "libguile/validate.h"
31 #include "libguile/vectors.h"
32 #include "libguile/unif.h"
33 #include "libguile/ramap.h"
34 #include "libguile/srfi-4.h"
35 #include "libguile/strings.h"
36 #include "libguile/srfi-13.h"
37 #include "libguile/dynwind.h"
38 #include "libguile/deprecation.h"
42 #define VECTOR_MAX_LENGTH (SCM_T_BITS_MAX >> 8)
45 scm_is_vector (SCM obj
)
47 if (SCM_I_IS_VECTOR (obj
))
49 if (SCM_I_ARRAYP (obj
) && SCM_I_ARRAY_NDIM (obj
) == 1)
51 SCM v
= SCM_I_ARRAY_V (obj
);
52 return SCM_I_IS_VECTOR (v
);
58 scm_is_simple_vector (SCM obj
)
60 return SCM_I_IS_VECTOR (obj
);
64 scm_vector_elements (SCM vec
, scm_t_array_handle
*h
,
65 size_t *lenp
, ssize_t
*incp
)
67 scm_generalized_vector_get_handle (vec
, h
);
70 scm_t_array_dim
*dim
= scm_array_handle_dims (h
);
71 *lenp
= dim
->ubnd
- dim
->lbnd
+ 1;
74 return scm_array_handle_elements (h
);
78 scm_vector_writable_elements (SCM vec
, scm_t_array_handle
*h
,
79 size_t *lenp
, ssize_t
*incp
)
81 scm_generalized_vector_get_handle (vec
, h
);
84 scm_t_array_dim
*dim
= scm_array_handle_dims (h
);
85 *lenp
= dim
->ubnd
- dim
->lbnd
+ 1;
88 return scm_array_handle_writable_elements (h
);
91 SCM_DEFINE (scm_vector_p
, "vector?", 1, 0, 0,
93 "Return @code{#t} if @var{obj} is a vector, otherwise return\n"
95 #define FUNC_NAME s_scm_vector_p
97 return scm_from_bool (scm_is_vector (obj
));
101 SCM_GPROC (s_vector_length
, "vector-length", 1, 0, 0, scm_vector_length
, g_vector_length
);
102 /* Returns the number of elements in @var{vector} as an exact integer. */
104 scm_vector_length (SCM v
)
106 if (SCM_I_IS_VECTOR (v
))
107 return scm_from_size_t (SCM_I_VECTOR_LENGTH (v
));
108 else if (SCM_I_ARRAYP (v
) && SCM_I_ARRAY_NDIM (v
) == 1)
110 scm_t_array_dim
*dim
= SCM_I_ARRAY_DIMS (v
);
111 return scm_from_size_t (dim
->ubnd
- dim
->lbnd
+ 1);
114 SCM_WTA_DISPATCH_1 (g_vector_length
, v
, 1, NULL
);
118 scm_c_vector_length (SCM v
)
120 if (SCM_I_IS_VECTOR (v
))
121 return SCM_I_VECTOR_LENGTH (v
);
123 return scm_to_size_t (scm_vector_length (v
));
126 SCM_REGISTER_PROC (s_list_to_vector
, "list->vector", 1, 0, 0, scm_vector
);
128 "Return a newly created vector initialized to the elements of"
129 "the list @var{list}.\n\n"
131 "(vector->list '#(dah dah didah)) @result{} (dah dah didah)\n"
132 "(list->vector '(dididit dah)) @result{} #(dididit dah)\n"
135 SCM_DEFINE (scm_vector
, "vector", 0, 0, 1,
137 "@deffnx {Scheme Procedure} list->vector l\n"
138 "Return a newly allocated vector composed of the\n"
139 "given arguments. Analogous to @code{list}.\n"
142 "(vector 'a 'b 'c) @result{} #(a b c)\n"
144 #define FUNC_NAME s_scm_vector
149 scm_t_array_handle handle
;
151 SCM_VALIDATE_LIST_COPYLEN (1, l
, len
);
153 res
= scm_c_make_vector (len
, SCM_UNSPECIFIED
);
154 data
= scm_vector_writable_elements (res
, &handle
, NULL
, NULL
);
156 while (scm_is_pair (l
) && i
< len
)
158 data
[i
] = SCM_CAR (l
);
163 scm_array_handle_release (&handle
);
169 SCM_GPROC (s_vector_ref
, "vector-ref", 2, 0, 0, scm_vector_ref
, g_vector_ref
);
172 "@var{k} must be a valid index of @var{vector}.\n"
173 "@samp{Vector-ref} returns the contents of element @var{k} of\n"
176 "(vector-ref '#(1 1 2 3 5 8 13 21) 5) @result{} 8\n"
177 "(vector-ref '#(1 1 2 3 5 8 13 21)\n"
178 " (let ((i (round (* 2 (acos -1)))))\n"
179 " (if (inexact? i)\n"
180 " (inexact->exact i)\n"
181 " i))) @result{} 13\n"
186 scm_vector_ref (SCM v
, SCM k
)
187 #define FUNC_NAME s_vector_ref
189 return scm_c_vector_ref (v
, scm_to_size_t (k
));
194 scm_c_vector_ref (SCM v
, size_t k
)
196 if (SCM_I_IS_VECTOR (v
))
198 if (k
>= SCM_I_VECTOR_LENGTH (v
))
199 scm_out_of_range (NULL
, scm_from_size_t (k
));
200 return (SCM_I_VECTOR_ELTS(v
))[k
];
202 else if (SCM_I_ARRAYP (v
) && SCM_I_ARRAY_NDIM (v
) == 1)
204 scm_t_array_dim
*dim
= SCM_I_ARRAY_DIMS (v
);
205 SCM vv
= SCM_I_ARRAY_V (v
);
206 if (SCM_I_IS_VECTOR (vv
))
208 if (k
>= dim
->ubnd
- dim
->lbnd
+ 1)
209 scm_out_of_range (NULL
, scm_from_size_t (k
));
210 k
= SCM_I_ARRAY_BASE (v
) + k
*dim
->inc
;
211 return (SCM_I_VECTOR_ELTS (vv
))[k
];
213 scm_wrong_type_arg_msg (NULL
, 0, v
, "non-uniform vector");
216 SCM_WTA_DISPATCH_2 (g_vector_ref
, v
, scm_from_size_t (k
), 2, NULL
);
219 SCM_GPROC (s_vector_set_x
, "vector-set!", 3, 0, 0, scm_vector_set_x
, g_vector_set_x
);
221 /* "@var{k} must be a valid index of @var{vector}.\n"
222 "@code{Vector-set!} stores @var{obj} in element @var{k} of @var{vector}.\n"
223 "The value returned by @samp{vector-set!} is unspecified.\n"
225 "(let ((vec (vector 0 '(2 2 2 2) "Anna")))\n"
226 " (vector-set! vec 1 '("Sue" "Sue"))\n"
227 " vec) @result{} #(0 ("Sue" "Sue") "Anna")\n"
228 "(vector-set! '#(0 1 2) 1 "doe") @result{} @emph{error} ; constant vector\n"
233 scm_vector_set_x (SCM v
, SCM k
, SCM obj
)
234 #define FUNC_NAME s_vector_set_x
236 scm_c_vector_set_x (v
, scm_to_size_t (k
), obj
);
237 return SCM_UNSPECIFIED
;
242 scm_c_vector_set_x (SCM v
, size_t k
, SCM obj
)
244 if (SCM_I_IS_VECTOR (v
))
246 if (k
>= SCM_I_VECTOR_LENGTH (v
))
247 scm_out_of_range (NULL
, scm_from_size_t (k
));
248 (SCM_I_VECTOR_WELTS(v
))[k
] = obj
;
250 else if (SCM_I_ARRAYP (v
) && SCM_I_ARRAY_NDIM (v
) == 1)
252 scm_t_array_dim
*dim
= SCM_I_ARRAY_DIMS (v
);
253 SCM vv
= SCM_I_ARRAY_V (v
);
254 if (SCM_I_IS_VECTOR (vv
))
256 if (k
>= dim
->ubnd
- dim
->lbnd
+ 1)
257 scm_out_of_range (NULL
, scm_from_size_t (k
));
258 k
= SCM_I_ARRAY_BASE (v
) + k
*dim
->inc
;
259 (SCM_I_VECTOR_WELTS (vv
))[k
] = obj
;
262 scm_wrong_type_arg_msg (NULL
, 0, v
, "non-uniform vector");
266 if (SCM_UNPACK (g_vector_set_x
))
267 scm_apply_generic (g_vector_set_x
,
268 scm_list_3 (v
, scm_from_size_t (k
), obj
));
270 scm_wrong_type_arg_msg (NULL
, 0, v
, "vector");
274 SCM_DEFINE (scm_make_vector
, "make-vector", 1, 1, 0,
276 "Return a newly allocated vector of @var{k} elements. If a\n"
277 "second argument is given, then each position is initialized to\n"
278 "@var{fill}. Otherwise the initial contents of each position is\n"
280 #define FUNC_NAME s_scm_make_vector
282 size_t l
= scm_to_unsigned_integer (k
, 0, VECTOR_MAX_LENGTH
);
284 if (SCM_UNBNDP (fill
))
285 fill
= SCM_UNSPECIFIED
;
287 return scm_c_make_vector (l
, fill
);
293 scm_c_make_vector (size_t k
, SCM fill
)
294 #define FUNC_NAME s_scm_make_vector
303 SCM_ASSERT_RANGE (1, scm_from_ulong (k
), k
<= VECTOR_MAX_LENGTH
);
305 base
= scm_gc_malloc (k
* sizeof (SCM
), "vector");
306 for (j
= 0; j
!= k
; ++j
)
312 v
= scm_cell ((k
<< 8) | scm_tc7_vector
, (scm_t_bits
) base
);
313 scm_remember_upto_here_1 (fill
);
319 SCM_DEFINE (scm_vector_copy
, "vector-copy", 1, 0, 0,
321 "Return a copy of @var{vec}.")
322 #define FUNC_NAME s_scm_vector_copy
324 scm_t_array_handle handle
;
330 src
= scm_vector_elements (vec
, &handle
, &len
, &inc
);
331 dst
= scm_gc_malloc (len
* sizeof (SCM
), "vector");
332 for (i
= 0; i
< len
; i
++, src
+= inc
)
334 scm_array_handle_release (&handle
);
336 return scm_cell ((len
<< 8) | scm_tc7_vector
, (scm_t_bits
) dst
);
341 scm_i_vector_free (SCM vec
)
343 scm_gc_free (SCM_I_VECTOR_WELTS (vec
),
344 SCM_I_VECTOR_LENGTH (vec
) * sizeof(SCM
),
348 /* Allocate memory for a weak vector on behalf of the caller. The allocated
349 * vector will be of the given weak vector subtype. It will contain size
350 * elements which are initialized with the 'fill' object, or, if 'fill' is
351 * undefined, with an unspecified object.
354 scm_i_allocate_weak_vector (scm_t_bits type
, SCM size
, SCM fill
)
360 c_size
= scm_to_unsigned_integer (size
, 0, VECTOR_MAX_LENGTH
);
366 if (SCM_UNBNDP (fill
))
367 fill
= SCM_UNSPECIFIED
;
369 base
= scm_gc_malloc (c_size
* sizeof (SCM
), "weak vector");
370 for (j
= 0; j
!= c_size
; ++j
)
376 v
= scm_double_cell ((c_size
<< 8) | scm_tc7_wvect
,
379 SCM_UNPACK (SCM_EOL
));
380 scm_remember_upto_here_1 (fill
);
385 SCM_DEFINE (scm_vector_to_list
, "vector->list", 1, 0, 0,
387 "Return a newly allocated list composed of the elements of @var{v}.\n"
390 "(vector->list '#(dah dah didah)) @result{} (dah dah didah)\n"
391 "(list->vector '(dididit dah)) @result{} #(dididit dah)\n"
393 #define FUNC_NAME s_scm_vector_to_list
397 scm_t_array_handle handle
;
398 size_t i
, count
, len
;
401 data
= scm_vector_elements (v
, &handle
, &len
, &inc
);
402 for (i
= (len
- 1) * inc
, count
= 0;
405 res
= scm_cons (data
[i
], res
);
407 scm_array_handle_release (&handle
);
413 SCM_DEFINE (scm_vector_fill_x
, "vector-fill!", 2, 0, 0,
415 "Store @var{fill} in every position of @var{vector}. The value\n"
416 "returned by @code{vector-fill!} is unspecified.")
417 #define FUNC_NAME s_scm_vector_fill_x
419 scm_t_array_handle handle
;
424 data
= scm_vector_writable_elements (v
, &handle
, &len
, &inc
);
425 for (i
= 0; i
< len
; i
+= inc
)
427 scm_array_handle_release (&handle
);
428 return SCM_UNSPECIFIED
;
434 scm_i_vector_equal_p (SCM x
, SCM y
)
437 for (i
= SCM_I_VECTOR_LENGTH (x
) - 1; i
>= 0; i
--)
438 if (scm_is_false (scm_equal_p (SCM_I_VECTOR_ELTS (x
)[i
],
439 SCM_I_VECTOR_ELTS (y
)[i
])))
445 SCM_DEFINE (scm_vector_move_left_x
, "vector-move-left!", 5, 0, 0,
446 (SCM vec1
, SCM start1
, SCM end1
, SCM vec2
, SCM start2
),
447 "Copy elements from @var{vec1}, positions @var{start1} to @var{end1},\n"
448 "to @var{vec2} starting at position @var{start2}. @var{start1} and\n"
449 "@var{start2} are inclusive indices; @var{end1} is exclusive.\n\n"
450 "@code{vector-move-left!} copies elements in leftmost order.\n"
451 "Therefore, in the case where @var{vec1} and @var{vec2} refer to the\n"
452 "same vector, @code{vector-move-left!} is usually appropriate when\n"
453 "@var{start1} is greater than @var{start2}.")
454 #define FUNC_NAME s_scm_vector_move_left_x
456 scm_t_array_handle handle1
, handle2
;
463 elts1
= scm_vector_elements (vec1
, &handle1
, &len1
, &inc1
);
464 elts2
= scm_vector_writable_elements (vec2
, &handle2
, &len2
, &inc2
);
466 i
= scm_to_unsigned_integer (start1
, 0, len1
);
467 e
= scm_to_unsigned_integer (end1
, i
, len1
);
468 j
= scm_to_unsigned_integer (start2
, 0, len2
- (i
-e
));
473 for (; i
< e
; i
+= inc1
, j
+= inc2
)
476 scm_array_handle_release (&handle2
);
477 scm_array_handle_release (&handle1
);
479 return SCM_UNSPECIFIED
;
483 SCM_DEFINE (scm_vector_move_right_x
, "vector-move-right!", 5, 0, 0,
484 (SCM vec1
, SCM start1
, SCM end1
, SCM vec2
, SCM start2
),
485 "Copy elements from @var{vec1}, positions @var{start1} to @var{end1},\n"
486 "to @var{vec2} starting at position @var{start2}. @var{start1} and\n"
487 "@var{start2} are inclusive indices; @var{end1} is exclusive.\n\n"
488 "@code{vector-move-right!} copies elements in rightmost order.\n"
489 "Therefore, in the case where @var{vec1} and @var{vec2} refer to the\n"
490 "same vector, @code{vector-move-right!} is usually appropriate when\n"
491 "@var{start1} is less than @var{start2}.")
492 #define FUNC_NAME s_scm_vector_move_right_x
494 scm_t_array_handle handle1
, handle2
;
501 elts1
= scm_vector_elements (vec1
, &handle1
, &len1
, &inc1
);
502 elts2
= scm_vector_writable_elements (vec2
, &handle2
, &len2
, &inc2
);
504 i
= scm_to_unsigned_integer (start1
, 0, len1
);
505 e
= scm_to_unsigned_integer (end1
, i
, len1
);
506 j
= scm_to_unsigned_integer (start2
, 0, len2
- (i
-e
));
518 scm_array_handle_release (&handle2
);
519 scm_array_handle_release (&handle1
);
521 return SCM_UNSPECIFIED
;
526 /* Generalized vectors. */
529 scm_is_generalized_vector (SCM obj
)
531 return (scm_is_vector (obj
)
532 || scm_is_string (obj
)
533 || scm_is_bitvector (obj
)
534 || scm_is_uniform_vector (obj
));
537 SCM_DEFINE (scm_generalized_vector_p
, "generalized-vector?", 1, 0, 0,
539 "Return @code{#t} if @var{obj} is a vector, string,\n"
540 "bitvector, or uniform numeric vector.")
541 #define FUNC_NAME s_scm_generalized_vector_p
543 return scm_from_bool (scm_is_generalized_vector (obj
));
548 scm_generalized_vector_get_handle (SCM vec
, scm_t_array_handle
*h
)
550 scm_array_get_handle (vec
, h
);
551 if (scm_array_handle_rank (h
) != 1)
552 scm_wrong_type_arg_msg (NULL
, 0, vec
, "vector");
556 scm_c_generalized_vector_length (SCM v
)
558 if (scm_is_vector (v
))
559 return scm_c_vector_length (v
);
560 else if (scm_is_string (v
))
561 return scm_c_string_length (v
);
562 else if (scm_is_bitvector (v
))
563 return scm_c_bitvector_length (v
);
564 else if (scm_is_uniform_vector (v
))
565 return scm_c_uniform_vector_length (v
);
567 scm_wrong_type_arg_msg (NULL
, 0, v
, "generalized vector");
570 SCM_DEFINE (scm_generalized_vector_length
, "generalized-vector-length", 1, 0, 0,
572 "Return the length of the generalized vector @var{v}.")
573 #define FUNC_NAME s_scm_generalized_vector_length
575 return scm_from_size_t (scm_c_generalized_vector_length (v
));
580 scm_c_generalized_vector_ref (SCM v
, size_t idx
)
582 if (scm_is_vector (v
))
583 return scm_c_vector_ref (v
, idx
);
584 else if (scm_is_string (v
))
585 return scm_c_string_ref (v
, idx
);
586 else if (scm_is_bitvector (v
))
587 return scm_c_bitvector_ref (v
, idx
);
588 else if (scm_is_uniform_vector (v
))
589 return scm_c_uniform_vector_ref (v
, idx
);
591 scm_wrong_type_arg_msg (NULL
, 0, v
, "generalized vector");
594 SCM_DEFINE (scm_generalized_vector_ref
, "generalized-vector-ref", 2, 0, 0,
596 "Return the element at index @var{idx} of the\n"
597 "generalized vector @var{v}.")
598 #define FUNC_NAME s_scm_generalized_vector_ref
600 return scm_c_generalized_vector_ref (v
, scm_to_size_t (idx
));
605 scm_c_generalized_vector_set_x (SCM v
, size_t idx
, SCM val
)
607 if (scm_is_vector (v
))
608 scm_c_vector_set_x (v
, idx
, val
);
609 else if (scm_is_string (v
))
610 scm_c_string_set_x (v
, idx
, val
);
611 else if (scm_is_bitvector (v
))
612 scm_c_bitvector_set_x (v
, idx
, val
);
613 else if (scm_is_uniform_vector (v
))
614 scm_c_uniform_vector_set_x (v
, idx
, val
);
616 scm_wrong_type_arg_msg (NULL
, 0, v
, "generalized vector");
619 SCM_DEFINE (scm_generalized_vector_set_x
, "generalized-vector-set!", 3, 0, 0,
620 (SCM v
, SCM idx
, SCM val
),
621 "Set the element at index @var{idx} of the\n"
622 "generalized vector @var{v} to @var{val}.")
623 #define FUNC_NAME s_scm_generalized_vector_set_x
625 scm_c_generalized_vector_set_x (v
, scm_to_size_t (idx
), val
);
626 return SCM_UNSPECIFIED
;
630 SCM_DEFINE (scm_generalized_vector_to_list
, "generalized-vector->list", 1, 0, 0,
632 "Return a new list whose elements are the elements of the\n"
633 "generalized vector @var{v}.")
634 #define FUNC_NAME s_scm_generalized_vector_to_list
636 if (scm_is_vector (v
))
637 return scm_vector_to_list (v
);
638 else if (scm_is_string (v
))
639 return scm_string_to_list (v
);
640 else if (scm_is_bitvector (v
))
641 return scm_bitvector_to_list (v
);
642 else if (scm_is_uniform_vector (v
))
643 return scm_uniform_vector_to_list (v
);
645 scm_wrong_type_arg_msg (NULL
, 0, v
, "generalized vector");
653 scm_nullvect
= scm_c_make_vector (0, SCM_UNDEFINED
);
655 #include "libguile/vectors.x"