Merge branch 'ossau-gds-dev'
[bpt/guile.git] / libguile / vectors.c
1 /* Copyright (C) 1995,1996,1998,1999,2000,2001, 2006, 2008, 2009 Free Software Foundation, Inc.
2 *
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.
7 *
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.
12 *
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
16 * 02110-1301 USA
17 */
18
19
20 \f
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
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"
30
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"
43
44 \f
45
46 #define VECTOR_MAX_LENGTH (SCM_T_BITS_MAX >> 8)
47
48 int
49 scm_is_vector (SCM obj)
50 {
51 if (SCM_I_IS_VECTOR (obj))
52 return 1;
53 if (SCM_I_ARRAYP (obj) && SCM_I_ARRAY_NDIM (obj) == 1)
54 {
55 SCM v = SCM_I_ARRAY_V (obj);
56 return SCM_I_IS_VECTOR (v);
57 }
58 return 0;
59 }
60
61 int
62 scm_is_simple_vector (SCM obj)
63 {
64 return SCM_I_IS_VECTOR (obj);
65 }
66
67 const SCM *
68 scm_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
81 SCM *
82 scm_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
95 SCM_DEFINE (scm_vector_p, "vector?", 1, 0, 0,
96 (SCM obj),
97 "Return @code{#t} if @var{obj} is a vector, otherwise return\n"
98 "@code{#f}.")
99 #define FUNC_NAME s_scm_vector_p
100 {
101 return scm_from_bool (scm_is_vector (obj));
102 }
103 #undef FUNC_NAME
104
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. */
107 SCM
108 scm_vector_length (SCM v)
109 {
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)
113 {
114 scm_t_array_dim *dim = SCM_I_ARRAY_DIMS (v);
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);
119 }
120
121 size_t
122 scm_c_vector_length (SCM v)
123 {
124 if (SCM_I_IS_VECTOR (v))
125 return SCM_I_VECTOR_LENGTH (v);
126 else
127 return scm_to_size_t (scm_vector_length (v));
128 }
129
130 SCM_REGISTER_PROC (s_list_to_vector, "list->vector", 1, 0, 0, scm_vector);
131 /*
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")
138 */
139 SCM_DEFINE (scm_vector, "vector", 0, 0, 1,
140 (SCM l),
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"
144 "\n"
145 "@lisp\n"
146 "(vector 'a 'b 'c) @result{} #(a b c)\n"
147 "@end lisp")
148 #define FUNC_NAME s_scm_vector
149 {
150 SCM res;
151 SCM *data;
152 long i, len;
153 scm_t_array_handle handle;
154
155 SCM_VALIDATE_LIST_COPYLEN (1, l, len);
156
157 res = scm_c_make_vector (len, SCM_UNSPECIFIED);
158 data = scm_vector_writable_elements (res, &handle, NULL, NULL);
159 i = 0;
160 while (scm_is_pair (l) && i < len)
161 {
162 data[i] = SCM_CAR (l);
163 l = SCM_CDR (l);
164 i += 1;
165 }
166
167 scm_array_handle_release (&handle);
168
169 return res;
170 }
171 #undef FUNC_NAME
172
173 SCM_GPROC (s_vector_ref, "vector-ref", 2, 0, 0, scm_vector_ref, g_vector_ref);
174
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"
179 "@lisp\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"
186 "@end lisp"
187 */
188
189 SCM
190 scm_vector_ref (SCM v, SCM k)
191 #define FUNC_NAME s_vector_ref
192 {
193 return scm_c_vector_ref (v, scm_to_size_t (k));
194 }
195 #undef FUNC_NAME
196
197 SCM
198 scm_c_vector_ref (SCM v, size_t k)
199 {
200 if (SCM_I_IS_VECTOR (v))
201 {
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];
205 }
206 else if (SCM_I_ARRAYP (v) && SCM_I_ARRAY_NDIM (v) == 1)
207 {
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))
211 {
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];
216 }
217 scm_wrong_type_arg_msg (NULL, 0, v, "non-uniform vector");
218 }
219 else
220 SCM_WTA_DISPATCH_2 (g_vector_ref, v, scm_from_size_t (k), 2, NULL);
221 }
222
223 SCM_GPROC (s_vector_set_x, "vector-set!", 3, 0, 0, scm_vector_set_x, g_vector_set_x);
224
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"
234 */
235
236 SCM
237 scm_vector_set_x (SCM v, SCM k, SCM obj)
238 #define FUNC_NAME s_vector_set_x
239 {
240 scm_c_vector_set_x (v, scm_to_size_t (k), obj);
241 return SCM_UNSPECIFIED;
242 }
243 #undef FUNC_NAME
244
245 void
246 scm_c_vector_set_x (SCM v, size_t k, SCM obj)
247 {
248 if (SCM_I_IS_VECTOR (v))
249 {
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;
253 }
254 else if (SCM_I_ARRAYP (v) && SCM_I_ARRAY_NDIM (v) == 1)
255 {
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))
259 {
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;
264 }
265 else
266 scm_wrong_type_arg_msg (NULL, 0, v, "non-uniform vector");
267 }
268 else
269 {
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");
275 }
276 }
277
278 SCM_DEFINE (scm_make_vector, "make-vector", 1, 1, 0,
279 (SCM k, SCM fill),
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"
283 "unspecified.")
284 #define FUNC_NAME s_scm_make_vector
285 {
286 size_t l = scm_to_unsigned_integer (k, 0, VECTOR_MAX_LENGTH);
287
288 if (SCM_UNBNDP (fill))
289 fill = SCM_UNSPECIFIED;
290
291 return scm_c_make_vector (l, fill);
292 }
293 #undef FUNC_NAME
294
295
296 SCM
297 scm_c_make_vector (size_t k, SCM fill)
298 #define FUNC_NAME s_scm_make_vector
299 {
300 SCM v;
301 SCM *base;
302
303 if (k > 0)
304 {
305 unsigned long int j;
306
307 SCM_ASSERT_RANGE (1, scm_from_ulong (k), k <= VECTOR_MAX_LENGTH);
308
309 base = scm_gc_malloc (k * sizeof (SCM), "vector");
310 for (j = 0; j != k; ++j)
311 base[j] = fill;
312 }
313 else
314 base = NULL;
315
316 v = scm_cell ((k << 8) | scm_tc7_vector, (scm_t_bits) base);
317 scm_remember_upto_here_1 (fill);
318
319 return v;
320 }
321 #undef FUNC_NAME
322
323 SCM_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;
338 scm_array_handle_release (&handle);
339
340 return scm_cell ((len << 8) | scm_tc7_vector, (scm_t_bits) dst);
341 }
342 #undef FUNC_NAME
343
344 void
345 scm_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 */
357 SCM
358 scm_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 }
388
389 SCM_DEFINE (scm_vector_to_list, "vector->list", 1, 0, 0,
390 (SCM v),
391 "Return a newly allocated list composed of the elements of @var{v}.\n"
392 "\n"
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")
397 #define FUNC_NAME s_scm_vector_to_list
398 {
399 SCM res = SCM_EOL;
400 const SCM *data;
401 scm_t_array_handle handle;
402 size_t i, count, len;
403 ssize_t inc;
404
405 data = scm_vector_elements (v, &handle, &len, &inc);
406 for (i = (len - 1) * inc, count = 0;
407 count < len;
408 i -= inc, count++)
409 res = scm_cons (data[i], res);
410
411 scm_array_handle_release (&handle);
412 return res;
413 }
414 #undef FUNC_NAME
415
416
417 SCM_DEFINE (scm_vector_fill_x, "vector-fill!", 2, 0, 0,
418 (SCM v, SCM fill),
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
422 {
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;
431 scm_array_handle_release (&handle);
432 return SCM_UNSPECIFIED;
433 }
434 #undef FUNC_NAME
435
436
437 SCM
438 scm_i_vector_equal_p (SCM x, SCM y)
439 {
440 long i;
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])))
444 return SCM_BOOL_F;
445 return SCM_BOOL_T;
446 }
447
448
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
459 {
460 scm_t_array_handle handle1, handle2;
461 const SCM *elts1;
462 SCM *elts2;
463 size_t len1, len2;
464 ssize_t inc1, inc2;
465 size_t i, j, e;
466
467 elts1 = scm_vector_elements (vec1, &handle1, &len1, &inc1);
468 elts2 = scm_vector_writable_elements (vec2, &handle2, &len2, &inc2);
469
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
474 i *= inc1;
475 e *= inc1;
476 j *= inc2;
477 for (; i < e; i += inc1, j += inc2)
478 elts2[j] = elts1[i];
479
480 scm_array_handle_release (&handle2);
481 scm_array_handle_release (&handle1);
482
483 return SCM_UNSPECIFIED;
484 }
485 #undef FUNC_NAME
486
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
497 {
498 scm_t_array_handle handle1, handle2;
499 const SCM *elts1;
500 SCM *elts2;
501 size_t len1, len2;
502 ssize_t inc1, inc2;
503 size_t i, j, e;
504
505 elts1 = scm_vector_elements (vec1, &handle1, &len1, &inc1);
506 elts2 = scm_vector_writable_elements (vec2, &handle2, &len2, &inc2);
507
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
512 i *= inc1;
513 e *= inc1;
514 j *= inc2;
515 while (i < e)
516 {
517 e -= inc1;
518 j -= inc2;
519 elts2[j] = elts1[e];
520 }
521
522 scm_array_handle_release (&handle2);
523 scm_array_handle_release (&handle1);
524
525 return SCM_UNSPECIFIED;
526 }
527 #undef FUNC_NAME
528
529 \f
530 static SCM
531 vector_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
538 static void
539 vector_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
546 static void
547 vector_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
559 SCM_ARRAY_IMPLEMENTATION (scm_tc7_vector, 0x7f & ~2,
560 vector_handle_ref, vector_handle_set,
561 vector_get_handle);
562 SCM_ARRAY_IMPLEMENTATION (scm_tc7_wvect, 0x7f & ~2,
563 vector_handle_ref, vector_handle_set,
564 vector_get_handle);
565 SCM_VECTOR_IMPLEMENTATION (SCM_ARRAY_ELEMENT_TYPE_SCM, scm_make_vector);
566
567
568 void
569 scm_init_vectors ()
570 {
571 scm_nullvect = scm_c_make_vector (0, SCM_UNDEFINED);
572
573 #include "libguile/vectors.x"
574 }
575
576
577 /*
578 Local Variables:
579 c-file-style: "gnu"
580 End:
581 */