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