revert the ill-considered part of 2001-05-23 changes
[bpt/guile.git] / libguile / vectors.c
CommitLineData
22a52da1 1/* Copyright (C) 1995,1996,1998,1999,2000,2001 Free Software Foundation, Inc.
0f2d19dd
JB
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
6 * any later version.
7 *
8 * This program 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
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
82892bed
JB
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
0f2d19dd
JB
17 *
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
20 *
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
26 *
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
29 *
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
37 *
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
82892bed 40 * If you do not wish that, delete this exception notice. */
1bbd0b84
GB
41
42/* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
44
0f2d19dd
JB
45\f
46
a0599745
MD
47#include "libguile/_scm.h"
48#include "libguile/eq.h"
49#include "libguile/root.h"
50#include "libguile/strings.h"
51
52#include "libguile/validate.h"
53#include "libguile/vectors.h"
54#include "libguile/unif.h"
0f2d19dd
JB
55\f
56
1b9be268
DH
57#if (SCM_DEBUG_DEPRECATED == 0)
58
59/* The function scm_vector_set_length_x will disappear in the next release of
60 * guile.
61 */
62
afe5177e
GH
63/*
64 * This complicates things too much if allowed on any array.
65 * C code can safely call it on arrays known to be used in a single
66 * threaded manner.
67 *
1bbd0b84 68 * SCM_REGISTER_PROC(s_vector_set_length_x, "vector-set-length!", 2, 0, 0, scm_vector_set_length_x);
afe5177e
GH
69 */
70static char s_vector_set_length_x[] = "vector-set-length!";
71
72
73SCM
1bbd0b84 74scm_vector_set_length_x (SCM vect, SCM len)
afe5177e 75{
1be6b49c
ML
76 scm_bits_t l;
77 size_t siz;
78 size_t sz;
9eb364fc 79 char *base;
afe5177e
GH
80
81 l = SCM_INUM (len);
82 SCM_ASRTGO (SCM_NIMP (vect), badarg1);
83
84#ifdef HAVE_ARRAYS
85 if (SCM_TYP7 (vect) == scm_tc7_bvect)
86 {
1be6b49c 87 l = (l + SCM_BITS_LENGTH - 1) / SCM_BITS_LENGTH;
afe5177e
GH
88 }
89 sz = scm_uniform_element_size (vect);
9eb364fc
DH
90 if (sz != 0)
91 base = SCM_UVECTOR_BASE (vect);
92 else
afe5177e
GH
93#endif
94 switch (SCM_TYP7 (vect))
95 {
96 default:
97 badarg1: scm_wta (vect, (char *) SCM_ARG1, s_vector_set_length_x);
98 case scm_tc7_string:
4260a7fc 99 SCM_ASRTGO (!SCM_EQ_P (vect, scm_nullstr), badarg1);
afe5177e 100 sz = sizeof (char);
9eb364fc 101 base = SCM_STRING_CHARS (vect);
afe5177e
GH
102 l++;
103 break;
104 case scm_tc7_vector:
105 case scm_tc7_wvect:
4260a7fc 106 SCM_ASRTGO (!SCM_EQ_P (vect, scm_nullvect), badarg1);
afe5177e 107 sz = sizeof (SCM);
9eb364fc 108 base = (char *) SCM_VECTOR_BASE (vect);
afe5177e
GH
109 break;
110 }
111 SCM_ASSERT (SCM_INUMP (len), len, SCM_ARG2, s_vector_set_length_x);
112 if (!l)
113 l = 1L;
114 siz = l * sz;
115 if (siz != l * sz)
2500356c 116 scm_memory_error (s_vector_set_length_x);
afe5177e
GH
117 SCM_REDEFER_INTS;
118 SCM_SETCHARS (vect,
119 ((char *)
9eb364fc 120 scm_must_realloc (base,
1be6b49c
ML
121 (size_t) SCM_LENGTH (vect) * sz,
122 (size_t) siz,
afe5177e
GH
123 s_vector_set_length_x)));
124 if (SCM_VECTORP (vect))
125 {
126 sz = SCM_LENGTH (vect);
127 while (l > sz)
128 SCM_VELTS (vect)[--l] = SCM_UNSPECIFIED;
129 }
130 else if (SCM_STRINGP (vect))
9eb364fc 131 SCM_STRING_CHARS (vect)[l - 1] = 0;
afe5177e
GH
132 SCM_SETLENGTH (vect, SCM_INUM (len), SCM_TYP7 (vect));
133 SCM_REALLOW_INTS;
134 return vect;
135}
0f2d19dd 136
1b9be268
DH
137#endif /* (SCM_DEBUG_DEPRECATED == 0) */
138
a1ec6916 139SCM_DEFINE (scm_vector_p, "vector?", 1, 0, 0,
5ffe9968 140 (SCM obj),
1e6808ea
MG
141 "Return @code{#t} if @var{obj} is a vector, otherwise return\n"
142 "@code{#f}.")
1bbd0b84 143#define FUNC_NAME s_scm_vector_p
0f2d19dd 144{
5ffe9968 145 if (SCM_IMP (obj))
f172c0b7 146 return SCM_BOOL_F;
5ffe9968 147 return SCM_BOOL (SCM_VECTORP (obj));
0f2d19dd 148}
1bbd0b84 149#undef FUNC_NAME
0f2d19dd 150
f172c0b7 151SCM_GPROC (s_vector_length, "vector-length", 1, 0, 0, scm_vector_length, g_vector_length);
1e6808ea 152/* Returns the number of elements in @var{vector} as an exact integer. */
0f2d19dd 153SCM
f172c0b7 154scm_vector_length (SCM v)
0f2d19dd 155{
f172c0b7
MD
156 SCM_GASSERT1 (SCM_VECTORP(v),
157 g_vector_length, v, SCM_ARG1, s_vector_length);
b5c2579a 158 return SCM_MAKINUM (SCM_VECTOR_LENGTH (v));
0f2d19dd
JB
159}
160
f172c0b7 161SCM_REGISTER_PROC (s_list_to_vector, "list->vector", 1, 0, 0, scm_vector);
5ffe9968 162/*
942e5b91
MG
163 "Return a newly created vector initialized to the elements of"
164 "the list @var{list}.\n\n"
165 "@lisp\n"
166 "(vector->list '#(dah dah didah)) @result{} (dah dah didah)\n"
167 "(list->vector '(dididit dah)) @result{} #(dididit dah)\n"
168 "@end lisp")
5ffe9968 169*/
a1ec6916 170SCM_DEFINE (scm_vector, "vector", 0, 0, 1,
f172c0b7 171 (SCM l),
31daeb2d 172 "@deffnx primitive list->vector l\n"
1e6808ea
MG
173 "Return a newly allocated vector whose elements contain the\n"
174 "given arguments. Analogous to @code{list}.\n"
175 "\n"
942e5b91 176 "@lisp\n"
1e6808ea 177 "(vector 'a 'b 'c) @result{} #(a b c)\n"
942e5b91 178 "@end lisp")
1bbd0b84 179#define FUNC_NAME s_scm_vector
0f2d19dd
JB
180{
181 SCM res;
22a52da1 182 SCM *data;
1be6b49c 183 scm_bits_t i;
22a52da1
DH
184
185 /* Dirk:FIXME:: In case of multiple threads, the list might get corrupted
186 while the vector is being created. */
187 SCM_VALIDATE_LIST_COPYLEN (1, l, i);
00ffa0e7 188 res = scm_c_make_vector (i, SCM_UNSPECIFIED);
f172c0b7 189 data = SCM_VELTS (res);
22a52da1
DH
190 while (!SCM_NULLP (l))
191 {
192 *data++ = SCM_CAR (l);
193 l = SCM_CDR (l);
194 }
195
0f2d19dd
JB
196 return res;
197}
1bbd0b84 198#undef FUNC_NAME
0f2d19dd 199
f172c0b7 200SCM_GPROC (s_vector_ref, "vector-ref", 2, 0, 0, scm_vector_ref, g_vector_ref);
1cc91f1b 201
5ffe9968
GB
202/*
203 "@var{k} must be a valid index of @var{vector}.\n"
204 "@samp{Vector-ref} returns the contents of element @var{k} of\n"
205 "@var{vector}.\n\n"
942e5b91
MG
206 "@lisp\n"
207 "(vector-ref '#(1 1 2 3 5 8 13 21) 5) @result{} 8\n"
5ffe9968
GB
208 "(vector-ref '#(1 1 2 3 5 8 13 21)\n"
209 " (let ((i (round (* 2 (acos -1)))))\n"
210 " (if (inexact? i)\n"
211 " (inexact->exact i)\n"
942e5b91
MG
212 " i))) @result{} 13\n"
213 "@end lisp"
5ffe9968
GB
214*/
215
0f2d19dd 216SCM
ea633082 217scm_vector_ref (SCM v, SCM k)
685c0d71 218#define FUNC_NAME s_vector_ref
0f2d19dd 219{
0c95b57d 220 SCM_GASSERT2 (SCM_VECTORP (v),
9eb8500a
MD
221 g_vector_ref, v, k, SCM_ARG1, s_vector_ref);
222 SCM_GASSERT2 (SCM_INUMP (k),
223 g_vector_ref, v, k, SCM_ARG2, s_vector_ref);
b5c2579a 224 SCM_ASSERT_RANGE (2, k, SCM_INUM (k) < SCM_VECTOR_LENGTH (v) && SCM_INUM (k) >= 0);
1be6b49c 225 return SCM_VELTS (v)[(ptrdiff_t) SCM_INUM (k)];
0f2d19dd 226}
685c0d71 227#undef FUNC_NAME
0f2d19dd 228
f172c0b7 229SCM_GPROC (s_vector_set_x, "vector-set!", 3, 0, 0, scm_vector_set_x, g_vector_set_x);
1cc91f1b 230
942e5b91
MG
231/* "@var{k} must be a valid index of @var{vector}.\n"
232 "@code{Vector-set!} stores @var{obj} in element @var{k} of @var{vector}.\n"
233 "The value returned by @samp{vector-set!} is unspecified.\n"
234 "@lisp\n"
235 "(let ((vec (vector 0 '(2 2 2 2) "Anna")))\n"
236 " (vector-set! vec 1 '("Sue" "Sue"))\n"
237 " vec) @result{} #(0 ("Sue" "Sue") "Anna")\n"
238 "(vector-set! '#(0 1 2) 1 "doe") @result{} @emph{error} ; constant vector\n"
239 "@end lisp"
5ffe9968
GB
240*/
241
0f2d19dd 242SCM
f172c0b7 243scm_vector_set_x (SCM v, SCM k, SCM obj)
685c0d71 244#define FUNC_NAME s_vector_set_x
0f2d19dd 245{
f172c0b7 246 SCM_GASSERTn (SCM_VECTORP (v),
9eb8500a
MD
247 g_vector_set_x, SCM_LIST3 (v, k, obj),
248 SCM_ARG1, s_vector_set_x);
f172c0b7 249 SCM_GASSERTn (SCM_INUMP (k),
9eb8500a
MD
250 g_vector_set_x, SCM_LIST3 (v, k, obj),
251 SCM_ARG2, s_vector_set_x);
b5c2579a 252 SCM_ASSERT_RANGE (2, k, SCM_INUM (k) < SCM_VECTOR_LENGTH (v) && SCM_INUM (k) >= 0);
1be6b49c 253 SCM_VELTS(v)[(ptrdiff_t) SCM_INUM(k)] = obj;
60c497a3 254 return SCM_UNSPECIFIED;
0f2d19dd 255}
685c0d71 256#undef FUNC_NAME
0f2d19dd
JB
257
258
a1ec6916 259SCM_DEFINE (scm_make_vector, "make-vector", 1, 1, 0,
1bbd0b84 260 (SCM k, SCM fill),
1e6808ea
MG
261 "Return a newly allocated vector of @var{k} elements. If a\n"
262 "second argument is given, then each element is initialized to\n"
263 "@var{fill}. Otherwise the initial contents of each element is\n"
264 "unspecified.")
1bbd0b84 265#define FUNC_NAME s_scm_make_vector
0f2d19dd 266{
1b9be268 267 if (SCM_UNBNDP (fill))
d60cebe2 268 fill = SCM_UNSPECIFIED;
e382fdbe
DH
269
270 if (SCM_INUMP (k))
271 {
cb0d8be2 272 SCM_ASSERT_RANGE (1, k, SCM_INUM (k) >= 0);
e382fdbe
DH
273 return scm_c_make_vector (SCM_INUM (k), fill);
274 }
275 else if (SCM_BIGP (k))
276 SCM_OUT_OF_RANGE (1, k);
277 else
278 SCM_WRONG_TYPE_ARG (1, k);
00ffa0e7
KN
279}
280#undef FUNC_NAME
281
e382fdbe 282
00ffa0e7 283SCM
1be6b49c 284scm_c_make_vector (size_t k, SCM fill)
00ffa0e7
KN
285#define FUNC_NAME s_scm_make_vector
286{
287 SCM v;
e382fdbe 288 scm_bits_t *base;
1b9be268 289
e382fdbe
DH
290 if (k > 0)
291 {
1be6b49c 292 size_t j;
1b9be268 293
1be6b49c 294 SCM_ASSERT_RANGE (1, scm_size2num (k), k <= SCM_VECTOR_MAX_LENGTH);
1b9be268 295
e382fdbe
DH
296 base = scm_must_malloc (k * sizeof (scm_bits_t), FUNC_NAME);
297 for (j = 0; j != k; ++j)
298 base[j] = SCM_UNPACK (fill);
299 }
300 else
301 base = NULL;
1b9be268 302
e382fdbe
DH
303 SCM_NEWCELL (v);
304 SCM_SET_VECTOR_BASE (v, base);
305 SCM_SET_VECTOR_LENGTH (v, k, scm_tc7_vector);
306 scm_remember_upto_here_1 (fill);
1b9be268 307
0f2d19dd
JB
308 return v;
309}
1bbd0b84 310#undef FUNC_NAME
0f2d19dd 311
e382fdbe 312
3b3b36dd 313SCM_DEFINE (scm_vector_to_list, "vector->list", 1, 0, 0,
1e6808ea
MG
314 (SCM v),
315 "Return a newly allocated list of the objects contained in the\n"
316 "elements of @var{vector}.\n"
317 "\n"
942e5b91
MG
318 "@lisp\n"
319 "(vector->list '#(dah dah didah)) @result{} (dah dah didah)\n"
320 "(list->vector '(dididit dah)) @result{} #(dididit dah)\n"
321 "@end lisp")
1bbd0b84 322#define FUNC_NAME s_scm_vector_to_list
0f2d19dd
JB
323{
324 SCM res = SCM_EOL;
1be6b49c 325 scm_bits_t i;
0f2d19dd 326 SCM *data;
3b3b36dd 327 SCM_VALIDATE_VECTOR (1,v);
0f2d19dd 328 data = SCM_VELTS(v);
b5c2579a 329 for(i = SCM_VECTOR_LENGTH(v)-1;i >= 0;i--) res = scm_cons(data[i], res);
0f2d19dd
JB
330 return res;
331}
1bbd0b84 332#undef FUNC_NAME
0f2d19dd
JB
333
334
a1ec6916 335SCM_DEFINE (scm_vector_fill_x, "vector-fill!", 2, 0, 0,
1e6808ea
MG
336 (SCM v, SCM fill),
337 "Store @var{fill} in every element of @var{vector}. The value\n"
338 "returned by @code{vector-fill!} is unspecified.")
1bbd0b84 339#define FUNC_NAME s_scm_vector_fill_x
0f2d19dd 340{
1be6b49c 341 register scm_bits_t i;
0f2d19dd 342 register SCM *data;
1be6b49c
ML
343 SCM_VALIDATE_VECTOR (1, v);
344 data = SCM_VELTS (v);
345 for(i = SCM_VECTOR_LENGTH (v) - 1; i >= 0; i--)
1e6808ea 346 data[i] = fill;
0f2d19dd
JB
347 return SCM_UNSPECIFIED;
348}
1bbd0b84 349#undef FUNC_NAME
0f2d19dd
JB
350
351
0f2d19dd 352SCM
1bbd0b84 353scm_vector_equal_p(SCM x, SCM y)
0f2d19dd 354{
1be6b49c
ML
355 scm_bits_t i;
356 for(i = SCM_VECTOR_LENGTH (x) - 1; i >= 0; i--)
357 if (SCM_FALSEP (scm_equal_p (SCM_VELTS (x)[i], SCM_VELTS (y)[i])))
0f2d19dd
JB
358 return SCM_BOOL_F;
359 return SCM_BOOL_T;
360}
361
362
a1ec6916 363SCM_DEFINE (scm_vector_move_left_x, "vector-move-left!", 5, 0, 0,
1bbd0b84 364 (SCM vec1, SCM start1, SCM end1, SCM vec2, SCM start2),
b380b885 365 "Vector version of @code{substring-move-left!}.")
1bbd0b84 366#define FUNC_NAME s_scm_vector_move_left_x
0f2d19dd 367{
1be6b49c
ML
368 scm_bits_t i;
369 scm_bits_t j;
370 scm_bits_t e;
0f2d19dd 371
3b3b36dd
GB
372 SCM_VALIDATE_VECTOR (1,vec1);
373 SCM_VALIDATE_INUM_COPY (2,start1,i);
374 SCM_VALIDATE_INUM_COPY (3,end1,e);
375 SCM_VALIDATE_VECTOR (4,vec2);
376 SCM_VALIDATE_INUM_COPY (5,start2,j);
b5c2579a
DH
377 SCM_ASSERT_RANGE (2, start1, i <= SCM_VECTOR_LENGTH (vec1) && i >= 0);
378 SCM_ASSERT_RANGE (5, start2, j <= SCM_VECTOR_LENGTH (vec2) && j >= 0);
379 SCM_ASSERT_RANGE (3, end1, e <= SCM_VECTOR_LENGTH (vec1) && e >= 0);
380 SCM_ASSERT_RANGE (5, start2, e-i+j <= SCM_VECTOR_LENGTH (vec2));
0f2d19dd
JB
381 while (i<e) SCM_VELTS (vec2)[j++] = SCM_VELTS (vec1)[i++];
382 return SCM_UNSPECIFIED;
383}
1bbd0b84 384#undef FUNC_NAME
0f2d19dd 385
a1ec6916 386SCM_DEFINE (scm_vector_move_right_x, "vector-move-right!", 5, 0, 0,
1bbd0b84 387 (SCM vec1, SCM start1, SCM end1, SCM vec2, SCM start2),
b380b885 388 "Vector version of @code{substring-move-right!}.")
1bbd0b84 389#define FUNC_NAME s_scm_vector_move_right_x
0f2d19dd 390{
1be6b49c
ML
391 scm_bits_t i;
392 scm_bits_t j;
393 scm_bits_t e;
0f2d19dd 394
3b3b36dd
GB
395 SCM_VALIDATE_VECTOR (1,vec1);
396 SCM_VALIDATE_INUM_COPY (2,start1,i);
397 SCM_VALIDATE_INUM_COPY (3,end1,e);
398 SCM_VALIDATE_VECTOR (4,vec2);
399 SCM_VALIDATE_INUM_COPY (5,start2,j);
b5c2579a
DH
400 SCM_ASSERT_RANGE (2, start1, i <= SCM_VECTOR_LENGTH (vec1) && i >= 0);
401 SCM_ASSERT_RANGE (5, start2, j <= SCM_VECTOR_LENGTH (vec2) && j >= 0);
402 SCM_ASSERT_RANGE (3, end1, e <= SCM_VECTOR_LENGTH (vec1) && e >= 0);
2cc41672 403 j = e - i + j;
b5c2579a 404 SCM_ASSERT_RANGE (5, start2, j <= SCM_VECTOR_LENGTH (vec2));
2cc41672
MD
405 while (i < e)
406 SCM_VELTS (vec2)[--j] = SCM_VELTS (vec1)[--e];
0f2d19dd
JB
407 return SCM_UNSPECIFIED;
408}
1bbd0b84 409#undef FUNC_NAME
0f2d19dd
JB
410
411
1cc91f1b 412
0f2d19dd
JB
413void
414scm_init_vectors ()
0f2d19dd 415{
7c33806a
DH
416 scm_nullvect = scm_c_make_vector (0, SCM_UNDEFINED);
417
8dc9439f 418#ifndef SCM_MAGIC_SNARFER
a0599745 419#include "libguile/vectors.x"
8dc9439f 420#endif
0f2d19dd
JB
421}
422
89e00824
ML
423
424/*
425 Local Variables:
426 c-file-style: "gnu"
427 End:
428*/