*** 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. */
0f2d19dd
JB
41\f
42
43#include <stdio.h>
44#include "_scm.h"
20e6290e 45#include "eq.h"
0f2d19dd 46
20e6290e 47#include "vectors.h"
6e706938 48#include "unif.h"
0f2d19dd
JB
49\f
50
afe5177e
GH
51/*
52 * This complicates things too much if allowed on any array.
53 * C code can safely call it on arrays known to be used in a single
54 * threaded manner.
55 *
56 * SCM_PROC(s_vector_set_length_x, "vector-set-length!", 2, 0, 0, scm_vector_set_length_x);
57 */
58static char s_vector_set_length_x[] = "vector-set-length!";
59
60
61SCM
62scm_vector_set_length_x (vect, len)
63 SCM vect;
64 SCM len;
65{
66 long l;
67 scm_sizet siz;
68 scm_sizet sz;
69
70 l = SCM_INUM (len);
71 SCM_ASRTGO (SCM_NIMP (vect), badarg1);
72
73#ifdef HAVE_ARRAYS
74 if (SCM_TYP7 (vect) == scm_tc7_bvect)
75 {
76 l = (l + SCM_LONG_BIT - 1) / SCM_LONG_BIT;
77 }
78 sz = scm_uniform_element_size (vect);
79 if (sz == 0)
80#endif
81 switch (SCM_TYP7 (vect))
82 {
83 default:
84 badarg1: scm_wta (vect, (char *) SCM_ARG1, s_vector_set_length_x);
85 case scm_tc7_string:
86 SCM_ASRTGO (vect != scm_nullstr, badarg1);
87 sz = sizeof (char);
88 l++;
89 break;
90 case scm_tc7_vector:
91 case scm_tc7_wvect:
92 SCM_ASRTGO (vect != scm_nullvect, badarg1);
93 sz = sizeof (SCM);
94 break;
95 }
96 SCM_ASSERT (SCM_INUMP (len), len, SCM_ARG2, s_vector_set_length_x);
97 if (!l)
98 l = 1L;
99 siz = l * sz;
100 if (siz != l * sz)
101 scm_wta (SCM_MAKINUM (l * sz), (char *) SCM_NALLOC, s_vector_set_length_x);
102 SCM_REDEFER_INTS;
103 SCM_SETCHARS (vect,
104 ((char *)
105 scm_must_realloc (SCM_CHARS (vect),
106 (long) SCM_LENGTH (vect) * sz,
107 (long) siz,
108 s_vector_set_length_x)));
109 if (SCM_VECTORP (vect))
110 {
111 sz = SCM_LENGTH (vect);
112 while (l > sz)
113 SCM_VELTS (vect)[--l] = SCM_UNSPECIFIED;
114 }
115 else if (SCM_STRINGP (vect))
116 SCM_CHARS (vect)[l - 1] = 0;
117 SCM_SETLENGTH (vect, SCM_INUM (len), SCM_TYP7 (vect));
118 SCM_REALLOW_INTS;
119 return vect;
120}
0f2d19dd
JB
121
122SCM_PROC(s_vector_p, "vector?", 1, 0, 0, scm_vector_p);
1cc91f1b 123
0f2d19dd
JB
124SCM
125scm_vector_p(x)
126 SCM x;
0f2d19dd 127{
ff467021 128 if (SCM_IMP(x)) return SCM_BOOL_F;
0f2d19dd
JB
129 return SCM_VECTORP(x) ? SCM_BOOL_T : SCM_BOOL_F;
130}
131
9eb8500a 132SCM_GPROC(s_vector_length, "vector-length", 1, 0, 0, scm_vector_length, g_vector_length);
1cc91f1b 133
0f2d19dd
JB
134SCM
135scm_vector_length(v)
136 SCM v;
0f2d19dd 137{
9eb8500a
MD
138 SCM_GASSERT1(SCM_NIMP(v) && SCM_VECTORP(v),
139 g_vector_length, v, SCM_ARG1, s_vector_length);
0f2d19dd
JB
140 return SCM_MAKINUM(SCM_LENGTH(v));
141}
142
143SCM_PROC(s_list_to_vector, "list->vector", 1, 0, 0, scm_vector);
144SCM_PROC(s_vector, "vector", 0, 0, 1, scm_vector);
1cc91f1b 145
0f2d19dd
JB
146SCM
147scm_vector(l)
148 SCM l;
0f2d19dd
JB
149{
150 SCM res;
151 register SCM *data;
152 long i = scm_ilength(l);
153 SCM_ASSERT(i >= 0, l, SCM_ARG1, s_vector);
a61ef59b 154 res = scm_make_vector (SCM_MAKINUM(i), SCM_UNSPECIFIED);
0f2d19dd
JB
155 data = SCM_VELTS(res);
156 for(;i && SCM_NIMP(l);--i, l = SCM_CDR(l))
157 *data++ = SCM_CAR(l);
158 return res;
159}
160
9eb8500a 161SCM_GPROC(s_vector_ref, "vector-ref", 2, 0, 0, scm_vector_ref, g_vector_ref);
1cc91f1b 162
0f2d19dd 163SCM
ea633082 164scm_vector_ref (SCM v, SCM k)
0f2d19dd 165{
9eb8500a
MD
166 SCM_GASSERT2 (SCM_NIMP (v) && SCM_VECTORP (v),
167 g_vector_ref, v, k, SCM_ARG1, s_vector_ref);
168 SCM_GASSERT2 (SCM_INUMP (k),
169 g_vector_ref, v, k, SCM_ARG2, s_vector_ref);
ea633082
MD
170 SCM_ASSERT (SCM_INUM (k) < SCM_LENGTH (v) && SCM_INUM (k) >= 0,
171 k, SCM_OUTOFRANGE, s_vector_ref);
172 return SCM_VELTS (v)[(long) SCM_INUM (k)];
0f2d19dd
JB
173}
174
9eb8500a 175SCM_GPROC(s_vector_set_x, "vector-set!", 3, 0, 0, scm_vector_set_x, g_vector_set_x);
1cc91f1b 176
0f2d19dd
JB
177SCM
178scm_vector_set_x(v, k, obj)
179 SCM v;
180 SCM k;
181 SCM obj;
0f2d19dd 182{
9eb8500a
MD
183 SCM_GASSERTn (SCM_NIMP(v) && SCM_VECTORP(v),
184 g_vector_set_x, SCM_LIST3 (v, k, obj),
185 SCM_ARG1, s_vector_set_x);
186 SCM_GASSERTn (SCM_INUMP(k),
187 g_vector_set_x, SCM_LIST3 (v, k, obj),
188 SCM_ARG2, s_vector_set_x);
189 SCM_ASSERT ((SCM_INUM(k) < SCM_LENGTH(v)) && (SCM_INUM(k) >= 0),
190 k, SCM_OUTOFRANGE, s_vector_set_x);
0f2d19dd
JB
191 SCM_VELTS(v)[((long) SCM_INUM(k))] = obj;
192 return obj;
193}
194
195
a61ef59b 196SCM_PROC (s_make_vector, "make-vector", 1, 1, 0, scm_make_vector);
1cc91f1b 197
0f2d19dd 198SCM
a61ef59b 199scm_make_vector (k, fill)
0f2d19dd
JB
200 SCM k;
201 SCM fill;
0f2d19dd
JB
202{
203 SCM v;
0f2d19dd
JB
204 register long i;
205 register long j;
206 register SCM *velts;
207
208 SCM_ASSERT(SCM_INUMP(k) && (0 <= SCM_INUM (k)), k, SCM_ARG1, s_make_vector);
209 if (SCM_UNBNDP(fill))
d60cebe2 210 fill = SCM_UNSPECIFIED;
0f2d19dd
JB
211 i = SCM_INUM(k);
212 SCM_NEWCELL(v);
213 SCM_DEFER_INTS;
214 SCM_SETCHARS(v, scm_must_malloc(i?(long)(i*sizeof(SCM)):1L, s_vector));
215 SCM_SETLENGTH(v, i, scm_tc7_vector);
216 velts = SCM_VELTS(v);
217 j = 0;
0f2d19dd
JB
218 while(--i >= j) (velts)[i] = fill;
219 SCM_ALLOW_INTS;
220 return v;
221}
222
223
224SCM_PROC(s_vector_to_list, "vector->list", 1, 0, 0, scm_vector_to_list);
1cc91f1b 225
0f2d19dd
JB
226SCM
227scm_vector_to_list(v)
228 SCM v;
0f2d19dd
JB
229{
230 SCM res = SCM_EOL;
231 long i;
232 SCM *data;
233 SCM_ASSERT(SCM_NIMP(v) && SCM_VECTORP(v), v, SCM_ARG1, s_vector_to_list);
234 data = SCM_VELTS(v);
235 for(i = SCM_LENGTH(v)-1;i >= 0;i--) res = scm_cons(data[i], res);
236 return res;
237}
238
239
a61ef59b 240SCM_PROC (s_vector_fill_x, "vector-fill!", 2, 0, 0, scm_vector_fill_x);
1cc91f1b 241
0f2d19dd 242SCM
a61ef59b 243scm_vector_fill_x (v, fill_x)
0f2d19dd
JB
244 SCM v;
245 SCM fill_x;
0f2d19dd
JB
246{
247 register long i;
248 register SCM *data;
249 SCM_ASSERT(SCM_NIMP(v) && SCM_VECTORP(v), v, SCM_ARG1, s_vector_fill_x);
250 data = SCM_VELTS(v);
a61ef59b
MD
251 for(i = SCM_LENGTH(v) - 1; i >= 0; i--)
252 data[i] = fill_x;
0f2d19dd
JB
253 return SCM_UNSPECIFIED;
254}
255
256
1cc91f1b 257
0f2d19dd
JB
258SCM
259scm_vector_equal_p(x, y)
260 SCM x;
261 SCM y;
0f2d19dd
JB
262{
263 long i;
264 for(i = SCM_LENGTH(x)-1;i >= 0;i--)
265 if (SCM_FALSEP(scm_equal_p(SCM_VELTS(x)[i], SCM_VELTS(y)[i])))
266 return SCM_BOOL_F;
267 return SCM_BOOL_T;
268}
269
270
271SCM_PROC (s_vector_move_left_x, "vector-move-left!", 5, 0, 0, scm_vector_move_left_x);
1cc91f1b 272
0f2d19dd
JB
273SCM
274scm_vector_move_left_x (vec1, start1, end1, vec2, start2)
275 SCM vec1;
276 SCM start1;
277 SCM end1;
278 SCM vec2;
279 SCM start2;
0f2d19dd
JB
280{
281 long i;
282 long j;
283 long e;
284
285 SCM_ASSERT (SCM_NIMP (vec1) && SCM_VECTORP (vec1), vec1, SCM_ARG1, s_vector_move_left_x);
286 SCM_ASSERT (SCM_INUMP (start1), start1, SCM_ARG2, s_vector_move_left_x);
287 SCM_ASSERT (SCM_INUMP (end1), end1, SCM_ARG3, s_vector_move_left_x);
288 SCM_ASSERT (SCM_NIMP (vec2) && SCM_VECTORP (vec2), vec2, SCM_ARG4, s_vector_move_left_x);
289 SCM_ASSERT (SCM_INUMP (start2), start2, SCM_ARG5, s_vector_move_left_x);
290 i = SCM_INUM (start1);
291 j = SCM_INUM (start2);
292 e = SCM_INUM (end1);
293 SCM_ASSERT (i <= SCM_LENGTH (vec1) && i >= 0, start1, SCM_OUTOFRANGE, s_vector_move_left_x);
294 SCM_ASSERT (j <= SCM_LENGTH (vec2) && j >= 0, start2, SCM_OUTOFRANGE, s_vector_move_left_x);
295 SCM_ASSERT (e <= SCM_LENGTH (vec1) && e >= 0, end1, SCM_OUTOFRANGE, s_vector_move_left_x);
296 SCM_ASSERT (e-i+j <= SCM_LENGTH (vec2), start2, SCM_OUTOFRANGE, s_vector_move_left_x);
297 while (i<e) SCM_VELTS (vec2)[j++] = SCM_VELTS (vec1)[i++];
298 return SCM_UNSPECIFIED;
299}
300
301SCM_PROC (s_vector_move_right_x, "vector-move-right!", 5, 0, 0, scm_vector_move_right_x);
1cc91f1b 302
0f2d19dd
JB
303SCM
304scm_vector_move_right_x (vec1, start1, end1, vec2, start2)
305 SCM vec1;
306 SCM start1;
307 SCM end1;
308 SCM vec2;
309 SCM start2;
0f2d19dd
JB
310{
311 long i;
312 long j;
313 long e;
314
2cc41672
MD
315 SCM_ASSERT (SCM_NIMP (vec1) && SCM_VECTORP (vec1),
316 vec1, SCM_ARG1, s_vector_move_right_x);
0f2d19dd
JB
317 SCM_ASSERT (SCM_INUMP (start1), start1, SCM_ARG2, s_vector_move_right_x);
318 SCM_ASSERT (SCM_INUMP (end1), end1, SCM_ARG3, s_vector_move_right_x);
2cc41672
MD
319 SCM_ASSERT (SCM_NIMP (vec2) && SCM_VECTORP (vec2),
320 vec2, SCM_ARG4, s_vector_move_right_x);
0f2d19dd
JB
321 SCM_ASSERT (SCM_INUMP (start2), start2, SCM_ARG5, s_vector_move_right_x);
322 i = SCM_INUM (start1);
323 j = SCM_INUM (start2);
324 e = SCM_INUM (end1);
2cc41672
MD
325 SCM_ASSERT (i <= SCM_LENGTH (vec1) && i >= 0,
326 start1, SCM_OUTOFRANGE, s_vector_move_right_x);
327 SCM_ASSERT (j <= SCM_LENGTH (vec2) && j >= 0,
328 start2, SCM_OUTOFRANGE, s_vector_move_right_x);
329 SCM_ASSERT (e <= SCM_LENGTH (vec1) && e >= 0,
330 end1, SCM_OUTOFRANGE, s_vector_move_right_x);
331 j = e - i + j;
332 SCM_ASSERT (j <= SCM_LENGTH (vec2),
333 start2, SCM_OUTOFRANGE, s_vector_move_right_x);
334 while (i < e)
335 SCM_VELTS (vec2)[--j] = SCM_VELTS (vec1)[--e];
0f2d19dd
JB
336 return SCM_UNSPECIFIED;
337}
338
339
1cc91f1b 340
0f2d19dd
JB
341void
342scm_init_vectors ()
0f2d19dd
JB
343{
344#include "vectors.x"
afe5177e
GH
345 /*
346 scm_make_subr (s_resizuve, scm_tc7_subr_2, scm_vector_set_length_x); */
0f2d19dd
JB
347}
348