Commit | Line | Data |
---|---|---|
336c9211 | 1 | /* Copyright (C) 1995,1996,1997,1998,2000,2001,2002,2003,2004, 2005, 2006, 2009, 2010, 2013 Free Software Foundation, Inc. |
1030b450 AW |
2 | * |
3 | * This library is free software; you can redistribute it and/or | |
4 | * modify it under the terms of the GNU Lesser General Public License | |
5 | * as published by the Free Software Foundation; either version 3 of | |
6 | * the License, or (at your option) any later version. | |
7 | * | |
8 | * This library is distributed in the hope that it will be useful, but | |
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
11 | * Lesser General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU Lesser General Public | |
14 | * License along with this library; if not, write to the Free Software | |
15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
16 | * 02110-1301 USA | |
17 | */ | |
18 | ||
19 | ||
20 | \f | |
21 | ||
22 | #ifdef HAVE_CONFIG_H | |
23 | # include <config.h> | |
24 | #endif | |
25 | ||
26 | #include <stdio.h> | |
27 | #include <errno.h> | |
28 | #include <string.h> | |
29 | ||
30 | #include "libguile/_scm.h" | |
31 | #include "libguile/__scm.h" | |
32 | #include "libguile/array-handle.h" | |
33 | #include "libguile/generalized-arrays.h" | |
34 | ||
35 | ||
336c9211 AW |
36 | SCM_INTERNAL SCM scm_i_array_ref (SCM v, |
37 | SCM idx0, SCM idx1, SCM idxN); | |
38 | SCM_INTERNAL SCM scm_i_array_set_x (SCM v, SCM obj, | |
39 | SCM idx0, SCM idx1, SCM idxN); | |
40 | ||
41 | ||
1030b450 AW |
42 | int |
43 | scm_is_array (SCM obj) | |
44 | { | |
45 | return scm_i_array_implementation_for_obj (obj) ? 1 : 0; | |
46 | } | |
47 | ||
ec370c6f LC |
48 | SCM_DEFINE (scm_array_p_2, "array?", 1, 0, 0, |
49 | (SCM obj), | |
1030b450 AW |
50 | "Return @code{#t} if the @var{obj} is an array, and @code{#f} if\n" |
51 | "not.") | |
61aab1c3 | 52 | #define FUNC_NAME s_scm_array_p_2 |
1030b450 AW |
53 | { |
54 | return scm_from_bool (scm_is_array (obj)); | |
55 | } | |
56 | #undef FUNC_NAME | |
57 | ||
ec370c6f LC |
58 | /* The array type predicate, with an extra argument kept for backward |
59 | compatibility. Note that we can't use `SCM_DEFINE' directly because there | |
60 | would be an argument count mismatch that would be caught by | |
61 | `snarf-check-and-output-texi.scm'. */ | |
62 | SCM | |
63 | scm_array_p (SCM obj, SCM unused) | |
64 | { | |
65 | return scm_array_p_2 (obj); | |
66 | } | |
67 | ||
1030b450 AW |
68 | int |
69 | scm_is_typed_array (SCM obj, SCM type) | |
70 | { | |
71 | int ret = 0; | |
72 | if (scm_i_array_implementation_for_obj (obj)) | |
73 | { | |
74 | scm_t_array_handle h; | |
75 | ||
76 | scm_array_get_handle (obj, &h); | |
77 | ret = scm_is_eq (scm_array_handle_element_type (&h), type); | |
78 | scm_array_handle_release (&h); | |
79 | } | |
80 | ||
81 | return ret; | |
82 | } | |
83 | ||
84 | SCM_DEFINE (scm_typed_array_p, "typed-array?", 2, 0, 0, | |
85 | (SCM obj, SCM type), | |
86 | "Return @code{#t} if the @var{obj} is an array of type\n" | |
87 | "@var{type}, and @code{#f} if not.") | |
88 | #define FUNC_NAME s_scm_typed_array_p | |
89 | { | |
90 | return scm_from_bool (scm_is_typed_array (obj, type)); | |
91 | } | |
92 | #undef FUNC_NAME | |
93 | ||
94 | size_t | |
95 | scm_c_array_rank (SCM array) | |
96 | { | |
97 | scm_t_array_handle handle; | |
98 | size_t res; | |
99 | ||
100 | scm_array_get_handle (array, &handle); | |
101 | res = scm_array_handle_rank (&handle); | |
102 | scm_array_handle_release (&handle); | |
103 | return res; | |
104 | } | |
105 | ||
106 | SCM_DEFINE (scm_array_rank, "array-rank", 1, 0, 0, | |
107 | (SCM array), | |
108 | "Return the number of dimensions of the array @var{array.}\n") | |
109 | #define FUNC_NAME s_scm_array_rank | |
110 | { | |
111 | return scm_from_size_t (scm_c_array_rank (array)); | |
112 | } | |
113 | #undef FUNC_NAME | |
114 | ||
115 | ||
116 | SCM_DEFINE (scm_array_dimensions, "array-dimensions", 1, 0, 0, | |
117 | (SCM ra), | |
118 | "@code{array-dimensions} is similar to @code{array-shape} but replaces\n" | |
119 | "elements with a @code{0} minimum with one greater than the maximum. So:\n" | |
120 | "@lisp\n" | |
121 | "(array-dimensions (make-array 'foo '(-1 3) 5)) @result{} ((-1 3) 5)\n" | |
122 | "@end lisp") | |
123 | #define FUNC_NAME s_scm_array_dimensions | |
124 | { | |
125 | scm_t_array_handle handle; | |
126 | scm_t_array_dim *s; | |
127 | SCM res = SCM_EOL; | |
128 | size_t k; | |
129 | ||
130 | scm_array_get_handle (ra, &handle); | |
131 | s = scm_array_handle_dims (&handle); | |
132 | k = scm_array_handle_rank (&handle); | |
133 | ||
134 | while (k--) | |
135 | res = scm_cons (s[k].lbnd | |
136 | ? scm_cons2 (scm_from_ssize_t (s[k].lbnd), | |
137 | scm_from_ssize_t (s[k].ubnd), | |
138 | SCM_EOL) | |
139 | : scm_from_ssize_t (1 + s[k].ubnd), | |
140 | res); | |
141 | ||
142 | scm_array_handle_release (&handle); | |
143 | return res; | |
144 | } | |
145 | #undef FUNC_NAME | |
146 | ||
1030b450 AW |
147 | SCM_DEFINE (scm_array_type, "array-type", 1, 0, 0, |
148 | (SCM ra), | |
149 | "") | |
150 | #define FUNC_NAME s_scm_array_type | |
151 | { | |
152 | scm_t_array_handle h; | |
153 | SCM type; | |
154 | ||
1030b450 AW |
155 | scm_array_get_handle (ra, &h); |
156 | type = scm_array_handle_element_type (&h); | |
157 | scm_array_handle_release (&h); | |
158 | ||
159 | return type; | |
160 | } | |
161 | #undef FUNC_NAME | |
162 | ||
163 | SCM_DEFINE (scm_array_in_bounds_p, "array-in-bounds?", 1, 0, 1, | |
164 | (SCM ra, SCM args), | |
165 | "Return @code{#t} if its arguments would be acceptable to\n" | |
166 | "@code{array-ref}.") | |
167 | #define FUNC_NAME s_scm_array_in_bounds_p | |
168 | { | |
169 | SCM res = SCM_BOOL_T; | |
170 | size_t k, ndim; | |
171 | scm_t_array_dim *s; | |
172 | scm_t_array_handle handle; | |
173 | ||
174 | SCM_VALIDATE_REST_ARGUMENT (args); | |
175 | ||
176 | scm_array_get_handle (ra, &handle); | |
177 | s = scm_array_handle_dims (&handle); | |
178 | ndim = scm_array_handle_rank (&handle); | |
179 | ||
180 | for (k = 0; k < ndim; k++) | |
181 | { | |
182 | long ind; | |
183 | ||
184 | if (!scm_is_pair (args)) | |
185 | SCM_WRONG_NUM_ARGS (); | |
186 | ind = scm_to_long (SCM_CAR (args)); | |
187 | args = SCM_CDR (args); | |
188 | ||
189 | if (ind < s[k].lbnd || ind > s[k].ubnd) | |
190 | { | |
191 | res = SCM_BOOL_F; | |
192 | /* We do not stop the checking after finding a violation | |
193 | since we want to validate the type-correctness and | |
194 | number of arguments in any case. | |
195 | */ | |
196 | } | |
197 | } | |
198 | ||
199 | scm_array_handle_release (&handle); | |
200 | return res; | |
201 | } | |
202 | #undef FUNC_NAME | |
203 | ||
336c9211 AW |
204 | |
205 | SCM | |
206 | scm_c_array_ref_1 (SCM array, ssize_t idx0) | |
207 | { | |
208 | scm_t_array_handle handle; | |
209 | SCM res; | |
210 | ||
211 | scm_array_get_handle (array, &handle); | |
212 | res = scm_array_handle_ref (&handle, scm_array_handle_pos_1 (&handle, idx0)); | |
213 | scm_array_handle_release (&handle); | |
214 | return res; | |
215 | } | |
216 | ||
217 | ||
218 | SCM | |
219 | scm_c_array_ref_2 (SCM array, ssize_t idx0, ssize_t idx1) | |
220 | { | |
221 | scm_t_array_handle handle; | |
222 | SCM res; | |
223 | ||
224 | scm_array_get_handle (array, &handle); | |
225 | res = scm_array_handle_ref (&handle, scm_array_handle_pos_2 (&handle, idx0, idx1)); | |
226 | scm_array_handle_release (&handle); | |
227 | return res; | |
228 | } | |
229 | ||
230 | ||
231 | SCM | |
232 | scm_array_ref (SCM v, SCM args) | |
1030b450 AW |
233 | { |
234 | scm_t_array_handle handle; | |
235 | SCM res; | |
236 | ||
237 | scm_array_get_handle (v, &handle); | |
238 | res = scm_array_handle_ref (&handle, scm_array_handle_pos (&handle, args)); | |
239 | scm_array_handle_release (&handle); | |
240 | return res; | |
241 | } | |
1030b450 AW |
242 | |
243 | ||
336c9211 AW |
244 | void |
245 | scm_c_array_set_1_x (SCM array, SCM obj, ssize_t idx0) | |
246 | { | |
247 | scm_t_array_handle handle; | |
248 | ||
249 | scm_array_get_handle (array, &handle); | |
250 | scm_array_handle_set (&handle, scm_array_handle_pos_1 (&handle, idx0), | |
251 | obj); | |
252 | scm_array_handle_release (&handle); | |
253 | } | |
254 | ||
255 | ||
256 | void | |
257 | scm_c_array_set_2_x (SCM array, SCM obj, ssize_t idx0, ssize_t idx1) | |
258 | { | |
259 | scm_t_array_handle handle; | |
260 | ||
261 | scm_array_get_handle (array, &handle); | |
262 | scm_array_handle_set (&handle, scm_array_handle_pos_2 (&handle, idx0, idx1), | |
263 | obj); | |
264 | scm_array_handle_release (&handle); | |
265 | } | |
266 | ||
267 | ||
268 | SCM | |
269 | scm_array_set_x (SCM v, SCM obj, SCM args) | |
1030b450 AW |
270 | { |
271 | scm_t_array_handle handle; | |
272 | ||
273 | scm_array_get_handle (v, &handle); | |
274 | scm_array_handle_set (&handle, scm_array_handle_pos (&handle, args), obj); | |
275 | scm_array_handle_release (&handle); | |
276 | return SCM_UNSPECIFIED; | |
277 | } | |
336c9211 AW |
278 | |
279 | ||
280 | SCM_DEFINE (scm_i_array_ref, "array-ref", 1, 2, 1, | |
281 | (SCM v, SCM idx0, SCM idx1, SCM idxN), | |
282 | "Return the element at the @code{(idx0, idx1, idxN...)}\n" | |
283 | "position in array @var{v}.") | |
284 | #define FUNC_NAME s_scm_i_array_ref | |
285 | { | |
286 | if (SCM_UNBNDP (idx0)) | |
287 | return scm_array_ref (v, SCM_EOL); | |
288 | else if (SCM_UNBNDP (idx1)) | |
289 | return scm_c_array_ref_1 (v, scm_to_ssize_t (idx0)); | |
290 | else if (scm_is_null (idxN)) | |
291 | return scm_c_array_ref_2 (v, scm_to_ssize_t (idx0), scm_to_ssize_t (idx1)); | |
292 | else | |
293 | return scm_array_ref (v, scm_cons (idx0, scm_cons (idx1, idxN))); | |
294 | } | |
295 | #undef FUNC_NAME | |
296 | ||
297 | ||
298 | SCM_DEFINE (scm_i_array_set_x, "array-set!", 2, 2, 1, | |
299 | (SCM v, SCM obj, SCM idx0, SCM idx1, SCM idxN), | |
300 | "Set the element at the @code{(idx0, idx1, idxN...)} position\n" | |
301 | "in the array @var{v} to @var{obj}. The value returned by\n" | |
302 | "@code{array-set!} is unspecified.") | |
303 | #define FUNC_NAME s_scm_i_array_set_x | |
304 | { | |
305 | if (SCM_UNBNDP (idx0)) | |
306 | scm_array_set_x (v, obj, SCM_EOL); | |
307 | else if (SCM_UNBNDP (idx1)) | |
308 | scm_c_array_set_1_x (v, obj, scm_to_ssize_t (idx0)); | |
309 | else if (scm_is_null (idxN)) | |
310 | scm_c_array_set_2_x (v, obj, scm_to_ssize_t (idx0), scm_to_ssize_t (idx1)); | |
311 | else | |
312 | scm_array_set_x (v, obj, scm_cons (idx0, scm_cons (idx1, idxN))); | |
313 | ||
314 | return SCM_UNSPECIFIED; | |
315 | } | |
1030b450 AW |
316 | #undef FUNC_NAME |
317 | ||
336c9211 | 318 | |
1030b450 AW |
319 | static SCM |
320 | array_to_list (scm_t_array_handle *h, size_t dim, unsigned long pos) | |
321 | { | |
322 | if (dim == scm_array_handle_rank (h)) | |
323 | return scm_array_handle_ref (h, pos); | |
324 | else | |
325 | { | |
326 | SCM res = SCM_EOL; | |
327 | long inc; | |
cf9a806d | 328 | size_t i; |
1030b450 | 329 | |
cf9a806d | 330 | i = h->dims[dim].ubnd - h->dims[dim].lbnd + 1; |
1030b450 | 331 | inc = h->dims[dim].inc; |
cf9a806d | 332 | pos += (i - 1) * inc; |
1030b450 | 333 | |
cf9a806d | 334 | for (; i > 0; i--, pos -= inc) |
1030b450 AW |
335 | res = scm_cons (array_to_list (h, dim + 1, pos), res); |
336 | return res; | |
337 | } | |
338 | } | |
339 | ||
340 | SCM_DEFINE (scm_array_to_list, "array->list", 1, 0, 0, | |
341 | (SCM array), | |
e48a2f87 AW |
342 | "Return a list representation of @var{array}.\n\n" |
343 | "It is easiest to specify the behavior of this function by\n" | |
344 | "example:\n" | |
345 | "@example\n" | |
346 | "(array->list #0(a)) @result{} 1\n" | |
347 | "(array->list #1(a b)) @result{} (a b)\n" | |
348 | "(array->list #2((aa ab) (ba bb)) @result{} ((aa ab) (ba bb))\n" | |
349 | "@end example\n") | |
1030b450 AW |
350 | #define FUNC_NAME s_scm_array_to_list |
351 | { | |
352 | scm_t_array_handle h; | |
353 | SCM res; | |
354 | ||
355 | scm_array_get_handle (array, &h); | |
356 | res = array_to_list (&h, 0, 0); | |
357 | scm_array_handle_release (&h); | |
358 | ||
359 | return res; | |
360 | } | |
361 | #undef FUNC_NAME | |
362 | ||
363 | void | |
364 | scm_init_generalized_arrays () | |
365 | { | |
366 | #include "libguile/generalized-arrays.x" | |
367 | } | |
368 | ||
369 | /* | |
370 | Local Variables: | |
371 | c-file-style: "gnu" | |
372 | End: | |
373 | */ |