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