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"
32 SCM_DEFINE (scm_vector_p
, "vector?", 1, 0, 0,
34 "Return @code{#t} if @var{obj} is a vector, otherwise return\n"
36 #define FUNC_NAME s_scm_vector_p
38 return scm_from_bool (SCM_VECTORP (obj
));
42 SCM_GPROC (s_vector_length
, "vector-length", 1, 0, 0, scm_vector_length
, g_vector_length
);
43 /* Returns the number of elements in @var{vector} as an exact integer. */
45 scm_vector_length (SCM v
)
47 SCM_GASSERT1 (SCM_VECTORP(v
),
48 g_vector_length
, v
, SCM_ARG1
, s_vector_length
);
49 return scm_from_size_t (SCM_VECTOR_LENGTH (v
));
52 SCM_REGISTER_PROC (s_list_to_vector
, "list->vector", 1, 0, 0, scm_vector
);
54 "Return a newly created vector initialized to the elements of"
55 "the list @var{list}.\n\n"
57 "(vector->list '#(dah dah didah)) @result{} (dah dah didah)\n"
58 "(list->vector '(dididit dah)) @result{} #(dididit dah)\n"
61 SCM_DEFINE (scm_vector
, "vector", 0, 0, 1,
63 "@deffnx {Scheme Procedure} list->vector l\n"
64 "Return a newly allocated vector composed of the\n"
65 "given arguments. Analogous to @code{list}.\n"
68 "(vector 'a 'b 'c) @result{} #(a b c)\n"
70 #define FUNC_NAME s_scm_vector
76 /* Dirk:FIXME:: In case of multiple threads, the list might get corrupted
77 while the vector is being created. */
78 SCM_VALIDATE_LIST_COPYLEN (1, l
, i
);
79 res
= scm_c_make_vector (i
, SCM_UNSPECIFIED
);
82 this code doesn't alloc. -- accessing RES is safe.
84 data
= SCM_WRITABLE_VELTS (res
);
85 while (!SCM_NULL_OR_NIL_P (l
))
87 *data
++ = SCM_CAR (l
);
95 SCM_GPROC (s_vector_ref
, "vector-ref", 2, 0, 0, scm_vector_ref
, g_vector_ref
);
98 "@var{k} must be a valid index of @var{vector}.\n"
99 "@samp{Vector-ref} returns the contents of element @var{k} of\n"
102 "(vector-ref '#(1 1 2 3 5 8 13 21) 5) @result{} 8\n"
103 "(vector-ref '#(1 1 2 3 5 8 13 21)\n"
104 " (let ((i (round (* 2 (acos -1)))))\n"
105 " (if (inexact? i)\n"
106 " (inexact->exact i)\n"
107 " i))) @result{} 13\n"
112 scm_vector_ref (SCM v
, SCM k
)
113 #define FUNC_NAME s_vector_ref
115 SCM_GASSERT2 (SCM_VECTORP (v
),
116 g_vector_ref
, v
, k
, SCM_ARG1
, s_vector_ref
);
117 SCM_GASSERT2 (SCM_I_INUMP (k
),
118 g_vector_ref
, v
, k
, SCM_ARG2
, s_vector_ref
);
119 SCM_ASSERT_RANGE (2, k
,
120 SCM_I_INUM (k
) < SCM_VECTOR_LENGTH (v
)
121 && SCM_I_INUM (k
) >= 0);
122 return SCM_VELTS (v
)[(long) SCM_I_INUM (k
)];
126 SCM_GPROC (s_vector_set_x
, "vector-set!", 3, 0, 0, scm_vector_set_x
, g_vector_set_x
);
128 /* "@var{k} must be a valid index of @var{vector}.\n"
129 "@code{Vector-set!} stores @var{obj} in element @var{k} of @var{vector}.\n"
130 "The value returned by @samp{vector-set!} is unspecified.\n"
132 "(let ((vec (vector 0 '(2 2 2 2) "Anna")))\n"
133 " (vector-set! vec 1 '("Sue" "Sue"))\n"
134 " vec) @result{} #(0 ("Sue" "Sue") "Anna")\n"
135 "(vector-set! '#(0 1 2) 1 "doe") @result{} @emph{error} ; constant vector\n"
140 scm_vector_set_x (SCM v
, SCM k
, SCM obj
)
141 #define FUNC_NAME s_vector_set_x
143 SCM_GASSERTn (SCM_VECTORP (v
),
144 g_vector_set_x
, scm_list_3 (v
, k
, obj
),
145 SCM_ARG1
, s_vector_set_x
);
146 SCM_GASSERTn (SCM_I_INUMP (k
),
147 g_vector_set_x
, scm_list_3 (v
, k
, obj
),
148 SCM_ARG2
, s_vector_set_x
);
149 SCM_ASSERT_RANGE (2, k
,
150 SCM_I_INUM (k
) < SCM_VECTOR_LENGTH (v
)
151 && SCM_I_INUM (k
) >= 0);
152 SCM_VECTOR_SET (v
, (long) SCM_I_INUM(k
), obj
);
153 return SCM_UNSPECIFIED
;
158 SCM_DEFINE (scm_make_vector
, "make-vector", 1, 1, 0,
160 "Return a newly allocated vector of @var{k} elements. If a\n"
161 "second argument is given, then each position is initialized to\n"
162 "@var{fill}. Otherwise the initial contents of each position is\n"
164 #define FUNC_NAME s_scm_make_vector
166 size_t l
= scm_to_unsigned_integer (k
, 0, SCM_VECTOR_MAX_LENGTH
);
168 if (SCM_UNBNDP (fill
))
169 fill
= SCM_UNSPECIFIED
;
171 return scm_c_make_vector (l
, fill
);
177 scm_c_make_vector (unsigned long int k
, SCM fill
)
178 #define FUNC_NAME s_scm_make_vector
187 SCM_ASSERT_RANGE (1, scm_ulong2num (k
), k
<= SCM_VECTOR_MAX_LENGTH
);
189 base
= scm_gc_malloc (k
* sizeof (scm_t_bits
), "vector");
190 for (j
= 0; j
!= k
; ++j
)
191 base
[j
] = SCM_UNPACK (fill
);
196 v
= scm_cell (SCM_MAKE_VECTOR_TAG (k
, scm_tc7_vector
), (scm_t_bits
) base
);
197 scm_remember_upto_here_1 (fill
);
204 SCM_DEFINE (scm_vector_to_list
, "vector->list", 1, 0, 0,
206 "Return a newly allocated list composed of the elements of @var{v}.\n"
209 "(vector->list '#(dah dah didah)) @result{} (dah dah didah)\n"
210 "(list->vector '(dididit dah)) @result{} #(dididit dah)\n"
212 #define FUNC_NAME s_scm_vector_to_list
217 SCM_VALIDATE_VECTOR (1, v
);
219 for(i
= SCM_VECTOR_LENGTH(v
)-1;i
>= 0;i
--) res
= scm_cons(data
[i
], res
);
225 SCM_DEFINE (scm_vector_fill_x
, "vector-fill!", 2, 0, 0,
227 "Store @var{fill} in every position of @var{vector}. The value\n"
228 "returned by @code{vector-fill!} is unspecified.")
229 #define FUNC_NAME s_scm_vector_fill_x
232 SCM_VALIDATE_VECTOR (1, v
);
234 for(i
= SCM_VECTOR_LENGTH (v
) - 1; i
>= 0; i
--)
235 SCM_VECTOR_SET(v
, i
, fill
);
236 return SCM_UNSPECIFIED
;
242 scm_vector_equal_p(SCM x
, SCM y
)
245 for(i
= SCM_VECTOR_LENGTH (x
) - 1; i
>= 0; i
--)
246 if (scm_is_false (scm_equal_p (SCM_VELTS (x
)[i
], SCM_VELTS (y
)[i
])))
252 SCM_DEFINE (scm_vector_move_left_x
, "vector-move-left!", 5, 0, 0,
253 (SCM vec1
, SCM start1
, SCM end1
, SCM vec2
, SCM start2
),
254 "Copy elements from @var{vec1}, positions @var{start1} to @var{end1},\n"
255 "to @var{vec2} starting at position @var{start2}. @var{start1} and\n"
256 "@var{start2} are inclusive indices; @var{end1} is exclusive.\n\n"
257 "@code{vector-move-left!} copies elements in leftmost order.\n"
258 "Therefore, in the case where @var{vec1} and @var{vec2} refer to the\n"
259 "same vector, @code{vector-move-left!} is usually appropriate when\n"
260 "@var{start1} is greater than @var{start2}.")
261 #define FUNC_NAME s_scm_vector_move_left_x
265 SCM_VALIDATE_VECTOR (1, vec1
);
266 SCM_VALIDATE_VECTOR (4, vec2
);
267 i
= scm_to_unsigned_integer (start1
, 0, SCM_VECTOR_LENGTH(vec1
));
268 e
= scm_to_unsigned_integer (end1
, i
, SCM_VECTOR_LENGTH(vec1
));
269 j
= scm_to_unsigned_integer (start2
, 0, SCM_VECTOR_LENGTH(vec2
)-(i
-e
));
273 SCM_VECTOR_SET (vec2
, j
, SCM_VELTS (vec1
)[i
]);
278 return SCM_UNSPECIFIED
;
282 SCM_DEFINE (scm_vector_move_right_x
, "vector-move-right!", 5, 0, 0,
283 (SCM vec1
, SCM start1
, SCM end1
, SCM vec2
, SCM start2
),
284 "Copy elements from @var{vec1}, positions @var{start1} to @var{end1},\n"
285 "to @var{vec2} starting at position @var{start2}. @var{start1} and\n"
286 "@var{start2} are inclusive indices; @var{end1} is exclusive.\n\n"
287 "@code{vector-move-right!} copies elements in rightmost order.\n"
288 "Therefore, in the case where @var{vec1} and @var{vec2} refer to the\n"
289 "same vector, @code{vector-move-right!} is usually appropriate when\n"
290 "@var{start1} is less than @var{start2}.")
291 #define FUNC_NAME s_scm_vector_move_right_x
295 SCM_VALIDATE_VECTOR (1, vec1
);
296 SCM_VALIDATE_VECTOR (4, vec2
);
297 i
= scm_to_unsigned_integer (start1
, 0, SCM_VECTOR_LENGTH(vec1
));
298 e
= scm_to_unsigned_integer (end1
, i
, SCM_VECTOR_LENGTH(vec1
));
299 j
= scm_to_unsigned_integer (start2
, 0, SCM_VECTOR_LENGTH(vec2
)-(i
-e
));
306 SCM_VECTOR_SET (vec2
, j
, SCM_VELTS (vec1
)[e
]);
309 return SCM_UNSPECIFIED
;
318 scm_nullvect
= scm_c_make_vector (0, SCM_UNDEFINED
);
320 #include "libguile/vectors.x"