Update README on using libraries in non-standard locations
[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
MV
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.
0f2d19dd 7 *
73be1d9e
MV
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.
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
92205699 15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
73be1d9e 16 */
1bbd0b84 17
1bbd0b84 18
0f2d19dd 19\f
dbb605f5
LC
20#ifdef HAVE_CONFIG_H
21# include <config.h>
22#endif
0f2d19dd 23
a0599745
MD
24#include "libguile/_scm.h"
25#include "libguile/eq.h"
26#include "libguile/root.h"
27#include "libguile/strings.h"
c96d76b8 28#include "libguile/lang.h"
a0599745
MD
29
30#include "libguile/validate.h"
31#include "libguile/vectors.h"
32#include "libguile/unif.h"
de5c0f58 33#include "libguile/ramap.h"
88797580
MV
34#include "libguile/srfi-4.h"
35#include "libguile/strings.h"
36#include "libguile/srfi-13.h"
1d0df896 37#include "libguile/dynwind.h"
6e708ef2 38#include "libguile/deprecation.h"
88797580 39
0f2d19dd
JB
40\f
41
6e708ef2
MV
42#define VECTOR_MAX_LENGTH (SCM_T_BITS_MAX >> 8)
43
6e708ef2
MV
44int
45scm_is_vector (SCM obj)
46{
47 if (SCM_I_IS_VECTOR (obj))
48 return 1;
04b87de5 49 if (SCM_I_ARRAYP (obj) && SCM_I_ARRAY_NDIM (obj) == 1)
6e708ef2 50 {
04b87de5 51 SCM v = SCM_I_ARRAY_V (obj);
6e708ef2
MV
52 return SCM_I_IS_VECTOR (v);
53 }
54 return 0;
55}
56
57int
58scm_is_simple_vector (SCM obj)
1d0df896 59{
6e708ef2 60 return SCM_I_IS_VECTOR (obj);
1d0df896
MV
61}
62
354116f7
MV
63const SCM *
64scm_vector_elements (SCM vec, scm_t_array_handle *h,
65 size_t *lenp, ssize_t *incp)
66{
67 scm_generalized_vector_get_handle (vec, h);
68 if (lenp)
69 {
70 scm_t_array_dim *dim = scm_array_handle_dims (h);
71 *lenp = dim->ubnd - dim->lbnd + 1;
72 *incp = dim->inc;
73 }
74 return scm_array_handle_elements (h);
75}
76
77SCM *
78scm_vector_writable_elements (SCM vec, scm_t_array_handle *h,
79 size_t *lenp, ssize_t *incp)
80{
81 scm_generalized_vector_get_handle (vec, h);
82 if (lenp)
83 {
84 scm_t_array_dim *dim = scm_array_handle_dims (h);
85 *lenp = dim->ubnd - dim->lbnd + 1;
86 *incp = dim->inc;
87 }
88 return scm_array_handle_writable_elements (h);
89}
90
a1ec6916 91SCM_DEFINE (scm_vector_p, "vector?", 1, 0, 0,
5ffe9968 92 (SCM obj),
1e6808ea
MG
93 "Return @code{#t} if @var{obj} is a vector, otherwise return\n"
94 "@code{#f}.")
1bbd0b84 95#define FUNC_NAME s_scm_vector_p
0f2d19dd 96{
de5c0f58 97 return scm_from_bool (scm_is_vector (obj));
0f2d19dd 98}
1bbd0b84 99#undef FUNC_NAME
0f2d19dd 100
f172c0b7 101SCM_GPROC (s_vector_length, "vector-length", 1, 0, 0, scm_vector_length, g_vector_length);
1e6808ea 102/* Returns the number of elements in @var{vector} as an exact integer. */
0f2d19dd 103SCM
f172c0b7 104scm_vector_length (SCM v)
0f2d19dd 105{
6e708ef2
MV
106 if (SCM_I_IS_VECTOR (v))
107 return scm_from_size_t (SCM_I_VECTOR_LENGTH (v));
04b87de5 108 else if (SCM_I_ARRAYP (v) && SCM_I_ARRAY_NDIM (v) == 1)
de5c0f58 109 {
04b87de5 110 scm_t_array_dim *dim = SCM_I_ARRAY_DIMS (v);
de5c0f58
MV
111 return scm_from_size_t (dim->ubnd - dim->lbnd + 1);
112 }
113 else
114 SCM_WTA_DISPATCH_1 (g_vector_length, v, 1, NULL);
0f2d19dd
JB
115}
116
88797580
MV
117size_t
118scm_c_vector_length (SCM v)
119{
6e708ef2
MV
120 if (SCM_I_IS_VECTOR (v))
121 return SCM_I_VECTOR_LENGTH (v);
88797580 122 else
de5c0f58 123 return scm_to_size_t (scm_vector_length (v));
88797580
MV
124}
125
f172c0b7 126SCM_REGISTER_PROC (s_list_to_vector, "list->vector", 1, 0, 0, scm_vector);
5ffe9968 127/*
942e5b91
MG
128 "Return a newly created vector initialized to the elements of"
129 "the list @var{list}.\n\n"
130 "@lisp\n"
131 "(vector->list '#(dah dah didah)) @result{} (dah dah didah)\n"
132 "(list->vector '(dididit dah)) @result{} #(dididit dah)\n"
133 "@end lisp")
5ffe9968 134*/
a1ec6916 135SCM_DEFINE (scm_vector, "vector", 0, 0, 1,
f172c0b7 136 (SCM l),
8f85c0c6
NJ
137 "@deffnx {Scheme Procedure} list->vector l\n"
138 "Return a newly allocated vector composed of the\n"
1e6808ea
MG
139 "given arguments. Analogous to @code{list}.\n"
140 "\n"
942e5b91 141 "@lisp\n"
1e6808ea 142 "(vector 'a 'b 'c) @result{} #(a b c)\n"
942e5b91 143 "@end lisp")
1bbd0b84 144#define FUNC_NAME s_scm_vector
0f2d19dd
JB
145{
146 SCM res;
22a52da1 147 SCM *data;
1d0df896 148 long i, len;
6e708ef2 149 scm_t_array_handle handle;
22a52da1 150
1d0df896 151 SCM_VALIDATE_LIST_COPYLEN (1, l, len);
34d19ef6 152
6e708ef2
MV
153 res = scm_c_make_vector (len, SCM_UNSPECIFIED);
154 data = scm_vector_writable_elements (res, &handle, NULL, NULL);
1d0df896 155 i = 0;
c8857a4d 156 while (scm_is_pair (l) && i < len)
22a52da1 157 {
1d0df896 158 data[i] = SCM_CAR (l);
22a52da1 159 l = SCM_CDR (l);
6e708ef2 160 i += 1;
22a52da1
DH
161 }
162
c8857a4d
MV
163 scm_array_handle_release (&handle);
164
0f2d19dd
JB
165 return res;
166}
1bbd0b84 167#undef FUNC_NAME
0f2d19dd 168
f172c0b7 169SCM_GPROC (s_vector_ref, "vector-ref", 2, 0, 0, scm_vector_ref, g_vector_ref);
1cc91f1b 170
5ffe9968
GB
171/*
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"
174 "@var{vector}.\n\n"
942e5b91
MG
175 "@lisp\n"
176 "(vector-ref '#(1 1 2 3 5 8 13 21) 5) @result{} 8\n"
5ffe9968
GB
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"
942e5b91
MG
181 " i))) @result{} 13\n"
182 "@end lisp"
5ffe9968
GB
183*/
184
0f2d19dd 185SCM
ea633082 186scm_vector_ref (SCM v, SCM k)
685c0d71 187#define FUNC_NAME s_vector_ref
0f2d19dd 188{
de5c0f58 189 return scm_c_vector_ref (v, scm_to_size_t (k));
0f2d19dd 190}
685c0d71 191#undef FUNC_NAME
0f2d19dd 192
88797580
MV
193SCM
194scm_c_vector_ref (SCM v, size_t k)
195{
6e708ef2 196 if (SCM_I_IS_VECTOR (v))
de5c0f58 197 {
6e708ef2 198 if (k >= SCM_I_VECTOR_LENGTH (v))
de5c0f58 199 scm_out_of_range (NULL, scm_from_size_t (k));
6e708ef2 200 return (SCM_I_VECTOR_ELTS(v))[k];
de5c0f58 201 }
04b87de5 202 else if (SCM_I_ARRAYP (v) && SCM_I_ARRAY_NDIM (v) == 1)
88797580 203 {
04b87de5
MV
204 scm_t_array_dim *dim = SCM_I_ARRAY_DIMS (v);
205 SCM vv = SCM_I_ARRAY_V (v);
6e708ef2
MV
206 if (SCM_I_IS_VECTOR (vv))
207 {
208 if (k >= dim->ubnd - dim->lbnd + 1)
209 scm_out_of_range (NULL, scm_from_size_t (k));
04b87de5 210 k = SCM_I_ARRAY_BASE (v) + k*dim->inc;
6e708ef2
MV
211 return (SCM_I_VECTOR_ELTS (vv))[k];
212 }
213 scm_wrong_type_arg_msg (NULL, 0, v, "non-uniform vector");
88797580 214 }
de5c0f58
MV
215 else
216 SCM_WTA_DISPATCH_2 (g_vector_ref, v, scm_from_size_t (k), 2, NULL);
88797580
MV
217}
218
f172c0b7 219SCM_GPROC (s_vector_set_x, "vector-set!", 3, 0, 0, scm_vector_set_x, g_vector_set_x);
1cc91f1b 220
942e5b91
MG
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"
224 "@lisp\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"
229 "@end lisp"
5ffe9968
GB
230*/
231
0f2d19dd 232SCM
f172c0b7 233scm_vector_set_x (SCM v, SCM k, SCM obj)
685c0d71 234#define FUNC_NAME s_vector_set_x
0f2d19dd 235{
de5c0f58 236 scm_c_vector_set_x (v, scm_to_size_t (k), obj);
60c497a3 237 return SCM_UNSPECIFIED;
0f2d19dd 238}
685c0d71 239#undef FUNC_NAME
0f2d19dd 240
de5c0f58 241void
88797580
MV
242scm_c_vector_set_x (SCM v, size_t k, SCM obj)
243{
6e708ef2 244 if (SCM_I_IS_VECTOR (v))
88797580 245 {
6e708ef2 246 if (k >= SCM_I_VECTOR_LENGTH (v))
de5c0f58 247 scm_out_of_range (NULL, scm_from_size_t (k));
6e708ef2 248 (SCM_I_VECTOR_WELTS(v))[k] = obj;
de5c0f58 249 }
04b87de5 250 else if (SCM_I_ARRAYP (v) && SCM_I_ARRAY_NDIM (v) == 1)
de5c0f58 251 {
04b87de5
MV
252 scm_t_array_dim *dim = SCM_I_ARRAY_DIMS (v);
253 SCM vv = SCM_I_ARRAY_V (v);
6e708ef2
MV
254 if (SCM_I_IS_VECTOR (vv))
255 {
256 if (k >= dim->ubnd - dim->lbnd + 1)
257 scm_out_of_range (NULL, scm_from_size_t (k));
04b87de5 258 k = SCM_I_ARRAY_BASE (v) + k*dim->inc;
6e708ef2
MV
259 (SCM_I_VECTOR_WELTS (vv))[k] = obj;
260 }
261 else
262 scm_wrong_type_arg_msg (NULL, 0, v, "non-uniform vector");
88797580
MV
263 }
264 else
265 {
de5c0f58
MV
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));
269 else
270 scm_wrong_type_arg_msg (NULL, 0, v, "vector");
88797580
MV
271 }
272}
0f2d19dd 273
a1ec6916 274SCM_DEFINE (scm_make_vector, "make-vector", 1, 1, 0,
1bbd0b84 275 (SCM k, SCM fill),
1e6808ea 276 "Return a newly allocated vector of @var{k} elements. If a\n"
8f85c0c6
NJ
277 "second argument is given, then each position is initialized to\n"
278 "@var{fill}. Otherwise the initial contents of each position is\n"
1e6808ea 279 "unspecified.")
1bbd0b84 280#define FUNC_NAME s_scm_make_vector
0f2d19dd 281{
6e708ef2 282 size_t l = scm_to_unsigned_integer (k, 0, VECTOR_MAX_LENGTH);
e11e83f3 283
1b9be268 284 if (SCM_UNBNDP (fill))
d60cebe2 285 fill = SCM_UNSPECIFIED;
e11e83f3
MV
286
287 return scm_c_make_vector (l, fill);
00ffa0e7
KN
288}
289#undef FUNC_NAME
290
e382fdbe 291
00ffa0e7 292SCM
88797580 293scm_c_make_vector (size_t k, SCM fill)
00ffa0e7
KN
294#define FUNC_NAME s_scm_make_vector
295{
296 SCM v;
6e708ef2 297 SCM *base;
1b9be268 298
e382fdbe
DH
299 if (k > 0)
300 {
c014a02e 301 unsigned long int j;
1b9be268 302
6e708ef2 303 SCM_ASSERT_RANGE (1, scm_from_ulong (k), k <= VECTOR_MAX_LENGTH);
1b9be268 304
6e708ef2 305 base = scm_gc_malloc (k * sizeof (SCM), "vector");
e382fdbe 306 for (j = 0; j != k; ++j)
6e708ef2 307 base[j] = fill;
e382fdbe
DH
308 }
309 else
310 base = NULL;
1b9be268 311
6e708ef2 312 v = scm_cell ((k << 8) | scm_tc7_vector, (scm_t_bits) base);
e382fdbe 313 scm_remember_upto_here_1 (fill);
1b9be268 314
0f2d19dd
JB
315 return v;
316}
1bbd0b84 317#undef FUNC_NAME
0f2d19dd 318
6e708ef2
MV
319SCM_DEFINE (scm_vector_copy, "vector-copy", 1, 0, 0,
320 (SCM vec),
321 "Return a copy of @var{vec}.")
322#define FUNC_NAME s_scm_vector_copy
323{
324 scm_t_array_handle handle;
325 size_t i, len;
326 ssize_t inc;
327 const SCM *src;
328 SCM *dst;
329
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)
333 dst[i] = *src;
c8857a4d 334 scm_array_handle_release (&handle);
6e708ef2
MV
335
336 return scm_cell ((len << 8) | scm_tc7_vector, (scm_t_bits) dst);
337}
338#undef FUNC_NAME
339
340void
341scm_i_vector_free (SCM vec)
342{
343 scm_gc_free (SCM_I_VECTOR_WELTS (vec),
344 SCM_I_VECTOR_LENGTH (vec) * sizeof(SCM),
345 "vector");
346}
347
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.
352 */
353SCM
354scm_i_allocate_weak_vector (scm_t_bits type, SCM size, SCM fill)
355{
356 size_t c_size;
357 SCM *base;
358 SCM v;
359
360 c_size = scm_to_unsigned_integer (size, 0, VECTOR_MAX_LENGTH);
361
362 if (c_size > 0)
363 {
364 size_t j;
365
366 if (SCM_UNBNDP (fill))
367 fill = SCM_UNSPECIFIED;
368
369 base = scm_gc_malloc (c_size * sizeof (SCM), "weak vector");
370 for (j = 0; j != c_size; ++j)
371 base[j] = fill;
372 }
373 else
374 base = NULL;
375
376 v = scm_double_cell ((c_size << 8) | scm_tc7_wvect,
377 (scm_t_bits) base,
378 type,
379 SCM_UNPACK (SCM_EOL));
380 scm_remember_upto_here_1 (fill);
381
382 return v;
383}
e382fdbe 384
3b3b36dd 385SCM_DEFINE (scm_vector_to_list, "vector->list", 1, 0, 0,
1e6808ea 386 (SCM v),
8f85c0c6 387 "Return a newly allocated list composed of the elements of @var{v}.\n"
1e6808ea 388 "\n"
942e5b91
MG
389 "@lisp\n"
390 "(vector->list '#(dah dah didah)) @result{} (dah dah didah)\n"
391 "(list->vector '(dididit dah)) @result{} #(dididit dah)\n"
392 "@end lisp")
1bbd0b84 393#define FUNC_NAME s_scm_vector_to_list
0f2d19dd 394{
6e708ef2
MV
395 SCM res = SCM_EOL;
396 const SCM *data;
397 scm_t_array_handle handle;
22be72d3 398 size_t i, count, len;
6e708ef2
MV
399 ssize_t inc;
400
401 data = scm_vector_elements (v, &handle, &len, &inc);
22be72d3
LC
402 for (i = (len - 1) * inc, count = 0;
403 count < len;
404 i -= inc, count++)
405 res = scm_cons (data[i], res);
406
c8857a4d 407 scm_array_handle_release (&handle);
6e708ef2 408 return res;
0f2d19dd 409}
1bbd0b84 410#undef FUNC_NAME
0f2d19dd
JB
411
412
a1ec6916 413SCM_DEFINE (scm_vector_fill_x, "vector-fill!", 2, 0, 0,
1e6808ea 414 (SCM v, SCM fill),
8f85c0c6 415 "Store @var{fill} in every position of @var{vector}. The value\n"
1e6808ea 416 "returned by @code{vector-fill!} is unspecified.")
1bbd0b84 417#define FUNC_NAME s_scm_vector_fill_x
0f2d19dd 418{
6e708ef2
MV
419 scm_t_array_handle handle;
420 SCM *data;
421 size_t i, len;
422 ssize_t inc;
423
424 data = scm_vector_writable_elements (v, &handle, &len, &inc);
425 for (i = 0; i < len; i += inc)
426 data[i] = fill;
c8857a4d 427 scm_array_handle_release (&handle);
6e708ef2 428 return SCM_UNSPECIFIED;
0f2d19dd 429}
1bbd0b84 430#undef FUNC_NAME
0f2d19dd
JB
431
432
0f2d19dd 433SCM
354116f7 434scm_i_vector_equal_p (SCM x, SCM y)
0f2d19dd 435{
c014a02e 436 long i;
6e708ef2
MV
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])))
0f2d19dd
JB
440 return SCM_BOOL_F;
441 return SCM_BOOL_T;
442}
443
444
a1ec6916 445SCM_DEFINE (scm_vector_move_left_x, "vector-move-left!", 5, 0, 0,
1bbd0b84 446 (SCM vec1, SCM start1, SCM end1, SCM vec2, SCM start2),
694a9bb3
NJ
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}.")
1bbd0b84 454#define FUNC_NAME s_scm_vector_move_left_x
0f2d19dd 455{
6e708ef2
MV
456 scm_t_array_handle handle1, handle2;
457 const SCM *elts1;
458 SCM *elts2;
de5c0f58 459 size_t len1, len2;
6e708ef2 460 ssize_t inc1, inc2;
a55c2b68 461 size_t i, j, e;
6e708ef2
MV
462
463 elts1 = scm_vector_elements (vec1, &handle1, &len1, &inc1);
464 elts2 = scm_vector_writable_elements (vec2, &handle2, &len2, &inc2);
34d19ef6 465
de5c0f58
MV
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));
469
6e708ef2
MV
470 i *= inc1;
471 e *= inc1;
472 j *= inc2;
473 for (; i < e; i += inc1, j += inc2)
474 elts2[j] = elts1[i];
475
c8857a4d
MV
476 scm_array_handle_release (&handle2);
477 scm_array_handle_release (&handle1);
478
0f2d19dd
JB
479 return SCM_UNSPECIFIED;
480}
1bbd0b84 481#undef FUNC_NAME
0f2d19dd 482
a1ec6916 483SCM_DEFINE (scm_vector_move_right_x, "vector-move-right!", 5, 0, 0,
1bbd0b84 484 (SCM vec1, SCM start1, SCM end1, SCM vec2, SCM start2),
694a9bb3
NJ
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}.")
1bbd0b84 492#define FUNC_NAME s_scm_vector_move_right_x
0f2d19dd 493{
6e708ef2
MV
494 scm_t_array_handle handle1, handle2;
495 const SCM *elts1;
496 SCM *elts2;
de5c0f58 497 size_t len1, len2;
6e708ef2 498 ssize_t inc1, inc2;
a55c2b68 499 size_t i, j, e;
6e708ef2
MV
500
501 elts1 = scm_vector_elements (vec1, &handle1, &len1, &inc1);
502 elts2 = scm_vector_writable_elements (vec2, &handle2, &len2, &inc2);
0f2d19dd 503
de5c0f58
MV
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));
507
6e708ef2
MV
508 i *= inc1;
509 e *= inc1;
510 j *= inc2;
511 while (i < e)
de5c0f58 512 {
6e708ef2
MV
513 e -= inc1;
514 j -= inc2;
515 elts2[j] = elts1[e];
de5c0f58 516 }
6e708ef2 517
c8857a4d
MV
518 scm_array_handle_release (&handle2);
519 scm_array_handle_release (&handle1);
520
0f2d19dd
JB
521 return SCM_UNSPECIFIED;
522}
1bbd0b84 523#undef FUNC_NAME
0f2d19dd
JB
524
525
88797580
MV
526/* Generalized vectors. */
527
528int
529scm_is_generalized_vector (SCM obj)
530{
531 return (scm_is_vector (obj)
532 || scm_is_string (obj)
533 || scm_is_bitvector (obj)
534 || scm_is_uniform_vector (obj));
535}
536
537SCM_DEFINE (scm_generalized_vector_p, "generalized-vector?", 1, 0, 0,
538 (SCM obj),
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
542{
543 return scm_from_bool (scm_is_generalized_vector (obj));
544}
545#undef FUNC_NAME
546
354116f7
MV
547void
548scm_generalized_vector_get_handle (SCM vec, scm_t_array_handle *h)
549{
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");
553}
554
88797580
MV
555size_t
556scm_c_generalized_vector_length (SCM v)
557{
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);
566 else
567 scm_wrong_type_arg_msg (NULL, 0, v, "generalized vector");
568}
569
570SCM_DEFINE (scm_generalized_vector_length, "generalized-vector-length", 1, 0, 0,
571 (SCM v),
572 "Return the length of the generalized vector @var{v}.")
573#define FUNC_NAME s_scm_generalized_vector_length
574{
575 return scm_from_size_t (scm_c_generalized_vector_length (v));
576}
577#undef FUNC_NAME
578
579SCM
580scm_c_generalized_vector_ref (SCM v, size_t idx)
581{
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);
590 else
591 scm_wrong_type_arg_msg (NULL, 0, v, "generalized vector");
592}
593
594SCM_DEFINE (scm_generalized_vector_ref, "generalized-vector-ref", 2, 0, 0,
595 (SCM v, SCM idx),
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
599{
600 return scm_c_generalized_vector_ref (v, scm_to_size_t (idx));
601}
602#undef FUNC_NAME
603
604void
605scm_c_generalized_vector_set_x (SCM v, size_t idx, SCM val)
606{
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);
615 else
616 scm_wrong_type_arg_msg (NULL, 0, v, "generalized vector");
617}
618
619SCM_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
624{
625 scm_c_generalized_vector_set_x (v, scm_to_size_t (idx), val);
626 return SCM_UNSPECIFIED;
627}
628#undef FUNC_NAME
629
630SCM_DEFINE (scm_generalized_vector_to_list, "generalized-vector->list", 1, 0, 0,
631 (SCM v),
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
635{
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);
644 else
645 scm_wrong_type_arg_msg (NULL, 0, v, "generalized vector");
646}
647#undef FUNC_NAME
648
1cc91f1b 649
0f2d19dd
JB
650void
651scm_init_vectors ()
0f2d19dd 652{
7c33806a
DH
653 scm_nullvect = scm_c_make_vector (0, SCM_UNDEFINED);
654
a0599745 655#include "libguile/vectors.x"
0f2d19dd
JB
656}
657
89e00824
ML
658
659/*
660 Local Variables:
661 c-file-style: "gnu"
662 End:
663*/