* __scm.h, alist.c, alist.h, append.c, append.h, appinit.c,
[bpt/guile.git] / libguile / vectors.c
CommitLineData
0f2d19dd
JB
1/* Copyright (C) 1995,1996 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, 675 Mass Ave, Cambridge, MA 02139, USA.
16 *
17 * As a special exception, the Free Software Foundation gives permission
18 * for additional uses of the text contained in its release of GUILE.
19 *
20 * The exception is that, if you link the GUILE library with other files
21 * to produce an executable, this does not by itself cause the
22 * resulting executable to be covered by the GNU General Public License.
23 * Your use of that executable is in no way restricted on account of
24 * linking the GUILE library code into it.
25 *
26 * This exception does not however invalidate any other reasons why
27 * the executable file might be covered by the GNU General Public License.
28 *
29 * This exception applies only to the code released by the
30 * Free Software Foundation under the name GUILE. If you copy
31 * code from other Free Software Foundation releases into a copy of
32 * GUILE, as the General Public License permits, the exception does
33 * not apply to the code that you add in this way. To avoid misleading
34 * anyone as to the status of such modified files, you must delete
35 * this exception notice from them.
36 *
37 * If you write modifications of your own for GUILE, it is your choice
38 * whether to permit this exception to apply to your modifications.
39 * If you do not wish that, delete this exception notice.
40 */
41\f
42
43#include <stdio.h>
44#include "_scm.h"
20e6290e 45#include "eq.h"
0f2d19dd 46
20e6290e 47#include "vectors.h"
0f2d19dd
JB
48\f
49
50
51SCM_PROC(s_vector_p, "vector?", 1, 0, 0, scm_vector_p);
52#ifdef __STDC__
53SCM
54scm_vector_p(SCM x)
55#else
56SCM
57scm_vector_p(x)
58 SCM x;
59#endif
60{
61 if SCM_IMP(x) return SCM_BOOL_F;
62 return SCM_VECTORP(x) ? SCM_BOOL_T : SCM_BOOL_F;
63}
64
65SCM_PROC(s_vector_length, "vector-length", 1, 0, 0, scm_vector_length);
66#ifdef __STDC__
67SCM
68scm_vector_length(SCM v)
69#else
70SCM
71scm_vector_length(v)
72 SCM v;
73#endif
74{
75 SCM_ASSERT(SCM_NIMP(v) && SCM_VECTORP(v), v, SCM_ARG1, s_vector_length);
76 return SCM_MAKINUM(SCM_LENGTH(v));
77}
78
79SCM_PROC(s_list_to_vector, "list->vector", 1, 0, 0, scm_vector);
80SCM_PROC(s_vector, "vector", 0, 0, 1, scm_vector);
81#ifdef __STDC__
82SCM
83scm_vector(SCM l)
84#else
85SCM
86scm_vector(l)
87 SCM l;
88#endif
89{
90 SCM res;
91 register SCM *data;
92 long i = scm_ilength(l);
93 SCM_ASSERT(i >= 0, l, SCM_ARG1, s_vector);
94 res = scm_make_vector(SCM_MAKINUM(i), SCM_UNSPECIFIED, SCM_UNDEFINED);
95 data = SCM_VELTS(res);
96 for(;i && SCM_NIMP(l);--i, l = SCM_CDR(l))
97 *data++ = SCM_CAR(l);
98 return res;
99}
100
101SCM_PROC(s_vector_ref, "vector-ref", 2, 0, 0, scm_vector_ref);
102#ifdef __STDC__
103SCM
104scm_vector_ref(SCM v, SCM k)
105#else
106SCM
107scm_vector_ref(v, k)
108 SCM v;
109 SCM k;
110#endif
111{
112 SCM_ASSERT(SCM_NIMP(v) && SCM_VECTORP(v), v, SCM_ARG1, s_vector_ref);
113 SCM_ASSERT(SCM_INUMP(k), k, SCM_ARG2, s_vector_ref);
114 SCM_ASSERT((SCM_INUM(k) < SCM_LENGTH(v)) && (SCM_INUM(k) >= 0), k, SCM_OUTOFRANGE, s_vector_ref);
115 return SCM_VELTS(v)[((long) SCM_INUM(k))];
116}
117
118
119SCM_PROC(s_vector_set_x, "vector-set!", 3, 0, 0, scm_vector_set_x);
120#ifdef __STDC__
121SCM
122scm_vector_set_x(SCM v, SCM k, SCM obj)
123#else
124SCM
125scm_vector_set_x(v, k, obj)
126 SCM v;
127 SCM k;
128 SCM obj;
129#endif
130{
131 SCM_ASSERT(SCM_NIMP(v) && SCM_VECTORP(v), v, SCM_ARG1, s_vector_set_x);
132 SCM_ASSERT(SCM_INUMP(k), k, SCM_ARG2, s_vector_set_x);
133 SCM_ASSERT((SCM_INUM(k) < SCM_LENGTH(v)) && (SCM_INUM(k) >= 0), k, SCM_OUTOFRANGE, s_vector_set_x);
134 SCM_VELTS(v)[((long) SCM_INUM(k))] = obj;
135 return obj;
136}
137
138
139SCM_PROC(s_make_vector, "make-vector", 1, 2, 0, scm_make_vector);
140#ifdef __STDC__
141SCM
142scm_make_vector(SCM k, SCM fill, SCM multip)
143#else
144SCM
145scm_make_vector(k, fill, multip)
146 SCM k;
147 SCM fill;
148 SCM multip;
149#endif
150{
151 SCM v;
152 int multi;
153 register long i;
154 register long j;
155 register SCM *velts;
156
157 SCM_ASSERT(SCM_INUMP(k) && (0 <= SCM_INUM (k)), k, SCM_ARG1, s_make_vector);
158 if (SCM_UNBNDP(fill))
d60cebe2 159 fill = SCM_UNSPECIFIED;
0f2d19dd
JB
160 multi = !(SCM_UNBNDP(multip) || SCM_FALSEP(multip));
161 i = SCM_INUM(k);
162 SCM_NEWCELL(v);
163 SCM_DEFER_INTS;
164 SCM_SETCHARS(v, scm_must_malloc(i?(long)(i*sizeof(SCM)):1L, s_vector));
165 SCM_SETLENGTH(v, i, scm_tc7_vector);
166 velts = SCM_VELTS(v);
167 j = 0;
168 if (multi)
169 {
170 while ((fill != SCM_EOL) && (j < i))
171 {
172 (velts)[j++] = SCM_CAR (fill);
173 fill = SCM_CDR (fill);
174 }
175 }
176 while(--i >= j) (velts)[i] = fill;
177 SCM_ALLOW_INTS;
178 return v;
179}
180
181
182SCM_PROC(s_vector_to_list, "vector->list", 1, 0, 0, scm_vector_to_list);
183#ifdef __STDC__
184SCM
185scm_vector_to_list(SCM v)
186#else
187SCM
188scm_vector_to_list(v)
189 SCM v;
190#endif
191{
192 SCM res = SCM_EOL;
193 long i;
194 SCM *data;
195 SCM_ASSERT(SCM_NIMP(v) && SCM_VECTORP(v), v, SCM_ARG1, s_vector_to_list);
196 data = SCM_VELTS(v);
197 for(i = SCM_LENGTH(v)-1;i >= 0;i--) res = scm_cons(data[i], res);
198 return res;
199}
200
201
202SCM_PROC(s_vector_fill_x, "vector-fill!", 2, 0, 0, scm_vector_fill_x);
203#ifdef __STDC__
204SCM
205scm_vector_fill_x(SCM v, SCM fill_x)
206#else
207SCM
208scm_vector_fill_x(v, fill_x)
209 SCM v;
210 SCM fill_x;
211#endif
212{
213 register long i;
214 register SCM *data;
215 SCM_ASSERT(SCM_NIMP(v) && SCM_VECTORP(v), v, SCM_ARG1, s_vector_fill_x);
216 data = SCM_VELTS(v);
217 for(i = SCM_LENGTH(v)-1;i >= 0;i--) data[i] = fill_x;
218 return SCM_UNSPECIFIED;
219}
220
221
222#ifdef __STDC__
223SCM
224scm_vector_equal_p(SCM x, SCM y)
225#else
226SCM
227scm_vector_equal_p(x, y)
228 SCM x;
229 SCM y;
230#endif
231{
232 long i;
233 for(i = SCM_LENGTH(x)-1;i >= 0;i--)
234 if (SCM_FALSEP(scm_equal_p(SCM_VELTS(x)[i], SCM_VELTS(y)[i])))
235 return SCM_BOOL_F;
236 return SCM_BOOL_T;
237}
238
239
240SCM_PROC (s_vector_move_left_x, "vector-move-left!", 5, 0, 0, scm_vector_move_left_x);
241#ifdef __STDC__
242SCM
243scm_vector_move_left_x (SCM vec1, SCM start1, SCM end1, SCM vec2, SCM start2)
244#else
245SCM
246scm_vector_move_left_x (vec1, start1, end1, vec2, start2)
247 SCM vec1;
248 SCM start1;
249 SCM end1;
250 SCM vec2;
251 SCM start2;
252#endif
253{
254 long i;
255 long j;
256 long e;
257
258 SCM_ASSERT (SCM_NIMP (vec1) && SCM_VECTORP (vec1), vec1, SCM_ARG1, s_vector_move_left_x);
259 SCM_ASSERT (SCM_INUMP (start1), start1, SCM_ARG2, s_vector_move_left_x);
260 SCM_ASSERT (SCM_INUMP (end1), end1, SCM_ARG3, s_vector_move_left_x);
261 SCM_ASSERT (SCM_NIMP (vec2) && SCM_VECTORP (vec2), vec2, SCM_ARG4, s_vector_move_left_x);
262 SCM_ASSERT (SCM_INUMP (start2), start2, SCM_ARG5, s_vector_move_left_x);
263 i = SCM_INUM (start1);
264 j = SCM_INUM (start2);
265 e = SCM_INUM (end1);
266 SCM_ASSERT (i <= SCM_LENGTH (vec1) && i >= 0, start1, SCM_OUTOFRANGE, s_vector_move_left_x);
267 SCM_ASSERT (j <= SCM_LENGTH (vec2) && j >= 0, start2, SCM_OUTOFRANGE, s_vector_move_left_x);
268 SCM_ASSERT (e <= SCM_LENGTH (vec1) && e >= 0, end1, SCM_OUTOFRANGE, s_vector_move_left_x);
269 SCM_ASSERT (e-i+j <= SCM_LENGTH (vec2), start2, SCM_OUTOFRANGE, s_vector_move_left_x);
270 while (i<e) SCM_VELTS (vec2)[j++] = SCM_VELTS (vec1)[i++];
271 return SCM_UNSPECIFIED;
272}
273
274SCM_PROC (s_vector_move_right_x, "vector-move-right!", 5, 0, 0, scm_vector_move_right_x);
275#ifdef __STDC__
276SCM
277scm_vector_move_right_x (SCM vec1, SCM start1, SCM end1, SCM vec2, SCM start2)
278#else
279SCM
280scm_vector_move_right_x (vec1, start1, end1, vec2, start2)
281 SCM vec1;
282 SCM start1;
283 SCM end1;
284 SCM vec2;
285 SCM start2;
286#endif
287{
288 long i;
289 long j;
290 long e;
291
292 SCM_ASSERT (SCM_NIMP (vec1) && SCM_VECTORP (vec1), vec1, SCM_ARG1, s_vector_move_right_x);
293 SCM_ASSERT (SCM_INUMP (start1), start1, SCM_ARG2, s_vector_move_right_x);
294 SCM_ASSERT (SCM_INUMP (end1), end1, SCM_ARG3, s_vector_move_right_x);
295 SCM_ASSERT (SCM_NIMP (vec2) && SCM_VECTORP (vec2), vec2, SCM_ARG4, s_vector_move_right_x);
296 SCM_ASSERT (SCM_INUMP (start2), start2, SCM_ARG5, s_vector_move_right_x);
297 i = SCM_INUM (start1);
298 j = SCM_INUM (start2);
299 e = SCM_INUM (end1);
300 SCM_ASSERT (i <= SCM_LENGTH (vec1) && i >= 0, start1, SCM_OUTOFRANGE, s_vector_move_right_x);
301 SCM_ASSERT (j <= SCM_LENGTH (vec2) && j >= 0, start2, SCM_OUTOFRANGE, s_vector_move_right_x);
302 SCM_ASSERT (e <= SCM_LENGTH (vec1) && e >= 0, end1, SCM_OUTOFRANGE, s_vector_move_right_x);
303 SCM_ASSERT ((j = e-i+j) <= SCM_LENGTH (vec2), start2, SCM_OUTOFRANGE, s_vector_move_right_x);
304 while (i<e) SCM_VELTS (vec2)[--j] = SCM_VELTS (vec1)[--e];
305 return SCM_UNSPECIFIED;
306}
307
308
309#ifdef __STDC__
310void
311scm_init_vectors (void)
312#else
313void
314scm_init_vectors ()
315#endif
316{
317#include "vectors.x"
318}
319