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