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/generalized-vectors.h"
34 #include "libguile/arrays.h"
35 #include "libguile/bitvectors.h"
36 #include "libguile/bytevectors.h"
37 #include "libguile/array-map.h"
38 #include "libguile/srfi-4.h"
39 #include "libguile/strings.h"
40 #include "libguile/srfi-13.h"
41 #include "libguile/dynwind.h"
42 #include "libguile/deprecation.h"
46 #define VECTOR_MAX_LENGTH (SCM_T_BITS_MAX >> 8)
49 scm_is_vector (SCM obj
)
51 if (SCM_I_IS_VECTOR (obj
))
53 if (SCM_I_ARRAYP (obj
) && SCM_I_ARRAY_NDIM (obj
) == 1)
55 SCM v
= SCM_I_ARRAY_V (obj
);
56 return SCM_I_IS_VECTOR (v
);
62 scm_is_simple_vector (SCM obj
)
64 return SCM_I_IS_VECTOR (obj
);
68 scm_vector_elements (SCM vec
, scm_t_array_handle
*h
,
69 size_t *lenp
, ssize_t
*incp
)
71 scm_generalized_vector_get_handle (vec
, h
);
74 scm_t_array_dim
*dim
= scm_array_handle_dims (h
);
75 *lenp
= dim
->ubnd
- dim
->lbnd
+ 1;
78 return scm_array_handle_elements (h
);
82 scm_vector_writable_elements (SCM vec
, scm_t_array_handle
*h
,
83 size_t *lenp
, ssize_t
*incp
)
85 scm_generalized_vector_get_handle (vec
, h
);
88 scm_t_array_dim
*dim
= scm_array_handle_dims (h
);
89 *lenp
= dim
->ubnd
- dim
->lbnd
+ 1;
92 return scm_array_handle_writable_elements (h
);
95 SCM_DEFINE (scm_vector_p
, "vector?", 1, 0, 0,
97 "Return @code{#t} if @var{obj} is a vector, otherwise return\n"
99 #define FUNC_NAME s_scm_vector_p
101 return scm_from_bool (scm_is_vector (obj
));
105 SCM_GPROC (s_vector_length
, "vector-length", 1, 0, 0, scm_vector_length
, g_vector_length
);
106 /* Returns the number of elements in @var{vector} as an exact integer. */
108 scm_vector_length (SCM v
)
110 if (SCM_I_IS_VECTOR (v
))
111 return scm_from_size_t (SCM_I_VECTOR_LENGTH (v
));
112 else if (SCM_I_ARRAYP (v
) && SCM_I_ARRAY_NDIM (v
) == 1)
114 scm_t_array_dim
*dim
= SCM_I_ARRAY_DIMS (v
);
115 return scm_from_size_t (dim
->ubnd
- dim
->lbnd
+ 1);
118 SCM_WTA_DISPATCH_1 (g_vector_length
, v
, 1, NULL
);
122 scm_c_vector_length (SCM v
)
124 if (SCM_I_IS_VECTOR (v
))
125 return SCM_I_VECTOR_LENGTH (v
);
127 return scm_to_size_t (scm_vector_length (v
));
130 SCM_REGISTER_PROC (s_list_to_vector
, "list->vector", 1, 0, 0, scm_vector
);
132 "Return a newly created vector initialized to the elements of"
133 "the list @var{list}.\n\n"
135 "(vector->list '#(dah dah didah)) @result{} (dah dah didah)\n"
136 "(list->vector '(dididit dah)) @result{} #(dididit dah)\n"
139 SCM_DEFINE (scm_vector
, "vector", 0, 0, 1,
141 "@deffnx {Scheme Procedure} list->vector l\n"
142 "Return a newly allocated vector composed of the\n"
143 "given arguments. Analogous to @code{list}.\n"
146 "(vector 'a 'b 'c) @result{} #(a b c)\n"
148 #define FUNC_NAME s_scm_vector
153 scm_t_array_handle handle
;
155 SCM_VALIDATE_LIST_COPYLEN (1, l
, len
);
157 res
= scm_c_make_vector (len
, SCM_UNSPECIFIED
);
158 data
= scm_vector_writable_elements (res
, &handle
, NULL
, NULL
);
160 while (scm_is_pair (l
) && i
< len
)
162 data
[i
] = SCM_CAR (l
);
167 scm_array_handle_release (&handle
);
173 SCM_GPROC (s_vector_ref
, "vector-ref", 2, 0, 0, scm_vector_ref
, g_vector_ref
);
176 "@var{k} must be a valid index of @var{vector}.\n"
177 "@samp{Vector-ref} returns the contents of element @var{k} of\n"
180 "(vector-ref '#(1 1 2 3 5 8 13 21) 5) @result{} 8\n"
181 "(vector-ref '#(1 1 2 3 5 8 13 21)\n"
182 " (let ((i (round (* 2 (acos -1)))))\n"
183 " (if (inexact? i)\n"
184 " (inexact->exact i)\n"
185 " i))) @result{} 13\n"
190 scm_vector_ref (SCM v
, SCM k
)
191 #define FUNC_NAME s_vector_ref
193 return scm_c_vector_ref (v
, scm_to_size_t (k
));
198 scm_c_vector_ref (SCM v
, size_t k
)
200 if (SCM_I_IS_VECTOR (v
))
202 if (k
>= SCM_I_VECTOR_LENGTH (v
))
203 scm_out_of_range (NULL
, scm_from_size_t (k
));
204 return (SCM_I_VECTOR_ELTS(v
))[k
];
206 else if (SCM_I_ARRAYP (v
) && SCM_I_ARRAY_NDIM (v
) == 1)
208 scm_t_array_dim
*dim
= SCM_I_ARRAY_DIMS (v
);
209 SCM vv
= SCM_I_ARRAY_V (v
);
210 if (SCM_I_IS_VECTOR (vv
))
212 if (k
>= dim
->ubnd
- dim
->lbnd
+ 1)
213 scm_out_of_range (NULL
, scm_from_size_t (k
));
214 k
= SCM_I_ARRAY_BASE (v
) + k
*dim
->inc
;
215 return (SCM_I_VECTOR_ELTS (vv
))[k
];
217 scm_wrong_type_arg_msg (NULL
, 0, v
, "non-uniform vector");
220 SCM_WTA_DISPATCH_2 (g_vector_ref
, v
, scm_from_size_t (k
), 2, NULL
);
223 SCM_GPROC (s_vector_set_x
, "vector-set!", 3, 0, 0, scm_vector_set_x
, g_vector_set_x
);
225 /* "@var{k} must be a valid index of @var{vector}.\n"
226 "@code{Vector-set!} stores @var{obj} in element @var{k} of @var{vector}.\n"
227 "The value returned by @samp{vector-set!} is unspecified.\n"
229 "(let ((vec (vector 0 '(2 2 2 2) "Anna")))\n"
230 " (vector-set! vec 1 '("Sue" "Sue"))\n"
231 " vec) @result{} #(0 ("Sue" "Sue") "Anna")\n"
232 "(vector-set! '#(0 1 2) 1 "doe") @result{} @emph{error} ; constant vector\n"
237 scm_vector_set_x (SCM v
, SCM k
, SCM obj
)
238 #define FUNC_NAME s_vector_set_x
240 scm_c_vector_set_x (v
, scm_to_size_t (k
), obj
);
241 return SCM_UNSPECIFIED
;
246 scm_c_vector_set_x (SCM v
, size_t k
, SCM obj
)
248 if (SCM_I_IS_VECTOR (v
))
250 if (k
>= SCM_I_VECTOR_LENGTH (v
))
251 scm_out_of_range (NULL
, scm_from_size_t (k
));
252 (SCM_I_VECTOR_WELTS(v
))[k
] = obj
;
254 else if (SCM_I_ARRAYP (v
) && SCM_I_ARRAY_NDIM (v
) == 1)
256 scm_t_array_dim
*dim
= SCM_I_ARRAY_DIMS (v
);
257 SCM vv
= SCM_I_ARRAY_V (v
);
258 if (SCM_I_IS_VECTOR (vv
))
260 if (k
>= dim
->ubnd
- dim
->lbnd
+ 1)
261 scm_out_of_range (NULL
, scm_from_size_t (k
));
262 k
= SCM_I_ARRAY_BASE (v
) + k
*dim
->inc
;
263 (SCM_I_VECTOR_WELTS (vv
))[k
] = obj
;
266 scm_wrong_type_arg_msg (NULL
, 0, v
, "non-uniform vector");
270 if (SCM_UNPACK (g_vector_set_x
))
271 scm_apply_generic (g_vector_set_x
,
272 scm_list_3 (v
, scm_from_size_t (k
), obj
));
274 scm_wrong_type_arg_msg (NULL
, 0, v
, "vector");
278 SCM_DEFINE (scm_make_vector
, "make-vector", 1, 1, 0,
280 "Return a newly allocated vector of @var{k} elements. If a\n"
281 "second argument is given, then each position is initialized to\n"
282 "@var{fill}. Otherwise the initial contents of each position is\n"
284 #define FUNC_NAME s_scm_make_vector
286 size_t l
= scm_to_unsigned_integer (k
, 0, VECTOR_MAX_LENGTH
);
288 if (SCM_UNBNDP (fill
))
289 fill
= SCM_UNSPECIFIED
;
291 return scm_c_make_vector (l
, fill
);
297 scm_c_make_vector (size_t k
, SCM fill
)
298 #define FUNC_NAME s_scm_make_vector
307 SCM_ASSERT_RANGE (1, scm_from_ulong (k
), k
<= VECTOR_MAX_LENGTH
);
309 base
= scm_gc_malloc (k
* sizeof (SCM
), "vector");
310 for (j
= 0; j
!= k
; ++j
)
316 v
= scm_cell ((k
<< 8) | scm_tc7_vector
, (scm_t_bits
) base
);
317 scm_remember_upto_here_1 (fill
);
323 SCM_DEFINE (scm_vector_copy
, "vector-copy", 1, 0, 0,
325 "Return a copy of @var{vec}.")
326 #define FUNC_NAME s_scm_vector_copy
328 scm_t_array_handle handle
;
334 src
= scm_vector_elements (vec
, &handle
, &len
, &inc
);
335 dst
= scm_gc_malloc (len
* sizeof (SCM
), "vector");
336 for (i
= 0; i
< len
; i
++, src
+= inc
)
338 scm_array_handle_release (&handle
);
340 return scm_cell ((len
<< 8) | scm_tc7_vector
, (scm_t_bits
) dst
);
345 scm_i_vector_free (SCM vec
)
347 scm_gc_free (SCM_I_VECTOR_WELTS (vec
),
348 SCM_I_VECTOR_LENGTH (vec
) * sizeof(SCM
),
352 /* Allocate memory for a weak vector on behalf of the caller. The allocated
353 * vector will be of the given weak vector subtype. It will contain size
354 * elements which are initialized with the 'fill' object, or, if 'fill' is
355 * undefined, with an unspecified object.
358 scm_i_allocate_weak_vector (scm_t_bits type
, SCM size
, SCM fill
)
364 c_size
= scm_to_unsigned_integer (size
, 0, VECTOR_MAX_LENGTH
);
370 if (SCM_UNBNDP (fill
))
371 fill
= SCM_UNSPECIFIED
;
373 base
= scm_gc_malloc (c_size
* sizeof (SCM
), "weak vector");
374 for (j
= 0; j
!= c_size
; ++j
)
380 v
= scm_double_cell ((c_size
<< 8) | scm_tc7_wvect
,
383 SCM_UNPACK (SCM_EOL
));
384 scm_remember_upto_here_1 (fill
);
389 SCM_DEFINE (scm_vector_to_list
, "vector->list", 1, 0, 0,
391 "Return a newly allocated list composed of the elements of @var{v}.\n"
394 "(vector->list '#(dah dah didah)) @result{} (dah dah didah)\n"
395 "(list->vector '(dididit dah)) @result{} #(dididit dah)\n"
397 #define FUNC_NAME s_scm_vector_to_list
401 scm_t_array_handle handle
;
402 size_t i
, count
, len
;
405 data
= scm_vector_elements (v
, &handle
, &len
, &inc
);
406 for (i
= (len
- 1) * inc
, count
= 0;
409 res
= scm_cons (data
[i
], res
);
411 scm_array_handle_release (&handle
);
417 SCM_DEFINE (scm_vector_fill_x
, "vector-fill!", 2, 0, 0,
419 "Store @var{fill} in every position of @var{vector}. The value\n"
420 "returned by @code{vector-fill!} is unspecified.")
421 #define FUNC_NAME s_scm_vector_fill_x
423 scm_t_array_handle handle
;
428 data
= scm_vector_writable_elements (v
, &handle
, &len
, &inc
);
429 for (i
= 0; i
< len
; i
+= inc
)
431 scm_array_handle_release (&handle
);
432 return SCM_UNSPECIFIED
;
438 scm_i_vector_equal_p (SCM x
, SCM y
)
441 for (i
= SCM_I_VECTOR_LENGTH (x
) - 1; i
>= 0; i
--)
442 if (scm_is_false (scm_equal_p (SCM_I_VECTOR_ELTS (x
)[i
],
443 SCM_I_VECTOR_ELTS (y
)[i
])))
449 SCM_DEFINE (scm_vector_move_left_x
, "vector-move-left!", 5, 0, 0,
450 (SCM vec1
, SCM start1
, SCM end1
, SCM vec2
, SCM start2
),
451 "Copy elements from @var{vec1}, positions @var{start1} to @var{end1},\n"
452 "to @var{vec2} starting at position @var{start2}. @var{start1} and\n"
453 "@var{start2} are inclusive indices; @var{end1} is exclusive.\n\n"
454 "@code{vector-move-left!} copies elements in leftmost order.\n"
455 "Therefore, in the case where @var{vec1} and @var{vec2} refer to the\n"
456 "same vector, @code{vector-move-left!} is usually appropriate when\n"
457 "@var{start1} is greater than @var{start2}.")
458 #define FUNC_NAME s_scm_vector_move_left_x
460 scm_t_array_handle handle1
, handle2
;
467 elts1
= scm_vector_elements (vec1
, &handle1
, &len1
, &inc1
);
468 elts2
= scm_vector_writable_elements (vec2
, &handle2
, &len2
, &inc2
);
470 i
= scm_to_unsigned_integer (start1
, 0, len1
);
471 e
= scm_to_unsigned_integer (end1
, i
, len1
);
472 j
= scm_to_unsigned_integer (start2
, 0, len2
- (i
-e
));
477 for (; i
< e
; i
+= inc1
, j
+= inc2
)
480 scm_array_handle_release (&handle2
);
481 scm_array_handle_release (&handle1
);
483 return SCM_UNSPECIFIED
;
487 SCM_DEFINE (scm_vector_move_right_x
, "vector-move-right!", 5, 0, 0,
488 (SCM vec1
, SCM start1
, SCM end1
, SCM vec2
, SCM start2
),
489 "Copy elements from @var{vec1}, positions @var{start1} to @var{end1},\n"
490 "to @var{vec2} starting at position @var{start2}. @var{start1} and\n"
491 "@var{start2} are inclusive indices; @var{end1} is exclusive.\n\n"
492 "@code{vector-move-right!} copies elements in rightmost order.\n"
493 "Therefore, in the case where @var{vec1} and @var{vec2} refer to the\n"
494 "same vector, @code{vector-move-right!} is usually appropriate when\n"
495 "@var{start1} is less than @var{start2}.")
496 #define FUNC_NAME s_scm_vector_move_right_x
498 scm_t_array_handle handle1
, handle2
;
505 elts1
= scm_vector_elements (vec1
, &handle1
, &len1
, &inc1
);
506 elts2
= scm_vector_writable_elements (vec2
, &handle2
, &len2
, &inc2
);
508 i
= scm_to_unsigned_integer (start1
, 0, len1
);
509 e
= scm_to_unsigned_integer (end1
, i
, len1
);
510 j
= scm_to_unsigned_integer (start2
, 0, len2
- (i
-e
));
522 scm_array_handle_release (&handle2
);
523 scm_array_handle_release (&handle1
);
525 return SCM_UNSPECIFIED
;
531 vector_handle_ref (scm_t_array_handle
*h
, size_t idx
)
533 if (idx
> h
->dims
[0].ubnd
)
534 scm_out_of_range ("vector-handle-ref", scm_from_size_t (idx
));
535 return ((SCM
*)h
->elements
)[idx
];
539 vector_handle_set (scm_t_array_handle
*h
, size_t idx
, SCM val
)
541 if (idx
> h
->dims
[0].ubnd
)
542 scm_out_of_range ("vector-handle-set!", scm_from_size_t (idx
));
543 ((SCM
*)h
->writable_elements
)[idx
] = val
;
547 vector_get_handle (SCM v
, scm_t_array_handle
*h
)
553 h
->dim0
.ubnd
= SCM_I_VECTOR_LENGTH (v
) - 1;
555 h
->element_type
= SCM_ARRAY_ELEMENT_TYPE_SCM
;
556 h
->elements
= h
->writable_elements
= SCM_I_VECTOR_WELTS (v
);
559 SCM_ARRAY_IMPLEMENTATION (scm_tc7_vector
, 0x7f & ~2,
560 vector_handle_ref
, vector_handle_set
,
562 SCM_ARRAY_IMPLEMENTATION (scm_tc7_wvect
, 0x7f & ~2,
563 vector_handle_ref
, vector_handle_set
,
565 SCM_VECTOR_IMPLEMENTATION (SCM_ARRAY_ELEMENT_TYPE_SCM
, scm_make_vector
);
571 scm_nullvect
= scm_c_make_vector (0, SCM_UNDEFINED
);
573 #include "libguile/vectors.x"