Change Guile license to LGPLv3+
[bpt/guile.git] / libguile / vectors.c
CommitLineData
dbb605f5 1/* Copyright (C) 1995,1996,1998,1999,2000,2001, 2006, 2008 Free Software Foundation, Inc.
0f2d19dd 2 *
73be1d9e 3 * This library is free software; you can redistribute it and/or
53befeb7
NJ
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.
0f2d19dd 7 *
53befeb7
NJ
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
0f2d19dd 12 *
73be1d9e
MV
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
53befeb7
NJ
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301 USA
73be1d9e 17 */
1bbd0b84 18
1bbd0b84 19
0f2d19dd 20\f
dbb605f5
LC
21#ifdef HAVE_CONFIG_H
22# include <config.h>
23#endif
0f2d19dd 24
a0599745
MD
25#include "libguile/_scm.h"
26#include "libguile/eq.h"
27#include "libguile/root.h"
28#include "libguile/strings.h"
c96d76b8 29#include "libguile/lang.h"
a0599745
MD
30
31#include "libguile/validate.h"
32#include "libguile/vectors.h"
33#include "libguile/unif.h"
de5c0f58 34#include "libguile/ramap.h"
88797580
MV
35#include "libguile/srfi-4.h"
36#include "libguile/strings.h"
37#include "libguile/srfi-13.h"
1d0df896 38#include "libguile/dynwind.h"
6e708ef2 39#include "libguile/deprecation.h"
88797580 40
0f2d19dd
JB
41\f
42
6e708ef2
MV
43#define VECTOR_MAX_LENGTH (SCM_T_BITS_MAX >> 8)
44
6e708ef2
MV
45int
46scm_is_vector (SCM obj)
47{
48 if (SCM_I_IS_VECTOR (obj))
49 return 1;
04b87de5 50 if (SCM_I_ARRAYP (obj) && SCM_I_ARRAY_NDIM (obj) == 1)
6e708ef2 51 {
04b87de5 52 SCM v = SCM_I_ARRAY_V (obj);
6e708ef2
MV
53 return SCM_I_IS_VECTOR (v);
54 }
55 return 0;
56}
57
58int
59scm_is_simple_vector (SCM obj)
1d0df896 60{
6e708ef2 61 return SCM_I_IS_VECTOR (obj);
1d0df896
MV
62}
63
354116f7
MV
64const SCM *
65scm_vector_elements (SCM vec, scm_t_array_handle *h,
66 size_t *lenp, ssize_t *incp)
67{
68 scm_generalized_vector_get_handle (vec, h);
69 if (lenp)
70 {
71 scm_t_array_dim *dim = scm_array_handle_dims (h);
72 *lenp = dim->ubnd - dim->lbnd + 1;
73 *incp = dim->inc;
74 }
75 return scm_array_handle_elements (h);
76}
77
78SCM *
79scm_vector_writable_elements (SCM vec, scm_t_array_handle *h,
80 size_t *lenp, ssize_t *incp)
81{
82 scm_generalized_vector_get_handle (vec, h);
83 if (lenp)
84 {
85 scm_t_array_dim *dim = scm_array_handle_dims (h);
86 *lenp = dim->ubnd - dim->lbnd + 1;
87 *incp = dim->inc;
88 }
89 return scm_array_handle_writable_elements (h);
90}
91
a1ec6916 92SCM_DEFINE (scm_vector_p, "vector?", 1, 0, 0,
5ffe9968 93 (SCM obj),
1e6808ea
MG
94 "Return @code{#t} if @var{obj} is a vector, otherwise return\n"
95 "@code{#f}.")
1bbd0b84 96#define FUNC_NAME s_scm_vector_p
0f2d19dd 97{
de5c0f58 98 return scm_from_bool (scm_is_vector (obj));
0f2d19dd 99}
1bbd0b84 100#undef FUNC_NAME
0f2d19dd 101
f172c0b7 102SCM_GPROC (s_vector_length, "vector-length", 1, 0, 0, scm_vector_length, g_vector_length);
1e6808ea 103/* Returns the number of elements in @var{vector} as an exact integer. */
0f2d19dd 104SCM
f172c0b7 105scm_vector_length (SCM v)
0f2d19dd 106{
6e708ef2
MV
107 if (SCM_I_IS_VECTOR (v))
108 return scm_from_size_t (SCM_I_VECTOR_LENGTH (v));
04b87de5 109 else if (SCM_I_ARRAYP (v) && SCM_I_ARRAY_NDIM (v) == 1)
de5c0f58 110 {
04b87de5 111 scm_t_array_dim *dim = SCM_I_ARRAY_DIMS (v);
de5c0f58
MV
112 return scm_from_size_t (dim->ubnd - dim->lbnd + 1);
113 }
114 else
115 SCM_WTA_DISPATCH_1 (g_vector_length, v, 1, NULL);
0f2d19dd
JB
116}
117
88797580
MV
118size_t
119scm_c_vector_length (SCM v)
120{
6e708ef2
MV
121 if (SCM_I_IS_VECTOR (v))
122 return SCM_I_VECTOR_LENGTH (v);
88797580 123 else
de5c0f58 124 return scm_to_size_t (scm_vector_length (v));
88797580
MV
125}
126
f172c0b7 127SCM_REGISTER_PROC (s_list_to_vector, "list->vector", 1, 0, 0, scm_vector);
5ffe9968 128/*
942e5b91
MG
129 "Return a newly created vector initialized to the elements of"
130 "the list @var{list}.\n\n"
131 "@lisp\n"
132 "(vector->list '#(dah dah didah)) @result{} (dah dah didah)\n"
133 "(list->vector '(dididit dah)) @result{} #(dididit dah)\n"
134 "@end lisp")
5ffe9968 135*/
a1ec6916 136SCM_DEFINE (scm_vector, "vector", 0, 0, 1,
f172c0b7 137 (SCM l),
8f85c0c6
NJ
138 "@deffnx {Scheme Procedure} list->vector l\n"
139 "Return a newly allocated vector composed of the\n"
1e6808ea
MG
140 "given arguments. Analogous to @code{list}.\n"
141 "\n"
942e5b91 142 "@lisp\n"
1e6808ea 143 "(vector 'a 'b 'c) @result{} #(a b c)\n"
942e5b91 144 "@end lisp")
1bbd0b84 145#define FUNC_NAME s_scm_vector
0f2d19dd
JB
146{
147 SCM res;
22a52da1 148 SCM *data;
1d0df896 149 long i, len;
6e708ef2 150 scm_t_array_handle handle;
22a52da1 151
1d0df896 152 SCM_VALIDATE_LIST_COPYLEN (1, l, len);
34d19ef6 153
6e708ef2
MV
154 res = scm_c_make_vector (len, SCM_UNSPECIFIED);
155 data = scm_vector_writable_elements (res, &handle, NULL, NULL);
1d0df896 156 i = 0;
c8857a4d 157 while (scm_is_pair (l) && i < len)
22a52da1 158 {
1d0df896 159 data[i] = SCM_CAR (l);
22a52da1 160 l = SCM_CDR (l);
6e708ef2 161 i += 1;
22a52da1
DH
162 }
163
c8857a4d
MV
164 scm_array_handle_release (&handle);
165
0f2d19dd
JB
166 return res;
167}
1bbd0b84 168#undef FUNC_NAME
0f2d19dd 169
f172c0b7 170SCM_GPROC (s_vector_ref, "vector-ref", 2, 0, 0, scm_vector_ref, g_vector_ref);
1cc91f1b 171
5ffe9968
GB
172/*
173 "@var{k} must be a valid index of @var{vector}.\n"
174 "@samp{Vector-ref} returns the contents of element @var{k} of\n"
175 "@var{vector}.\n\n"
942e5b91
MG
176 "@lisp\n"
177 "(vector-ref '#(1 1 2 3 5 8 13 21) 5) @result{} 8\n"
5ffe9968
GB
178 "(vector-ref '#(1 1 2 3 5 8 13 21)\n"
179 " (let ((i (round (* 2 (acos -1)))))\n"
180 " (if (inexact? i)\n"
181 " (inexact->exact i)\n"
942e5b91
MG
182 " i))) @result{} 13\n"
183 "@end lisp"
5ffe9968
GB
184*/
185
0f2d19dd 186SCM
ea633082 187scm_vector_ref (SCM v, SCM k)
685c0d71 188#define FUNC_NAME s_vector_ref
0f2d19dd 189{
de5c0f58 190 return scm_c_vector_ref (v, scm_to_size_t (k));
0f2d19dd 191}
685c0d71 192#undef FUNC_NAME
0f2d19dd 193
88797580
MV
194SCM
195scm_c_vector_ref (SCM v, size_t k)
196{
6e708ef2 197 if (SCM_I_IS_VECTOR (v))
de5c0f58 198 {
6e708ef2 199 if (k >= SCM_I_VECTOR_LENGTH (v))
de5c0f58 200 scm_out_of_range (NULL, scm_from_size_t (k));
6e708ef2 201 return (SCM_I_VECTOR_ELTS(v))[k];
de5c0f58 202 }
04b87de5 203 else if (SCM_I_ARRAYP (v) && SCM_I_ARRAY_NDIM (v) == 1)
88797580 204 {
04b87de5
MV
205 scm_t_array_dim *dim = SCM_I_ARRAY_DIMS (v);
206 SCM vv = SCM_I_ARRAY_V (v);
6e708ef2
MV
207 if (SCM_I_IS_VECTOR (vv))
208 {
209 if (k >= dim->ubnd - dim->lbnd + 1)
210 scm_out_of_range (NULL, scm_from_size_t (k));
04b87de5 211 k = SCM_I_ARRAY_BASE (v) + k*dim->inc;
6e708ef2
MV
212 return (SCM_I_VECTOR_ELTS (vv))[k];
213 }
214 scm_wrong_type_arg_msg (NULL, 0, v, "non-uniform vector");
88797580 215 }
de5c0f58
MV
216 else
217 SCM_WTA_DISPATCH_2 (g_vector_ref, v, scm_from_size_t (k), 2, NULL);
88797580
MV
218}
219
f172c0b7 220SCM_GPROC (s_vector_set_x, "vector-set!", 3, 0, 0, scm_vector_set_x, g_vector_set_x);
1cc91f1b 221
942e5b91
MG
222/* "@var{k} must be a valid index of @var{vector}.\n"
223 "@code{Vector-set!} stores @var{obj} in element @var{k} of @var{vector}.\n"
224 "The value returned by @samp{vector-set!} is unspecified.\n"
225 "@lisp\n"
226 "(let ((vec (vector 0 '(2 2 2 2) "Anna")))\n"
227 " (vector-set! vec 1 '("Sue" "Sue"))\n"
228 " vec) @result{} #(0 ("Sue" "Sue") "Anna")\n"
229 "(vector-set! '#(0 1 2) 1 "doe") @result{} @emph{error} ; constant vector\n"
230 "@end lisp"
5ffe9968
GB
231*/
232
0f2d19dd 233SCM
f172c0b7 234scm_vector_set_x (SCM v, SCM k, SCM obj)
685c0d71 235#define FUNC_NAME s_vector_set_x
0f2d19dd 236{
de5c0f58 237 scm_c_vector_set_x (v, scm_to_size_t (k), obj);
60c497a3 238 return SCM_UNSPECIFIED;
0f2d19dd 239}
685c0d71 240#undef FUNC_NAME
0f2d19dd 241
de5c0f58 242void
88797580
MV
243scm_c_vector_set_x (SCM v, size_t k, SCM obj)
244{
6e708ef2 245 if (SCM_I_IS_VECTOR (v))
88797580 246 {
6e708ef2 247 if (k >= SCM_I_VECTOR_LENGTH (v))
de5c0f58 248 scm_out_of_range (NULL, scm_from_size_t (k));
6e708ef2 249 (SCM_I_VECTOR_WELTS(v))[k] = obj;
de5c0f58 250 }
04b87de5 251 else if (SCM_I_ARRAYP (v) && SCM_I_ARRAY_NDIM (v) == 1)
de5c0f58 252 {
04b87de5
MV
253 scm_t_array_dim *dim = SCM_I_ARRAY_DIMS (v);
254 SCM vv = SCM_I_ARRAY_V (v);
6e708ef2
MV
255 if (SCM_I_IS_VECTOR (vv))
256 {
257 if (k >= dim->ubnd - dim->lbnd + 1)
258 scm_out_of_range (NULL, scm_from_size_t (k));
04b87de5 259 k = SCM_I_ARRAY_BASE (v) + k*dim->inc;
6e708ef2
MV
260 (SCM_I_VECTOR_WELTS (vv))[k] = obj;
261 }
262 else
263 scm_wrong_type_arg_msg (NULL, 0, v, "non-uniform vector");
88797580
MV
264 }
265 else
266 {
de5c0f58
MV
267 if (SCM_UNPACK (g_vector_set_x))
268 scm_apply_generic (g_vector_set_x,
269 scm_list_3 (v, scm_from_size_t (k), obj));
270 else
271 scm_wrong_type_arg_msg (NULL, 0, v, "vector");
88797580
MV
272 }
273}
0f2d19dd 274
a1ec6916 275SCM_DEFINE (scm_make_vector, "make-vector", 1, 1, 0,
1bbd0b84 276 (SCM k, SCM fill),
1e6808ea 277 "Return a newly allocated vector of @var{k} elements. If a\n"
8f85c0c6
NJ
278 "second argument is given, then each position is initialized to\n"
279 "@var{fill}. Otherwise the initial contents of each position is\n"
1e6808ea 280 "unspecified.")
1bbd0b84 281#define FUNC_NAME s_scm_make_vector
0f2d19dd 282{
6e708ef2 283 size_t l = scm_to_unsigned_integer (k, 0, VECTOR_MAX_LENGTH);
e11e83f3 284
1b9be268 285 if (SCM_UNBNDP (fill))
d60cebe2 286 fill = SCM_UNSPECIFIED;
e11e83f3
MV
287
288 return scm_c_make_vector (l, fill);
00ffa0e7
KN
289}
290#undef FUNC_NAME
291
e382fdbe 292
00ffa0e7 293SCM
88797580 294scm_c_make_vector (size_t k, SCM fill)
00ffa0e7
KN
295#define FUNC_NAME s_scm_make_vector
296{
297 SCM v;
6e708ef2 298 SCM *base;
1b9be268 299
e382fdbe
DH
300 if (k > 0)
301 {
c014a02e 302 unsigned long int j;
1b9be268 303
6e708ef2 304 SCM_ASSERT_RANGE (1, scm_from_ulong (k), k <= VECTOR_MAX_LENGTH);
1b9be268 305
6e708ef2 306 base = scm_gc_malloc (k * sizeof (SCM), "vector");
e382fdbe 307 for (j = 0; j != k; ++j)
6e708ef2 308 base[j] = fill;
e382fdbe
DH
309 }
310 else
311 base = NULL;
1b9be268 312
6e708ef2 313 v = scm_cell ((k << 8) | scm_tc7_vector, (scm_t_bits) base);
e382fdbe 314 scm_remember_upto_here_1 (fill);
1b9be268 315
0f2d19dd
JB
316 return v;
317}
1bbd0b84 318#undef FUNC_NAME
0f2d19dd 319
6e708ef2
MV
320SCM_DEFINE (scm_vector_copy, "vector-copy", 1, 0, 0,
321 (SCM vec),
322 "Return a copy of @var{vec}.")
323#define FUNC_NAME s_scm_vector_copy
324{
325 scm_t_array_handle handle;
326 size_t i, len;
327 ssize_t inc;
328 const SCM *src;
329 SCM *dst;
330
331 src = scm_vector_elements (vec, &handle, &len, &inc);
332 dst = scm_gc_malloc (len * sizeof (SCM), "vector");
333 for (i = 0; i < len; i++, src += inc)
334 dst[i] = *src;
c8857a4d 335 scm_array_handle_release (&handle);
6e708ef2
MV
336
337 return scm_cell ((len << 8) | scm_tc7_vector, (scm_t_bits) dst);
338}
339#undef FUNC_NAME
340
341void
342scm_i_vector_free (SCM vec)
343{
344 scm_gc_free (SCM_I_VECTOR_WELTS (vec),
345 SCM_I_VECTOR_LENGTH (vec) * sizeof(SCM),
346 "vector");
347}
348
349/* Allocate memory for a weak vector on behalf of the caller. The allocated
350 * vector will be of the given weak vector subtype. It will contain size
351 * elements which are initialized with the 'fill' object, or, if 'fill' is
352 * undefined, with an unspecified object.
353 */
354SCM
355scm_i_allocate_weak_vector (scm_t_bits type, SCM size, SCM fill)
356{
357 size_t c_size;
358 SCM *base;
359 SCM v;
360
361 c_size = scm_to_unsigned_integer (size, 0, VECTOR_MAX_LENGTH);
362
363 if (c_size > 0)
364 {
365 size_t j;
366
367 if (SCM_UNBNDP (fill))
368 fill = SCM_UNSPECIFIED;
369
370 base = scm_gc_malloc (c_size * sizeof (SCM), "weak vector");
371 for (j = 0; j != c_size; ++j)
372 base[j] = fill;
373 }
374 else
375 base = NULL;
376
377 v = scm_double_cell ((c_size << 8) | scm_tc7_wvect,
378 (scm_t_bits) base,
379 type,
380 SCM_UNPACK (SCM_EOL));
381 scm_remember_upto_here_1 (fill);
382
383 return v;
384}
e382fdbe 385
3b3b36dd 386SCM_DEFINE (scm_vector_to_list, "vector->list", 1, 0, 0,
1e6808ea 387 (SCM v),
8f85c0c6 388 "Return a newly allocated list composed of the elements of @var{v}.\n"
1e6808ea 389 "\n"
942e5b91
MG
390 "@lisp\n"
391 "(vector->list '#(dah dah didah)) @result{} (dah dah didah)\n"
392 "(list->vector '(dididit dah)) @result{} #(dididit dah)\n"
393 "@end lisp")
1bbd0b84 394#define FUNC_NAME s_scm_vector_to_list
0f2d19dd 395{
6e708ef2
MV
396 SCM res = SCM_EOL;
397 const SCM *data;
398 scm_t_array_handle handle;
22be72d3 399 size_t i, count, len;
6e708ef2
MV
400 ssize_t inc;
401
402 data = scm_vector_elements (v, &handle, &len, &inc);
22be72d3
LC
403 for (i = (len - 1) * inc, count = 0;
404 count < len;
405 i -= inc, count++)
406 res = scm_cons (data[i], res);
407
c8857a4d 408 scm_array_handle_release (&handle);
6e708ef2 409 return res;
0f2d19dd 410}
1bbd0b84 411#undef FUNC_NAME
0f2d19dd
JB
412
413
a1ec6916 414SCM_DEFINE (scm_vector_fill_x, "vector-fill!", 2, 0, 0,
1e6808ea 415 (SCM v, SCM fill),
8f85c0c6 416 "Store @var{fill} in every position of @var{vector}. The value\n"
1e6808ea 417 "returned by @code{vector-fill!} is unspecified.")
1bbd0b84 418#define FUNC_NAME s_scm_vector_fill_x
0f2d19dd 419{
6e708ef2
MV
420 scm_t_array_handle handle;
421 SCM *data;
422 size_t i, len;
423 ssize_t inc;
424
425 data = scm_vector_writable_elements (v, &handle, &len, &inc);
426 for (i = 0; i < len; i += inc)
427 data[i] = fill;
c8857a4d 428 scm_array_handle_release (&handle);
6e708ef2 429 return SCM_UNSPECIFIED;
0f2d19dd 430}
1bbd0b84 431#undef FUNC_NAME
0f2d19dd
JB
432
433
0f2d19dd 434SCM
354116f7 435scm_i_vector_equal_p (SCM x, SCM y)
0f2d19dd 436{
c014a02e 437 long i;
6e708ef2
MV
438 for (i = SCM_I_VECTOR_LENGTH (x) - 1; i >= 0; i--)
439 if (scm_is_false (scm_equal_p (SCM_I_VECTOR_ELTS (x)[i],
440 SCM_I_VECTOR_ELTS (y)[i])))
0f2d19dd
JB
441 return SCM_BOOL_F;
442 return SCM_BOOL_T;
443}
444
445
a1ec6916 446SCM_DEFINE (scm_vector_move_left_x, "vector-move-left!", 5, 0, 0,
1bbd0b84 447 (SCM vec1, SCM start1, SCM end1, SCM vec2, SCM start2),
694a9bb3
NJ
448 "Copy elements from @var{vec1}, positions @var{start1} to @var{end1},\n"
449 "to @var{vec2} starting at position @var{start2}. @var{start1} and\n"
450 "@var{start2} are inclusive indices; @var{end1} is exclusive.\n\n"
451 "@code{vector-move-left!} copies elements in leftmost order.\n"
452 "Therefore, in the case where @var{vec1} and @var{vec2} refer to the\n"
453 "same vector, @code{vector-move-left!} is usually appropriate when\n"
454 "@var{start1} is greater than @var{start2}.")
1bbd0b84 455#define FUNC_NAME s_scm_vector_move_left_x
0f2d19dd 456{
6e708ef2
MV
457 scm_t_array_handle handle1, handle2;
458 const SCM *elts1;
459 SCM *elts2;
de5c0f58 460 size_t len1, len2;
6e708ef2 461 ssize_t inc1, inc2;
a55c2b68 462 size_t i, j, e;
6e708ef2
MV
463
464 elts1 = scm_vector_elements (vec1, &handle1, &len1, &inc1);
465 elts2 = scm_vector_writable_elements (vec2, &handle2, &len2, &inc2);
34d19ef6 466
de5c0f58
MV
467 i = scm_to_unsigned_integer (start1, 0, len1);
468 e = scm_to_unsigned_integer (end1, i, len1);
469 j = scm_to_unsigned_integer (start2, 0, len2 - (i-e));
470
6e708ef2
MV
471 i *= inc1;
472 e *= inc1;
473 j *= inc2;
474 for (; i < e; i += inc1, j += inc2)
475 elts2[j] = elts1[i];
476
c8857a4d
MV
477 scm_array_handle_release (&handle2);
478 scm_array_handle_release (&handle1);
479
0f2d19dd
JB
480 return SCM_UNSPECIFIED;
481}
1bbd0b84 482#undef FUNC_NAME
0f2d19dd 483
a1ec6916 484SCM_DEFINE (scm_vector_move_right_x, "vector-move-right!", 5, 0, 0,
1bbd0b84 485 (SCM vec1, SCM start1, SCM end1, SCM vec2, SCM start2),
694a9bb3
NJ
486 "Copy elements from @var{vec1}, positions @var{start1} to @var{end1},\n"
487 "to @var{vec2} starting at position @var{start2}. @var{start1} and\n"
488 "@var{start2} are inclusive indices; @var{end1} is exclusive.\n\n"
489 "@code{vector-move-right!} copies elements in rightmost order.\n"
490 "Therefore, in the case where @var{vec1} and @var{vec2} refer to the\n"
491 "same vector, @code{vector-move-right!} is usually appropriate when\n"
492 "@var{start1} is less than @var{start2}.")
1bbd0b84 493#define FUNC_NAME s_scm_vector_move_right_x
0f2d19dd 494{
6e708ef2
MV
495 scm_t_array_handle handle1, handle2;
496 const SCM *elts1;
497 SCM *elts2;
de5c0f58 498 size_t len1, len2;
6e708ef2 499 ssize_t inc1, inc2;
a55c2b68 500 size_t i, j, e;
6e708ef2
MV
501
502 elts1 = scm_vector_elements (vec1, &handle1, &len1, &inc1);
503 elts2 = scm_vector_writable_elements (vec2, &handle2, &len2, &inc2);
0f2d19dd 504
de5c0f58
MV
505 i = scm_to_unsigned_integer (start1, 0, len1);
506 e = scm_to_unsigned_integer (end1, i, len1);
507 j = scm_to_unsigned_integer (start2, 0, len2 - (i-e));
508
6e708ef2
MV
509 i *= inc1;
510 e *= inc1;
511 j *= inc2;
512 while (i < e)
de5c0f58 513 {
6e708ef2
MV
514 e -= inc1;
515 j -= inc2;
516 elts2[j] = elts1[e];
de5c0f58 517 }
6e708ef2 518
c8857a4d
MV
519 scm_array_handle_release (&handle2);
520 scm_array_handle_release (&handle1);
521
0f2d19dd
JB
522 return SCM_UNSPECIFIED;
523}
1bbd0b84 524#undef FUNC_NAME
0f2d19dd
JB
525
526
88797580
MV
527/* Generalized vectors. */
528
529int
530scm_is_generalized_vector (SCM obj)
531{
532 return (scm_is_vector (obj)
533 || scm_is_string (obj)
534 || scm_is_bitvector (obj)
535 || scm_is_uniform_vector (obj));
536}
537
538SCM_DEFINE (scm_generalized_vector_p, "generalized-vector?", 1, 0, 0,
539 (SCM obj),
540 "Return @code{#t} if @var{obj} is a vector, string,\n"
541 "bitvector, or uniform numeric vector.")
542#define FUNC_NAME s_scm_generalized_vector_p
543{
544 return scm_from_bool (scm_is_generalized_vector (obj));
545}
546#undef FUNC_NAME
547
354116f7
MV
548void
549scm_generalized_vector_get_handle (SCM vec, scm_t_array_handle *h)
550{
551 scm_array_get_handle (vec, h);
552 if (scm_array_handle_rank (h) != 1)
553 scm_wrong_type_arg_msg (NULL, 0, vec, "vector");
554}
555
88797580
MV
556size_t
557scm_c_generalized_vector_length (SCM v)
558{
559 if (scm_is_vector (v))
560 return scm_c_vector_length (v);
561 else if (scm_is_string (v))
562 return scm_c_string_length (v);
563 else if (scm_is_bitvector (v))
564 return scm_c_bitvector_length (v);
565 else if (scm_is_uniform_vector (v))
566 return scm_c_uniform_vector_length (v);
567 else
568 scm_wrong_type_arg_msg (NULL, 0, v, "generalized vector");
569}
570
571SCM_DEFINE (scm_generalized_vector_length, "generalized-vector-length", 1, 0, 0,
572 (SCM v),
573 "Return the length of the generalized vector @var{v}.")
574#define FUNC_NAME s_scm_generalized_vector_length
575{
576 return scm_from_size_t (scm_c_generalized_vector_length (v));
577}
578#undef FUNC_NAME
579
580SCM
581scm_c_generalized_vector_ref (SCM v, size_t idx)
582{
583 if (scm_is_vector (v))
584 return scm_c_vector_ref (v, idx);
585 else if (scm_is_string (v))
586 return scm_c_string_ref (v, idx);
587 else if (scm_is_bitvector (v))
588 return scm_c_bitvector_ref (v, idx);
589 else if (scm_is_uniform_vector (v))
590 return scm_c_uniform_vector_ref (v, idx);
591 else
592 scm_wrong_type_arg_msg (NULL, 0, v, "generalized vector");
593}
594
595SCM_DEFINE (scm_generalized_vector_ref, "generalized-vector-ref", 2, 0, 0,
596 (SCM v, SCM idx),
597 "Return the element at index @var{idx} of the\n"
598 "generalized vector @var{v}.")
599#define FUNC_NAME s_scm_generalized_vector_ref
600{
601 return scm_c_generalized_vector_ref (v, scm_to_size_t (idx));
602}
603#undef FUNC_NAME
604
605void
606scm_c_generalized_vector_set_x (SCM v, size_t idx, SCM val)
607{
608 if (scm_is_vector (v))
609 scm_c_vector_set_x (v, idx, val);
610 else if (scm_is_string (v))
611 scm_c_string_set_x (v, idx, val);
612 else if (scm_is_bitvector (v))
613 scm_c_bitvector_set_x (v, idx, val);
614 else if (scm_is_uniform_vector (v))
615 scm_c_uniform_vector_set_x (v, idx, val);
616 else
617 scm_wrong_type_arg_msg (NULL, 0, v, "generalized vector");
618}
619
620SCM_DEFINE (scm_generalized_vector_set_x, "generalized-vector-set!", 3, 0, 0,
621 (SCM v, SCM idx, SCM val),
622 "Set the element at index @var{idx} of the\n"
623 "generalized vector @var{v} to @var{val}.")
624#define FUNC_NAME s_scm_generalized_vector_set_x
625{
626 scm_c_generalized_vector_set_x (v, scm_to_size_t (idx), val);
627 return SCM_UNSPECIFIED;
628}
629#undef FUNC_NAME
630
631SCM_DEFINE (scm_generalized_vector_to_list, "generalized-vector->list", 1, 0, 0,
632 (SCM v),
633 "Return a new list whose elements are the elements of the\n"
634 "generalized vector @var{v}.")
635#define FUNC_NAME s_scm_generalized_vector_to_list
636{
637 if (scm_is_vector (v))
638 return scm_vector_to_list (v);
639 else if (scm_is_string (v))
640 return scm_string_to_list (v);
641 else if (scm_is_bitvector (v))
642 return scm_bitvector_to_list (v);
643 else if (scm_is_uniform_vector (v))
644 return scm_uniform_vector_to_list (v);
645 else
646 scm_wrong_type_arg_msg (NULL, 0, v, "generalized vector");
647}
648#undef FUNC_NAME
649
1cc91f1b 650
0f2d19dd
JB
651void
652scm_init_vectors ()
0f2d19dd 653{
7c33806a
DH
654 scm_nullvect = scm_c_make_vector (0, SCM_UNDEFINED);
655
a0599745 656#include "libguile/vectors.x"
0f2d19dd
JB
657}
658
89e00824
ML
659
660/*
661 Local Variables:
662 c-file-style: "gnu"
663 End:
664*/