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