32-way branching in intmap.scm, not 16-way
[bpt/guile.git] / libguile / vectors.c
1 /* Copyright (C) 1995,1996,1998,1999,2000,2001, 2006, 2008, 2009, 2010,
2 * 2011, 2012, 2014 Free Software Foundation, Inc.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public License
6 * as published by the Free Software Foundation; either version 3 of
7 * the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301 USA
18 */
19
20
21 \f
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #include "libguile/_scm.h"
27 #include "libguile/eq.h"
28 #include "libguile/root.h"
29 #include "libguile/strings.h"
30
31 #include "libguile/validate.h"
32 #include "libguile/vectors.h"
33 #include "libguile/arrays.h" /* Hit me with the ugly stick */
34 #include "libguile/generalized-vectors.h"
35 #include "libguile/strings.h"
36 #include "libguile/srfi-13.h"
37 #include "libguile/dynwind.h"
38
39 #include "libguile/bdw-gc.h"
40
41
42 \f
43
44 #define VECTOR_MAX_LENGTH (SCM_T_BITS_MAX >> 8)
45
46 int
47 scm_is_vector (SCM obj)
48 {
49 return SCM_I_IS_VECTOR (obj);
50 }
51
52 int
53 scm_is_simple_vector (SCM obj)
54 {
55 return SCM_I_IS_VECTOR (obj);
56 }
57
58 const SCM *
59 scm_vector_elements (SCM vec, scm_t_array_handle *h,
60 size_t *lenp, ssize_t *incp)
61 {
62 if (SCM_I_WVECTP (vec))
63 scm_wrong_type_arg_msg (NULL, 0, vec, "non-weak vector");
64
65 scm_generalized_vector_get_handle (vec, h);
66 if (lenp)
67 {
68 scm_t_array_dim *dim = scm_array_handle_dims (h);
69 *lenp = dim->ubnd - dim->lbnd + 1;
70 *incp = dim->inc;
71 }
72 return scm_array_handle_elements (h);
73 }
74
75 SCM *
76 scm_vector_writable_elements (SCM vec, scm_t_array_handle *h,
77 size_t *lenp, ssize_t *incp)
78 {
79 if (SCM_I_WVECTP (vec))
80 scm_wrong_type_arg_msg (NULL, 0, vec, "non-weak vector");
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
92 SCM_DEFINE (scm_vector_p, "vector?", 1, 0, 0,
93 (SCM obj),
94 "Return @code{#t} if @var{obj} is a vector, otherwise return\n"
95 "@code{#f}.")
96 #define FUNC_NAME s_scm_vector_p
97 {
98 return scm_from_bool (scm_is_vector (obj));
99 }
100 #undef FUNC_NAME
101
102 SCM_DEFINE (scm_vector_length, "vector-length", 1, 0, 0,
103 (SCM v),
104 "Returns the number of elements in @var{vector} as an exact integer.")
105 #define FUNC_NAME s_scm_vector_length
106 {
107 return scm_from_size_t (scm_c_vector_length (v));
108 }
109 #undef FUNC_NAME
110
111 size_t
112 scm_c_vector_length (SCM v)
113 #define FUNC_NAME s_scm_vector_length
114 {
115 SCM_VALIDATE_VECTOR (1, v);
116
117 return SCM_I_VECTOR_LENGTH (v);
118 }
119 #undef FUNC_NAME
120
121 SCM_REGISTER_PROC (s_list_to_vector, "list->vector", 1, 0, 0, scm_vector);
122 /*
123 "Return a newly created vector initialized to the elements of"
124 "the list @var{list}.\n\n"
125 "@lisp\n"
126 "(vector->list '#(dah dah didah)) @result{} (dah dah didah)\n"
127 "(list->vector '(dididit dah)) @result{} #(dididit dah)\n"
128 "@end lisp")
129 */
130 SCM_DEFINE (scm_vector, "vector", 0, 0, 1,
131 (SCM l),
132 "@deffnx {Scheme Procedure} list->vector l\n"
133 "Return a newly allocated vector composed of the\n"
134 "given arguments. Analogous to @code{list}.\n"
135 "\n"
136 "@lisp\n"
137 "(vector 'a 'b 'c) @result{} #(a b c)\n"
138 "@end lisp")
139 #define FUNC_NAME s_scm_vector
140 {
141 SCM res;
142 SCM *data;
143 long i, len;
144 scm_t_array_handle handle;
145
146 SCM_VALIDATE_LIST_COPYLEN (1, l, len);
147
148 res = scm_c_make_vector (len, SCM_UNSPECIFIED);
149 data = scm_vector_writable_elements (res, &handle, NULL, NULL);
150 i = 0;
151 while (scm_is_pair (l) && i < len)
152 {
153 data[i] = SCM_CAR (l);
154 l = SCM_CDR (l);
155 i += 1;
156 }
157
158 scm_array_handle_release (&handle);
159
160 return res;
161 }
162 #undef FUNC_NAME
163
164 SCM_DEFINE (scm_vector_ref, "vector-ref", 2, 0, 0,
165 (SCM vector, SCM k),
166 "@var{k} must be a valid index of @var{vector}.\n"
167 "@samp{Vector-ref} returns the contents of element @var{k} of\n"
168 "@var{vector}.\n\n"
169 "@lisp\n"
170 "(vector-ref '#(1 1 2 3 5 8 13 21) 5) @result{} 8\n"
171 "(vector-ref '#(1 1 2 3 5 8 13 21)\n"
172 " (let ((i (round (* 2 (acos -1)))))\n"
173 " (if (inexact? i)\n"
174 " (inexact->exact i)\n"
175 " i))) @result{} 13\n"
176 "@end lisp")
177 #define FUNC_NAME s_scm_vector_ref
178 {
179 return scm_c_vector_ref (vector, scm_to_size_t (k));
180 }
181 #undef FUNC_NAME
182
183 SCM
184 scm_c_vector_ref (SCM v, size_t k)
185 #define FUNC_NAME s_scm_vector_ref
186 {
187 SCM_VALIDATE_VECTOR (1, v);
188
189 if (k >= SCM_I_VECTOR_LENGTH (v))
190 scm_out_of_range (NULL, scm_from_size_t (k));
191
192 return SCM_SIMPLE_VECTOR_REF (v, k);
193 }
194 #undef FUNC_NAME
195
196 SCM_DEFINE (scm_vector_set_x, "vector-set!", 3, 0, 0,
197 (SCM vector, SCM k, SCM obj),
198 "@var{k} must be a valid index of @var{vector}.\n"
199 "@code{Vector-set!} stores @var{obj} in element @var{k} of @var{vector}.\n"
200 "The value returned by @samp{vector-set!} is unspecified.\n"
201 "@lisp\n"
202 "(let ((vec (vector 0 '(2 2 2 2) \"Anna\")))\n"
203 " (vector-set! vec 1 '(\"Sue\" \"Sue\"))\n"
204 " vec) @result{} #(0 (\"Sue\" \"Sue\") \"Anna\")\n"
205 "(vector-set! '#(0 1 2) 1 \"doe\") @result{} @emph{error} ; constant vector\n"
206 "@end lisp")
207 #define FUNC_NAME s_scm_vector_set_x
208 {
209 scm_c_vector_set_x (vector, scm_to_size_t (k), obj);
210 return SCM_UNSPECIFIED;
211 }
212 #undef FUNC_NAME
213
214 void
215 scm_c_vector_set_x (SCM v, size_t k, SCM obj)
216 #define FUNC_NAME s_scm_vector_set_x
217 {
218 SCM_VALIDATE_VECTOR (1, v);
219
220 if (k >= SCM_I_VECTOR_LENGTH (v))
221 scm_out_of_range (NULL, scm_from_size_t (k));
222
223 SCM_SIMPLE_VECTOR_SET (v, k, obj);
224 }
225 #undef FUNC_NAME
226
227 SCM_DEFINE (scm_make_vector, "make-vector", 1, 1, 0,
228 (SCM k, SCM fill),
229 "Return a newly allocated vector of @var{k} elements. If a\n"
230 "second argument is given, then each position is initialized to\n"
231 "@var{fill}. Otherwise the initial contents of each position is\n"
232 "unspecified.")
233 #define FUNC_NAME s_scm_make_vector
234 {
235 size_t l = scm_to_unsigned_integer (k, 0, VECTOR_MAX_LENGTH);
236
237 if (SCM_UNBNDP (fill))
238 fill = SCM_UNSPECIFIED;
239
240 return scm_c_make_vector (l, fill);
241 }
242 #undef FUNC_NAME
243
244
245 SCM
246 scm_c_make_vector (size_t k, SCM fill)
247 #define FUNC_NAME s_scm_make_vector
248 {
249 SCM vector;
250 unsigned long int j;
251
252 SCM_ASSERT_RANGE (1, scm_from_size_t (k), k <= VECTOR_MAX_LENGTH);
253
254 vector = scm_words ((k << 8) | scm_tc7_vector, k + 1);
255
256 for (j = 0; j < k; ++j)
257 SCM_SIMPLE_VECTOR_SET (vector, j, fill);
258
259 return vector;
260 }
261 #undef FUNC_NAME
262
263 SCM_DEFINE (scm_vector_copy, "vector-copy", 1, 0, 0,
264 (SCM vec),
265 "Return a copy of @var{vec}.")
266 #define FUNC_NAME s_scm_vector_copy
267 {
268 scm_t_array_handle handle;
269 size_t i, len;
270 ssize_t inc;
271 const SCM *src;
272 SCM result, *dst;
273
274 src = scm_vector_elements (vec, &handle, &len, &inc);
275
276 result = scm_c_make_vector (len, SCM_UNDEFINED);
277 dst = SCM_I_VECTOR_WELTS (result);
278 for (i = 0; i < len; i++, src += inc)
279 dst[i] = *src;
280
281 scm_array_handle_release (&handle);
282
283 return result;
284 }
285 #undef FUNC_NAME
286
287 \f
288 SCM_DEFINE (scm_vector_to_list, "vector->list", 1, 0, 0,
289 (SCM v),
290 "Return a newly allocated list composed of the elements of @var{v}.\n"
291 "\n"
292 "@lisp\n"
293 "(vector->list '#(dah dah didah)) @result{} (dah dah didah)\n"
294 "(list->vector '(dididit dah)) @result{} #(dididit dah)\n"
295 "@end lisp")
296 #define FUNC_NAME s_scm_vector_to_list
297 {
298 SCM res = SCM_EOL;
299 const SCM *data;
300 scm_t_array_handle handle;
301 size_t i, count, len;
302 ssize_t inc;
303
304 data = scm_vector_elements (v, &handle, &len, &inc);
305 for (i = (len - 1) * inc, count = 0;
306 count < len;
307 i -= inc, count++)
308 res = scm_cons (data[i], res);
309
310 scm_array_handle_release (&handle);
311 return res;
312 }
313 #undef FUNC_NAME
314
315
316 SCM_DEFINE (scm_vector_fill_x, "vector-fill!", 2, 0, 0,
317 (SCM v, SCM fill),
318 "Store @var{fill} in every position of @var{vector}. The value\n"
319 "returned by @code{vector-fill!} is unspecified.")
320 #define FUNC_NAME s_scm_vector_fill_x
321 {
322 scm_t_array_handle handle;
323 SCM *data;
324 size_t i, len;
325 ssize_t inc;
326
327 data = scm_vector_writable_elements (v, &handle, &len, &inc);
328 for (i = 0; i < len; i += inc)
329 data[i] = fill;
330 scm_array_handle_release (&handle);
331 return SCM_UNSPECIFIED;
332 }
333 #undef FUNC_NAME
334
335
336 SCM
337 scm_i_vector_equal_p (SCM x, SCM y)
338 {
339 long i;
340 for (i = SCM_I_VECTOR_LENGTH (x) - 1; i >= 0; i--)
341 if (scm_is_false (scm_equal_p (SCM_I_VECTOR_ELTS (x)[i],
342 SCM_I_VECTOR_ELTS (y)[i])))
343 return SCM_BOOL_F;
344 return SCM_BOOL_T;
345 }
346
347
348 SCM_DEFINE (scm_vector_move_left_x, "vector-move-left!", 5, 0, 0,
349 (SCM vec1, SCM start1, SCM end1, SCM vec2, SCM start2),
350 "Copy elements from @var{vec1}, positions @var{start1} to @var{end1},\n"
351 "to @var{vec2} starting at position @var{start2}. @var{start1} and\n"
352 "@var{start2} are inclusive indices; @var{end1} is exclusive.\n\n"
353 "@code{vector-move-left!} copies elements in leftmost order.\n"
354 "Therefore, in the case where @var{vec1} and @var{vec2} refer to the\n"
355 "same vector, @code{vector-move-left!} is usually appropriate when\n"
356 "@var{start1} is greater than @var{start2}.")
357 #define FUNC_NAME s_scm_vector_move_left_x
358 {
359 scm_t_array_handle handle1, handle2;
360 const SCM *elts1;
361 SCM *elts2;
362 size_t len1, len2;
363 ssize_t inc1, inc2;
364 size_t i, j, e;
365
366 elts1 = scm_vector_elements (vec1, &handle1, &len1, &inc1);
367 elts2 = scm_vector_writable_elements (vec2, &handle2, &len2, &inc2);
368
369 i = scm_to_unsigned_integer (start1, 0, len1);
370 e = scm_to_unsigned_integer (end1, i, len1);
371 SCM_ASSERT_RANGE (SCM_ARG3, end1, (e-i) <= len2);
372 j = scm_to_unsigned_integer (start2, 0, len2);
373 SCM_ASSERT_RANGE (SCM_ARG5, start2, j <= len2 - (e - i));
374
375 i *= inc1;
376 e *= inc1;
377 j *= inc2;
378 for (; i < e; i += inc1, j += inc2)
379 elts2[j] = elts1[i];
380
381 scm_array_handle_release (&handle2);
382 scm_array_handle_release (&handle1);
383
384 return SCM_UNSPECIFIED;
385 }
386 #undef FUNC_NAME
387
388 SCM_DEFINE (scm_vector_move_right_x, "vector-move-right!", 5, 0, 0,
389 (SCM vec1, SCM start1, SCM end1, SCM vec2, SCM start2),
390 "Copy elements from @var{vec1}, positions @var{start1} to @var{end1},\n"
391 "to @var{vec2} starting at position @var{start2}. @var{start1} and\n"
392 "@var{start2} are inclusive indices; @var{end1} is exclusive.\n\n"
393 "@code{vector-move-right!} copies elements in rightmost order.\n"
394 "Therefore, in the case where @var{vec1} and @var{vec2} refer to the\n"
395 "same vector, @code{vector-move-right!} is usually appropriate when\n"
396 "@var{start1} is less than @var{start2}.")
397 #define FUNC_NAME s_scm_vector_move_right_x
398 {
399 scm_t_array_handle handle1, handle2;
400 const SCM *elts1;
401 SCM *elts2;
402 size_t len1, len2;
403 ssize_t inc1, inc2;
404 size_t i, j, e;
405
406 elts1 = scm_vector_elements (vec1, &handle1, &len1, &inc1);
407 elts2 = scm_vector_writable_elements (vec2, &handle2, &len2, &inc2);
408
409 i = scm_to_unsigned_integer (start1, 0, len1);
410 e = scm_to_unsigned_integer (end1, i, len1);
411 SCM_ASSERT_RANGE (SCM_ARG3, end1, (e-i) <= len2);
412 j = scm_to_unsigned_integer (start2, 0, len2);
413 SCM_ASSERT_RANGE (SCM_ARG5, start2, j <= len2 - (e - i));
414
415 j += (e - i);
416
417 i *= inc1;
418 e *= inc1;
419 j *= inc2;
420 while (i < e)
421 {
422 e -= inc1;
423 j -= inc2;
424 elts2[j] = elts1[e];
425 }
426
427 scm_array_handle_release (&handle2);
428 scm_array_handle_release (&handle1);
429
430 return SCM_UNSPECIFIED;
431 }
432 #undef FUNC_NAME
433
434 \f
435 SCM_VECTOR_IMPLEMENTATION (SCM_ARRAY_ELEMENT_TYPE_SCM, scm_make_vector)
436
437
438 void
439 scm_init_vectors ()
440 {
441 #include "libguile/vectors.x"
442 }
443
444
445 /*
446 Local Variables:
447 c-file-style: "gnu"
448 End:
449 */