* Added SCM_SET_VECTOR_LENGTH as one replacement for SCM_SETLENGTH.
[bpt/guile.git] / libguile / vectors.c
CommitLineData
60c497a3 1/* Copyright (C) 1995, 1996, 1998, 1999, 2000 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
47#include <stdio.h>
a0599745
MD
48#include "libguile/_scm.h"
49#include "libguile/eq.h"
50#include "libguile/root.h"
51#include "libguile/strings.h"
52
53#include "libguile/validate.h"
54#include "libguile/vectors.h"
55#include "libguile/unif.h"
0f2d19dd
JB
56\f
57
1b9be268
DH
58#if (SCM_DEBUG_DEPRECATED == 0)
59
60/* The function scm_vector_set_length_x will disappear in the next release of
61 * guile.
62 */
63
afe5177e
GH
64/*
65 * This complicates things too much if allowed on any array.
66 * C code can safely call it on arrays known to be used in a single
67 * threaded manner.
68 *
1bbd0b84 69 * SCM_REGISTER_PROC(s_vector_set_length_x, "vector-set-length!", 2, 0, 0, scm_vector_set_length_x);
afe5177e
GH
70 */
71static char s_vector_set_length_x[] = "vector-set-length!";
72
73
74SCM
1bbd0b84 75scm_vector_set_length_x (SCM vect, SCM len)
afe5177e
GH
76{
77 long l;
78 scm_sizet siz;
79 scm_sizet sz;
9eb364fc 80 char *base;
afe5177e
GH
81
82 l = SCM_INUM (len);
83 SCM_ASRTGO (SCM_NIMP (vect), badarg1);
84
85#ifdef HAVE_ARRAYS
86 if (SCM_TYP7 (vect) == scm_tc7_bvect)
87 {
88 l = (l + SCM_LONG_BIT - 1) / SCM_LONG_BIT;
89 }
90 sz = scm_uniform_element_size (vect);
9eb364fc
DH
91 if (sz != 0)
92 base = SCM_UVECTOR_BASE (vect);
93 else
afe5177e
GH
94#endif
95 switch (SCM_TYP7 (vect))
96 {
97 default:
98 badarg1: scm_wta (vect, (char *) SCM_ARG1, s_vector_set_length_x);
99 case scm_tc7_string:
4260a7fc 100 SCM_ASRTGO (!SCM_EQ_P (vect, scm_nullstr), badarg1);
afe5177e 101 sz = sizeof (char);
9eb364fc 102 base = SCM_STRING_CHARS (vect);
afe5177e
GH
103 l++;
104 break;
105 case scm_tc7_vector:
106 case scm_tc7_wvect:
4260a7fc 107 SCM_ASRTGO (!SCM_EQ_P (vect, scm_nullvect), badarg1);
afe5177e 108 sz = sizeof (SCM);
9eb364fc 109 base = (char *) SCM_VECTOR_BASE (vect);
afe5177e
GH
110 break;
111 }
112 SCM_ASSERT (SCM_INUMP (len), len, SCM_ARG2, s_vector_set_length_x);
113 if (!l)
114 l = 1L;
115 siz = l * sz;
116 if (siz != l * sz)
2500356c 117 scm_memory_error (s_vector_set_length_x);
afe5177e
GH
118 SCM_REDEFER_INTS;
119 SCM_SETCHARS (vect,
120 ((char *)
9eb364fc 121 scm_must_realloc (base,
afe5177e
GH
122 (long) SCM_LENGTH (vect) * sz,
123 (long) siz,
124 s_vector_set_length_x)));
125 if (SCM_VECTORP (vect))
126 {
127 sz = SCM_LENGTH (vect);
128 while (l > sz)
129 SCM_VELTS (vect)[--l] = SCM_UNSPECIFIED;
130 }
131 else if (SCM_STRINGP (vect))
9eb364fc 132 SCM_STRING_CHARS (vect)[l - 1] = 0;
afe5177e
GH
133 SCM_SETLENGTH (vect, SCM_INUM (len), SCM_TYP7 (vect));
134 SCM_REALLOW_INTS;
135 return vect;
136}
0f2d19dd 137
1b9be268
DH
138#endif /* (SCM_DEBUG_DEPRECATED == 0) */
139
a1ec6916 140SCM_DEFINE (scm_vector_p, "vector?", 1, 0, 0,
5ffe9968
GB
141 (SCM obj),
142 "Returns @t{#t} if @var{obj} is a vector, otherwise returns @t{#f}. (r5rs)")
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);
5ffe9968 152/* Returns the number of elements in @var{vector} as an exact integer. (r5rs) */
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
GB
162/*
163 "@samp{List->vector} returns a newly\n"
164 "created vector initialized to the elements of the list @var{list}.\n\n"
165 "@format\n"
166 "@t{(vector->list '#(dah dah didah))\n"
167 "=> (dah dah didah)\n"
168 "list->vector '(dididit dah))\n"
169 "=> #(dididit dah)\n"
170 "}\n"
171 "@end format")
172*/
a1ec6916 173SCM_DEFINE (scm_vector, "vector", 0, 0, 1,
f172c0b7 174 (SCM l),
31daeb2d 175 "@deffnx primitive list->vector l\n"
5ffe9968
GB
176 "Returns a newly allocated vector whose elements contain the given\n"
177 "arguments. Analogous to @samp{list}. (r5rs)\n\n"
178 "@format\n"
179 "@t{(vector 'a 'b 'c) ==> #(a b c) }\n"
180 "@end format")
1bbd0b84 181#define FUNC_NAME s_scm_vector
0f2d19dd
JB
182{
183 SCM res;
184 register SCM *data;
1bbd0b84 185 int i;
f172c0b7
MD
186 SCM_VALIDATE_LIST_COPYLEN (1,l,i);
187 res = scm_make_vector (SCM_MAKINUM (i), SCM_UNSPECIFIED);
188 data = SCM_VELTS (res);
189 for(; i && SCM_NIMP(l); --i, l = SCM_CDR (l))
190 *data++ = SCM_CAR (l);
0f2d19dd
JB
191 return res;
192}
1bbd0b84 193#undef FUNC_NAME
0f2d19dd 194
f172c0b7 195SCM_GPROC (s_vector_ref, "vector-ref", 2, 0, 0, scm_vector_ref, g_vector_ref);
1cc91f1b 196
5ffe9968
GB
197/*
198 "@var{k} must be a valid index of @var{vector}.\n"
199 "@samp{Vector-ref} returns the contents of element @var{k} of\n"
200 "@var{vector}.\n\n"
201 "@format\n"
202 "@t{(vector-ref '#(1 1 2 3 5 8 13 21)\n"
203 " 5)\n"
204 " ==> 8\n"
205 "(vector-ref '#(1 1 2 3 5 8 13 21)\n"
206 " (let ((i (round (* 2 (acos -1)))))\n"
207 " (if (inexact? i)\n"
208 " (inexact->exact i)\n"
209 " i))) \n"
210 " ==> 13\n"
211 "}\n"
212 "@end format"
213*/
214
0f2d19dd 215SCM
ea633082 216scm_vector_ref (SCM v, SCM k)
685c0d71 217#define FUNC_NAME s_vector_ref
0f2d19dd 218{
0c95b57d 219 SCM_GASSERT2 (SCM_VECTORP (v),
9eb8500a
MD
220 g_vector_ref, v, k, SCM_ARG1, s_vector_ref);
221 SCM_GASSERT2 (SCM_INUMP (k),
222 g_vector_ref, v, k, SCM_ARG2, s_vector_ref);
b5c2579a 223 SCM_ASSERT_RANGE (2, k, SCM_INUM (k) < SCM_VECTOR_LENGTH (v) && SCM_INUM (k) >= 0);
ea633082 224 return SCM_VELTS (v)[(long) SCM_INUM (k)];
0f2d19dd 225}
685c0d71 226#undef FUNC_NAME
0f2d19dd 227
f172c0b7 228SCM_GPROC (s_vector_set_x, "vector-set!", 3, 0, 0, scm_vector_set_x, g_vector_set_x);
1cc91f1b 229
5ffe9968
GB
230/* (r5rs)
231@var{k} must be a valid index of @var{vector}.
232@samp{Vector-set!} stores @var{obj} in element @var{k} of @var{vector}.
233The value returned by @samp{vector-set!} is unspecified.
234@c <!>
235
236
237@format
238@t{(let ((vec (vector 0 '(2 2 2 2) "Anna")))
239 (vector-set! vec 1 '("Sue" "Sue"))
240 vec)
241 ==> #(0 ("Sue" "Sue") "Anna")
242
243(vector-set! '#(0 1 2) 1 "doe")
244 ==> @emph{error} ; constant vector
245}
246@end format
247*/
248
0f2d19dd 249SCM
f172c0b7 250scm_vector_set_x (SCM v, SCM k, SCM obj)
685c0d71 251#define FUNC_NAME s_vector_set_x
0f2d19dd 252{
f172c0b7 253 SCM_GASSERTn (SCM_VECTORP (v),
9eb8500a
MD
254 g_vector_set_x, SCM_LIST3 (v, k, obj),
255 SCM_ARG1, s_vector_set_x);
f172c0b7 256 SCM_GASSERTn (SCM_INUMP (k),
9eb8500a
MD
257 g_vector_set_x, SCM_LIST3 (v, k, obj),
258 SCM_ARG2, s_vector_set_x);
b5c2579a 259 SCM_ASSERT_RANGE (2, k, SCM_INUM (k) < SCM_VECTOR_LENGTH (v) && SCM_INUM (k) >= 0);
f172c0b7 260 SCM_VELTS(v)[(long) SCM_INUM(k)] = obj;
60c497a3 261 return SCM_UNSPECIFIED;
0f2d19dd 262}
685c0d71 263#undef FUNC_NAME
0f2d19dd
JB
264
265
a1ec6916 266SCM_DEFINE (scm_make_vector, "make-vector", 1, 1, 0,
1bbd0b84 267 (SCM k, SCM fill),
5ffe9968
GB
268 "Returns a newly allocated vector of @var{k} elements. If a second\n"
269 "argument is given, then each element is initialized to @var{fill}.\n"
270 "Otherwise the initial contents of each element is unspecified. (r5rs)")
1bbd0b84 271#define FUNC_NAME s_scm_make_vector
0f2d19dd
JB
272{
273 SCM v;
1b9be268
DH
274 unsigned long int i;
275 scm_bits_t *velts;
0f2d19dd 276
1b9be268
DH
277 SCM_VALIDATE_INUM_MIN (1, k, 0);
278 if (SCM_UNBNDP (fill))
d60cebe2 279 fill = SCM_UNSPECIFIED;
1b9be268
DH
280
281 i = SCM_INUM (k);
282 SCM_NEWCELL (v);
283
284 velts = (i != 0)
285 ? scm_must_malloc (i * sizeof (scm_bits_t), FUNC_NAME)
286 : NULL;
287
0f2d19dd 288 SCM_DEFER_INTS;
1b9be268
DH
289 {
290 unsigned long int j;
291
292 for (j = 0; j != i; ++j)
293 velts[j] = SCM_UNPACK (fill);
294
295 SCM_SETCHARS (v, velts);
bc0eaf7b 296 SCM_SET_VECTOR_LENGTH (v, i, scm_tc7_vector);
1b9be268 297 }
0f2d19dd 298 SCM_ALLOW_INTS;
1b9be268 299
0f2d19dd
JB
300 return v;
301}
1bbd0b84 302#undef FUNC_NAME
0f2d19dd
JB
303
304
3b3b36dd 305SCM_DEFINE (scm_vector_to_list, "vector->list", 1, 0, 0,
1bbd0b84 306 (SCM v),
5ffe9968
GB
307 "@samp{Vector->list} returns a newly allocated list of the objects contained\n"
308 "in the elements of @var{vector}. (r5rs)\n\n"
309 "@format\n"
310 "@t{(vector->list '#(dah dah didah))\n"
311 "=> (dah dah didah)\n"
312 "list->vector '(dididit dah))\n"
313 "=> #(dididit dah)\n"
314 "}\n"
315 "@end format")
1bbd0b84 316#define FUNC_NAME s_scm_vector_to_list
0f2d19dd
JB
317{
318 SCM res = SCM_EOL;
319 long i;
320 SCM *data;
3b3b36dd 321 SCM_VALIDATE_VECTOR (1,v);
0f2d19dd 322 data = SCM_VELTS(v);
b5c2579a 323 for(i = SCM_VECTOR_LENGTH(v)-1;i >= 0;i--) res = scm_cons(data[i], res);
0f2d19dd
JB
324 return res;
325}
1bbd0b84 326#undef FUNC_NAME
0f2d19dd
JB
327
328
a1ec6916 329SCM_DEFINE (scm_vector_fill_x, "vector-fill!", 2, 0, 0,
1bbd0b84 330 (SCM v, SCM fill_x),
5ffe9968
GB
331 "Stores @var{fill} in every element of @var{vector}.\n"
332 "The value returned by @samp{vector-fill!} is unspecified. (r5rs)")
1bbd0b84 333#define FUNC_NAME s_scm_vector_fill_x
0f2d19dd
JB
334{
335 register long i;
336 register SCM *data;
3b3b36dd 337 SCM_VALIDATE_VECTOR (1,v);
0f2d19dd 338 data = SCM_VELTS(v);
b5c2579a 339 for(i = SCM_VECTOR_LENGTH(v) - 1; i >= 0; i--)
a61ef59b 340 data[i] = fill_x;
0f2d19dd
JB
341 return SCM_UNSPECIFIED;
342}
1bbd0b84 343#undef FUNC_NAME
0f2d19dd
JB
344
345
0f2d19dd 346SCM
1bbd0b84 347scm_vector_equal_p(SCM x, SCM y)
0f2d19dd
JB
348{
349 long i;
b5c2579a 350 for(i = SCM_VECTOR_LENGTH(x)-1;i >= 0;i--)
0f2d19dd
JB
351 if (SCM_FALSEP(scm_equal_p(SCM_VELTS(x)[i], SCM_VELTS(y)[i])))
352 return SCM_BOOL_F;
353 return SCM_BOOL_T;
354}
355
356
a1ec6916 357SCM_DEFINE (scm_vector_move_left_x, "vector-move-left!", 5, 0, 0,
1bbd0b84 358 (SCM vec1, SCM start1, SCM end1, SCM vec2, SCM start2),
b380b885 359 "Vector version of @code{substring-move-left!}.")
1bbd0b84 360#define FUNC_NAME s_scm_vector_move_left_x
0f2d19dd
JB
361{
362 long i;
363 long j;
364 long e;
365
3b3b36dd
GB
366 SCM_VALIDATE_VECTOR (1,vec1);
367 SCM_VALIDATE_INUM_COPY (2,start1,i);
368 SCM_VALIDATE_INUM_COPY (3,end1,e);
369 SCM_VALIDATE_VECTOR (4,vec2);
370 SCM_VALIDATE_INUM_COPY (5,start2,j);
b5c2579a
DH
371 SCM_ASSERT_RANGE (2, start1, i <= SCM_VECTOR_LENGTH (vec1) && i >= 0);
372 SCM_ASSERT_RANGE (5, start2, j <= SCM_VECTOR_LENGTH (vec2) && j >= 0);
373 SCM_ASSERT_RANGE (3, end1, e <= SCM_VECTOR_LENGTH (vec1) && e >= 0);
374 SCM_ASSERT_RANGE (5, start2, e-i+j <= SCM_VECTOR_LENGTH (vec2));
0f2d19dd
JB
375 while (i<e) SCM_VELTS (vec2)[j++] = SCM_VELTS (vec1)[i++];
376 return SCM_UNSPECIFIED;
377}
1bbd0b84 378#undef FUNC_NAME
0f2d19dd 379
a1ec6916 380SCM_DEFINE (scm_vector_move_right_x, "vector-move-right!", 5, 0, 0,
1bbd0b84 381 (SCM vec1, SCM start1, SCM end1, SCM vec2, SCM start2),
b380b885 382 "Vector version of @code{substring-move-right!}.")
1bbd0b84 383#define FUNC_NAME s_scm_vector_move_right_x
0f2d19dd
JB
384{
385 long i;
386 long j;
387 long e;
388
3b3b36dd
GB
389 SCM_VALIDATE_VECTOR (1,vec1);
390 SCM_VALIDATE_INUM_COPY (2,start1,i);
391 SCM_VALIDATE_INUM_COPY (3,end1,e);
392 SCM_VALIDATE_VECTOR (4,vec2);
393 SCM_VALIDATE_INUM_COPY (5,start2,j);
b5c2579a
DH
394 SCM_ASSERT_RANGE (2, start1, i <= SCM_VECTOR_LENGTH (vec1) && i >= 0);
395 SCM_ASSERT_RANGE (5, start2, j <= SCM_VECTOR_LENGTH (vec2) && j >= 0);
396 SCM_ASSERT_RANGE (3, end1, e <= SCM_VECTOR_LENGTH (vec1) && e >= 0);
2cc41672 397 j = e - i + j;
b5c2579a 398 SCM_ASSERT_RANGE (5, start2, j <= SCM_VECTOR_LENGTH (vec2));
2cc41672
MD
399 while (i < e)
400 SCM_VELTS (vec2)[--j] = SCM_VELTS (vec1)[--e];
0f2d19dd
JB
401 return SCM_UNSPECIFIED;
402}
1bbd0b84 403#undef FUNC_NAME
0f2d19dd
JB
404
405
1cc91f1b 406
0f2d19dd
JB
407void
408scm_init_vectors ()
0f2d19dd 409{
8dc9439f 410#ifndef SCM_MAGIC_SNARFER
a0599745 411#include "libguile/vectors.x"
8dc9439f 412#endif
0f2d19dd
JB
413}
414
89e00824
ML
415
416/*
417 Local Variables:
418 c-file-style: "gnu"
419 End:
420*/