add registry of vector constructors, make-generalized-vector
[bpt/guile.git] / libguile / vectors.c
CommitLineData
438974d0 1/* Copyright (C) 1995,1996,1998,1999,2000,2001, 2006, 2008, 2009 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"
f332e957 33#include "libguile/generalized-vectors.h"
2fa901a5 34#include "libguile/arrays.h"
cf396142 35#include "libguile/bitvectors.h"
438974d0 36#include "libguile/bytevectors.h"
5d1b3b2d 37#include "libguile/array-map.h"
88797580
MV
38#include "libguile/srfi-4.h"
39#include "libguile/strings.h"
40#include "libguile/srfi-13.h"
1d0df896 41#include "libguile/dynwind.h"
6e708ef2 42#include "libguile/deprecation.h"
88797580 43
0f2d19dd
JB
44\f
45
6e708ef2
MV
46#define VECTOR_MAX_LENGTH (SCM_T_BITS_MAX >> 8)
47
6e708ef2
MV
48int
49scm_is_vector (SCM obj)
50{
51 if (SCM_I_IS_VECTOR (obj))
52 return 1;
04b87de5 53 if (SCM_I_ARRAYP (obj) && SCM_I_ARRAY_NDIM (obj) == 1)
6e708ef2 54 {
04b87de5 55 SCM v = SCM_I_ARRAY_V (obj);
6e708ef2
MV
56 return SCM_I_IS_VECTOR (v);
57 }
58 return 0;
59}
60
61int
62scm_is_simple_vector (SCM obj)
1d0df896 63{
6e708ef2 64 return SCM_I_IS_VECTOR (obj);
1d0df896
MV
65}
66
354116f7
MV
67const SCM *
68scm_vector_elements (SCM vec, scm_t_array_handle *h,
69 size_t *lenp, ssize_t *incp)
70{
71 scm_generalized_vector_get_handle (vec, h);
72 if (lenp)
73 {
74 scm_t_array_dim *dim = scm_array_handle_dims (h);
75 *lenp = dim->ubnd - dim->lbnd + 1;
76 *incp = dim->inc;
77 }
78 return scm_array_handle_elements (h);
79}
80
81SCM *
82scm_vector_writable_elements (SCM vec, scm_t_array_handle *h,
83 size_t *lenp, ssize_t *incp)
84{
85 scm_generalized_vector_get_handle (vec, h);
86 if (lenp)
87 {
88 scm_t_array_dim *dim = scm_array_handle_dims (h);
89 *lenp = dim->ubnd - dim->lbnd + 1;
90 *incp = dim->inc;
91 }
92 return scm_array_handle_writable_elements (h);
93}
94
a1ec6916 95SCM_DEFINE (scm_vector_p, "vector?", 1, 0, 0,
5ffe9968 96 (SCM obj),
1e6808ea
MG
97 "Return @code{#t} if @var{obj} is a vector, otherwise return\n"
98 "@code{#f}.")
1bbd0b84 99#define FUNC_NAME s_scm_vector_p
0f2d19dd 100{
de5c0f58 101 return scm_from_bool (scm_is_vector (obj));
0f2d19dd 102}
1bbd0b84 103#undef FUNC_NAME
0f2d19dd 104
f172c0b7 105SCM_GPROC (s_vector_length, "vector-length", 1, 0, 0, scm_vector_length, g_vector_length);
1e6808ea 106/* Returns the number of elements in @var{vector} as an exact integer. */
0f2d19dd 107SCM
f172c0b7 108scm_vector_length (SCM v)
0f2d19dd 109{
6e708ef2
MV
110 if (SCM_I_IS_VECTOR (v))
111 return scm_from_size_t (SCM_I_VECTOR_LENGTH (v));
04b87de5 112 else if (SCM_I_ARRAYP (v) && SCM_I_ARRAY_NDIM (v) == 1)
de5c0f58 113 {
04b87de5 114 scm_t_array_dim *dim = SCM_I_ARRAY_DIMS (v);
de5c0f58
MV
115 return scm_from_size_t (dim->ubnd - dim->lbnd + 1);
116 }
117 else
118 SCM_WTA_DISPATCH_1 (g_vector_length, v, 1, NULL);
0f2d19dd
JB
119}
120
88797580
MV
121size_t
122scm_c_vector_length (SCM v)
123{
6e708ef2
MV
124 if (SCM_I_IS_VECTOR (v))
125 return SCM_I_VECTOR_LENGTH (v);
88797580 126 else
de5c0f58 127 return scm_to_size_t (scm_vector_length (v));
88797580
MV
128}
129
f172c0b7 130SCM_REGISTER_PROC (s_list_to_vector, "list->vector", 1, 0, 0, scm_vector);
5ffe9968 131/*
942e5b91
MG
132 "Return a newly created vector initialized to the elements of"
133 "the list @var{list}.\n\n"
134 "@lisp\n"
135 "(vector->list '#(dah dah didah)) @result{} (dah dah didah)\n"
136 "(list->vector '(dididit dah)) @result{} #(dididit dah)\n"
137 "@end lisp")
5ffe9968 138*/
a1ec6916 139SCM_DEFINE (scm_vector, "vector", 0, 0, 1,
f172c0b7 140 (SCM l),
8f85c0c6
NJ
141 "@deffnx {Scheme Procedure} list->vector l\n"
142 "Return a newly allocated vector composed of the\n"
1e6808ea
MG
143 "given arguments. Analogous to @code{list}.\n"
144 "\n"
942e5b91 145 "@lisp\n"
1e6808ea 146 "(vector 'a 'b 'c) @result{} #(a b c)\n"
942e5b91 147 "@end lisp")
1bbd0b84 148#define FUNC_NAME s_scm_vector
0f2d19dd
JB
149{
150 SCM res;
22a52da1 151 SCM *data;
1d0df896 152 long i, len;
6e708ef2 153 scm_t_array_handle handle;
22a52da1 154
1d0df896 155 SCM_VALIDATE_LIST_COPYLEN (1, l, len);
34d19ef6 156
6e708ef2
MV
157 res = scm_c_make_vector (len, SCM_UNSPECIFIED);
158 data = scm_vector_writable_elements (res, &handle, NULL, NULL);
1d0df896 159 i = 0;
c8857a4d 160 while (scm_is_pair (l) && i < len)
22a52da1 161 {
1d0df896 162 data[i] = SCM_CAR (l);
22a52da1 163 l = SCM_CDR (l);
6e708ef2 164 i += 1;
22a52da1
DH
165 }
166
c8857a4d
MV
167 scm_array_handle_release (&handle);
168
0f2d19dd
JB
169 return res;
170}
1bbd0b84 171#undef FUNC_NAME
0f2d19dd 172
f172c0b7 173SCM_GPROC (s_vector_ref, "vector-ref", 2, 0, 0, scm_vector_ref, g_vector_ref);
1cc91f1b 174
5ffe9968
GB
175/*
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"
178 "@var{vector}.\n\n"
942e5b91
MG
179 "@lisp\n"
180 "(vector-ref '#(1 1 2 3 5 8 13 21) 5) @result{} 8\n"
5ffe9968
GB
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"
942e5b91
MG
185 " i))) @result{} 13\n"
186 "@end lisp"
5ffe9968
GB
187*/
188
0f2d19dd 189SCM
ea633082 190scm_vector_ref (SCM v, SCM k)
685c0d71 191#define FUNC_NAME s_vector_ref
0f2d19dd 192{
de5c0f58 193 return scm_c_vector_ref (v, scm_to_size_t (k));
0f2d19dd 194}
685c0d71 195#undef FUNC_NAME
0f2d19dd 196
88797580
MV
197SCM
198scm_c_vector_ref (SCM v, size_t k)
199{
6e708ef2 200 if (SCM_I_IS_VECTOR (v))
de5c0f58 201 {
6e708ef2 202 if (k >= SCM_I_VECTOR_LENGTH (v))
de5c0f58 203 scm_out_of_range (NULL, scm_from_size_t (k));
6e708ef2 204 return (SCM_I_VECTOR_ELTS(v))[k];
de5c0f58 205 }
04b87de5 206 else if (SCM_I_ARRAYP (v) && SCM_I_ARRAY_NDIM (v) == 1)
88797580 207 {
04b87de5
MV
208 scm_t_array_dim *dim = SCM_I_ARRAY_DIMS (v);
209 SCM vv = SCM_I_ARRAY_V (v);
6e708ef2
MV
210 if (SCM_I_IS_VECTOR (vv))
211 {
212 if (k >= dim->ubnd - dim->lbnd + 1)
213 scm_out_of_range (NULL, scm_from_size_t (k));
04b87de5 214 k = SCM_I_ARRAY_BASE (v) + k*dim->inc;
6e708ef2
MV
215 return (SCM_I_VECTOR_ELTS (vv))[k];
216 }
217 scm_wrong_type_arg_msg (NULL, 0, v, "non-uniform vector");
88797580 218 }
de5c0f58
MV
219 else
220 SCM_WTA_DISPATCH_2 (g_vector_ref, v, scm_from_size_t (k), 2, NULL);
88797580
MV
221}
222
f172c0b7 223SCM_GPROC (s_vector_set_x, "vector-set!", 3, 0, 0, scm_vector_set_x, g_vector_set_x);
1cc91f1b 224
942e5b91
MG
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"
228 "@lisp\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"
233 "@end lisp"
5ffe9968
GB
234*/
235
0f2d19dd 236SCM
f172c0b7 237scm_vector_set_x (SCM v, SCM k, SCM obj)
685c0d71 238#define FUNC_NAME s_vector_set_x
0f2d19dd 239{
de5c0f58 240 scm_c_vector_set_x (v, scm_to_size_t (k), obj);
60c497a3 241 return SCM_UNSPECIFIED;
0f2d19dd 242}
685c0d71 243#undef FUNC_NAME
0f2d19dd 244
de5c0f58 245void
88797580
MV
246scm_c_vector_set_x (SCM v, size_t k, SCM obj)
247{
6e708ef2 248 if (SCM_I_IS_VECTOR (v))
88797580 249 {
6e708ef2 250 if (k >= SCM_I_VECTOR_LENGTH (v))
de5c0f58 251 scm_out_of_range (NULL, scm_from_size_t (k));
6e708ef2 252 (SCM_I_VECTOR_WELTS(v))[k] = obj;
de5c0f58 253 }
04b87de5 254 else if (SCM_I_ARRAYP (v) && SCM_I_ARRAY_NDIM (v) == 1)
de5c0f58 255 {
04b87de5
MV
256 scm_t_array_dim *dim = SCM_I_ARRAY_DIMS (v);
257 SCM vv = SCM_I_ARRAY_V (v);
6e708ef2
MV
258 if (SCM_I_IS_VECTOR (vv))
259 {
260 if (k >= dim->ubnd - dim->lbnd + 1)
261 scm_out_of_range (NULL, scm_from_size_t (k));
04b87de5 262 k = SCM_I_ARRAY_BASE (v) + k*dim->inc;
6e708ef2
MV
263 (SCM_I_VECTOR_WELTS (vv))[k] = obj;
264 }
265 else
266 scm_wrong_type_arg_msg (NULL, 0, v, "non-uniform vector");
88797580
MV
267 }
268 else
269 {
de5c0f58
MV
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));
273 else
274 scm_wrong_type_arg_msg (NULL, 0, v, "vector");
88797580
MV
275 }
276}
0f2d19dd 277
a1ec6916 278SCM_DEFINE (scm_make_vector, "make-vector", 1, 1, 0,
1bbd0b84 279 (SCM k, SCM fill),
1e6808ea 280 "Return a newly allocated vector of @var{k} elements. If a\n"
8f85c0c6
NJ
281 "second argument is given, then each position is initialized to\n"
282 "@var{fill}. Otherwise the initial contents of each position is\n"
1e6808ea 283 "unspecified.")
1bbd0b84 284#define FUNC_NAME s_scm_make_vector
0f2d19dd 285{
6e708ef2 286 size_t l = scm_to_unsigned_integer (k, 0, VECTOR_MAX_LENGTH);
e11e83f3 287
1b9be268 288 if (SCM_UNBNDP (fill))
d60cebe2 289 fill = SCM_UNSPECIFIED;
e11e83f3
MV
290
291 return scm_c_make_vector (l, fill);
00ffa0e7
KN
292}
293#undef FUNC_NAME
294
e382fdbe 295
00ffa0e7 296SCM
88797580 297scm_c_make_vector (size_t k, SCM fill)
00ffa0e7
KN
298#define FUNC_NAME s_scm_make_vector
299{
300 SCM v;
6e708ef2 301 SCM *base;
1b9be268 302
e382fdbe
DH
303 if (k > 0)
304 {
c014a02e 305 unsigned long int j;
1b9be268 306
6e708ef2 307 SCM_ASSERT_RANGE (1, scm_from_ulong (k), k <= VECTOR_MAX_LENGTH);
1b9be268 308
6e708ef2 309 base = scm_gc_malloc (k * sizeof (SCM), "vector");
e382fdbe 310 for (j = 0; j != k; ++j)
6e708ef2 311 base[j] = fill;
e382fdbe
DH
312 }
313 else
314 base = NULL;
1b9be268 315
6e708ef2 316 v = scm_cell ((k << 8) | scm_tc7_vector, (scm_t_bits) base);
e382fdbe 317 scm_remember_upto_here_1 (fill);
1b9be268 318
0f2d19dd
JB
319 return v;
320}
1bbd0b84 321#undef FUNC_NAME
0f2d19dd 322
6e708ef2
MV
323SCM_DEFINE (scm_vector_copy, "vector-copy", 1, 0, 0,
324 (SCM vec),
325 "Return a copy of @var{vec}.")
326#define FUNC_NAME s_scm_vector_copy
327{
328 scm_t_array_handle handle;
329 size_t i, len;
330 ssize_t inc;
331 const SCM *src;
332 SCM *dst;
333
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)
337 dst[i] = *src;
c8857a4d 338 scm_array_handle_release (&handle);
6e708ef2
MV
339
340 return scm_cell ((len << 8) | scm_tc7_vector, (scm_t_bits) dst);
341}
342#undef FUNC_NAME
343
344void
345scm_i_vector_free (SCM vec)
346{
347 scm_gc_free (SCM_I_VECTOR_WELTS (vec),
348 SCM_I_VECTOR_LENGTH (vec) * sizeof(SCM),
349 "vector");
350}
351
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.
356 */
357SCM
358scm_i_allocate_weak_vector (scm_t_bits type, SCM size, SCM fill)
359{
360 size_t c_size;
361 SCM *base;
362 SCM v;
363
364 c_size = scm_to_unsigned_integer (size, 0, VECTOR_MAX_LENGTH);
365
366 if (c_size > 0)
367 {
368 size_t j;
369
370 if (SCM_UNBNDP (fill))
371 fill = SCM_UNSPECIFIED;
372
373 base = scm_gc_malloc (c_size * sizeof (SCM), "weak vector");
374 for (j = 0; j != c_size; ++j)
375 base[j] = fill;
376 }
377 else
378 base = NULL;
379
380 v = scm_double_cell ((c_size << 8) | scm_tc7_wvect,
381 (scm_t_bits) base,
382 type,
383 SCM_UNPACK (SCM_EOL));
384 scm_remember_upto_here_1 (fill);
385
386 return v;
387}
e382fdbe 388
3b3b36dd 389SCM_DEFINE (scm_vector_to_list, "vector->list", 1, 0, 0,
1e6808ea 390 (SCM v),
8f85c0c6 391 "Return a newly allocated list composed of the elements of @var{v}.\n"
1e6808ea 392 "\n"
942e5b91
MG
393 "@lisp\n"
394 "(vector->list '#(dah dah didah)) @result{} (dah dah didah)\n"
395 "(list->vector '(dididit dah)) @result{} #(dididit dah)\n"
396 "@end lisp")
1bbd0b84 397#define FUNC_NAME s_scm_vector_to_list
0f2d19dd 398{
6e708ef2
MV
399 SCM res = SCM_EOL;
400 const SCM *data;
401 scm_t_array_handle handle;
22be72d3 402 size_t i, count, len;
6e708ef2
MV
403 ssize_t inc;
404
405 data = scm_vector_elements (v, &handle, &len, &inc);
22be72d3
LC
406 for (i = (len - 1) * inc, count = 0;
407 count < len;
408 i -= inc, count++)
409 res = scm_cons (data[i], res);
410
c8857a4d 411 scm_array_handle_release (&handle);
6e708ef2 412 return res;
0f2d19dd 413}
1bbd0b84 414#undef FUNC_NAME
0f2d19dd
JB
415
416
a1ec6916 417SCM_DEFINE (scm_vector_fill_x, "vector-fill!", 2, 0, 0,
1e6808ea 418 (SCM v, SCM fill),
8f85c0c6 419 "Store @var{fill} in every position of @var{vector}. The value\n"
1e6808ea 420 "returned by @code{vector-fill!} is unspecified.")
1bbd0b84 421#define FUNC_NAME s_scm_vector_fill_x
0f2d19dd 422{
6e708ef2
MV
423 scm_t_array_handle handle;
424 SCM *data;
425 size_t i, len;
426 ssize_t inc;
427
428 data = scm_vector_writable_elements (v, &handle, &len, &inc);
429 for (i = 0; i < len; i += inc)
430 data[i] = fill;
c8857a4d 431 scm_array_handle_release (&handle);
6e708ef2 432 return SCM_UNSPECIFIED;
0f2d19dd 433}
1bbd0b84 434#undef FUNC_NAME
0f2d19dd
JB
435
436
0f2d19dd 437SCM
354116f7 438scm_i_vector_equal_p (SCM x, SCM y)
0f2d19dd 439{
c014a02e 440 long i;
6e708ef2
MV
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])))
0f2d19dd
JB
444 return SCM_BOOL_F;
445 return SCM_BOOL_T;
446}
447
448
a1ec6916 449SCM_DEFINE (scm_vector_move_left_x, "vector-move-left!", 5, 0, 0,
1bbd0b84 450 (SCM vec1, SCM start1, SCM end1, SCM vec2, SCM start2),
694a9bb3
NJ
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}.")
1bbd0b84 458#define FUNC_NAME s_scm_vector_move_left_x
0f2d19dd 459{
6e708ef2
MV
460 scm_t_array_handle handle1, handle2;
461 const SCM *elts1;
462 SCM *elts2;
de5c0f58 463 size_t len1, len2;
6e708ef2 464 ssize_t inc1, inc2;
a55c2b68 465 size_t i, j, e;
6e708ef2
MV
466
467 elts1 = scm_vector_elements (vec1, &handle1, &len1, &inc1);
468 elts2 = scm_vector_writable_elements (vec2, &handle2, &len2, &inc2);
34d19ef6 469
de5c0f58
MV
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));
473
6e708ef2
MV
474 i *= inc1;
475 e *= inc1;
476 j *= inc2;
477 for (; i < e; i += inc1, j += inc2)
478 elts2[j] = elts1[i];
479
c8857a4d
MV
480 scm_array_handle_release (&handle2);
481 scm_array_handle_release (&handle1);
482
0f2d19dd
JB
483 return SCM_UNSPECIFIED;
484}
1bbd0b84 485#undef FUNC_NAME
0f2d19dd 486
a1ec6916 487SCM_DEFINE (scm_vector_move_right_x, "vector-move-right!", 5, 0, 0,
1bbd0b84 488 (SCM vec1, SCM start1, SCM end1, SCM vec2, SCM start2),
694a9bb3
NJ
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}.")
1bbd0b84 496#define FUNC_NAME s_scm_vector_move_right_x
0f2d19dd 497{
6e708ef2
MV
498 scm_t_array_handle handle1, handle2;
499 const SCM *elts1;
500 SCM *elts2;
de5c0f58 501 size_t len1, len2;
6e708ef2 502 ssize_t inc1, inc2;
a55c2b68 503 size_t i, j, e;
6e708ef2
MV
504
505 elts1 = scm_vector_elements (vec1, &handle1, &len1, &inc1);
506 elts2 = scm_vector_writable_elements (vec2, &handle2, &len2, &inc2);
0f2d19dd 507
de5c0f58
MV
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));
511
6e708ef2
MV
512 i *= inc1;
513 e *= inc1;
514 j *= inc2;
515 while (i < e)
de5c0f58 516 {
6e708ef2
MV
517 e -= inc1;
518 j -= inc2;
519 elts2[j] = elts1[e];
de5c0f58 520 }
6e708ef2 521
c8857a4d
MV
522 scm_array_handle_release (&handle2);
523 scm_array_handle_release (&handle1);
524
0f2d19dd
JB
525 return SCM_UNSPECIFIED;
526}
1bbd0b84 527#undef FUNC_NAME
0f2d19dd 528
438974d0 529\f
2a610be5
AW
530static SCM
531vector_handle_ref (scm_t_array_handle *h, size_t idx)
532{
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];
536}
537
538static void
539vector_handle_set (scm_t_array_handle *h, size_t idx, SCM val)
540{
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;
544}
545
546static void
547vector_get_handle (SCM v, scm_t_array_handle *h)
548{
549 h->array = v;
550 h->ndims = 1;
551 h->dims = &h->dim0;
552 h->dim0.lbnd = 0;
553 h->dim0.ubnd = SCM_I_VECTOR_LENGTH (v) - 1;
554 h->dim0.inc = 1;
555 h->element_type = SCM_ARRAY_ELEMENT_TYPE_SCM;
556 h->elements = h->writable_elements = SCM_I_VECTOR_WELTS (v);
557}
558
559SCM_ARRAY_IMPLEMENTATION (scm_tc7_vector, 0x7f & ~2,
560 vector_handle_ref, vector_handle_set,
561 vector_get_handle);
562SCM_ARRAY_IMPLEMENTATION (scm_tc7_wvect, 0x7f & ~2,
563 vector_handle_ref, vector_handle_set,
564 vector_get_handle);
f45eccff
AW
565SCM_VECTOR_IMPLEMENTATION (SCM_ARRAY_ELEMENT_TYPE_SCM, scm_make_vector);
566
1cc91f1b 567
0f2d19dd
JB
568void
569scm_init_vectors ()
0f2d19dd 570{
7c33806a
DH
571 scm_nullvect = scm_c_make_vector (0, SCM_UNDEFINED);
572
a0599745 573#include "libguile/vectors.x"
0f2d19dd
JB
574}
575
89e00824
ML
576
577/*
578 Local Variables:
579 c-file-style: "gnu"
580 End:
581*/