maintainer changed: was lord, now jimb; first import
[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"
45
46\f
47
48
49SCM_PROC(s_vector_p, "vector?", 1, 0, 0, scm_vector_p);
50#ifdef __STDC__
51SCM
52scm_vector_p(SCM x)
53#else
54SCM
55scm_vector_p(x)
56 SCM x;
57#endif
58{
59 if SCM_IMP(x) return SCM_BOOL_F;
60 return SCM_VECTORP(x) ? SCM_BOOL_T : SCM_BOOL_F;
61}
62
63SCM_PROC(s_vector_length, "vector-length", 1, 0, 0, scm_vector_length);
64#ifdef __STDC__
65SCM
66scm_vector_length(SCM v)
67#else
68SCM
69scm_vector_length(v)
70 SCM v;
71#endif
72{
73 SCM_ASSERT(SCM_NIMP(v) && SCM_VECTORP(v), v, SCM_ARG1, s_vector_length);
74 return SCM_MAKINUM(SCM_LENGTH(v));
75}
76
77SCM_PROC(s_list_to_vector, "list->vector", 1, 0, 0, scm_vector);
78SCM_PROC(s_vector, "vector", 0, 0, 1, scm_vector);
79#ifdef __STDC__
80SCM
81scm_vector(SCM l)
82#else
83SCM
84scm_vector(l)
85 SCM l;
86#endif
87{
88 SCM res;
89 register SCM *data;
90 long i = scm_ilength(l);
91 SCM_ASSERT(i >= 0, l, SCM_ARG1, s_vector);
92 res = scm_make_vector(SCM_MAKINUM(i), SCM_UNSPECIFIED, SCM_UNDEFINED);
93 data = SCM_VELTS(res);
94 for(;i && SCM_NIMP(l);--i, l = SCM_CDR(l))
95 *data++ = SCM_CAR(l);
96 return res;
97}
98
99SCM_PROC(s_vector_ref, "vector-ref", 2, 0, 0, scm_vector_ref);
100#ifdef __STDC__
101SCM
102scm_vector_ref(SCM v, SCM k)
103#else
104SCM
105scm_vector_ref(v, k)
106 SCM v;
107 SCM k;
108#endif
109{
110 SCM_ASSERT(SCM_NIMP(v) && SCM_VECTORP(v), v, SCM_ARG1, s_vector_ref);
111 SCM_ASSERT(SCM_INUMP(k), k, SCM_ARG2, s_vector_ref);
112 SCM_ASSERT((SCM_INUM(k) < SCM_LENGTH(v)) && (SCM_INUM(k) >= 0), k, SCM_OUTOFRANGE, s_vector_ref);
113 return SCM_VELTS(v)[((long) SCM_INUM(k))];
114}
115
116
117SCM_PROC(s_vector_set_x, "vector-set!", 3, 0, 0, scm_vector_set_x);
118#ifdef __STDC__
119SCM
120scm_vector_set_x(SCM v, SCM k, SCM obj)
121#else
122SCM
123scm_vector_set_x(v, k, obj)
124 SCM v;
125 SCM k;
126 SCM obj;
127#endif
128{
129 SCM_ASSERT(SCM_NIMP(v) && SCM_VECTORP(v), v, SCM_ARG1, s_vector_set_x);
130 SCM_ASSERT(SCM_INUMP(k), k, SCM_ARG2, s_vector_set_x);
131 SCM_ASSERT((SCM_INUM(k) < SCM_LENGTH(v)) && (SCM_INUM(k) >= 0), k, SCM_OUTOFRANGE, s_vector_set_x);
132 SCM_VELTS(v)[((long) SCM_INUM(k))] = obj;
133 return obj;
134}
135
136
137SCM_PROC(s_make_vector, "make-vector", 1, 2, 0, scm_make_vector);
138#ifdef __STDC__
139SCM
140scm_make_vector(SCM k, SCM fill, SCM multip)
141#else
142SCM
143scm_make_vector(k, fill, multip)
144 SCM k;
145 SCM fill;
146 SCM multip;
147#endif
148{
149 SCM v;
150 int multi;
151 register long i;
152 register long j;
153 register SCM *velts;
154
155 SCM_ASSERT(SCM_INUMP(k) && (0 <= SCM_INUM (k)), k, SCM_ARG1, s_make_vector);
156 if (SCM_UNBNDP(fill))
157 fill = SCM_EOL;
158 multi = !(SCM_UNBNDP(multip) || SCM_FALSEP(multip));
159 i = SCM_INUM(k);
160 SCM_NEWCELL(v);
161 SCM_DEFER_INTS;
162 SCM_SETCHARS(v, scm_must_malloc(i?(long)(i*sizeof(SCM)):1L, s_vector));
163 SCM_SETLENGTH(v, i, scm_tc7_vector);
164 velts = SCM_VELTS(v);
165 j = 0;
166 if (multi)
167 {
168 while ((fill != SCM_EOL) && (j < i))
169 {
170 (velts)[j++] = SCM_CAR (fill);
171 fill = SCM_CDR (fill);
172 }
173 }
174 while(--i >= j) (velts)[i] = fill;
175 SCM_ALLOW_INTS;
176 return v;
177}
178
179
180SCM_PROC(s_vector_to_list, "vector->list", 1, 0, 0, scm_vector_to_list);
181#ifdef __STDC__
182SCM
183scm_vector_to_list(SCM v)
184#else
185SCM
186scm_vector_to_list(v)
187 SCM v;
188#endif
189{
190 SCM res = SCM_EOL;
191 long i;
192 SCM *data;
193 SCM_ASSERT(SCM_NIMP(v) && SCM_VECTORP(v), v, SCM_ARG1, s_vector_to_list);
194 data = SCM_VELTS(v);
195 for(i = SCM_LENGTH(v)-1;i >= 0;i--) res = scm_cons(data[i], res);
196 return res;
197}
198
199
200SCM_PROC(s_vector_fill_x, "vector-fill!", 2, 0, 0, scm_vector_fill_x);
201#ifdef __STDC__
202SCM
203scm_vector_fill_x(SCM v, SCM fill_x)
204#else
205SCM
206scm_vector_fill_x(v, fill_x)
207 SCM v;
208 SCM fill_x;
209#endif
210{
211 register long i;
212 register SCM *data;
213 SCM_ASSERT(SCM_NIMP(v) && SCM_VECTORP(v), v, SCM_ARG1, s_vector_fill_x);
214 data = SCM_VELTS(v);
215 for(i = SCM_LENGTH(v)-1;i >= 0;i--) data[i] = fill_x;
216 return SCM_UNSPECIFIED;
217}
218
219
220#ifdef __STDC__
221SCM
222scm_vector_equal_p(SCM x, SCM y)
223#else
224SCM
225scm_vector_equal_p(x, y)
226 SCM x;
227 SCM y;
228#endif
229{
230 long i;
231 for(i = SCM_LENGTH(x)-1;i >= 0;i--)
232 if (SCM_FALSEP(scm_equal_p(SCM_VELTS(x)[i], SCM_VELTS(y)[i])))
233 return SCM_BOOL_F;
234 return SCM_BOOL_T;
235}
236
237
238SCM_PROC (s_vector_move_left_x, "vector-move-left!", 5, 0, 0, scm_vector_move_left_x);
239#ifdef __STDC__
240SCM
241scm_vector_move_left_x (SCM vec1, SCM start1, SCM end1, SCM vec2, SCM start2)
242#else
243SCM
244scm_vector_move_left_x (vec1, start1, end1, vec2, start2)
245 SCM vec1;
246 SCM start1;
247 SCM end1;
248 SCM vec2;
249 SCM start2;
250#endif
251{
252 long i;
253 long j;
254 long e;
255
256 SCM_ASSERT (SCM_NIMP (vec1) && SCM_VECTORP (vec1), vec1, SCM_ARG1, s_vector_move_left_x);
257 SCM_ASSERT (SCM_INUMP (start1), start1, SCM_ARG2, s_vector_move_left_x);
258 SCM_ASSERT (SCM_INUMP (end1), end1, SCM_ARG3, s_vector_move_left_x);
259 SCM_ASSERT (SCM_NIMP (vec2) && SCM_VECTORP (vec2), vec2, SCM_ARG4, s_vector_move_left_x);
260 SCM_ASSERT (SCM_INUMP (start2), start2, SCM_ARG5, s_vector_move_left_x);
261 i = SCM_INUM (start1);
262 j = SCM_INUM (start2);
263 e = SCM_INUM (end1);
264 SCM_ASSERT (i <= SCM_LENGTH (vec1) && i >= 0, start1, SCM_OUTOFRANGE, s_vector_move_left_x);
265 SCM_ASSERT (j <= SCM_LENGTH (vec2) && j >= 0, start2, SCM_OUTOFRANGE, s_vector_move_left_x);
266 SCM_ASSERT (e <= SCM_LENGTH (vec1) && e >= 0, end1, SCM_OUTOFRANGE, s_vector_move_left_x);
267 SCM_ASSERT (e-i+j <= SCM_LENGTH (vec2), start2, SCM_OUTOFRANGE, s_vector_move_left_x);
268 while (i<e) SCM_VELTS (vec2)[j++] = SCM_VELTS (vec1)[i++];
269 return SCM_UNSPECIFIED;
270}
271
272SCM_PROC (s_vector_move_right_x, "vector-move-right!", 5, 0, 0, scm_vector_move_right_x);
273#ifdef __STDC__
274SCM
275scm_vector_move_right_x (SCM vec1, SCM start1, SCM end1, SCM vec2, SCM start2)
276#else
277SCM
278scm_vector_move_right_x (vec1, start1, end1, vec2, start2)
279 SCM vec1;
280 SCM start1;
281 SCM end1;
282 SCM vec2;
283 SCM start2;
284#endif
285{
286 long i;
287 long j;
288 long e;
289
290 SCM_ASSERT (SCM_NIMP (vec1) && SCM_VECTORP (vec1), vec1, SCM_ARG1, s_vector_move_right_x);
291 SCM_ASSERT (SCM_INUMP (start1), start1, SCM_ARG2, s_vector_move_right_x);
292 SCM_ASSERT (SCM_INUMP (end1), end1, SCM_ARG3, s_vector_move_right_x);
293 SCM_ASSERT (SCM_NIMP (vec2) && SCM_VECTORP (vec2), vec2, SCM_ARG4, s_vector_move_right_x);
294 SCM_ASSERT (SCM_INUMP (start2), start2, SCM_ARG5, s_vector_move_right_x);
295 i = SCM_INUM (start1);
296 j = SCM_INUM (start2);
297 e = SCM_INUM (end1);
298 SCM_ASSERT (i <= SCM_LENGTH (vec1) && i >= 0, start1, SCM_OUTOFRANGE, s_vector_move_right_x);
299 SCM_ASSERT (j <= SCM_LENGTH (vec2) && j >= 0, start2, SCM_OUTOFRANGE, s_vector_move_right_x);
300 SCM_ASSERT (e <= SCM_LENGTH (vec1) && e >= 0, end1, SCM_OUTOFRANGE, s_vector_move_right_x);
301 SCM_ASSERT ((j = e-i+j) <= SCM_LENGTH (vec2), start2, SCM_OUTOFRANGE, s_vector_move_right_x);
302 while (i<e) SCM_VELTS (vec2)[--j] = SCM_VELTS (vec1)[--e];
303 return SCM_UNSPECIFIED;
304}
305
306
307#ifdef __STDC__
308void
309scm_init_vectors (void)
310#else
311void
312scm_init_vectors ()
313#endif
314{
315#include "vectors.x"
316}
317