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