Fix intmap bug for maps with only one element
[bpt/guile.git] / libguile / vectors.c
CommitLineData
6922d92f 1/* Copyright (C) 1995,1996,1998,1999,2000,2001, 2006, 2008, 2009, 2010,
9b5da400 2 * 2011, 2012, 2014 Free Software Foundation, Inc.
6922d92f 3 *
73be1d9e 4 * This library is free software; you can redistribute it and/or
53befeb7
NJ
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.
0f2d19dd 8 *
53befeb7
NJ
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
0f2d19dd 13 *
73be1d9e
MV
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
53befeb7
NJ
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301 USA
73be1d9e 18 */
1bbd0b84 19
1bbd0b84 20
0f2d19dd 21\f
dbb605f5
LC
22#ifdef HAVE_CONFIG_H
23# include <config.h>
24#endif
0f2d19dd 25
a0599745
MD
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"
5c39373f 33#include "libguile/arrays.h" /* Hit me with the ugly stick */
f332e957 34#include "libguile/generalized-vectors.h"
88797580
MV
35#include "libguile/strings.h"
36#include "libguile/srfi-13.h"
1d0df896 37#include "libguile/dynwind.h"
88797580 38
1c44468d 39#include "libguile/bdw-gc.h"
3a2de079
LC
40
41
0f2d19dd
JB
42\f
43
6e708ef2
MV
44#define VECTOR_MAX_LENGTH (SCM_T_BITS_MAX >> 8)
45
6e708ef2
MV
46int
47scm_is_vector (SCM obj)
48{
a32488ba 49 return SCM_I_IS_VECTOR (obj);
6e708ef2
MV
50}
51
52int
53scm_is_simple_vector (SCM obj)
1d0df896 54{
a32488ba 55 return SCM_I_IS_VECTOR (obj);
1d0df896
MV
56}
57
354116f7
MV
58const SCM *
59scm_vector_elements (SCM vec, scm_t_array_handle *h,
60 size_t *lenp, ssize_t *incp)
61{
3a2de079 62 if (SCM_I_WVECTP (vec))
a141db86 63 scm_wrong_type_arg_msg (NULL, 0, vec, "non-weak vector");
3a2de079 64
354116f7
MV
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
75SCM *
76scm_vector_writable_elements (SCM vec, scm_t_array_handle *h,
77 size_t *lenp, ssize_t *incp)
78{
3a2de079 79 if (SCM_I_WVECTP (vec))
a141db86 80 scm_wrong_type_arg_msg (NULL, 0, vec, "non-weak vector");
3a2de079 81
354116f7
MV
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
a1ec6916 92SCM_DEFINE (scm_vector_p, "vector?", 1, 0, 0,
5ffe9968 93 (SCM obj),
1e6808ea
MG
94 "Return @code{#t} if @var{obj} is a vector, otherwise return\n"
95 "@code{#f}.")
1bbd0b84 96#define FUNC_NAME s_scm_vector_p
0f2d19dd 97{
de5c0f58 98 return scm_from_bool (scm_is_vector (obj));
0f2d19dd 99}
1bbd0b84 100#undef FUNC_NAME
0f2d19dd 101
e0a00fe7
AW
102SCM_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
0f2d19dd 106{
787f7b64 107 return scm_from_size_t (scm_c_vector_length (v));
0f2d19dd 108}
e0a00fe7 109#undef FUNC_NAME
0f2d19dd 110
88797580
MV
111size_t
112scm_c_vector_length (SCM v)
787f7b64 113#define FUNC_NAME s_scm_vector_length
88797580 114{
787f7b64
AW
115 SCM_VALIDATE_VECTOR (1, v);
116
117 return SCM_I_VECTOR_LENGTH (v);
88797580 118}
787f7b64 119#undef FUNC_NAME
88797580 120
f172c0b7 121SCM_REGISTER_PROC (s_list_to_vector, "list->vector", 1, 0, 0, scm_vector);
5ffe9968 122/*
942e5b91
MG
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")
5ffe9968 129*/
a1ec6916 130SCM_DEFINE (scm_vector, "vector", 0, 0, 1,
f172c0b7 131 (SCM l),
8f85c0c6
NJ
132 "@deffnx {Scheme Procedure} list->vector l\n"
133 "Return a newly allocated vector composed of the\n"
1e6808ea
MG
134 "given arguments. Analogous to @code{list}.\n"
135 "\n"
942e5b91 136 "@lisp\n"
1e6808ea 137 "(vector 'a 'b 'c) @result{} #(a b c)\n"
942e5b91 138 "@end lisp")
1bbd0b84 139#define FUNC_NAME s_scm_vector
0f2d19dd
JB
140{
141 SCM res;
22a52da1 142 SCM *data;
1d0df896 143 long i, len;
6e708ef2 144 scm_t_array_handle handle;
22a52da1 145
1d0df896 146 SCM_VALIDATE_LIST_COPYLEN (1, l, len);
34d19ef6 147
6e708ef2
MV
148 res = scm_c_make_vector (len, SCM_UNSPECIFIED);
149 data = scm_vector_writable_elements (res, &handle, NULL, NULL);
1d0df896 150 i = 0;
c8857a4d 151 while (scm_is_pair (l) && i < len)
22a52da1 152 {
1d0df896 153 data[i] = SCM_CAR (l);
22a52da1 154 l = SCM_CDR (l);
6e708ef2 155 i += 1;
22a52da1
DH
156 }
157
c8857a4d
MV
158 scm_array_handle_release (&handle);
159
0f2d19dd
JB
160 return res;
161}
1bbd0b84 162#undef FUNC_NAME
0f2d19dd 163
e0a00fe7
AW
164SCM_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
0f2d19dd 178{
e0a00fe7 179 return scm_c_vector_ref (vector, scm_to_size_t (k));
0f2d19dd 180}
685c0d71 181#undef FUNC_NAME
0f2d19dd 182
88797580
MV
183SCM
184scm_c_vector_ref (SCM v, size_t k)
787f7b64 185#define FUNC_NAME s_scm_vector_ref
88797580 186{
787f7b64 187 SCM_VALIDATE_VECTOR (1, v);
a141db86 188
787f7b64
AW
189 if (k >= SCM_I_VECTOR_LENGTH (v))
190 scm_out_of_range (NULL, scm_from_size_t (k));
a141db86 191
787f7b64 192 return SCM_SIMPLE_VECTOR_REF (v, k);
88797580 193}
787f7b64 194#undef FUNC_NAME
88797580 195
e0a00fe7
AW
196SCM_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
0f2d19dd 208{
e0a00fe7 209 scm_c_vector_set_x (vector, scm_to_size_t (k), obj);
60c497a3 210 return SCM_UNSPECIFIED;
0f2d19dd 211}
685c0d71 212#undef FUNC_NAME
0f2d19dd 213
de5c0f58 214void
88797580 215scm_c_vector_set_x (SCM v, size_t k, SCM obj)
787f7b64 216#define FUNC_NAME s_scm_vector_set_x
88797580 217{
787f7b64 218 SCM_VALIDATE_VECTOR (1, v);
a141db86 219
787f7b64
AW
220 if (k >= SCM_I_VECTOR_LENGTH (v))
221 scm_out_of_range (NULL, scm_from_size_t (k));
a141db86 222
787f7b64 223 SCM_SIMPLE_VECTOR_SET (v, k, obj);
88797580 224}
787f7b64 225#undef FUNC_NAME
0f2d19dd 226
a1ec6916 227SCM_DEFINE (scm_make_vector, "make-vector", 1, 1, 0,
1bbd0b84 228 (SCM k, SCM fill),
1e6808ea 229 "Return a newly allocated vector of @var{k} elements. If a\n"
8f85c0c6
NJ
230 "second argument is given, then each position is initialized to\n"
231 "@var{fill}. Otherwise the initial contents of each position is\n"
1e6808ea 232 "unspecified.")
1bbd0b84 233#define FUNC_NAME s_scm_make_vector
0f2d19dd 234{
6e708ef2 235 size_t l = scm_to_unsigned_integer (k, 0, VECTOR_MAX_LENGTH);
e11e83f3 236
1b9be268 237 if (SCM_UNBNDP (fill))
d60cebe2 238 fill = SCM_UNSPECIFIED;
e11e83f3
MV
239
240 return scm_c_make_vector (l, fill);
00ffa0e7
KN
241}
242#undef FUNC_NAME
243
e382fdbe 244
00ffa0e7 245SCM
88797580 246scm_c_make_vector (size_t k, SCM fill)
00ffa0e7
KN
247#define FUNC_NAME s_scm_make_vector
248{
a141db86
AW
249 SCM vector;
250 unsigned long int j;
ed7e0765 251
a141db86 252 SCM_ASSERT_RANGE (1, scm_from_size_t (k), k <= VECTOR_MAX_LENGTH);
1b9be268 253
a141db86 254 vector = scm_words ((k << 8) | scm_tc7_vector, k + 1);
1b9be268 255
a141db86
AW
256 for (j = 0; j < k; ++j)
257 SCM_SIMPLE_VECTOR_SET (vector, j, fill);
1b9be268 258
a141db86 259 return vector;
0f2d19dd 260}
1bbd0b84 261#undef FUNC_NAME
0f2d19dd 262
6e708ef2
MV
263SCM_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;
ed7e0765 272 SCM result, *dst;
6e708ef2
MV
273
274 src = scm_vector_elements (vec, &handle, &len, &inc);
ed7e0765
LC
275
276 result = scm_c_make_vector (len, SCM_UNDEFINED);
277 dst = SCM_I_VECTOR_WELTS (result);
6e708ef2
MV
278 for (i = 0; i < len; i++, src += inc)
279 dst[i] = *src;
ed7e0765 280
c8857a4d 281 scm_array_handle_release (&handle);
6e708ef2 282
ed7e0765 283 return result;
6e708ef2
MV
284}
285#undef FUNC_NAME
286
d525e4f9 287\f
3b3b36dd 288SCM_DEFINE (scm_vector_to_list, "vector->list", 1, 0, 0,
1e6808ea 289 (SCM v),
8f85c0c6 290 "Return a newly allocated list composed of the elements of @var{v}.\n"
1e6808ea 291 "\n"
942e5b91
MG
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")
1bbd0b84 296#define FUNC_NAME s_scm_vector_to_list
0f2d19dd 297{
6e708ef2
MV
298 SCM res = SCM_EOL;
299 const SCM *data;
300 scm_t_array_handle handle;
22be72d3 301 size_t i, count, len;
6e708ef2
MV
302 ssize_t inc;
303
304 data = scm_vector_elements (v, &handle, &len, &inc);
22be72d3
LC
305 for (i = (len - 1) * inc, count = 0;
306 count < len;
307 i -= inc, count++)
308 res = scm_cons (data[i], res);
309
c8857a4d 310 scm_array_handle_release (&handle);
6e708ef2 311 return res;
0f2d19dd 312}
1bbd0b84 313#undef FUNC_NAME
0f2d19dd
JB
314
315
a1ec6916 316SCM_DEFINE (scm_vector_fill_x, "vector-fill!", 2, 0, 0,
1e6808ea 317 (SCM v, SCM fill),
8f85c0c6 318 "Store @var{fill} in every position of @var{vector}. The value\n"
1e6808ea 319 "returned by @code{vector-fill!} is unspecified.")
1bbd0b84 320#define FUNC_NAME s_scm_vector_fill_x
0f2d19dd 321{
6e708ef2
MV
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;
c8857a4d 330 scm_array_handle_release (&handle);
6e708ef2 331 return SCM_UNSPECIFIED;
0f2d19dd 332}
1bbd0b84 333#undef FUNC_NAME
0f2d19dd
JB
334
335
0f2d19dd 336SCM
354116f7 337scm_i_vector_equal_p (SCM x, SCM y)
0f2d19dd 338{
c014a02e 339 long i;
6e708ef2
MV
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])))
0f2d19dd
JB
343 return SCM_BOOL_F;
344 return SCM_BOOL_T;
345}
346
347
a1ec6916 348SCM_DEFINE (scm_vector_move_left_x, "vector-move-left!", 5, 0, 0,
1bbd0b84 349 (SCM vec1, SCM start1, SCM end1, SCM vec2, SCM start2),
694a9bb3
NJ
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}.")
1bbd0b84 357#define FUNC_NAME s_scm_vector_move_left_x
0f2d19dd 358{
6e708ef2
MV
359 scm_t_array_handle handle1, handle2;
360 const SCM *elts1;
361 SCM *elts2;
de5c0f58 362 size_t len1, len2;
6e708ef2 363 ssize_t inc1, inc2;
a55c2b68 364 size_t i, j, e;
6e708ef2
MV
365
366 elts1 = scm_vector_elements (vec1, &handle1, &len1, &inc1);
367 elts2 = scm_vector_writable_elements (vec2, &handle2, &len2, &inc2);
34d19ef6 368
de5c0f58
MV
369 i = scm_to_unsigned_integer (start1, 0, len1);
370 e = scm_to_unsigned_integer (end1, i, len1);
ca659673 371 SCM_ASSERT_RANGE (SCM_ARG3, end1, (e-i) <= len2);
551b96d2
AW
372 j = scm_to_unsigned_integer (start2, 0, len2);
373 SCM_ASSERT_RANGE (SCM_ARG5, start2, j <= len2 - (e - i));
de5c0f58 374
6e708ef2
MV
375 i *= inc1;
376 e *= inc1;
377 j *= inc2;
378 for (; i < e; i += inc1, j += inc2)
379 elts2[j] = elts1[i];
380
c8857a4d
MV
381 scm_array_handle_release (&handle2);
382 scm_array_handle_release (&handle1);
383
0f2d19dd
JB
384 return SCM_UNSPECIFIED;
385}
1bbd0b84 386#undef FUNC_NAME
0f2d19dd 387
a1ec6916 388SCM_DEFINE (scm_vector_move_right_x, "vector-move-right!", 5, 0, 0,
1bbd0b84 389 (SCM vec1, SCM start1, SCM end1, SCM vec2, SCM start2),
694a9bb3
NJ
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}.")
1bbd0b84 397#define FUNC_NAME s_scm_vector_move_right_x
0f2d19dd 398{
6e708ef2
MV
399 scm_t_array_handle handle1, handle2;
400 const SCM *elts1;
401 SCM *elts2;
de5c0f58 402 size_t len1, len2;
6e708ef2 403 ssize_t inc1, inc2;
a55c2b68 404 size_t i, j, e;
6e708ef2
MV
405
406 elts1 = scm_vector_elements (vec1, &handle1, &len1, &inc1);
407 elts2 = scm_vector_writable_elements (vec2, &handle2, &len2, &inc2);
0f2d19dd 408
de5c0f58
MV
409 i = scm_to_unsigned_integer (start1, 0, len1);
410 e = scm_to_unsigned_integer (end1, i, len1);
ca659673 411 SCM_ASSERT_RANGE (SCM_ARG3, end1, (e-i) <= len2);
551b96d2
AW
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);
de5c0f58 416
6e708ef2
MV
417 i *= inc1;
418 e *= inc1;
419 j *= inc2;
420 while (i < e)
de5c0f58 421 {
6e708ef2
MV
422 e -= inc1;
423 j -= inc2;
424 elts2[j] = elts1[e];
de5c0f58 425 }
6e708ef2 426
c8857a4d
MV
427 scm_array_handle_release (&handle2);
428 scm_array_handle_release (&handle1);
429
0f2d19dd
JB
430 return SCM_UNSPECIFIED;
431}
1bbd0b84 432#undef FUNC_NAME
0f2d19dd 433
438974d0 434\f
f65e0168 435SCM_VECTOR_IMPLEMENTATION (SCM_ARRAY_ELEMENT_TYPE_SCM, scm_make_vector)
88797580 436
1cc91f1b 437
0f2d19dd
JB
438void
439scm_init_vectors ()
0f2d19dd 440{
a0599745 441#include "libguile/vectors.x"
0f2d19dd
JB
442}
443
89e00824
ML
444
445/*
446 Local Variables:
447 c-file-style: "gnu"
448 End:
449*/