(scm_is_vector, scm_c_vector_length, scm_c_vector_ref,
[bpt/guile.git] / libguile / vectors.c
1 /* Copyright (C) 1995,1996,1998,1999,2000,2001 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
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
7 *
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17
18
19 \f
20
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"
26
27 #include "libguile/validate.h"
28 #include "libguile/vectors.h"
29 #include "libguile/unif.h"
30 #include "libguile/srfi-4.h"
31 #include "libguile/strings.h"
32 #include "libguile/srfi-13.h"
33
34 \f
35
36 SCM_DEFINE (scm_vector_p, "vector?", 1, 0, 0,
37 (SCM obj),
38 "Return @code{#t} if @var{obj} is a vector, otherwise return\n"
39 "@code{#f}.")
40 #define FUNC_NAME s_scm_vector_p
41 {
42 return scm_from_bool (SCM_VECTORP (obj));
43 }
44 #undef FUNC_NAME
45
46 int
47 scm_is_vector (SCM obj)
48 {
49 return SCM_VECTORP (obj);
50 }
51
52 SCM_GPROC (s_vector_length, "vector-length", 1, 0, 0, scm_vector_length, g_vector_length);
53 /* Returns the number of elements in @var{vector} as an exact integer. */
54 SCM
55 scm_vector_length (SCM v)
56 {
57 SCM_GASSERT1 (SCM_VECTORP(v),
58 g_vector_length, v, SCM_ARG1, s_vector_length);
59 return scm_from_size_t (SCM_VECTOR_LENGTH (v));
60 }
61
62 size_t
63 scm_c_vector_length (SCM v)
64 {
65 if (SCM_VECTORP (v))
66 return SCM_VECTOR_LENGTH (v);
67 else
68 {
69 /* Call primitive generic to get an error or maybe dispatch to a
70 method.
71 */
72 return scm_to_size_t (scm_vector_length (v));
73 }
74 }
75
76 SCM_REGISTER_PROC (s_list_to_vector, "list->vector", 1, 0, 0, scm_vector);
77 /*
78 "Return a newly created vector initialized to the elements of"
79 "the list @var{list}.\n\n"
80 "@lisp\n"
81 "(vector->list '#(dah dah didah)) @result{} (dah dah didah)\n"
82 "(list->vector '(dididit dah)) @result{} #(dididit dah)\n"
83 "@end lisp")
84 */
85 SCM_DEFINE (scm_vector, "vector", 0, 0, 1,
86 (SCM l),
87 "@deffnx {Scheme Procedure} list->vector l\n"
88 "Return a newly allocated vector composed of the\n"
89 "given arguments. Analogous to @code{list}.\n"
90 "\n"
91 "@lisp\n"
92 "(vector 'a 'b 'c) @result{} #(a b c)\n"
93 "@end lisp")
94 #define FUNC_NAME s_scm_vector
95 {
96 SCM res;
97 SCM *data;
98 long i;
99
100 /* Dirk:FIXME:: In case of multiple threads, the list might get corrupted
101 while the vector is being created. */
102 SCM_VALIDATE_LIST_COPYLEN (1, l, i);
103 res = scm_c_make_vector (i, SCM_UNSPECIFIED);
104
105 /*
106 this code doesn't alloc. -- accessing RES is safe.
107 */
108 data = SCM_WRITABLE_VELTS (res);
109 while (!SCM_NULL_OR_NIL_P (l))
110 {
111 *data++ = SCM_CAR (l);
112 l = SCM_CDR (l);
113 }
114
115 return res;
116 }
117 #undef FUNC_NAME
118
119 SCM_GPROC (s_vector_ref, "vector-ref", 2, 0, 0, scm_vector_ref, g_vector_ref);
120
121 /*
122 "@var{k} must be a valid index of @var{vector}.\n"
123 "@samp{Vector-ref} returns the contents of element @var{k} of\n"
124 "@var{vector}.\n\n"
125 "@lisp\n"
126 "(vector-ref '#(1 1 2 3 5 8 13 21) 5) @result{} 8\n"
127 "(vector-ref '#(1 1 2 3 5 8 13 21)\n"
128 " (let ((i (round (* 2 (acos -1)))))\n"
129 " (if (inexact? i)\n"
130 " (inexact->exact i)\n"
131 " i))) @result{} 13\n"
132 "@end lisp"
133 */
134
135 SCM
136 scm_vector_ref (SCM v, SCM k)
137 #define FUNC_NAME s_vector_ref
138 {
139 SCM_GASSERT2 (SCM_VECTORP (v),
140 g_vector_ref, v, k, SCM_ARG1, s_vector_ref);
141 SCM_GASSERT2 (SCM_I_INUMP (k),
142 g_vector_ref, v, k, SCM_ARG2, s_vector_ref);
143 SCM_ASSERT_RANGE (2, k,
144 SCM_I_INUM (k) < SCM_VECTOR_LENGTH (v)
145 && SCM_I_INUM (k) >= 0);
146 return SCM_VELTS (v)[(long) SCM_I_INUM (k)];
147 }
148 #undef FUNC_NAME
149
150 SCM
151 scm_c_vector_ref (SCM v, size_t k)
152 {
153 if (SCM_VECTORP (v) && k < SCM_VECTOR_LENGTH (v))
154 return SCM_VECTOR_REF (v, k);
155 else
156 {
157 /* Call primitive generic to get an error or maybe dispatch to a
158 method.
159 */
160 return scm_vector_ref (v, scm_from_size_t (k));
161 }
162 }
163
164 SCM_GPROC (s_vector_set_x, "vector-set!", 3, 0, 0, scm_vector_set_x, g_vector_set_x);
165
166 /* "@var{k} must be a valid index of @var{vector}.\n"
167 "@code{Vector-set!} stores @var{obj} in element @var{k} of @var{vector}.\n"
168 "The value returned by @samp{vector-set!} is unspecified.\n"
169 "@lisp\n"
170 "(let ((vec (vector 0 '(2 2 2 2) "Anna")))\n"
171 " (vector-set! vec 1 '("Sue" "Sue"))\n"
172 " vec) @result{} #(0 ("Sue" "Sue") "Anna")\n"
173 "(vector-set! '#(0 1 2) 1 "doe") @result{} @emph{error} ; constant vector\n"
174 "@end lisp"
175 */
176
177 SCM
178 scm_vector_set_x (SCM v, SCM k, SCM obj)
179 #define FUNC_NAME s_vector_set_x
180 {
181 SCM_GASSERTn (SCM_VECTORP (v),
182 g_vector_set_x, scm_list_3 (v, k, obj),
183 SCM_ARG1, s_vector_set_x);
184 SCM_GASSERTn (SCM_I_INUMP (k),
185 g_vector_set_x, scm_list_3 (v, k, obj),
186 SCM_ARG2, s_vector_set_x);
187 SCM_ASSERT_RANGE (2, k,
188 SCM_I_INUM (k) < SCM_VECTOR_LENGTH (v)
189 && SCM_I_INUM (k) >= 0);
190 SCM_VECTOR_SET (v, (long) SCM_I_INUM(k), obj);
191 return SCM_UNSPECIFIED;
192 }
193 #undef FUNC_NAME
194
195 SCM
196 scm_c_vector_set_x (SCM v, size_t k, SCM obj)
197 {
198 if (SCM_VECTORP (v) && k < SCM_VECTOR_LENGTH (v))
199 {
200 SCM_VECTOR_SET (v, k, obj);
201 return SCM_UNSPECIFIED;
202 }
203 else
204 {
205 /* Call primitive generic to get an error or maybe dispatch to a
206 method.
207 */
208 return scm_vector_set_x (v, scm_from_size_t (k), obj);
209 }
210 }
211
212 SCM_DEFINE (scm_make_vector, "make-vector", 1, 1, 0,
213 (SCM k, SCM fill),
214 "Return a newly allocated vector of @var{k} elements. If a\n"
215 "second argument is given, then each position is initialized to\n"
216 "@var{fill}. Otherwise the initial contents of each position is\n"
217 "unspecified.")
218 #define FUNC_NAME s_scm_make_vector
219 {
220 size_t l = scm_to_unsigned_integer (k, 0, SCM_VECTOR_MAX_LENGTH);
221
222 if (SCM_UNBNDP (fill))
223 fill = SCM_UNSPECIFIED;
224
225 return scm_c_make_vector (l, fill);
226 }
227 #undef FUNC_NAME
228
229
230 SCM
231 scm_c_make_vector (size_t k, SCM fill)
232 #define FUNC_NAME s_scm_make_vector
233 {
234 SCM v;
235 scm_t_bits *base;
236
237 if (k > 0)
238 {
239 unsigned long int j;
240
241 SCM_ASSERT_RANGE (1, scm_from_ulong (k), k <= SCM_VECTOR_MAX_LENGTH);
242
243 base = scm_gc_malloc (k * sizeof (scm_t_bits), "vector");
244 for (j = 0; j != k; ++j)
245 base[j] = SCM_UNPACK (fill);
246 }
247 else
248 base = NULL;
249
250 v = scm_cell (SCM_MAKE_VECTOR_TAG (k, scm_tc7_vector), (scm_t_bits) base);
251 scm_remember_upto_here_1 (fill);
252
253 return v;
254 }
255 #undef FUNC_NAME
256
257
258 SCM_DEFINE (scm_vector_to_list, "vector->list", 1, 0, 0,
259 (SCM v),
260 "Return a newly allocated list composed of the elements of @var{v}.\n"
261 "\n"
262 "@lisp\n"
263 "(vector->list '#(dah dah didah)) @result{} (dah dah didah)\n"
264 "(list->vector '(dididit dah)) @result{} #(dididit dah)\n"
265 "@end lisp")
266 #define FUNC_NAME s_scm_vector_to_list
267 {
268 SCM res = SCM_EOL;
269 long i;
270 SCM const *data;
271 SCM_VALIDATE_VECTOR (1, v);
272 data = SCM_VELTS(v);
273 for(i = SCM_VECTOR_LENGTH(v)-1;i >= 0;i--) res = scm_cons(data[i], res);
274 return res;
275 }
276 #undef FUNC_NAME
277
278
279 SCM_DEFINE (scm_vector_fill_x, "vector-fill!", 2, 0, 0,
280 (SCM v, SCM fill),
281 "Store @var{fill} in every position of @var{vector}. The value\n"
282 "returned by @code{vector-fill!} is unspecified.")
283 #define FUNC_NAME s_scm_vector_fill_x
284 {
285 register long i;
286 SCM_VALIDATE_VECTOR (1, v);
287
288 for(i = SCM_VECTOR_LENGTH (v) - 1; i >= 0; i--)
289 SCM_VECTOR_SET(v, i, fill);
290 return SCM_UNSPECIFIED;
291 }
292 #undef FUNC_NAME
293
294
295 SCM
296 scm_vector_equal_p(SCM x, SCM y)
297 {
298 long i;
299 for(i = SCM_VECTOR_LENGTH (x) - 1; i >= 0; i--)
300 if (scm_is_false (scm_equal_p (SCM_VELTS (x)[i], SCM_VELTS (y)[i])))
301 return SCM_BOOL_F;
302 return SCM_BOOL_T;
303 }
304
305
306 SCM_DEFINE (scm_vector_move_left_x, "vector-move-left!", 5, 0, 0,
307 (SCM vec1, SCM start1, SCM end1, SCM vec2, SCM start2),
308 "Copy elements from @var{vec1}, positions @var{start1} to @var{end1},\n"
309 "to @var{vec2} starting at position @var{start2}. @var{start1} and\n"
310 "@var{start2} are inclusive indices; @var{end1} is exclusive.\n\n"
311 "@code{vector-move-left!} copies elements in leftmost order.\n"
312 "Therefore, in the case where @var{vec1} and @var{vec2} refer to the\n"
313 "same vector, @code{vector-move-left!} is usually appropriate when\n"
314 "@var{start1} is greater than @var{start2}.")
315 #define FUNC_NAME s_scm_vector_move_left_x
316 {
317 size_t i, j, e;
318
319 SCM_VALIDATE_VECTOR (1, vec1);
320 SCM_VALIDATE_VECTOR (4, vec2);
321 i = scm_to_unsigned_integer (start1, 0, SCM_VECTOR_LENGTH(vec1));
322 e = scm_to_unsigned_integer (end1, i, SCM_VECTOR_LENGTH(vec1));
323 j = scm_to_unsigned_integer (start2, 0, SCM_VECTOR_LENGTH(vec2)-(i-e));
324
325 while (i<e)
326 {
327 SCM_VECTOR_SET (vec2, j, SCM_VELTS (vec1)[i]);
328 i++;
329 j++;
330 }
331
332 return SCM_UNSPECIFIED;
333 }
334 #undef FUNC_NAME
335
336 SCM_DEFINE (scm_vector_move_right_x, "vector-move-right!", 5, 0, 0,
337 (SCM vec1, SCM start1, SCM end1, SCM vec2, SCM start2),
338 "Copy elements from @var{vec1}, positions @var{start1} to @var{end1},\n"
339 "to @var{vec2} starting at position @var{start2}. @var{start1} and\n"
340 "@var{start2} are inclusive indices; @var{end1} is exclusive.\n\n"
341 "@code{vector-move-right!} copies elements in rightmost order.\n"
342 "Therefore, in the case where @var{vec1} and @var{vec2} refer to the\n"
343 "same vector, @code{vector-move-right!} is usually appropriate when\n"
344 "@var{start1} is less than @var{start2}.")
345 #define FUNC_NAME s_scm_vector_move_right_x
346 {
347 size_t i, j, e;
348
349 SCM_VALIDATE_VECTOR (1, vec1);
350 SCM_VALIDATE_VECTOR (4, vec2);
351 i = scm_to_unsigned_integer (start1, 0, SCM_VECTOR_LENGTH(vec1));
352 e = scm_to_unsigned_integer (end1, i, SCM_VECTOR_LENGTH(vec1));
353 j = scm_to_unsigned_integer (start2, 0, SCM_VECTOR_LENGTH(vec2)-(i-e));
354
355 j += e - i;
356 while (i < e)
357 {
358 j--;
359 e--;
360 SCM_VECTOR_SET (vec2, j, SCM_VELTS (vec1)[e]);
361 }
362
363 return SCM_UNSPECIFIED;
364 }
365 #undef FUNC_NAME
366
367
368 /* Generalized vectors. */
369
370 int
371 scm_is_generalized_vector (SCM obj)
372 {
373 return (scm_is_vector (obj)
374 || scm_is_string (obj)
375 || scm_is_bitvector (obj)
376 || scm_is_uniform_vector (obj));
377 }
378
379 SCM_DEFINE (scm_generalized_vector_p, "generalized-vector?", 1, 0, 0,
380 (SCM obj),
381 "Return @code{#t} if @var{obj} is a vector, string,\n"
382 "bitvector, or uniform numeric vector.")
383 #define FUNC_NAME s_scm_generalized_vector_p
384 {
385 return scm_from_bool (scm_is_generalized_vector (obj));
386 }
387 #undef FUNC_NAME
388
389 size_t
390 scm_c_generalized_vector_length (SCM v)
391 {
392 if (scm_is_vector (v))
393 return scm_c_vector_length (v);
394 else if (scm_is_string (v))
395 return scm_c_string_length (v);
396 else if (scm_is_bitvector (v))
397 return scm_c_bitvector_length (v);
398 else if (scm_is_uniform_vector (v))
399 return scm_c_uniform_vector_length (v);
400 else
401 scm_wrong_type_arg_msg (NULL, 0, v, "generalized vector");
402 }
403
404 SCM_DEFINE (scm_generalized_vector_length, "generalized-vector-length", 1, 0, 0,
405 (SCM v),
406 "Return the length of the generalized vector @var{v}.")
407 #define FUNC_NAME s_scm_generalized_vector_length
408 {
409 return scm_from_size_t (scm_c_generalized_vector_length (v));
410 }
411 #undef FUNC_NAME
412
413 SCM
414 scm_c_generalized_vector_ref (SCM v, size_t idx)
415 {
416 if (scm_is_vector (v))
417 return scm_c_vector_ref (v, idx);
418 else if (scm_is_string (v))
419 return scm_c_string_ref (v, idx);
420 else if (scm_is_bitvector (v))
421 return scm_c_bitvector_ref (v, idx);
422 else if (scm_is_uniform_vector (v))
423 return scm_c_uniform_vector_ref (v, idx);
424 else
425 scm_wrong_type_arg_msg (NULL, 0, v, "generalized vector");
426 }
427
428 SCM_DEFINE (scm_generalized_vector_ref, "generalized-vector-ref", 2, 0, 0,
429 (SCM v, SCM idx),
430 "Return the element at index @var{idx} of the\n"
431 "generalized vector @var{v}.")
432 #define FUNC_NAME s_scm_generalized_vector_ref
433 {
434 return scm_c_generalized_vector_ref (v, scm_to_size_t (idx));
435 }
436 #undef FUNC_NAME
437
438 void
439 scm_c_generalized_vector_set_x (SCM v, size_t idx, SCM val)
440 {
441 if (scm_is_vector (v))
442 scm_c_vector_set_x (v, idx, val);
443 else if (scm_is_string (v))
444 scm_c_string_set_x (v, idx, val);
445 else if (scm_is_bitvector (v))
446 scm_c_bitvector_set_x (v, idx, val);
447 else if (scm_is_uniform_vector (v))
448 scm_c_uniform_vector_set_x (v, idx, val);
449 else
450 scm_wrong_type_arg_msg (NULL, 0, v, "generalized vector");
451 }
452
453 SCM_DEFINE (scm_generalized_vector_set_x, "generalized-vector-set!", 3, 0, 0,
454 (SCM v, SCM idx, SCM val),
455 "Set the element at index @var{idx} of the\n"
456 "generalized vector @var{v} to @var{val}.")
457 #define FUNC_NAME s_scm_generalized_vector_set_x
458 {
459 scm_c_generalized_vector_set_x (v, scm_to_size_t (idx), val);
460 return SCM_UNSPECIFIED;
461 }
462 #undef FUNC_NAME
463
464 SCM_DEFINE (scm_generalized_vector_to_list, "generalized-vector->list", 1, 0, 0,
465 (SCM v),
466 "Return a new list whose elements are the elements of the\n"
467 "generalized vector @var{v}.")
468 #define FUNC_NAME s_scm_generalized_vector_to_list
469 {
470 if (scm_is_vector (v))
471 return scm_vector_to_list (v);
472 else if (scm_is_string (v))
473 return scm_string_to_list (v);
474 else if (scm_is_bitvector (v))
475 return scm_bitvector_to_list (v);
476 else if (scm_is_uniform_vector (v))
477 return scm_uniform_vector_to_list (v);
478 else
479 scm_wrong_type_arg_msg (NULL, 0, v, "generalized vector");
480 }
481 #undef FUNC_NAME
482
483
484 void
485 scm_init_vectors ()
486 {
487 scm_nullvect = scm_c_make_vector (0, SCM_UNDEFINED);
488
489 #include "libguile/vectors.x"
490 }
491
492
493 /*
494 Local Variables:
495 c-file-style: "gnu"
496 End:
497 */