* vectors.c (s_scm_vector_p, list->vector, scm_vector)
[bpt/guile.git] / libguile / vectors.c
1 /* Copyright (C) 1995, 1996, 1998, 1999, 2000 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 "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"
56 \f
57
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
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 *
69 * SCM_REGISTER_PROC(s_vector_set_length_x, "vector-set-length!", 2, 0, 0, scm_vector_set_length_x);
70 */
71 static char s_vector_set_length_x[] = "vector-set-length!";
72
73
74 SCM
75 scm_vector_set_length_x (SCM vect, SCM len)
76 {
77 long l;
78 scm_sizet siz;
79 scm_sizet sz;
80 char *base;
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);
91 if (sz != 0)
92 base = SCM_UVECTOR_BASE (vect);
93 else
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:
100 SCM_ASRTGO (!SCM_EQ_P (vect, scm_nullstr), badarg1);
101 sz = sizeof (char);
102 base = SCM_STRING_CHARS (vect);
103 l++;
104 break;
105 case scm_tc7_vector:
106 case scm_tc7_wvect:
107 SCM_ASRTGO (!SCM_EQ_P (vect, scm_nullvect), badarg1);
108 sz = sizeof (SCM);
109 base = (char *) SCM_VECTOR_BASE (vect);
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)
117 scm_memory_error (s_vector_set_length_x);
118 SCM_REDEFER_INTS;
119 SCM_SETCHARS (vect,
120 ((char *)
121 scm_must_realloc (base,
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))
132 SCM_STRING_CHARS (vect)[l - 1] = 0;
133 SCM_SETLENGTH (vect, SCM_INUM (len), SCM_TYP7 (vect));
134 SCM_REALLOW_INTS;
135 return vect;
136 }
137
138 #endif /* (SCM_DEBUG_DEPRECATED == 0) */
139
140 SCM_DEFINE (scm_vector_p, "vector?", 1, 0, 0,
141 (SCM obj),
142 "Returns @code{#t} if @var{obj} is a vector, otherwise returns\n"
143 "@code{#f}. (r5rs)")
144 #define FUNC_NAME s_scm_vector_p
145 {
146 if (SCM_IMP (obj))
147 return SCM_BOOL_F;
148 return SCM_BOOL (SCM_VECTORP (obj));
149 }
150 #undef FUNC_NAME
151
152 SCM_GPROC (s_vector_length, "vector-length", 1, 0, 0, scm_vector_length, g_vector_length);
153 /* Returns the number of elements in @var{vector} as an exact integer. (r5rs) */
154 SCM
155 scm_vector_length (SCM v)
156 {
157 SCM_GASSERT1 (SCM_VECTORP(v),
158 g_vector_length, v, SCM_ARG1, s_vector_length);
159 return SCM_MAKINUM (SCM_VECTOR_LENGTH (v));
160 }
161
162 SCM_REGISTER_PROC (s_list_to_vector, "list->vector", 1, 0, 0, scm_vector);
163 /*
164 "Return a newly created vector initialized to the elements of"
165 "the list @var{list}.\n\n"
166 "@lisp\n"
167 "(vector->list '#(dah dah didah)) @result{} (dah dah didah)\n"
168 "(list->vector '(dididit dah)) @result{} #(dididit dah)\n"
169 "@end lisp")
170 */
171 SCM_DEFINE (scm_vector, "vector", 0, 0, 1,
172 (SCM l),
173 "@deffnx primitive list->vector l\n"
174 "Returns a newly allocated vector whose elements contain the\n"
175 "given arguments. Analogous to @code{list}. (r5rs)\n\n"
176 "@lisp\n"
177 "(vector 'a 'b 'c) @result{} #(a b c)\n"
178 "@end lisp")
179 #define FUNC_NAME s_scm_vector
180 {
181 SCM res;
182 register SCM *data;
183 int i;
184 SCM_VALIDATE_LIST_COPYLEN (1,l,i);
185 res = scm_c_make_vector (i, SCM_UNSPECIFIED);
186 data = SCM_VELTS (res);
187 for(; i && SCM_NIMP(l); --i, l = SCM_CDR (l))
188 *data++ = SCM_CAR (l);
189 return res;
190 }
191 #undef FUNC_NAME
192
193 SCM_GPROC (s_vector_ref, "vector-ref", 2, 0, 0, scm_vector_ref, g_vector_ref);
194
195 /*
196 "@var{k} must be a valid index of @var{vector}.\n"
197 "@samp{Vector-ref} returns the contents of element @var{k} of\n"
198 "@var{vector}.\n\n"
199 "@lisp\n"
200 "(vector-ref '#(1 1 2 3 5 8 13 21) 5) @result{} 8\n"
201 "(vector-ref '#(1 1 2 3 5 8 13 21)\n"
202 " (let ((i (round (* 2 (acos -1)))))\n"
203 " (if (inexact? i)\n"
204 " (inexact->exact i)\n"
205 " i))) @result{} 13\n"
206 "@end lisp"
207 */
208
209 SCM
210 scm_vector_ref (SCM v, SCM k)
211 #define FUNC_NAME s_vector_ref
212 {
213 SCM_GASSERT2 (SCM_VECTORP (v),
214 g_vector_ref, v, k, SCM_ARG1, s_vector_ref);
215 SCM_GASSERT2 (SCM_INUMP (k),
216 g_vector_ref, v, k, SCM_ARG2, s_vector_ref);
217 SCM_ASSERT_RANGE (2, k, SCM_INUM (k) < SCM_VECTOR_LENGTH (v) && SCM_INUM (k) >= 0);
218 return SCM_VELTS (v)[(long) SCM_INUM (k)];
219 }
220 #undef FUNC_NAME
221
222 SCM_GPROC (s_vector_set_x, "vector-set!", 3, 0, 0, scm_vector_set_x, g_vector_set_x);
223
224 /* "@var{k} must be a valid index of @var{vector}.\n"
225 "@code{Vector-set!} stores @var{obj} in element @var{k} of @var{vector}.\n"
226 "The value returned by @samp{vector-set!} is unspecified.\n"
227 "@lisp\n"
228 "(let ((vec (vector 0 '(2 2 2 2) "Anna")))\n"
229 " (vector-set! vec 1 '("Sue" "Sue"))\n"
230 " vec) @result{} #(0 ("Sue" "Sue") "Anna")\n"
231 "(vector-set! '#(0 1 2) 1 "doe") @result{} @emph{error} ; constant vector\n"
232 "@end lisp"
233 */
234
235 SCM
236 scm_vector_set_x (SCM v, SCM k, SCM obj)
237 #define FUNC_NAME s_vector_set_x
238 {
239 SCM_GASSERTn (SCM_VECTORP (v),
240 g_vector_set_x, SCM_LIST3 (v, k, obj),
241 SCM_ARG1, s_vector_set_x);
242 SCM_GASSERTn (SCM_INUMP (k),
243 g_vector_set_x, SCM_LIST3 (v, k, obj),
244 SCM_ARG2, s_vector_set_x);
245 SCM_ASSERT_RANGE (2, k, SCM_INUM (k) < SCM_VECTOR_LENGTH (v) && SCM_INUM (k) >= 0);
246 SCM_VELTS(v)[(long) SCM_INUM(k)] = obj;
247 return SCM_UNSPECIFIED;
248 }
249 #undef FUNC_NAME
250
251
252 SCM_DEFINE (scm_make_vector, "make-vector", 1, 1, 0,
253 (SCM k, SCM fill),
254 "Returns a newly allocated vector of @var{k} elements. If a second\n"
255 "argument is given, then each element is initialized to @var{fill}.\n"
256 "Otherwise the initial contents of each element is unspecified. (r5rs)")
257 #define FUNC_NAME s_scm_make_vector
258 {
259 if (SCM_UNBNDP (fill))
260 fill = SCM_UNSPECIFIED;
261
262 if (SCM_INUMP (k))
263 {
264 SCM_ASSERT_RANGE (1, k, SCM_INUM (k) >= 0);
265 return scm_c_make_vector (SCM_INUM (k), fill);
266 }
267 else if (SCM_BIGP (k))
268 SCM_OUT_OF_RANGE (1, k);
269 else
270 SCM_WRONG_TYPE_ARG (1, k);
271 }
272 #undef FUNC_NAME
273
274
275 SCM
276 scm_c_make_vector (unsigned long int k, SCM fill)
277 #define FUNC_NAME s_scm_make_vector
278 {
279 SCM v;
280 scm_bits_t *base;
281
282 if (k > 0)
283 {
284 unsigned long int j;
285
286 SCM_ASSERT_RANGE (1, scm_ulong2num (k), k <= SCM_VECTOR_MAX_LENGTH);
287
288 base = scm_must_malloc (k * sizeof (scm_bits_t), FUNC_NAME);
289 for (j = 0; j != k; ++j)
290 base[j] = SCM_UNPACK (fill);
291 }
292 else
293 base = NULL;
294
295 SCM_NEWCELL (v);
296 SCM_SET_VECTOR_BASE (v, base);
297 SCM_SET_VECTOR_LENGTH (v, k, scm_tc7_vector);
298 scm_remember_upto_here_1 (fill);
299
300 return v;
301 }
302 #undef FUNC_NAME
303
304
305 SCM_DEFINE (scm_vector_to_list, "vector->list", 1, 0, 0,
306 (SCM v),
307 "@samp{Vector->list} returns a newly allocated list of the\n"
308 "objects contained in the elements of @var{vector}. (r5rs)\n\n"
309 "@lisp\n"
310 "(vector->list '#(dah dah didah)) @result{} (dah dah didah)\n"
311 "(list->vector '(dididit dah)) @result{} #(dididit dah)\n"
312 "@end lisp")
313 #define FUNC_NAME s_scm_vector_to_list
314 {
315 SCM res = SCM_EOL;
316 long i;
317 SCM *data;
318 SCM_VALIDATE_VECTOR (1,v);
319 data = SCM_VELTS(v);
320 for(i = SCM_VECTOR_LENGTH(v)-1;i >= 0;i--) res = scm_cons(data[i], res);
321 return res;
322 }
323 #undef FUNC_NAME
324
325
326 SCM_DEFINE (scm_vector_fill_x, "vector-fill!", 2, 0, 0,
327 (SCM v, SCM fill_x),
328 "Stores @var{fill} in every element of @var{vector}.\n"
329 "The value returned by @code{vector-fill!} is unspecified. (r5rs)")
330 #define FUNC_NAME s_scm_vector_fill_x
331 {
332 register long i;
333 register SCM *data;
334 SCM_VALIDATE_VECTOR (1,v);
335 data = SCM_VELTS(v);
336 for(i = SCM_VECTOR_LENGTH(v) - 1; i >= 0; i--)
337 data[i] = fill_x;
338 return SCM_UNSPECIFIED;
339 }
340 #undef FUNC_NAME
341
342
343 SCM
344 scm_vector_equal_p(SCM x, SCM y)
345 {
346 long i;
347 for(i = SCM_VECTOR_LENGTH(x)-1;i >= 0;i--)
348 if (SCM_FALSEP(scm_equal_p(SCM_VELTS(x)[i], SCM_VELTS(y)[i])))
349 return SCM_BOOL_F;
350 return SCM_BOOL_T;
351 }
352
353
354 SCM_DEFINE (scm_vector_move_left_x, "vector-move-left!", 5, 0, 0,
355 (SCM vec1, SCM start1, SCM end1, SCM vec2, SCM start2),
356 "Vector version of @code{substring-move-left!}.")
357 #define FUNC_NAME s_scm_vector_move_left_x
358 {
359 long i;
360 long j;
361 long e;
362
363 SCM_VALIDATE_VECTOR (1,vec1);
364 SCM_VALIDATE_INUM_COPY (2,start1,i);
365 SCM_VALIDATE_INUM_COPY (3,end1,e);
366 SCM_VALIDATE_VECTOR (4,vec2);
367 SCM_VALIDATE_INUM_COPY (5,start2,j);
368 SCM_ASSERT_RANGE (2, start1, i <= SCM_VECTOR_LENGTH (vec1) && i >= 0);
369 SCM_ASSERT_RANGE (5, start2, j <= SCM_VECTOR_LENGTH (vec2) && j >= 0);
370 SCM_ASSERT_RANGE (3, end1, e <= SCM_VECTOR_LENGTH (vec1) && e >= 0);
371 SCM_ASSERT_RANGE (5, start2, e-i+j <= SCM_VECTOR_LENGTH (vec2));
372 while (i<e) SCM_VELTS (vec2)[j++] = SCM_VELTS (vec1)[i++];
373 return SCM_UNSPECIFIED;
374 }
375 #undef FUNC_NAME
376
377 SCM_DEFINE (scm_vector_move_right_x, "vector-move-right!", 5, 0, 0,
378 (SCM vec1, SCM start1, SCM end1, SCM vec2, SCM start2),
379 "Vector version of @code{substring-move-right!}.")
380 #define FUNC_NAME s_scm_vector_move_right_x
381 {
382 long i;
383 long j;
384 long e;
385
386 SCM_VALIDATE_VECTOR (1,vec1);
387 SCM_VALIDATE_INUM_COPY (2,start1,i);
388 SCM_VALIDATE_INUM_COPY (3,end1,e);
389 SCM_VALIDATE_VECTOR (4,vec2);
390 SCM_VALIDATE_INUM_COPY (5,start2,j);
391 SCM_ASSERT_RANGE (2, start1, i <= SCM_VECTOR_LENGTH (vec1) && i >= 0);
392 SCM_ASSERT_RANGE (5, start2, j <= SCM_VECTOR_LENGTH (vec2) && j >= 0);
393 SCM_ASSERT_RANGE (3, end1, e <= SCM_VECTOR_LENGTH (vec1) && e >= 0);
394 j = e - i + j;
395 SCM_ASSERT_RANGE (5, start2, j <= SCM_VECTOR_LENGTH (vec2));
396 while (i < e)
397 SCM_VELTS (vec2)[--j] = SCM_VELTS (vec1)[--e];
398 return SCM_UNSPECIFIED;
399 }
400 #undef FUNC_NAME
401
402
403
404 void
405 scm_init_vectors ()
406 {
407 #ifndef SCM_MAGIC_SNARFER
408 #include "libguile/vectors.x"
409 #endif
410 }
411
412
413 /*
414 Local Variables:
415 c-file-style: "gnu"
416 End:
417 */