*** empty log message ***
[bpt/guile.git] / libguile / unif.c
CommitLineData
7dc6e754 1/* Copyright (C) 1995,1996,1997,1998 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. */
1bbd0b84
GB
41
42/* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
44
0f2d19dd
JB
45\f
46
47#include <stdio.h>
48#include "_scm.h"
20e6290e
JB
49#include "chars.h"
50#include "eval.h"
ee149d03 51#include "fports.h"
20e6290e 52#include "smob.h"
20e6290e
JB
53#include "strop.h"
54#include "feature.h"
55
1bbd0b84 56#include "scm_validate.h"
20e6290e 57#include "unif.h"
95b88819 58#include "ramap.h"
0f2d19dd 59
3d8d56df
GH
60#ifdef HAVE_UNISTD_H
61#include <unistd.h>
62#endif
63
0f2d19dd
JB
64\f
65/* The set of uniform scm_vector types is:
66 * Vector of: Called:
67 * unsigned char string
68 * char byvect
69 * boolean bvect
a515d287
MD
70 * signed long ivect
71 * unsigned long uvect
0f2d19dd
JB
72 * float fvect
73 * double dvect
74 * complex double cvect
75 * short svect
5c11cc9d 76 * long long llvect
0f2d19dd
JB
77 */
78
79long scm_tc16_array;
80
afe5177e
GH
81/* return the size of an element in a uniform array or 0 if type not
82 found. */
83scm_sizet
84scm_uniform_element_size (SCM obj)
0f2d19dd 85{
afe5177e 86 scm_sizet result;
0f2d19dd 87
afe5177e 88 switch (SCM_TYP7 (obj))
0f2d19dd 89 {
0f2d19dd 90 case scm_tc7_bvect:
0f2d19dd
JB
91 case scm_tc7_uvect:
92 case scm_tc7_ivect:
afe5177e 93 result = sizeof (long);
0f2d19dd 94 break;
afe5177e 95
0f2d19dd 96 case scm_tc7_byvect:
afe5177e 97 result = sizeof (char);
0f2d19dd
JB
98 break;
99
100 case scm_tc7_svect:
afe5177e 101 result = sizeof (short);
0f2d19dd 102 break;
afe5177e 103
5c11cc9d 104#ifdef HAVE_LONG_LONGS
0f2d19dd 105 case scm_tc7_llvect:
afe5177e 106 result = sizeof (long_long);
0f2d19dd
JB
107 break;
108#endif
109
110#ifdef SCM_FLOATS
111#ifdef SCM_SINGLES
112 case scm_tc7_fvect:
afe5177e 113 result = sizeof (float);
0f2d19dd
JB
114 break;
115#endif
afe5177e 116
0f2d19dd 117 case scm_tc7_dvect:
afe5177e 118 result = sizeof (double);
0f2d19dd 119 break;
afe5177e 120
0f2d19dd 121 case scm_tc7_cvect:
afe5177e 122 result = 2 * sizeof (double);
0f2d19dd
JB
123 break;
124#endif
afe5177e
GH
125
126 default:
127 result = 0;
0f2d19dd 128 }
afe5177e 129 return result;
0f2d19dd
JB
130}
131
132
0f2d19dd
JB
133#ifdef SCM_FLOATS
134#ifdef SCM_SINGLES
135
1cc91f1b 136
0f2d19dd 137SCM
805df3e8 138scm_makflo (float x)
0f2d19dd
JB
139{
140 SCM z;
141 if (x == 0.0)
142 return scm_flo0;
143 SCM_NEWCELL (z);
144 SCM_DEFER_INTS;
898a256f 145 SCM_SETCAR (z, scm_tc_flo);
0f2d19dd
JB
146 SCM_FLO (z) = x;
147 SCM_ALLOW_INTS;
148 return z;
149}
150#endif
151#endif
152
1cc91f1b 153
0f2d19dd 154SCM
1bbd0b84 155scm_make_uve (long k, SCM prot)
0f2d19dd
JB
156{
157 SCM v;
158 long i, type;
159 if (SCM_BOOL_T == prot)
160 {
161 i = sizeof (long) * ((k + SCM_LONG_BIT - 1) / SCM_LONG_BIT);
162 type = scm_tc7_bvect;
163 }
164 else if (SCM_ICHRP (prot) && (prot == SCM_MAKICHR ('\0')))
165 {
166 i = sizeof (char) * k;
167 type = scm_tc7_byvect;
168 }
169 else if (SCM_ICHRP (prot))
170 {
171 i = sizeof (char) * k;
172 type = scm_tc7_string;
173 }
174 else if (SCM_INUMP (prot))
175 {
176 i = sizeof (long) * k;
177 if (SCM_INUM (prot) > 0)
178 type = scm_tc7_uvect;
179 else
180 type = scm_tc7_ivect;
181 }
0c95b57d 182 else if (SCM_SYMBOLP (prot) && (1 == SCM_LENGTH (prot)))
0f2d19dd
JB
183 {
184 char s;
185
186 s = SCM_CHARS (prot)[0];
187 if (s == 's')
188 {
189 i = sizeof (short) * k;
190 type = scm_tc7_svect;
191 }
5c11cc9d 192#ifdef HAVE_LONG_LONGS
0f2d19dd
JB
193 else if (s == 'l')
194 {
195 i = sizeof (long_long) * k;
196 type = scm_tc7_llvect;
197 }
198#endif
199 else
200 {
a8741caa 201 return scm_make_vector (SCM_MAKINUM (k), SCM_UNDEFINED);
0f2d19dd
JB
202 }
203 }
204 else
205#ifdef SCM_FLOATS
206 if (SCM_IMP (prot) || !SCM_INEXP (prot))
207#endif
208 /* Huge non-unif vectors are NOT supported. */
5c11cc9d
GH
209 /* no special scm_vector */
210 return scm_make_vector (SCM_MAKINUM (k), SCM_UNDEFINED);
0f2d19dd
JB
211#ifdef SCM_FLOATS
212#ifdef SCM_SINGLES
213 else if (SCM_SINGP (prot))
214
215 {
216 i = sizeof (float) * k;
217 type = scm_tc7_fvect;
218 }
219#endif
220 else if (SCM_CPLXP (prot))
221 {
222 i = 2 * sizeof (double) * k;
223 type = scm_tc7_cvect;
224 }
225 else
226 {
227 i = sizeof (double) * k;
228 type = scm_tc7_dvect;
229 }
230#endif
231
232 SCM_NEWCELL (v);
233 SCM_DEFER_INTS;
5c11cc9d 234 SCM_SETCHARS (v, (char *) scm_must_malloc (i ? i : 1, "vector"));
0f2d19dd
JB
235 SCM_SETLENGTH (v, (k < SCM_LENGTH_MAX ? k : SCM_LENGTH_MAX), type);
236 SCM_ALLOW_INTS;
237 return v;
238}
239
3b3b36dd 240SCM_DEFINE (scm_uniform_vector_length, "uniform-vector-length", 1, 0, 0,
1bbd0b84 241 (SCM v),
4079f87e 242"Returns the number of elements in @var{uve}.")
1bbd0b84 243#define FUNC_NAME s_scm_uniform_vector_length
0f2d19dd
JB
244{
245 SCM_ASRTGO (SCM_NIMP (v), badarg1);
246 switch SCM_TYP7
247 (v)
248 {
249 default:
1bbd0b84 250 badarg1:SCM_WTA(1,v);
0f2d19dd
JB
251 case scm_tc7_bvect:
252 case scm_tc7_string:
253 case scm_tc7_byvect:
254 case scm_tc7_uvect:
255 case scm_tc7_ivect:
256 case scm_tc7_fvect:
257 case scm_tc7_dvect:
258 case scm_tc7_cvect:
259 case scm_tc7_vector:
95f5b0f5 260 case scm_tc7_wvect:
0f2d19dd 261 case scm_tc7_svect:
5c11cc9d 262#ifdef HAVE_LONG_LONGS
0f2d19dd
JB
263 case scm_tc7_llvect:
264#endif
265 return SCM_MAKINUM (SCM_LENGTH (v));
266 }
267}
1bbd0b84 268#undef FUNC_NAME
0f2d19dd 269
3b3b36dd 270SCM_DEFINE (scm_array_p, "array?", 1, 1, 0,
1bbd0b84 271 (SCM v, SCM prot),
4079f87e
GB
272"Returns @code{#t} if the @var{obj} is an array, and @code{#f} if not.
273
274The @var{prototype} argument is used with uniform arrays and is described
275elsewhere.")
1bbd0b84 276#define FUNC_NAME s_scm_array_p
0f2d19dd
JB
277{
278 int nprot;
279 int enclosed;
280 nprot = SCM_UNBNDP (prot);
281 enclosed = 0;
282 if (SCM_IMP (v))
283 return SCM_BOOL_F;
284loop:
285 switch (SCM_TYP7 (v))
286 {
287 case scm_tc7_smob:
288 if (!SCM_ARRAYP (v))
289 return SCM_BOOL_F;
290 if (nprot)
291 return SCM_BOOL_T;
292 if (enclosed++)
293 return SCM_BOOL_F;
294 v = SCM_ARRAY_V (v);
295 goto loop;
296 case scm_tc7_bvect:
1bbd0b84 297 return nprot || SCM_BOOL(SCM_BOOL_T==prot);
0f2d19dd 298 case scm_tc7_string:
1bbd0b84 299 return nprot || SCM_BOOL(SCM_ICHRP(prot) && (prot != SCM_MAKICHR('\0')));
0f2d19dd 300 case scm_tc7_byvect:
1bbd0b84 301 return nprot || SCM_BOOL(prot == SCM_MAKICHR('\0'));
0f2d19dd 302 case scm_tc7_uvect:
1bbd0b84 303 return nprot || SCM_BOOL(SCM_INUMP(prot) && SCM_INUM(prot)>0);
0f2d19dd 304 case scm_tc7_ivect:
1bbd0b84 305 return nprot || SCM_BOOL(SCM_INUMP(prot) && SCM_INUM(prot)<=0);
0f2d19dd
JB
306 case scm_tc7_svect:
307 return ( nprot
368cf54d 308 || (SCM_SYMBOLP (prot)
0f2d19dd
JB
309 && (1 == SCM_LENGTH (prot))
310 && ('s' == SCM_CHARS (prot)[0])));
5c11cc9d 311#ifdef HAVE_LONG_LONGS
0f2d19dd
JB
312 case scm_tc7_llvect:
313 return ( nprot
368cf54d 314 || (SCM_SYMBOLP (prot)
0f2d19dd
JB
315 && (1 == SCM_LENGTH (prot))
316 && ('s' == SCM_CHARS (prot)[0])));
317#endif
318# ifdef SCM_FLOATS
319# ifdef SCM_SINGLES
320 case scm_tc7_fvect:
0c95b57d 321 return nprot || SCM_BOOL(SCM_SINGP(prot));
0f2d19dd
JB
322# endif
323 case scm_tc7_dvect:
0c95b57d 324 return nprot || SCM_BOOL(SCM_REALP(prot));
0f2d19dd 325 case scm_tc7_cvect:
0c95b57d 326 return nprot || SCM_BOOL(SCM_CPLXP(prot));
0f2d19dd
JB
327# endif
328 case scm_tc7_vector:
95f5b0f5 329 case scm_tc7_wvect:
1bbd0b84 330 return nprot || SCM_BOOL(SCM_NULLP(prot));
0f2d19dd
JB
331 default:;
332 }
333 return SCM_BOOL_F;
334}
1bbd0b84 335#undef FUNC_NAME
0f2d19dd
JB
336
337
3b3b36dd 338SCM_DEFINE (scm_array_rank, "array-rank", 1, 0, 0,
1bbd0b84 339 (SCM ra),
4079f87e
GB
340"Returns the number of dimensions of @var{obj}. If @var{obj} is not an
341array, @code{0} is returned.")
1bbd0b84 342#define FUNC_NAME s_scm_array_rank
0f2d19dd
JB
343{
344 if (SCM_IMP (ra))
1bbd0b84 345 return SCM_INUM0;
0f2d19dd
JB
346 switch (SCM_TYP7 (ra))
347 {
348 default:
349 return SCM_INUM0;
350 case scm_tc7_string:
351 case scm_tc7_vector:
95f5b0f5 352 case scm_tc7_wvect:
0f2d19dd
JB
353 case scm_tc7_byvect:
354 case scm_tc7_uvect:
355 case scm_tc7_ivect:
356 case scm_tc7_fvect:
357 case scm_tc7_cvect:
358 case scm_tc7_dvect:
5c11cc9d 359#ifdef HAVE_LONG_LONGS
0f2d19dd
JB
360 case scm_tc7_llvect:
361#endif
362 case scm_tc7_svect:
363 return SCM_MAKINUM (1L);
364 case scm_tc7_smob:
365 if (SCM_ARRAYP (ra))
366 return SCM_MAKINUM (SCM_ARRAY_NDIM (ra));
367 return SCM_INUM0;
368 }
369}
1bbd0b84 370#undef FUNC_NAME
0f2d19dd
JB
371
372
3b3b36dd 373SCM_DEFINE (scm_array_dimensions, "array-dimensions", 1, 0, 0,
1bbd0b84 374 (SCM ra),
4079f87e
GB
375"@code{Array-dimensions} is similar to @code{array-shape} but replaces
376elements with a @code{0} minimum with one greater than the maximum. So:
377@example
378(array-dimensions (make-array 'foo '(-1 3) 5)) @result{} ((-1 3) 5)
379@end example")
1bbd0b84 380#define FUNC_NAME s_scm_array_dimensions
0f2d19dd
JB
381{
382 SCM res = SCM_EOL;
383 scm_sizet k;
384 scm_array_dim *s;
385 if (SCM_IMP (ra))
1bbd0b84 386 return SCM_BOOL_F;
0f2d19dd
JB
387 switch (SCM_TYP7 (ra))
388 {
389 default:
390 return SCM_BOOL_F;
391 case scm_tc7_string:
392 case scm_tc7_vector:
95f5b0f5 393 case scm_tc7_wvect:
0f2d19dd
JB
394 case scm_tc7_bvect:
395 case scm_tc7_byvect:
396 case scm_tc7_uvect:
397 case scm_tc7_ivect:
398 case scm_tc7_fvect:
399 case scm_tc7_cvect:
400 case scm_tc7_dvect:
401 case scm_tc7_svect:
5c11cc9d 402#ifdef HAVE_LONG_LONGS
0f2d19dd
JB
403 case scm_tc7_llvect:
404#endif
405 return scm_cons (SCM_MAKINUM (SCM_LENGTH (ra)), SCM_EOL);
406 case scm_tc7_smob:
407 if (!SCM_ARRAYP (ra))
408 return SCM_BOOL_F;
409 k = SCM_ARRAY_NDIM (ra);
410 s = SCM_ARRAY_DIMS (ra);
411 while (k--)
412 res = scm_cons (s[k].lbnd ? scm_cons2 (SCM_MAKINUM (s[k].lbnd), SCM_MAKINUM (s[k].ubnd), SCM_EOL) :
413 SCM_MAKINUM (1 + (s[k].ubnd))
414 , res);
415 return res;
416 }
417}
1bbd0b84 418#undef FUNC_NAME
0f2d19dd
JB
419
420
421static char s_bad_ind[] = "Bad scm_array index";
422
1cc91f1b 423
0f2d19dd 424long
1bbd0b84 425scm_aind (SCM ra, SCM args, const char *what)
0f2d19dd
JB
426{
427 SCM ind;
428 register long j;
429 register scm_sizet pos = SCM_ARRAY_BASE (ra);
430 register scm_sizet k = SCM_ARRAY_NDIM (ra);
431 scm_array_dim *s = SCM_ARRAY_DIMS (ra);
432 if (SCM_INUMP (args))
0f2d19dd 433 {
f5bf2977 434 SCM_ASSERT (1 == k, scm_makfrom0str (what), SCM_WNA, NULL);
0f2d19dd
JB
435 return pos + (SCM_INUM (args) - s->lbnd) * (s->inc);
436 }
437 while (k && SCM_NIMP (args))
438 {
439 ind = SCM_CAR (args);
440 args = SCM_CDR (args);
441 SCM_ASSERT (SCM_INUMP (ind), ind, s_bad_ind, what);
442 j = SCM_INUM (ind);
443 SCM_ASSERT (j >= (s->lbnd) && j <= (s->ubnd), ind, SCM_OUTOFRANGE, what);
444 pos += (j - s->lbnd) * (s->inc);
445 k--;
446 s++;
447 }
f5bf2977
GH
448 SCM_ASSERT (0 == k && SCM_NULLP (args), scm_makfrom0str (what), SCM_WNA,
449 NULL);
0f2d19dd
JB
450 return pos;
451}
452
453
1cc91f1b 454
0f2d19dd 455SCM
1bbd0b84 456scm_make_ra (int ndim)
0f2d19dd
JB
457{
458 SCM ra;
459 SCM_NEWCELL (ra);
460 SCM_DEFER_INTS;
23a62151
MD
461 SCM_NEWSMOB(ra, ((long) ndim << 17) + scm_tc16_array,
462 scm_must_malloc ((long) (sizeof (scm_array) + ndim * sizeof (scm_array_dim)),
0f2d19dd 463 "array"));
0f2d19dd
JB
464 SCM_ARRAY_V (ra) = scm_nullvect;
465 SCM_ALLOW_INTS;
466 return ra;
467}
468
469static char s_bad_spec[] = "Bad scm_array dimension";
470/* Increments will still need to be set. */
471
1cc91f1b 472
0f2d19dd 473SCM
1bbd0b84 474scm_shap2ra (SCM args, const char *what)
0f2d19dd
JB
475{
476 scm_array_dim *s;
477 SCM ra, spec, sp;
478 int ndim = scm_ilength (args);
479 SCM_ASSERT (0 <= ndim, args, s_bad_spec, what);
480 ra = scm_make_ra (ndim);
481 SCM_ARRAY_BASE (ra) = 0;
482 s = SCM_ARRAY_DIMS (ra);
483 for (; SCM_NIMP (args); s++, args = SCM_CDR (args))
484 {
485 spec = SCM_CAR (args);
486 if (SCM_IMP (spec))
487
488 {
20a54673
GH
489 SCM_ASSERT (SCM_INUMP (spec) && SCM_INUM (spec) >= 0, spec,
490 s_bad_spec, what);
0f2d19dd
JB
491 s->lbnd = 0;
492 s->ubnd = SCM_INUM (spec) - 1;
493 s->inc = 1;
494 }
495 else
496 {
20a54673
GH
497 SCM_ASSERT (SCM_CONSP (spec) && SCM_INUMP (SCM_CAR (spec)), spec,
498 s_bad_spec, what);
0f2d19dd
JB
499 s->lbnd = SCM_INUM (SCM_CAR (spec));
500 sp = SCM_CDR (spec);
0c95b57d 501 SCM_ASSERT (SCM_CONSP (sp)
20a54673
GH
502 && SCM_INUMP (SCM_CAR (sp)) && SCM_NULLP (SCM_CDR (sp)),
503 spec, s_bad_spec, what);
0f2d19dd
JB
504 s->ubnd = SCM_INUM (SCM_CAR (sp));
505 s->inc = 1;
506 }
507 }
508 return ra;
509}
510
3b3b36dd 511SCM_DEFINE (scm_dimensions_to_uniform_array, "dimensions->uniform-array", 2, 1, 0,
1bbd0b84 512 (SCM dims, SCM prot, SCM fill),
4079f87e
GB
513"@deffnx primitive make-uniform-vector length prototype [fill]
514Creates and returns a uniform array or vector of type corresponding to
515@var{prototype} with dimensions @var{dims} or length @var{length}. If
516@var{fill} is supplied, it's used to fill the array, otherwise
517@var{prototype} is used.")
1bbd0b84 518#define FUNC_NAME s_scm_dimensions_to_uniform_array
0f2d19dd
JB
519{
520 scm_sizet k, vlen = 1;
521 long rlen = 1;
522 scm_array_dim *s;
523 SCM ra;
524 if (SCM_INUMP (dims))
cda139a7 525 {
0f2d19dd
JB
526 if (SCM_INUM (dims) < SCM_LENGTH_MAX)
527 {
5c11cc9d
GH
528 SCM answer = scm_make_uve (SCM_INUM (dims), prot);
529
530 if (!SCM_UNBNDP (fill))
531 scm_array_fill_x (answer, fill);
0c95b57d 532 else if (SCM_SYMBOLP (prot))
0f2d19dd
JB
533 scm_array_fill_x (answer, SCM_MAKINUM (0));
534 else
535 scm_array_fill_x (answer, prot);
536 return answer;
537 }
538 else
539 dims = scm_cons (dims, SCM_EOL);
cda139a7 540 }
0c95b57d
GB
541 SCM_ASSERT (SCM_NULLP (dims) || SCM_CONSP (dims),
542 dims, SCM_ARG1, FUNC_NAME);
1bbd0b84 543 ra = scm_shap2ra (dims, FUNC_NAME);
898a256f 544 SCM_SETOR_CAR (ra, SCM_ARRAY_CONTIGUOUS);
0f2d19dd
JB
545 s = SCM_ARRAY_DIMS (ra);
546 k = SCM_ARRAY_NDIM (ra);
547 while (k--)
548 {
549 s[k].inc = (rlen > 0 ? rlen : 0);
550 rlen = (s[k].ubnd - s[k].lbnd + 1) * s[k].inc;
551 vlen *= (s[k].ubnd - s[k].lbnd + 1);
552 }
553 if (rlen < SCM_LENGTH_MAX)
554 SCM_ARRAY_V (ra) = scm_make_uve ((rlen > 0 ? rlen : 0L), prot);
555 else
556 {
557 scm_sizet bit;
558 switch (SCM_TYP7 (scm_make_uve (0L, prot)))
559 {
560 default:
561 bit = SCM_LONG_BIT;
562 break;
563 case scm_tc7_bvect:
564 bit = 1;
565 break;
566 case scm_tc7_string:
567 bit = SCM_CHAR_BIT;
568 break;
569 case scm_tc7_fvect:
570 bit = sizeof (float) * SCM_CHAR_BIT / sizeof (char);
571 break;
572 case scm_tc7_dvect:
573 bit = sizeof (double) * SCM_CHAR_BIT / sizeof (char);
574 break;
575 case scm_tc7_cvect:
576 bit = 2 * sizeof (double) * SCM_CHAR_BIT / sizeof (char);
577 break;
578 }
579 SCM_ARRAY_BASE (ra) = (SCM_LONG_BIT + bit - 1) / bit;
580 rlen += SCM_ARRAY_BASE (ra);
581 SCM_ARRAY_V (ra) = scm_make_uve (rlen, prot);
582 *((long *) SCM_VELTS (SCM_ARRAY_V (ra))) = rlen;
583 }
5c11cc9d 584 if (!SCM_UNBNDP (fill))
0f2d19dd 585 {
5c11cc9d 586 scm_array_fill_x (ra, fill);
0f2d19dd 587 }
0c95b57d 588 else if (SCM_SYMBOLP (prot))
0f2d19dd
JB
589 scm_array_fill_x (ra, SCM_MAKINUM (0));
590 else
591 scm_array_fill_x (ra, prot);
592 if (1 == SCM_ARRAY_NDIM (ra) && 0 == SCM_ARRAY_BASE (ra))
593 if (s->ubnd < s->lbnd || (0 == s->lbnd && 1 == s->inc))
594 return SCM_ARRAY_V (ra);
595 return ra;
596}
1bbd0b84 597#undef FUNC_NAME
0f2d19dd 598
1cc91f1b 599
0f2d19dd 600void
1bbd0b84 601scm_ra_set_contp (SCM ra)
0f2d19dd
JB
602{
603 scm_sizet k = SCM_ARRAY_NDIM (ra);
0f2d19dd 604 if (k)
0f2d19dd 605 {
fe0c6dae
JB
606 long inc = SCM_ARRAY_DIMS (ra)[k - 1].inc;
607 while (k--)
0f2d19dd 608 {
fe0c6dae
JB
609 if (inc != SCM_ARRAY_DIMS (ra)[k].inc)
610 {
898a256f 611 SCM_SETAND_CAR (ra, ~SCM_ARRAY_CONTIGUOUS);
fe0c6dae
JB
612 return;
613 }
614 inc *= (SCM_ARRAY_DIMS (ra)[k].ubnd
615 - SCM_ARRAY_DIMS (ra)[k].lbnd + 1);
0f2d19dd 616 }
0f2d19dd 617 }
898a256f 618 SCM_SETOR_CAR (ra, SCM_ARRAY_CONTIGUOUS);
0f2d19dd
JB
619}
620
621
3b3b36dd 622SCM_DEFINE (scm_make_shared_array, "make-shared-array", 2, 0, 1,
1bbd0b84 623 (SCM oldra, SCM mapfunc, SCM dims),
4079f87e
GB
624"@code{make-shared-array} can be used to create shared subarrays of other
625arrays. The @var{mapper} is a function that translates coordinates in
626the new array into coordinates in the old array. A @var{mapper} must be
627linear, and its range must stay within the bounds of the old array, but
628it can be otherwise arbitrary. A simple example:
629@example
630(define fred (make-array #f 8 8))
631(define freds-diagonal
632 (make-shared-array fred (lambda (i) (list i i)) 8))
633(array-set! freds-diagonal 'foo 3)
634(array-ref fred 3 3) @result{} foo
635(define freds-center
636 (make-shared-array fred (lambda (i j) (list (+ 3 i) (+ 3 j))) 2 2))
637(array-ref freds-center 0 0) @result{} foo
638@end example")
1bbd0b84 639#define FUNC_NAME s_scm_make_shared_array
0f2d19dd
JB
640{
641 SCM ra;
642 SCM inds, indptr;
643 SCM imap;
644 scm_sizet i, k;
645 long old_min, new_min, old_max, new_max;
646 scm_array_dim *s;
3b3b36dd
GB
647 SCM_VALIDATE_ARRAY (1,oldra);
648 SCM_VALIDATE_PROC (2,mapfunc);
1bbd0b84 649 ra = scm_shap2ra (dims, FUNC_NAME);
0f2d19dd
JB
650 if (SCM_ARRAYP (oldra))
651 {
652 SCM_ARRAY_V (ra) = SCM_ARRAY_V (oldra);
653 old_min = old_max = SCM_ARRAY_BASE (oldra);
654 s = SCM_ARRAY_DIMS (oldra);
655 k = SCM_ARRAY_NDIM (oldra);
656 while (k--)
657 {
658 if (s[k].inc > 0)
659 old_max += (s[k].ubnd - s[k].lbnd) * s[k].inc;
660 else
661 old_min += (s[k].ubnd - s[k].lbnd) * s[k].inc;
662 }
663 }
664 else
665 {
666 SCM_ARRAY_V (ra) = oldra;
667 old_min = 0;
668 old_max = (long) SCM_LENGTH (oldra) - 1;
669 }
670 inds = SCM_EOL;
671 s = SCM_ARRAY_DIMS (ra);
672 for (k = 0; k < SCM_ARRAY_NDIM (ra); k++)
673 {
674 inds = scm_cons (SCM_MAKINUM (s[k].lbnd), inds);
675 if (s[k].ubnd < s[k].lbnd)
676 {
677 if (1 == SCM_ARRAY_NDIM (ra))
678 ra = scm_make_uve (0L, scm_array_prototype (ra));
679 else
680 SCM_ARRAY_V (ra) = scm_make_uve (0L, scm_array_prototype (ra));
681 return ra;
682 }
683 }
92396c0a 684 imap = scm_apply (mapfunc, scm_reverse (inds), SCM_EOL);
0f2d19dd 685 if (SCM_ARRAYP (oldra))
1bbd0b84 686 i = (scm_sizet) scm_aind (oldra, imap, FUNC_NAME);
0f2d19dd
JB
687 else
688 {
689 if (SCM_NINUMP (imap))
690
691 {
692 SCM_ASSERT (1 == scm_ilength (imap) && SCM_INUMP (SCM_CAR (imap)),
1bbd0b84 693 imap, s_bad_ind, FUNC_NAME);
0f2d19dd
JB
694 imap = SCM_CAR (imap);
695 }
696 i = SCM_INUM (imap);
697 }
698 SCM_ARRAY_BASE (ra) = new_min = new_max = i;
699 indptr = inds;
700 k = SCM_ARRAY_NDIM (ra);
701 while (k--)
702 {
703 if (s[k].ubnd > s[k].lbnd)
704 {
898a256f 705 SCM_SETCAR (indptr, SCM_MAKINUM (SCM_INUM (SCM_CAR (indptr)) + 1));
0f2d19dd
JB
706 imap = scm_apply (mapfunc, scm_reverse (inds), SCM_EOL);
707 if (SCM_ARRAYP (oldra))
708
1bbd0b84 709 s[k].inc = scm_aind (oldra, imap, FUNC_NAME) - i;
0f2d19dd
JB
710 else
711 {
712 if (SCM_NINUMP (imap))
713
714 {
715 SCM_ASSERT (1 == scm_ilength (imap) && SCM_INUMP (SCM_CAR (imap)),
1bbd0b84 716 imap, s_bad_ind, FUNC_NAME);
0f2d19dd
JB
717 imap = SCM_CAR (imap);
718 }
719 s[k].inc = (long) SCM_INUM (imap) - i;
720 }
721 i += s[k].inc;
722 if (s[k].inc > 0)
723 new_max += (s[k].ubnd - s[k].lbnd) * s[k].inc;
724 else
725 new_min += (s[k].ubnd - s[k].lbnd) * s[k].inc;
726 }
727 else
728 s[k].inc = new_max - new_min + 1; /* contiguous by default */
729 indptr = SCM_CDR (indptr);
730 }
731 SCM_ASSERT (old_min <= new_min && old_max >= new_max, SCM_UNDEFINED,
1bbd0b84 732 "mapping out of range", FUNC_NAME);
0f2d19dd
JB
733 if (1 == SCM_ARRAY_NDIM (ra) && 0 == SCM_ARRAY_BASE (ra))
734 {
735 if (1 == s->inc && 0 == s->lbnd
736 && SCM_LENGTH (SCM_ARRAY_V (ra)) == 1 + s->ubnd)
737 return SCM_ARRAY_V (ra);
738 if (s->ubnd < s->lbnd)
739 return scm_make_uve (0L, scm_array_prototype (ra));
740 }
741 scm_ra_set_contp (ra);
742 return ra;
743}
1bbd0b84 744#undef FUNC_NAME
0f2d19dd
JB
745
746
747/* args are RA . DIMS */
3b3b36dd 748SCM_DEFINE (scm_transpose_array, "transpose-array", 0, 0, 1,
1bbd0b84 749 (SCM args),
4079f87e
GB
750"Returns an array sharing contents with @var{array}, but with dimensions
751arranged in a different order. There must be one @var{dim} argument for
752each dimension of @var{array}. @var{dim0}, @var{dim1}, @dots{} should
753be integers between 0 and the rank of the array to be returned. Each
754integer in that range must appear at least once in the argument list.
755
756The values of @var{dim0}, @var{dim1}, @dots{} correspond to dimensions
757in the array to be returned, their positions in the argument list to
758dimensions of @var{array}. Several @var{dim}s may have the same value,
759in which case the returned array will have smaller rank than
760@var{array}.
761
762examples:
763@example
764(transpose-array '#2((a b) (c d)) 1 0) @result{} #2((a c) (b d))
765(transpose-array '#2((a b) (c d)) 0 0) @result{} #1(a d)
766(transpose-array '#3(((a b c) (d e f)) ((1 2 3) (4 5 6))) 1 1 0) @result{}
767 #2((a 4) (b 5) (c 6))
768@end example")
1bbd0b84 769#define FUNC_NAME s_scm_transpose_array
0f2d19dd
JB
770{
771 SCM ra, res, vargs, *ve = &vargs;
772 scm_array_dim *s, *r;
773 int ndim, i, k;
1bbd0b84 774 SCM_ASSERT (SCM_NNULLP (args), scm_makfrom0str (FUNC_NAME),
f5bf2977 775 SCM_WNA, NULL);
0f2d19dd 776 ra = SCM_CAR (args);
1bbd0b84 777 SCM_ASSERT (SCM_NIMP (ra), ra, SCM_ARG1, FUNC_NAME);
0f2d19dd 778 args = SCM_CDR (args);
f5bf2977 779 switch (SCM_TYP7 (ra))
0f2d19dd
JB
780 {
781 default:
1bbd0b84 782 badarg:SCM_WTA (1,ra);
0f2d19dd
JB
783 case scm_tc7_bvect:
784 case scm_tc7_string:
785 case scm_tc7_byvect:
786 case scm_tc7_uvect:
787 case scm_tc7_ivect:
788 case scm_tc7_fvect:
789 case scm_tc7_dvect:
790 case scm_tc7_cvect:
791 case scm_tc7_svect:
5c11cc9d 792#ifdef HAVE_LONG_LONGS
0f2d19dd
JB
793 case scm_tc7_llvect:
794#endif
f5bf2977 795 SCM_ASSERT (SCM_NIMP (args) && SCM_NULLP (SCM_CDR (args)),
1bbd0b84 796 scm_makfrom0str (FUNC_NAME), SCM_WNA, NULL);
f5bf2977 797 SCM_ASSERT (SCM_INUMP (SCM_CAR (args)), SCM_CAR (args), SCM_ARG2,
1bbd0b84 798 FUNC_NAME);
f5bf2977 799 SCM_ASSERT (SCM_INUM0 == SCM_CAR (args), SCM_CAR (args), SCM_OUTOFRANGE,
1bbd0b84 800 FUNC_NAME);
0f2d19dd
JB
801 return ra;
802 case scm_tc7_smob:
803 SCM_ASRTGO (SCM_ARRAYP (ra), badarg);
804 vargs = scm_vector (args);
f5bf2977 805 SCM_ASSERT (SCM_LENGTH (vargs) == SCM_ARRAY_NDIM (ra),
1bbd0b84 806 scm_makfrom0str (FUNC_NAME), SCM_WNA, NULL);
f5bf2977 807 ve = SCM_VELTS (vargs);
0f2d19dd
JB
808 ndim = 0;
809 for (k = 0; k < SCM_ARRAY_NDIM (ra); k++)
810 {
20a54673 811 SCM_ASSERT (SCM_INUMP (ve[k]), ve[k], (SCM_ARG2 + k),
1bbd0b84 812 FUNC_NAME);
0f2d19dd 813 i = SCM_INUM (ve[k]);
20a54673 814 SCM_ASSERT (i >= 0 && i < SCM_ARRAY_NDIM (ra), ve[k],
1bbd0b84 815 SCM_OUTOFRANGE, FUNC_NAME);
0f2d19dd
JB
816 if (ndim < i)
817 ndim = i;
818 }
819 ndim++;
820 res = scm_make_ra (ndim);
821 SCM_ARRAY_V (res) = SCM_ARRAY_V (ra);
822 SCM_ARRAY_BASE (res) = SCM_ARRAY_BASE (ra);
823 for (k = ndim; k--;)
824 {
825 SCM_ARRAY_DIMS (res)[k].lbnd = 0;
826 SCM_ARRAY_DIMS (res)[k].ubnd = -1;
827 }
828 for (k = SCM_ARRAY_NDIM (ra); k--;)
829 {
830 i = SCM_INUM (ve[k]);
831 s = &(SCM_ARRAY_DIMS (ra)[k]);
832 r = &(SCM_ARRAY_DIMS (res)[i]);
833 if (r->ubnd < r->lbnd)
834 {
835 r->lbnd = s->lbnd;
836 r->ubnd = s->ubnd;
837 r->inc = s->inc;
838 ndim--;
839 }
840 else
841 {
842 if (r->ubnd > s->ubnd)
843 r->ubnd = s->ubnd;
844 if (r->lbnd < s->lbnd)
845 {
846 SCM_ARRAY_BASE (res) += (s->lbnd - r->lbnd) * r->inc;
847 r->lbnd = s->lbnd;
848 }
849 r->inc += s->inc;
850 }
851 }
1bbd0b84 852 SCM_ASSERT (ndim <= 0, args, "bad argument list", FUNC_NAME);
0f2d19dd
JB
853 scm_ra_set_contp (res);
854 return res;
855 }
856}
1bbd0b84 857#undef FUNC_NAME
0f2d19dd
JB
858
859/* args are RA . AXES */
3b3b36dd 860SCM_DEFINE (scm_enclose_array, "enclose-array", 0, 0, 1,
1bbd0b84 861 (SCM axes),
4079f87e
GB
862"@var{dim0}, @var{dim1} @dots{} should be nonnegative integers less than
863the rank of @var{array}. @var{enclose-array} returns an array
864resembling an array of shared arrays. The dimensions of each shared
865array are the same as the @var{dim}th dimensions of the original array,
866the dimensions of the outer array are the same as those of the original
867array that did not match a @var{dim}.
868
869An enclosed array is not a general Scheme array. Its elements may not
870be set using @code{array-set!}. Two references to the same element of
871an enclosed array will be @code{equal?} but will not in general be
872@code{eq?}. The value returned by @var{array-prototype} when given an
873enclosed array is unspecified.
874
875examples:
876@example
877(enclose-array '#3(((a b c) (d e f)) ((1 2 3) (4 5 6))) 1) @result{}
878 #<enclosed-array (#1(a d) #1(b e) #1(c f)) (#1(1 4) #1(2 5) #1(3 6))>
879
880(enclose-array '#3(((a b c) (d e f)) ((1 2 3) (4 5 6))) 1 0) @result{}
881 #<enclosed-array #2((a 1) (d 4)) #2((b 2) (e 5)) #2((c 3) (f 6))>
882@end example")
1bbd0b84 883#define FUNC_NAME s_scm_enclose_array
0f2d19dd
JB
884{
885 SCM axv, ra, res, ra_inr;
886 scm_array_dim vdim, *s = &vdim;
887 int ndim, j, k, ninr, noutr;
1bbd0b84 888 SCM_ASSERT (SCM_NIMP (axes), scm_makfrom0str (FUNC_NAME), SCM_WNA,
f5bf2977 889 NULL);
0f2d19dd
JB
890 ra = SCM_CAR (axes);
891 axes = SCM_CDR (axes);
892 if (SCM_NULLP (axes))
0f2d19dd
JB
893 axes = scm_cons ((SCM_ARRAYP (ra) ? SCM_MAKINUM (SCM_ARRAY_NDIM (ra) - 1) : SCM_INUM0), SCM_EOL);
894 ninr = scm_ilength (axes);
895 ra_inr = scm_make_ra (ninr);
896 SCM_ASRTGO (SCM_NIMP (ra), badarg1);
897 switch SCM_TYP7
898 (ra)
899 {
900 default:
1bbd0b84 901 badarg1:SCM_WTA (1,ra);
0f2d19dd
JB
902 case scm_tc7_string:
903 case scm_tc7_bvect:
904 case scm_tc7_byvect:
905 case scm_tc7_uvect:
906 case scm_tc7_ivect:
907 case scm_tc7_fvect:
908 case scm_tc7_dvect:
909 case scm_tc7_cvect:
910 case scm_tc7_vector:
95f5b0f5 911 case scm_tc7_wvect:
0f2d19dd 912 case scm_tc7_svect:
5c11cc9d 913#ifdef HAVE_LONG_LONGS
0f2d19dd
JB
914 case scm_tc7_llvect:
915#endif
916 s->lbnd = 0;
917 s->ubnd = SCM_LENGTH (ra) - 1;
918 s->inc = 1;
919 SCM_ARRAY_V (ra_inr) = ra;
920 SCM_ARRAY_BASE (ra_inr) = 0;
921 ndim = 1;
922 break;
923 case scm_tc7_smob:
924 SCM_ASRTGO (SCM_ARRAYP (ra), badarg1);
925 s = SCM_ARRAY_DIMS (ra);
926 SCM_ARRAY_V (ra_inr) = SCM_ARRAY_V (ra);
927 SCM_ARRAY_BASE (ra_inr) = SCM_ARRAY_BASE (ra);
928 ndim = SCM_ARRAY_NDIM (ra);
929 break;
930 }
931 noutr = ndim - ninr;
932 axv = scm_make_string (SCM_MAKINUM (ndim), SCM_MAKICHR (0));
1bbd0b84 933 SCM_ASSERT (0 <= noutr && 0 <= ninr, scm_makfrom0str (FUNC_NAME),
f5bf2977 934 SCM_WNA, NULL);
0f2d19dd
JB
935 res = scm_make_ra (noutr);
936 SCM_ARRAY_BASE (res) = SCM_ARRAY_BASE (ra_inr);
937 SCM_ARRAY_V (res) = ra_inr;
938 for (k = 0; k < ninr; k++, axes = SCM_CDR (axes))
939 {
1bbd0b84 940 SCM_ASSERT (SCM_INUMP (SCM_CAR (axes)), SCM_CAR (axes), "bad axis", FUNC_NAME);
0f2d19dd
JB
941 j = SCM_INUM (SCM_CAR (axes));
942 SCM_ARRAY_DIMS (ra_inr)[k].lbnd = s[j].lbnd;
943 SCM_ARRAY_DIMS (ra_inr)[k].ubnd = s[j].ubnd;
944 SCM_ARRAY_DIMS (ra_inr)[k].inc = s[j].inc;
945 SCM_CHARS (axv)[j] = 1;
946 }
947 for (j = 0, k = 0; k < noutr; k++, j++)
948 {
949 while (SCM_CHARS (axv)[j])
950 j++;
951 SCM_ARRAY_DIMS (res)[k].lbnd = s[j].lbnd;
952 SCM_ARRAY_DIMS (res)[k].ubnd = s[j].ubnd;
953 SCM_ARRAY_DIMS (res)[k].inc = s[j].inc;
954 }
955 scm_ra_set_contp (ra_inr);
956 scm_ra_set_contp (res);
957 return res;
958}
1bbd0b84 959#undef FUNC_NAME
0f2d19dd
JB
960
961
962
3b3b36dd 963SCM_DEFINE (scm_array_in_bounds_p, "array-in-bounds?", 0, 0, 1,
1bbd0b84 964 (SCM args),
4079f87e 965"Returns @code{#t} if its arguments would be acceptable to array-ref.")
1bbd0b84 966#define FUNC_NAME s_scm_array_in_bounds_p
0f2d19dd
JB
967{
968 SCM v, ind = SCM_EOL;
969 long pos = 0;
970 register scm_sizet k;
971 register long j;
972 scm_array_dim *s;
1bbd0b84 973 SCM_ASSERT (SCM_NIMP (args), scm_makfrom0str (FUNC_NAME),
f5bf2977 974 SCM_WNA, NULL);
0f2d19dd
JB
975 v = SCM_CAR (args);
976 args = SCM_CDR (args);
977 SCM_ASRTGO (SCM_NIMP (v), badarg1);
978 if (SCM_NIMP (args))
979
980 {
981 ind = SCM_CAR (args);
982 args = SCM_CDR (args);
1bbd0b84 983 SCM_ASSERT (SCM_INUMP (ind), ind, SCM_ARG2, FUNC_NAME);
0f2d19dd
JB
984 pos = SCM_INUM (ind);
985 }
986tail:
987 switch SCM_TYP7
988 (v)
989 {
990 default:
1bbd0b84
GB
991 badarg1:SCM_WTA (1,v);
992 wna: scm_wrong_num_args (scm_makfrom0str (FUNC_NAME));
0f2d19dd
JB
993 case scm_tc7_smob:
994 k = SCM_ARRAY_NDIM (v);
995 s = SCM_ARRAY_DIMS (v);
996 pos = SCM_ARRAY_BASE (v);
997 if (!k)
998 {
999 SCM_ASRTGO (SCM_NULLP (ind), wna);
1000 ind = SCM_INUM0;
1001 }
1002 else
1003 while (!0)
1004 {
1005 j = SCM_INUM (ind);
1006 if (!(j >= (s->lbnd) && j <= (s->ubnd)))
1007 {
1008 SCM_ASRTGO (--k == scm_ilength (args), wna);
1009 return SCM_BOOL_F;
1010 }
1011 pos += (j - s->lbnd) * (s->inc);
1012 if (!(--k && SCM_NIMP (args)))
1013 break;
1014 ind = SCM_CAR (args);
1015 args = SCM_CDR (args);
1016 s++;
1bbd0b84 1017 SCM_ASSERT (SCM_INUMP (ind), ind, s_bad_ind, FUNC_NAME);
0f2d19dd
JB
1018 }
1019 SCM_ASRTGO (0 == k, wna);
1020 v = SCM_ARRAY_V (v);
1021 goto tail;
1022 case scm_tc7_bvect:
1023 case scm_tc7_string:
1024 case scm_tc7_byvect:
1025 case scm_tc7_uvect:
1026 case scm_tc7_ivect:
1027 case scm_tc7_fvect:
1028 case scm_tc7_dvect:
1029 case scm_tc7_cvect:
1030 case scm_tc7_svect:
5c11cc9d 1031#ifdef HAVE_LONG_LONGS
0f2d19dd
JB
1032 case scm_tc7_llvect:
1033#endif
1034 case scm_tc7_vector:
95f5b0f5 1035 case scm_tc7_wvect:
0f2d19dd 1036 SCM_ASRTGO (SCM_NULLP (args) && SCM_INUMP (ind), wna);
1bbd0b84 1037 return SCM_BOOL(pos >= 0 && pos < SCM_LENGTH (v));
0f2d19dd
JB
1038 }
1039}
1bbd0b84 1040#undef FUNC_NAME
0f2d19dd
JB
1041
1042
1bbd0b84 1043SCM_REGISTER_PROC(s_array_ref, "array-ref", 1, 0, 1, scm_uniform_vector_ref);
1cc91f1b 1044
1bbd0b84 1045
3b3b36dd 1046SCM_DEFINE (scm_uniform_vector_ref, "uniform-vector-ref", 2, 0, 0,
1bbd0b84 1047 (SCM v, SCM args),
4079f87e 1048"Returns the element at the @code{(index1, index2)} element in @var{array}.")
1bbd0b84 1049#define FUNC_NAME s_scm_uniform_vector_ref
0f2d19dd
JB
1050{
1051 long pos;
0f2d19dd 1052
35de7ebe 1053 if (SCM_IMP (v))
0f2d19dd
JB
1054 {
1055 SCM_ASRTGO (SCM_NULLP (args), badarg);
1056 return v;
1057 }
1058 else if (SCM_ARRAYP (v))
0f2d19dd 1059 {
1bbd0b84 1060 pos = scm_aind (v, args, FUNC_NAME);
0f2d19dd
JB
1061 v = SCM_ARRAY_V (v);
1062 }
1063 else
1064 {
1065 if (SCM_NIMP (args))
1066
1067 {
1bbd0b84 1068 SCM_ASSERT (SCM_CONSP (args) && SCM_INUMP (SCM_CAR (args)), args, SCM_ARG2, FUNC_NAME);
0f2d19dd
JB
1069 pos = SCM_INUM (SCM_CAR (args));
1070 SCM_ASRTGO (SCM_NULLP (SCM_CDR (args)), wna);
1071 }
1072 else
1073 {
3b3b36dd 1074 SCM_VALIDATE_INUM (2,args);
0f2d19dd
JB
1075 pos = SCM_INUM (args);
1076 }
1077 SCM_ASRTGO (pos >= 0 && pos < SCM_LENGTH (v), outrng);
1078 }
1079 switch SCM_TYP7
1080 (v)
1081 {
1082 default:
1083 if (SCM_NULLP (args))
1084 return v;
35de7ebe 1085 badarg:
1bbd0b84 1086 SCM_WTA (1,v);
35de7ebe 1087 abort ();
1bbd0b84
GB
1088 outrng:scm_out_of_range (FUNC_NAME, SCM_MAKINUM (pos));
1089 wna: scm_wrong_num_args (SCM_FUNC_NAME);
0f2d19dd
JB
1090 case scm_tc7_smob:
1091 { /* enclosed */
1092 int k = SCM_ARRAY_NDIM (v);
1093 SCM res = scm_make_ra (k);
1094 SCM_ARRAY_V (res) = SCM_ARRAY_V (v);
1095 SCM_ARRAY_BASE (res) = pos;
1096 while (k--)
1097 {
1098 SCM_ARRAY_DIMS (res)[k].lbnd = SCM_ARRAY_DIMS (v)[k].lbnd;
1099 SCM_ARRAY_DIMS (res)[k].ubnd = SCM_ARRAY_DIMS (v)[k].ubnd;
1100 SCM_ARRAY_DIMS (res)[k].inc = SCM_ARRAY_DIMS (v)[k].inc;
1101 }
1102 return res;
1103 }
1104 case scm_tc7_bvect:
1105 if (SCM_VELTS (v)[pos / SCM_LONG_BIT] & (1L << (pos % SCM_LONG_BIT)))
1106 return SCM_BOOL_T;
1107 else
1108 return SCM_BOOL_F;
1109 case scm_tc7_string:
fc1d67c4 1110 return SCM_MAKICHR (SCM_UCHARS (v)[pos]);
0f2d19dd
JB
1111 case scm_tc7_byvect:
1112 return SCM_MAKINUM (((char *)SCM_CHARS (v))[pos]);
1113# ifdef SCM_INUMS_ONLY
1114 case scm_tc7_uvect:
1115 case scm_tc7_ivect:
1116 return SCM_MAKINUM (SCM_VELTS (v)[pos]);
1117# else
1118 case scm_tc7_uvect:
1119 return scm_ulong2num(SCM_VELTS(v)[pos]);
1120 case scm_tc7_ivect:
1121 return scm_long2num(SCM_VELTS(v)[pos]);
1122# endif
1123
1124 case scm_tc7_svect:
1125 return SCM_MAKINUM (((short *) SCM_CDR (v))[pos]);
5c11cc9d 1126#ifdef HAVE_LONG_LONGS
0f2d19dd
JB
1127 case scm_tc7_llvect:
1128 return scm_long_long2num (((long_long *) SCM_CDR (v))[pos]);
1129#endif
1130
1131#ifdef SCM_FLOATS
1132#ifdef SCM_SINGLES
1133 case scm_tc7_fvect:
1134 return scm_makflo (((float *) SCM_CDR (v))[pos]);
1135#endif
1136 case scm_tc7_dvect:
1137 return scm_makdbl (((double *) SCM_CDR (v))[pos], 0.0);
1138 case scm_tc7_cvect:
1139 return scm_makdbl (((double *) SCM_CDR (v))[2 * pos],
1140 ((double *) SCM_CDR (v))[2 * pos + 1]);
1141#endif
1142 case scm_tc7_vector:
95f5b0f5 1143 case scm_tc7_wvect:
0f2d19dd
JB
1144 return SCM_VELTS (v)[pos];
1145 }
1146}
1bbd0b84 1147#undef FUNC_NAME
0f2d19dd
JB
1148
1149/* Internal version of scm_uniform_vector_ref for uves that does no error checking and
1150 tries to recycle conses. (Make *sure* you want them recycled.) */
1cc91f1b 1151
0f2d19dd 1152SCM
6e8d25a6 1153scm_cvref (SCM v, scm_sizet pos, SCM last)
0f2d19dd 1154{
5c11cc9d 1155 switch SCM_TYP7 (v)
0f2d19dd
JB
1156 {
1157 default:
1158 scm_wta (v, (char *) SCM_ARG1, "PROGRAMMING ERROR: scm_cvref");
1159 case scm_tc7_bvect:
1160 if (SCM_VELTS (v)[pos / SCM_LONG_BIT] & (1L << (pos % SCM_LONG_BIT)))
1161 return SCM_BOOL_T;
1162 else
1163 return SCM_BOOL_F;
1164 case scm_tc7_string:
fc1d67c4 1165 return SCM_MAKICHR (SCM_UCHARS (v)[pos]);
0f2d19dd
JB
1166 case scm_tc7_byvect:
1167 return SCM_MAKINUM (((char *)SCM_CHARS (v))[pos]);
1168# ifdef SCM_INUMS_ONLY
1169 case scm_tc7_uvect:
1170 case scm_tc7_ivect:
1171 return SCM_MAKINUM (SCM_VELTS (v)[pos]);
1172# else
1173 case scm_tc7_uvect:
1174 return scm_ulong2num(SCM_VELTS(v)[pos]);
1175 case scm_tc7_ivect:
1176 return scm_long2num(SCM_VELTS(v)[pos]);
1177# endif
1178 case scm_tc7_svect:
1179 return SCM_MAKINUM (((short *) SCM_CDR (v))[pos]);
5c11cc9d 1180#ifdef HAVE_LONG_LONGS
0f2d19dd
JB
1181 case scm_tc7_llvect:
1182 return scm_long_long2num (((long_long *) SCM_CDR (v))[pos]);
1183#endif
1184#ifdef SCM_FLOATS
1185#ifdef SCM_SINGLES
1186 case scm_tc7_fvect:
1187 if (SCM_NIMP (last) && (last != scm_flo0) && (scm_tc_flo == SCM_CAR (last)))
1188 {
1189 SCM_FLO (last) = ((float *) SCM_CDR (v))[pos];
1190 return last;
1191 }
1192 return scm_makflo (((float *) SCM_CDR (v))[pos]);
1193#endif
1194 case scm_tc7_dvect:
1195#ifdef SCM_SINGLES
1196 if (SCM_NIMP (last) && scm_tc_dblr == SCM_CAR (last))
1197#else
1198 if (SCM_NIMP (last) && (last != scm_flo0) && (scm_tc_dblr == SCM_CAR (last)))
1199#endif
1200 {
1201 SCM_REAL (last) = ((double *) SCM_CDR (v))[pos];
1202 return last;
1203 }
1204 return scm_makdbl (((double *) SCM_CDR (v))[pos], 0.0);
1205 case scm_tc7_cvect:
1206 if (SCM_NIMP (last) && scm_tc_dblc == SCM_CAR (last))
1207 {
1208 SCM_REAL (last) = ((double *) SCM_CDR (v))[2 * pos];
1209 SCM_IMAG (last) = ((double *) SCM_CDR (v))[2 * pos + 1];
1210 return last;
1211 }
1212 return scm_makdbl (((double *) SCM_CDR (v))[2 * pos],
1213 ((double *) SCM_CDR (v))[2 * pos + 1]);
1214#endif
1215 case scm_tc7_vector:
95f5b0f5 1216 case scm_tc7_wvect:
0f2d19dd
JB
1217 return SCM_VELTS (v)[pos];
1218 case scm_tc7_smob:
1219 { /* enclosed scm_array */
1220 int k = SCM_ARRAY_NDIM (v);
1221 SCM res = scm_make_ra (k);
1222 SCM_ARRAY_V (res) = SCM_ARRAY_V (v);
1223 SCM_ARRAY_BASE (res) = pos;
1224 while (k--)
1225 {
1226 SCM_ARRAY_DIMS (res)[k].ubnd = SCM_ARRAY_DIMS (v)[k].ubnd;
1227 SCM_ARRAY_DIMS (res)[k].lbnd = SCM_ARRAY_DIMS (v)[k].lbnd;
1228 SCM_ARRAY_DIMS (res)[k].inc = SCM_ARRAY_DIMS (v)[k].inc;
1229 }
1230 return res;
1231 }
1232 }
1233}
1234
1bbd0b84
GB
1235SCM_REGISTER_PROC(s_uniform_array_set1_x, "uniform-array-set1!", 3, 0, 0, scm_array_set_x);
1236
1cc91f1b 1237
0aa0871f
GH
1238/* Note that args may be a list or an immediate object, depending which
1239 PROC is used (and it's called from C too). */
3b3b36dd 1240SCM_DEFINE (scm_array_set_x, "array-set!", 2, 0, 1,
1bbd0b84 1241 (SCM v, SCM obj, SCM args),
4079f87e
GB
1242"Sets the element at the @code{(index1, index2)} element in @var{array} to
1243@var{new-value}. The value returned by array-set! is unspecified.")
1bbd0b84 1244#define FUNC_NAME s_scm_array_set_x
0f2d19dd 1245{
f3667f52 1246 long pos = 0;
0f2d19dd
JB
1247 SCM_ASRTGO (SCM_NIMP (v), badarg1);
1248 if (SCM_ARRAYP (v))
0f2d19dd 1249 {
1bbd0b84 1250 pos = scm_aind (v, args, FUNC_NAME);
0f2d19dd
JB
1251 v = SCM_ARRAY_V (v);
1252 }
1253 else
1254 {
1255 if (SCM_NIMP (args))
0f2d19dd 1256 {
0aa0871f 1257 SCM_ASSERT (SCM_CONSP(args) && SCM_INUMP (SCM_CAR (args)), args,
1bbd0b84 1258 SCM_ARG3, FUNC_NAME);
0f2d19dd 1259 SCM_ASRTGO (SCM_NULLP (SCM_CDR (args)), wna);
0aa0871f 1260 pos = SCM_INUM (SCM_CAR (args));
0f2d19dd
JB
1261 }
1262 else
1263 {
3b3b36dd 1264 SCM_VALIDATE_INUM_COPY (3,args,pos);
0f2d19dd
JB
1265 }
1266 SCM_ASRTGO (pos >= 0 && pos < SCM_LENGTH (v), outrng);
1267 }
1268 switch (SCM_TYP7 (v))
1269 {
35de7ebe 1270 default: badarg1:
1bbd0b84 1271 SCM_WTA (1,v);
35de7ebe 1272 abort ();
1bbd0b84
GB
1273 outrng:scm_out_of_range (FUNC_NAME, SCM_MAKINUM (pos));
1274 wna: scm_wrong_num_args (SCM_FUNC_NAME);
0f2d19dd
JB
1275 case scm_tc7_smob: /* enclosed */
1276 goto badarg1;
1277 case scm_tc7_bvect:
1278 if (SCM_BOOL_F == obj)
1279 SCM_VELTS (v)[pos / SCM_LONG_BIT] &= ~(1L << (pos % SCM_LONG_BIT));
1280 else if (SCM_BOOL_T == obj)
1281 SCM_VELTS (v)[pos / SCM_LONG_BIT] |= (1L << (pos % SCM_LONG_BIT));
1282 else
1bbd0b84 1283 badobj:SCM_WTA (2,obj);
0f2d19dd
JB
1284 break;
1285 case scm_tc7_string:
0aa0871f 1286 SCM_ASRTGO (SCM_ICHRP (obj), badobj);
fc1d67c4 1287 SCM_UCHARS (v)[pos] = SCM_ICHR (obj);
0f2d19dd
JB
1288 break;
1289 case scm_tc7_byvect:
1290 if (SCM_ICHRP (obj))
b1d24656 1291 obj = SCM_MAKINUM ((char) SCM_ICHR (obj));
0aa0871f 1292 SCM_ASRTGO (SCM_INUMP (obj), badobj);
0f2d19dd
JB
1293 ((char *)SCM_CHARS (v))[pos] = SCM_INUM (obj);
1294 break;
1295# ifdef SCM_INUMS_ONLY
1296 case scm_tc7_uvect:
1bbd0b84
GB
1297 SCM_ASRTGO (SCM_INUM (obj) >= 0, badobj);
1298 /* fall through */
0f2d19dd 1299 case scm_tc7_ivect:
1bbd0b84 1300 SCM_ASRTGO(SCM_INUMP(obj), badobj); SCM_VELTS(v)[pos] = SCM_INUM(obj); break;
0f2d19dd 1301# else
1bbd0b84
GB
1302 case scm_tc7_uvect:
1303 SCM_VELTS(v)[pos] = scm_num2ulong(obj, (char *)SCM_ARG2, FUNC_NAME); break;
1304 case scm_tc7_ivect:
1305 SCM_VELTS(v)[pos] = scm_num2long(obj, (char *)SCM_ARG2, FUNC_NAME); break;
0f2d19dd 1306# endif
0f2d19dd 1307 case scm_tc7_svect:
0aa0871f 1308 SCM_ASRTGO (SCM_INUMP (obj), badobj);
0f2d19dd
JB
1309 ((short *) SCM_CDR (v))[pos] = SCM_INUM (obj);
1310 break;
5c11cc9d 1311#ifdef HAVE_LONG_LONGS
0f2d19dd 1312 case scm_tc7_llvect:
1bbd0b84 1313 ((long_long *) SCM_CDR (v))[pos] = scm_num2long_long (obj, (char *)SCM_ARG2, FUNC_NAME);
0f2d19dd
JB
1314 break;
1315#endif
1316
1317
1318#ifdef SCM_FLOATS
1319#ifdef SCM_SINGLES
1320 case scm_tc7_fvect:
1bbd0b84 1321 ((float *) SCM_CDR (v))[pos] = (float)scm_num2dbl(obj, FUNC_NAME); break;
0f2d19dd
JB
1322 break;
1323#endif
1324 case scm_tc7_dvect:
1bbd0b84 1325 ((double *) SCM_CDR (v))[pos] = scm_num2dbl(obj, FUNC_NAME); break;
0f2d19dd
JB
1326 break;
1327 case scm_tc7_cvect:
0c95b57d 1328 SCM_ASRTGO (SCM_INEXP (obj), badobj);
0f2d19dd
JB
1329 ((double *) SCM_CDR (v))[2 * pos] = SCM_REALPART (obj);
1330 ((double *) SCM_CDR (v))[2 * pos + 1] = SCM_CPLXP (obj) ? SCM_IMAG (obj) : 0.0;
1331 break;
1332#endif
1333 case scm_tc7_vector:
95f5b0f5 1334 case scm_tc7_wvect:
0f2d19dd
JB
1335 SCM_VELTS (v)[pos] = obj;
1336 break;
1337 }
1338 return SCM_UNSPECIFIED;
1339}
1bbd0b84 1340#undef FUNC_NAME
0f2d19dd 1341
1d7bdb25
GH
1342/* attempts to unroll an array into a one-dimensional array.
1343 returns the unrolled array or #f if it can't be done. */
1bbd0b84 1344 /* if strict is not SCM_UNDEFINED, return #f if returned array
1d7bdb25 1345 wouldn't have contiguous elements. */
3b3b36dd 1346SCM_DEFINE (scm_array_contents, "array-contents", 1, 1, 0,
1bbd0b84 1347 (SCM ra, SCM strict),
4079f87e
GB
1348"@deffnx primitive array-contents array strict
1349If @var{array} may be @dfn{unrolled} into a one dimensional shared array
1350without changing their order (last subscript changing fastest), then
1351@code{array-contents} returns that shared array, otherwise it returns
1352@code{#f}. All arrays made by @var{make-array} and
1353@var{make-uniform-array} may be unrolled, some arrays made by
1354@var{make-shared-array} may not be.
1355
1356If the optional argument @var{strict} is provided, a shared array will
1357be returned only if its elements are stored internally contiguous in
1358memory.")
1bbd0b84 1359#define FUNC_NAME s_scm_array_contents
0f2d19dd
JB
1360{
1361 SCM sra;
1362 if (SCM_IMP (ra))
f3667f52 1363 return SCM_BOOL_F;
5c11cc9d 1364 switch SCM_TYP7 (ra)
0f2d19dd
JB
1365 {
1366 default:
1367 return SCM_BOOL_F;
1368 case scm_tc7_vector:
95f5b0f5 1369 case scm_tc7_wvect:
0f2d19dd
JB
1370 case scm_tc7_string:
1371 case scm_tc7_bvect:
1372 case scm_tc7_byvect:
1373 case scm_tc7_uvect:
1374 case scm_tc7_ivect:
1375 case scm_tc7_fvect:
1376 case scm_tc7_dvect:
1377 case scm_tc7_cvect:
1378 case scm_tc7_svect:
5c11cc9d 1379#ifdef HAVE_LONG_LONGS
0f2d19dd
JB
1380 case scm_tc7_llvect:
1381#endif
1382 return ra;
1383 case scm_tc7_smob:
1384 {
1385 scm_sizet k, ndim = SCM_ARRAY_NDIM (ra), len = 1;
1386 if (!SCM_ARRAYP (ra) || !SCM_ARRAY_CONTP (ra))
1387 return SCM_BOOL_F;
1388 for (k = 0; k < ndim; k++)
1389 len *= SCM_ARRAY_DIMS (ra)[k].ubnd - SCM_ARRAY_DIMS (ra)[k].lbnd + 1;
1390 if (!SCM_UNBNDP (strict))
1391 {
0f2d19dd
JB
1392 if (ndim && (1 != SCM_ARRAY_DIMS (ra)[ndim - 1].inc))
1393 return SCM_BOOL_F;
1394 if (scm_tc7_bvect == SCM_TYP7 (SCM_ARRAY_V (ra)))
1395 {
1396 if (len != SCM_LENGTH (SCM_ARRAY_V (ra)) ||
1397 SCM_ARRAY_BASE (ra) % SCM_LONG_BIT ||
1398 len % SCM_LONG_BIT)
1399 return SCM_BOOL_F;
1400 }
1401 }
1402 if ((len == SCM_LENGTH (SCM_ARRAY_V (ra))) && 0 == SCM_ARRAY_BASE (ra) && SCM_ARRAY_DIMS (ra)->inc)
1403 return SCM_ARRAY_V (ra);
1404 sra = scm_make_ra (1);
1405 SCM_ARRAY_DIMS (sra)->lbnd = 0;
1406 SCM_ARRAY_DIMS (sra)->ubnd = len - 1;
1407 SCM_ARRAY_V (sra) = SCM_ARRAY_V (ra);
1408 SCM_ARRAY_BASE (sra) = SCM_ARRAY_BASE (ra);
1409 SCM_ARRAY_DIMS (sra)->inc = (ndim ? SCM_ARRAY_DIMS (ra)[ndim - 1].inc : 1);
1410 return sra;
1411 }
1412 }
1413}
1bbd0b84 1414#undef FUNC_NAME
0f2d19dd 1415
1cc91f1b 1416
0f2d19dd 1417SCM
6e8d25a6 1418scm_ra2contig (SCM ra, int copy)
0f2d19dd
JB
1419{
1420 SCM ret;
1421 long inc = 1;
1422 scm_sizet k, len = 1;
1423 for (k = SCM_ARRAY_NDIM (ra); k--;)
1424 len *= SCM_ARRAY_DIMS (ra)[k].ubnd - SCM_ARRAY_DIMS (ra)[k].lbnd + 1;
1425 k = SCM_ARRAY_NDIM (ra);
1426 if (SCM_ARRAY_CONTP (ra) && ((0 == k) || (1 == SCM_ARRAY_DIMS (ra)[k - 1].inc)))
1427 {
1428 if (scm_tc7_bvect != SCM_TYP7 (ra))
1429 return ra;
1430 if ((len == SCM_LENGTH (SCM_ARRAY_V (ra)) &&
1431 0 == SCM_ARRAY_BASE (ra) % SCM_LONG_BIT &&
1432 0 == len % SCM_LONG_BIT))
1433 return ra;
1434 }
1435 ret = scm_make_ra (k);
1436 SCM_ARRAY_BASE (ret) = 0;
1437 while (k--)
1438 {
1439 SCM_ARRAY_DIMS (ret)[k].lbnd = SCM_ARRAY_DIMS (ra)[k].lbnd;
1440 SCM_ARRAY_DIMS (ret)[k].ubnd = SCM_ARRAY_DIMS (ra)[k].ubnd;
1441 SCM_ARRAY_DIMS (ret)[k].inc = inc;
1442 inc *= SCM_ARRAY_DIMS (ra)[k].ubnd - SCM_ARRAY_DIMS (ra)[k].lbnd + 1;
1443 }
1444 SCM_ARRAY_V (ret) = scm_make_uve ((inc - 1), scm_array_prototype (ra));
1445 if (copy)
1446 scm_array_copy_x (ra, ret);
1447 return ret;
1448}
1449
1450
1451
3b3b36dd 1452SCM_DEFINE (scm_uniform_array_read_x, "uniform-array-read!", 1, 3, 0,
1bbd0b84 1453 (SCM ra, SCM port_or_fd, SCM start, SCM end),
4079f87e
GB
1454"@deffnx primitive uniform-vector-read! uve [port-or-fdes] [start] [end]
1455Attempts to read all elements of @var{ura}, in lexicographic order, as
1456binary objects from @var{port-or-fdes}.
1457If an end of file is encountered during
1458uniform-array-read! the objects up to that point only are put into @var{ura}
1459(starting at the beginning) and the remainder of the array is
1460unchanged.
1461
1462The optional arguments @var{start} and @var{end} allow
1463a specified region of a vector (or linearized array) to be read,
1464leaving the remainder of the vector unchanged.
1465
1466@code{uniform-array-read!} returns the number of objects read.
1467@var{port-or-fdes} may be omitted, in which case it defaults to the value
1468returned by @code{(current-input-port)}.")
1bbd0b84 1469#define FUNC_NAME s_scm_uniform_array_read_x
0f2d19dd 1470{
35de7ebe 1471 SCM cra = SCM_UNDEFINED, v = ra;
3d8d56df 1472 long sz, vlen, ans;
1146b6cd
GH
1473 long cstart = 0;
1474 long cend;
1475 long offset = 0;
35de7ebe 1476
0f2d19dd 1477 SCM_ASRTGO (SCM_NIMP (v), badarg1);
3d8d56df
GH
1478 if (SCM_UNBNDP (port_or_fd))
1479 port_or_fd = scm_cur_inp;
1480 else
1481 SCM_ASSERT (SCM_INUMP (port_or_fd)
0c95b57d 1482 || (SCM_OPINPORTP (port_or_fd)),
1bbd0b84 1483 port_or_fd, SCM_ARG2, FUNC_NAME);
3d8d56df 1484 vlen = SCM_LENGTH (v);
35de7ebe 1485
0f2d19dd 1486loop:
35de7ebe 1487 switch SCM_TYP7 (v)
0f2d19dd
JB
1488 {
1489 default:
5d2d2ffc 1490 badarg1:SCM_WTA (SCM_ARG1,v);
0f2d19dd
JB
1491 case scm_tc7_smob:
1492 SCM_ASRTGO (SCM_ARRAYP (v), badarg1);
1493 cra = scm_ra2contig (ra, 0);
1146b6cd 1494 cstart += SCM_ARRAY_BASE (cra);
3d8d56df 1495 vlen = SCM_ARRAY_DIMS (cra)->inc *
0f2d19dd
JB
1496 (SCM_ARRAY_DIMS (cra)->ubnd - SCM_ARRAY_DIMS (cra)->lbnd + 1);
1497 v = SCM_ARRAY_V (cra);
1498 goto loop;
1499 case scm_tc7_string:
1500 case scm_tc7_byvect:
1501 sz = sizeof (char);
1502 break;
1503 case scm_tc7_bvect:
3d8d56df 1504 vlen = (vlen + SCM_LONG_BIT - 1) / SCM_LONG_BIT;
1146b6cd 1505 cstart /= SCM_LONG_BIT;
0f2d19dd
JB
1506 case scm_tc7_uvect:
1507 case scm_tc7_ivect:
1508 sz = sizeof (long);
1509 break;
1510 case scm_tc7_svect:
1511 sz = sizeof (short);
1512 break;
5c11cc9d 1513#ifdef HAVE_LONG_LONGS
0f2d19dd
JB
1514 case scm_tc7_llvect:
1515 sz = sizeof (long_long);
1516 break;
1517#endif
1518#ifdef SCM_FLOATS
1519#ifdef SCM_SINGLES
1520 case scm_tc7_fvect:
1521 sz = sizeof (float);
1522 break;
1523#endif
1524 case scm_tc7_dvect:
1525 sz = sizeof (double);
1526 break;
1527 case scm_tc7_cvect:
1528 sz = 2 * sizeof (double);
1529 break;
1530#endif
1531 }
3d8d56df 1532
1146b6cd
GH
1533 cend = vlen;
1534 if (!SCM_UNBNDP (start))
3d8d56df 1535 {
1146b6cd 1536 offset =
4638e087 1537 SCM_NUM2LONG (3, start);
35de7ebe 1538
1146b6cd 1539 if (offset < 0 || offset >= cend)
1bbd0b84 1540 scm_out_of_range (FUNC_NAME, start);
1146b6cd
GH
1541
1542 if (!SCM_UNBNDP (end))
1543 {
1544 long tend =
4638e087 1545 SCM_NUM2LONG (4, end);
3d8d56df 1546
1146b6cd 1547 if (tend <= offset || tend > cend)
1bbd0b84 1548 scm_out_of_range (FUNC_NAME, end);
1146b6cd
GH
1549 cend = tend;
1550 }
0f2d19dd 1551 }
35de7ebe 1552
3d8d56df
GH
1553 if (SCM_NIMP (port_or_fd))
1554 {
6c951427
GH
1555 scm_port *pt = SCM_PTAB_ENTRY (port_or_fd);
1556 int remaining = (cend - offset) * sz;
1557 char *dest = SCM_CHARS (v) + (cstart + offset) * sz;
1558
1559 if (pt->rw_active == SCM_PORT_WRITE)
affc96b5 1560 scm_flush (port_or_fd);
6c951427
GH
1561
1562 ans = cend - offset;
1563 while (remaining > 0)
3d8d56df 1564 {
6c951427
GH
1565 if (pt->read_pos < pt->read_end)
1566 {
1567 int to_copy = min (pt->read_end - pt->read_pos,
1568 remaining);
1569
1570 memcpy (dest, pt->read_pos, to_copy);
1571 pt->read_pos += to_copy;
1572 remaining -= to_copy;
1573 dest += to_copy;
1574 }
1575 else
1576 {
affc96b5 1577 if (scm_fill_input (port_or_fd) == EOF)
6c951427
GH
1578 {
1579 if (remaining % sz != 0)
1580 {
5d2d2ffc 1581 SCM_MISC_ERROR ("unexpected EOF", SCM_EOL);
6c951427
GH
1582 }
1583 ans -= remaining / sz;
1584 break;
1585 }
6c951427 1586 }
3d8d56df 1587 }
6c951427
GH
1588
1589 if (pt->rw_random)
1590 pt->rw_active = SCM_PORT_READ;
3d8d56df
GH
1591 }
1592 else /* file descriptor. */
1593 {
1594 SCM_SYSCALL (ans = read (SCM_INUM (port_or_fd),
1146b6cd
GH
1595 SCM_CHARS (v) + (cstart + offset) * sz,
1596 (scm_sizet) (sz * (cend - offset))));
3d8d56df 1597 if (ans == -1)
1bbd0b84 1598 SCM_SYSERROR;
3d8d56df 1599 }
0f2d19dd
JB
1600 if (SCM_TYP7 (v) == scm_tc7_bvect)
1601 ans *= SCM_LONG_BIT;
35de7ebe 1602
0f2d19dd
JB
1603 if (v != ra && cra != ra)
1604 scm_array_copy_x (cra, ra);
35de7ebe 1605
0f2d19dd
JB
1606 return SCM_MAKINUM (ans);
1607}
1bbd0b84 1608#undef FUNC_NAME
0f2d19dd 1609
3b3b36dd 1610SCM_DEFINE (scm_uniform_array_write, "uniform-array-write", 1, 3, 0,
1bbd0b84 1611 (SCM v, SCM port_or_fd, SCM start, SCM end),
4079f87e
GB
1612"@deffnx primitive uniform-vector-write uve [port-or-fdes] [start] [end]
1613Writes all elements of @var{ura} as binary objects to
1614@var{port-or-fdes}.
1615
1616The optional arguments @var{start}
1617and @var{end} allow
1618a specified region of a vector (or linearized array) to be written.
1619
1620The number of objects actually written is returned.
1621@var{port-or-fdes} may be
1622omitted, in which case it defaults to the value returned by
1623@code{(current-output-port)}.")
1bbd0b84 1624#define FUNC_NAME s_scm_uniform_array_write
0f2d19dd 1625{
3d8d56df 1626 long sz, vlen, ans;
1146b6cd
GH
1627 long offset = 0;
1628 long cstart = 0;
1629 long cend;
3d8d56df 1630
78446828
MV
1631 port_or_fd = SCM_COERCE_OUTPORT (port_or_fd);
1632
0f2d19dd 1633 SCM_ASRTGO (SCM_NIMP (v), badarg1);
3d8d56df
GH
1634 if (SCM_UNBNDP (port_or_fd))
1635 port_or_fd = scm_cur_outp;
1636 else
1637 SCM_ASSERT (SCM_INUMP (port_or_fd)
0c95b57d 1638 || (SCM_OPOUTPORTP (port_or_fd)),
1bbd0b84 1639 port_or_fd, SCM_ARG2, FUNC_NAME);
3d8d56df
GH
1640 vlen = SCM_LENGTH (v);
1641
0f2d19dd 1642loop:
3d8d56df 1643 switch SCM_TYP7 (v)
0f2d19dd
JB
1644 {
1645 default:
4638e087 1646 badarg1:SCM_WTA (1, v);
0f2d19dd
JB
1647 case scm_tc7_smob:
1648 SCM_ASRTGO (SCM_ARRAYP (v), badarg1);
1649 v = scm_ra2contig (v, 1);
1146b6cd 1650 cstart = SCM_ARRAY_BASE (v);
3d8d56df
GH
1651 vlen = SCM_ARRAY_DIMS (v)->inc
1652 * (SCM_ARRAY_DIMS (v)->ubnd - SCM_ARRAY_DIMS (v)->lbnd + 1);
0f2d19dd
JB
1653 v = SCM_ARRAY_V (v);
1654 goto loop;
0f2d19dd 1655 case scm_tc7_string:
3d8d56df 1656 case scm_tc7_byvect:
0f2d19dd
JB
1657 sz = sizeof (char);
1658 break;
1659 case scm_tc7_bvect:
3d8d56df 1660 vlen = (vlen + SCM_LONG_BIT - 1) / SCM_LONG_BIT;
1146b6cd 1661 cstart /= SCM_LONG_BIT;
0f2d19dd
JB
1662 case scm_tc7_uvect:
1663 case scm_tc7_ivect:
1664 sz = sizeof (long);
1665 break;
1666 case scm_tc7_svect:
1667 sz = sizeof (short);
1668 break;
5c11cc9d 1669#ifdef HAVE_LONG_LONGS
0f2d19dd
JB
1670 case scm_tc7_llvect:
1671 sz = sizeof (long_long);
1672 break;
1673#endif
1674#ifdef SCM_FLOATS
1675#ifdef SCM_SINGLES
1676 case scm_tc7_fvect:
1677 sz = sizeof (float);
1678 break;
1679#endif
1680 case scm_tc7_dvect:
1681 sz = sizeof (double);
1682 break;
1683 case scm_tc7_cvect:
1684 sz = 2 * sizeof (double);
1685 break;
1686#endif
1687 }
3d8d56df 1688
1146b6cd
GH
1689 cend = vlen;
1690 if (!SCM_UNBNDP (start))
3d8d56df 1691 {
1146b6cd 1692 offset =
4638e087 1693 SCM_NUM2LONG (3, start);
3d8d56df 1694
1146b6cd 1695 if (offset < 0 || offset >= cend)
1bbd0b84 1696 scm_out_of_range (FUNC_NAME, start);
1146b6cd
GH
1697
1698 if (!SCM_UNBNDP (end))
1699 {
1700 long tend =
4638e087 1701 SCM_NUM2LONG (4, end);
3d8d56df 1702
1146b6cd 1703 if (tend <= offset || tend > cend)
1bbd0b84 1704 scm_out_of_range (FUNC_NAME, end);
1146b6cd
GH
1705 cend = tend;
1706 }
3d8d56df
GH
1707 }
1708
1709 if (SCM_NIMP (port_or_fd))
1710 {
6c951427 1711 char *source = SCM_CHARS (v) + (cstart + offset) * sz;
6c951427
GH
1712
1713 ans = cend - offset;
265e6a4d 1714 scm_lfwrite (source, ans * sz, port_or_fd);
3d8d56df
GH
1715 }
1716 else /* file descriptor. */
1717 {
1718 SCM_SYSCALL (ans = write (SCM_INUM (port_or_fd),
1146b6cd
GH
1719 SCM_CHARS (v) + (cstart + offset) * sz,
1720 (scm_sizet) (sz * (cend - offset))));
3d8d56df 1721 if (ans == -1)
1bbd0b84 1722 SCM_SYSERROR;
3d8d56df 1723 }
0f2d19dd
JB
1724 if (SCM_TYP7 (v) == scm_tc7_bvect)
1725 ans *= SCM_LONG_BIT;
3d8d56df 1726
0f2d19dd
JB
1727 return SCM_MAKINUM (ans);
1728}
1bbd0b84 1729#undef FUNC_NAME
0f2d19dd
JB
1730
1731
1732static char cnt_tab[16] =
1733{0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
1734
3b3b36dd 1735SCM_DEFINE (scm_bit_count, "bit-count", 2, 0, 0,
1bbd0b84 1736 (SCM item, SCM seq),
4079f87e 1737"Returns the number occurrences of @var{bool} in @var{bv}.")
1bbd0b84 1738#define FUNC_NAME s_scm_bit_count
0f2d19dd
JB
1739{
1740 long i;
1741 register unsigned long cnt = 0, w;
3b3b36dd 1742 SCM_VALIDATE_INUM (2,seq);
5c11cc9d 1743 switch SCM_TYP7 (seq)
0f2d19dd
JB
1744 {
1745 default:
1bbd0b84 1746 SCM_WTA (2,seq);
0f2d19dd
JB
1747 case scm_tc7_bvect:
1748 if (0 == SCM_LENGTH (seq))
1749 return SCM_INUM0;
1750 i = (SCM_LENGTH (seq) - 1) / SCM_LONG_BIT;
1751 w = SCM_VELTS (seq)[i];
1752 if (SCM_FALSEP (item))
1753 w = ~w;
1754 w <<= SCM_LONG_BIT - 1 - ((SCM_LENGTH (seq) - 1) % SCM_LONG_BIT);
1755 while (!0)
1756 {
1757 for (; w; w >>= 4)
1758 cnt += cnt_tab[w & 0x0f];
1759 if (0 == i--)
1760 return SCM_MAKINUM (cnt);
1761 w = SCM_VELTS (seq)[i];
1762 if (SCM_FALSEP (item))
1763 w = ~w;
1764 }
1765 }
1766}
1bbd0b84 1767#undef FUNC_NAME
0f2d19dd
JB
1768
1769
3b3b36dd 1770SCM_DEFINE (scm_bit_position, "bit-position", 3, 0, 0,
1bbd0b84 1771 (SCM item, SCM v, SCM k),
4079f87e
GB
1772"Returns the minimum index of an occurrence of @var{bool} in @var{bv}
1773which is at least @var{k}. If no @var{bool} occurs within the specified
1774range @code{#f} is returned.")
1bbd0b84 1775#define FUNC_NAME s_scm_bit_position
0f2d19dd 1776{
1bbd0b84 1777 long i, lenw, xbits, pos;
0f2d19dd 1778 register unsigned long w;
6b5a304f 1779 SCM_VALIDATE_NIM (2,v);
3b3b36dd 1780 SCM_VALIDATE_INUM_COPY (3,k,pos);
0f2d19dd 1781 SCM_ASSERT ((pos <= SCM_LENGTH (v)) && (pos >= 0),
1bbd0b84 1782 k, SCM_OUTOFRANGE, FUNC_NAME);
0f2d19dd
JB
1783 if (pos == SCM_LENGTH (v))
1784 return SCM_BOOL_F;
5c11cc9d 1785 switch SCM_TYP7 (v)
0f2d19dd
JB
1786 {
1787 default:
1bbd0b84 1788 SCM_WTA (2,v);
0f2d19dd
JB
1789 case scm_tc7_bvect:
1790 if (0 == SCM_LENGTH (v))
1791 return SCM_MAKINUM (-1L);
1792 lenw = (SCM_LENGTH (v) - 1) / SCM_LONG_BIT; /* watch for part words */
1793 i = pos / SCM_LONG_BIT;
1794 w = SCM_VELTS (v)[i];
1795 if (SCM_FALSEP (item))
1796 w = ~w;
1797 xbits = (pos % SCM_LONG_BIT);
1798 pos -= xbits;
1799 w = ((w >> xbits) << xbits);
1800 xbits = SCM_LONG_BIT - 1 - (SCM_LENGTH (v) - 1) % SCM_LONG_BIT;
1801 while (!0)
1802 {
1803 if (w && (i == lenw))
1804 w = ((w << xbits) >> xbits);
1805 if (w)
1806 while (w)
1807 switch (w & 0x0f)
1808 {
1809 default:
1810 return SCM_MAKINUM (pos);
1811 case 2:
1812 case 6:
1813 case 10:
1814 case 14:
1815 return SCM_MAKINUM (pos + 1);
1816 case 4:
1817 case 12:
1818 return SCM_MAKINUM (pos + 2);
1819 case 8:
1820 return SCM_MAKINUM (pos + 3);
1821 case 0:
1822 pos += 4;
1823 w >>= 4;
1824 }
1825 if (++i > lenw)
1826 break;
1827 pos += SCM_LONG_BIT;
1828 w = SCM_VELTS (v)[i];
1829 if (SCM_FALSEP (item))
1830 w = ~w;
1831 }
1832 return SCM_BOOL_F;
1833 }
1834}
1bbd0b84 1835#undef FUNC_NAME
0f2d19dd
JB
1836
1837
3b3b36dd 1838SCM_DEFINE (scm_bit_set_star_x, "bit-set*!", 3, 0, 0,
1bbd0b84 1839 (SCM v, SCM kv, SCM obj),
4079f87e
GB
1840"If uve is a bit-vector @var{bv} and uve must be of the same length. If
1841@var{bool} is @code{#t}, uve is OR'ed into @var{bv}; If @var{bool} is @code{#f}, the
1842inversion of uve is AND'ed into @var{bv}.
1843
1844If uve is a unsigned integer vector all the elements of uve must be
1845between 0 and the @code{LENGTH} of @var{bv}. The bits of @var{bv}
1846corresponding to the indexes in uve are set to @var{bool}.
1847
1848The return value is unspecified.")
1bbd0b84 1849#define FUNC_NAME s_scm_bit_set_star_x
0f2d19dd
JB
1850{
1851 register long i, k, vlen;
1852 SCM_ASRTGO (SCM_NIMP (v), badarg1);
1853 SCM_ASRTGO (SCM_NIMP (kv), badarg2);
5c11cc9d 1854 switch SCM_TYP7 (kv)
0f2d19dd
JB
1855 {
1856 default:
1bbd0b84 1857 badarg2:SCM_WTA (2,kv);
0f2d19dd 1858 case scm_tc7_uvect:
5c11cc9d 1859 switch SCM_TYP7 (v)
0f2d19dd
JB
1860 {
1861 default:
1bbd0b84 1862 badarg1:SCM_WTA (1,v);
0f2d19dd
JB
1863 case scm_tc7_bvect:
1864 vlen = SCM_LENGTH (v);
1865 if (SCM_BOOL_F == obj)
1866 for (i = SCM_LENGTH (kv); i;)
1867 {
1868 k = SCM_VELTS (kv)[--i];
1bbd0b84 1869 SCM_ASSERT ((k < vlen), SCM_MAKINUM (k), SCM_OUTOFRANGE, FUNC_NAME);
0f2d19dd
JB
1870 SCM_VELTS (v)[k / SCM_LONG_BIT] &= ~(1L << (k % SCM_LONG_BIT));
1871 }
1872 else if (SCM_BOOL_T == obj)
1873 for (i = SCM_LENGTH (kv); i;)
1874 {
1875 k = SCM_VELTS (kv)[--i];
1bbd0b84 1876 SCM_ASSERT ((k < vlen), SCM_MAKINUM (k), SCM_OUTOFRANGE, FUNC_NAME);
0f2d19dd
JB
1877 SCM_VELTS (v)[k / SCM_LONG_BIT] |= (1L << (k % SCM_LONG_BIT));
1878 }
1879 else
1bbd0b84 1880 badarg3:SCM_WTA (3,obj);
0f2d19dd
JB
1881 }
1882 break;
1883 case scm_tc7_bvect:
1884 SCM_ASRTGO (SCM_TYP7 (v) == scm_tc7_bvect && SCM_LENGTH (v) == SCM_LENGTH (kv), badarg1);
1885 if (SCM_BOOL_F == obj)
1886 for (k = (SCM_LENGTH (v) + SCM_LONG_BIT - 1) / SCM_LONG_BIT; k--;)
1887 SCM_VELTS (v)[k] &= ~(SCM_VELTS (kv)[k]);
1888 else if (SCM_BOOL_T == obj)
1889 for (k = (SCM_LENGTH (v) + SCM_LONG_BIT - 1) / SCM_LONG_BIT; k--;)
1890 SCM_VELTS (v)[k] |= SCM_VELTS (kv)[k];
1891 else
1892 goto badarg3;
1893 break;
1894 }
1895 return SCM_UNSPECIFIED;
1896}
1bbd0b84 1897#undef FUNC_NAME
0f2d19dd
JB
1898
1899
3b3b36dd 1900SCM_DEFINE (scm_bit_count_star, "bit-count*", 3, 0, 0,
1bbd0b84 1901 (SCM v, SCM kv, SCM obj),
4079f87e
GB
1902"Returns
1903@example
1904(bit-count (bit-set*! (if bool bv (bit-invert! bv)) uve #t) #t).
1905@end example
1906@var{bv} is not modified.")
1bbd0b84 1907#define FUNC_NAME s_scm_bit_count_star
0f2d19dd
JB
1908{
1909 register long i, vlen, count = 0;
1910 register unsigned long k;
1911 SCM_ASRTGO (SCM_NIMP (v), badarg1);
1912 SCM_ASRTGO (SCM_NIMP (kv), badarg2);
5c11cc9d 1913 switch SCM_TYP7 (kv)
0f2d19dd
JB
1914 {
1915 default:
1bbd0b84 1916 badarg2:SCM_WTA (2,kv);
0f2d19dd
JB
1917 case scm_tc7_uvect:
1918 switch SCM_TYP7
1919 (v)
1920 {
1921 default:
1bbd0b84 1922 badarg1:SCM_WTA (1,v);
0f2d19dd
JB
1923 case scm_tc7_bvect:
1924 vlen = SCM_LENGTH (v);
1925 if (SCM_BOOL_F == obj)
1926 for (i = SCM_LENGTH (kv); i;)
1927 {
1928 k = SCM_VELTS (kv)[--i];
1bbd0b84 1929 SCM_ASSERT ((k < vlen), SCM_MAKINUM (k), SCM_OUTOFRANGE, FUNC_NAME);
0f2d19dd
JB
1930 if (!(SCM_VELTS (v)[k / SCM_LONG_BIT] & (1L << (k % SCM_LONG_BIT))))
1931 count++;
1932 }
1933 else if (SCM_BOOL_T == obj)
1934 for (i = SCM_LENGTH (kv); i;)
1935 {
1936 k = SCM_VELTS (kv)[--i];
1bbd0b84 1937 SCM_ASSERT ((k < vlen), SCM_MAKINUM (k), SCM_OUTOFRANGE, FUNC_NAME);
0f2d19dd
JB
1938 if (SCM_VELTS (v)[k / SCM_LONG_BIT] & (1L << (k % SCM_LONG_BIT)))
1939 count++;
1940 }
1941 else
1bbd0b84 1942 badarg3:SCM_WTA (3,obj);
0f2d19dd
JB
1943 }
1944 break;
1945 case scm_tc7_bvect:
1946 SCM_ASRTGO (SCM_TYP7 (v) == scm_tc7_bvect && SCM_LENGTH (v) == SCM_LENGTH (kv), badarg1);
1947 if (0 == SCM_LENGTH (v))
1948 return SCM_INUM0;
1949 SCM_ASRTGO (SCM_BOOL_T == obj || SCM_BOOL_F == obj, badarg3);
1950 obj = (SCM_BOOL_T == obj);
1951 i = (SCM_LENGTH (v) - 1) / SCM_LONG_BIT;
1952 k = SCM_VELTS (kv)[i] & (obj ? SCM_VELTS (v)[i] : ~SCM_VELTS (v)[i]);
1953 k <<= SCM_LONG_BIT - 1 - ((SCM_LENGTH (v) - 1) % SCM_LONG_BIT);
1954 while (!0)
1955 {
1956 for (; k; k >>= 4)
1957 count += cnt_tab[k & 0x0f];
1958 if (0 == i--)
1959 return SCM_MAKINUM (count);
1960 k = SCM_VELTS (kv)[i] & (obj ? SCM_VELTS (v)[i] : ~SCM_VELTS (v)[i]);
1961 }
1962 }
1963 return SCM_MAKINUM (count);
1964}
1bbd0b84 1965#undef FUNC_NAME
0f2d19dd
JB
1966
1967
3b3b36dd 1968SCM_DEFINE (scm_bit_invert_x, "bit-invert!", 1, 0, 0,
1bbd0b84 1969 (SCM v),
4079f87e 1970"Modifies @var{bv} by replacing each element with its negation.")
1bbd0b84 1971#define FUNC_NAME s_scm_bit_invert_x
0f2d19dd
JB
1972{
1973 register long k;
1974 SCM_ASRTGO (SCM_NIMP (v), badarg1);
1975 k = SCM_LENGTH (v);
1976 switch SCM_TYP7
1977 (v)
1978 {
1979 case scm_tc7_bvect:
1980 for (k = (k + SCM_LONG_BIT - 1) / SCM_LONG_BIT; k--;)
1981 SCM_VELTS (v)[k] = ~SCM_VELTS (v)[k];
1982 break;
1983 default:
1bbd0b84 1984 badarg1:SCM_WTA (1,v);
0f2d19dd
JB
1985 }
1986 return SCM_UNSPECIFIED;
1987}
1bbd0b84 1988#undef FUNC_NAME
0f2d19dd
JB
1989
1990
0f2d19dd 1991SCM
1bbd0b84 1992scm_istr2bve (char *str, long len)
0f2d19dd
JB
1993{
1994 SCM v = scm_make_uve (len, SCM_BOOL_T);
1995 long *data = (long *) SCM_VELTS (v);
1996 register unsigned long mask;
1997 register long k;
1998 register long j;
1999 for (k = 0; k < (len + SCM_LONG_BIT - 1) / SCM_LONG_BIT; k++)
2000 {
2001 data[k] = 0L;
2002 j = len - k * SCM_LONG_BIT;
2003 if (j > SCM_LONG_BIT)
2004 j = SCM_LONG_BIT;
2005 for (mask = 1L; j--; mask <<= 1)
2006 switch (*str++)
2007 {
2008 case '0':
2009 break;
2010 case '1':
2011 data[k] |= mask;
2012 break;
2013 default:
2014 return SCM_BOOL_F;
2015 }
2016 }
2017 return v;
2018}
2019
2020
1cc91f1b 2021
0f2d19dd 2022static SCM
1bbd0b84 2023ra2l (SCM ra,scm_sizet base,scm_sizet k)
0f2d19dd
JB
2024{
2025 register SCM res = SCM_EOL;
2026 register long inc = SCM_ARRAY_DIMS (ra)[k].inc;
2027 register scm_sizet i;
2028 if (SCM_ARRAY_DIMS (ra)[k].ubnd < SCM_ARRAY_DIMS (ra)[k].lbnd)
2029 return SCM_EOL;
2030 i = base + (1 + SCM_ARRAY_DIMS (ra)[k].ubnd - SCM_ARRAY_DIMS (ra)[k].lbnd) * inc;
2031 if (k < SCM_ARRAY_NDIM (ra) - 1)
2032 {
2033 do
2034 {
2035 i -= inc;
2036 res = scm_cons (ra2l (ra, i, k + 1), res);
2037 }
2038 while (i != base);
2039 }
2040 else
2041 do
2042 {
2043 i -= inc;
2044 res = scm_cons (scm_uniform_vector_ref (SCM_ARRAY_V (ra), SCM_MAKINUM (i)), res);
2045 }
2046 while (i != base);
2047 return res;
2048}
2049
2050
3b3b36dd 2051SCM_DEFINE (scm_array_to_list, "array->list", 1, 0, 0,
1bbd0b84 2052 (SCM v),
4079f87e 2053"Returns a list consisting of all the elements, in order, of @var{array}.")
1bbd0b84 2054#define FUNC_NAME s_scm_array_to_list
0f2d19dd
JB
2055{
2056 SCM res = SCM_EOL;
2057 register long k;
2058 SCM_ASRTGO (SCM_NIMP (v), badarg1);
2059 switch SCM_TYP7
2060 (v)
2061 {
2062 default:
1bbd0b84 2063 badarg1:SCM_WTA (1,v);
0f2d19dd
JB
2064 case scm_tc7_smob:
2065 SCM_ASRTGO (SCM_ARRAYP (v), badarg1);
2066 return ra2l (v, SCM_ARRAY_BASE (v), 0);
2067 case scm_tc7_vector:
95f5b0f5 2068 case scm_tc7_wvect:
0f2d19dd
JB
2069 return scm_vector_to_list (v);
2070 case scm_tc7_string:
2071 return scm_string_to_list (v);
2072 case scm_tc7_bvect:
2073 {
2074 long *data = (long *) SCM_VELTS (v);
2075 register unsigned long mask;
2076 for (k = (SCM_LENGTH (v) - 1) / SCM_LONG_BIT; k > 0; k--)
cdbadcac 2077 for (mask = 1UL << (SCM_LONG_BIT - 1); mask; mask >>= 1)
156dcb09 2078 res = scm_cons (SCM_BOOL(((long *) data)[k] & mask), res);
0f2d19dd 2079 for (mask = 1L << ((SCM_LENGTH (v) % SCM_LONG_BIT) - 1); mask; mask >>= 1)
156dcb09 2080 res = scm_cons (SCM_BOOL(((long *) data)[k] & mask), res);
0f2d19dd
JB
2081 return res;
2082 }
2083# ifdef SCM_INUMS_ONLY
2084 case scm_tc7_uvect:
2085 case scm_tc7_ivect:
2086 {
2087 long *data = (long *) SCM_VELTS (v);
2088 for (k = SCM_LENGTH (v) - 1; k >= 0; k--)
2089 res = scm_cons (SCM_MAKINUM (data[k]), res);
2090 return res;
2091 }
2092# else
2093 case scm_tc7_uvect: {
2094 long *data = (long *)SCM_VELTS(v);
2095 for (k = SCM_LENGTH(v) - 1; k >= 0; k--)
2096 res = scm_cons(scm_ulong2num(data[k]), res);
2097 return res;
2098 }
2099 case scm_tc7_ivect: {
2100 long *data = (long *)SCM_VELTS(v);
2101 for (k = SCM_LENGTH(v) - 1; k >= 0; k--)
2102 res = scm_cons(scm_long2num(data[k]), res);
2103 return res;
2104 }
2105# endif
2106 case scm_tc7_svect: {
2107 short *data;
2108 data = (short *)SCM_VELTS(v);
2109 for (k = SCM_LENGTH(v) - 1; k >= 0; k--)
2110 res = scm_cons(SCM_MAKINUM (data[k]), res);
2111 return res;
2112 }
5c11cc9d 2113#ifdef HAVE_LONG_LONGS
0f2d19dd
JB
2114 case scm_tc7_llvect: {
2115 long_long *data;
2116 data = (long_long *)SCM_VELTS(v);
2117 for (k = SCM_LENGTH(v) - 1; k >= 0; k--)
2118 res = scm_cons(scm_long_long2num(data[k]), res);
2119 return res;
2120 }
2121#endif
2122
2123
2124#ifdef SCM_FLOATS
2125#ifdef SCM_SINGLES
2126 case scm_tc7_fvect:
2127 {
2128 float *data = (float *) SCM_VELTS (v);
2129 for (k = SCM_LENGTH (v) - 1; k >= 0; k--)
2130 res = scm_cons (scm_makflo (data[k]), res);
2131 return res;
2132 }
2133#endif /*SCM_SINGLES*/
2134 case scm_tc7_dvect:
2135 {
2136 double *data = (double *) SCM_VELTS (v);
2137 for (k = SCM_LENGTH (v) - 1; k >= 0; k--)
2138 res = scm_cons (scm_makdbl (data[k], 0.0), res);
2139 return res;
2140 }
2141 case scm_tc7_cvect:
2142 {
2143 double (*data)[2] = (double (*)[2]) SCM_VELTS (v);
2144 for (k = SCM_LENGTH (v) - 1; k >= 0; k--)
2145 res = scm_cons (scm_makdbl (data[k][0], data[k][1]), res);
2146 return res;
2147 }
2148#endif /*SCM_FLOATS*/
2149 }
2150}
1bbd0b84 2151#undef FUNC_NAME
0f2d19dd
JB
2152
2153
20a54673 2154static char s_bad_ralst[] = "Bad scm_array contents list";
1cc91f1b 2155
1bbd0b84 2156static int l2ra(SCM lst, SCM ra, scm_sizet base, scm_sizet k);
1cc91f1b 2157
3b3b36dd 2158SCM_DEFINE (scm_list_to_uniform_array, "list->uniform-array", 3, 0, 0,
1bbd0b84 2159 (SCM ndim, SCM prot, SCM lst),
4079f87e
GB
2160"@deffnx procedure list->uniform-vector prot lst
2161Returns a uniform array of the type indicated by prototype @var{prot}
2162with elements the same as those of @var{lst}. Elements must be of the
2163appropriate type, no coercions are done.")
1bbd0b84 2164#define FUNC_NAME s_scm_list_to_uniform_array
0f2d19dd
JB
2165{
2166 SCM shp = SCM_EOL;
2167 SCM row = lst;
2168 SCM ra;
2169 scm_sizet k;
2170 long n;
3b3b36dd 2171 SCM_VALIDATE_INUM_COPY (1,ndim,k);
0f2d19dd
JB
2172 while (k--)
2173 {
2174 n = scm_ilength (row);
1bbd0b84 2175 SCM_ASSERT (n >= 0, lst, SCM_ARG3, FUNC_NAME);
0f2d19dd
JB
2176 shp = scm_cons (SCM_MAKINUM (n), shp);
2177 if (SCM_NIMP (row))
2178 row = SCM_CAR (row);
2179 }
d12feca3
GH
2180 ra = scm_dimensions_to_uniform_array (scm_reverse (shp), prot,
2181 SCM_UNDEFINED);
0f2d19dd
JB
2182 if (SCM_NULLP (shp))
2183
2184 {
2185 SCM_ASRTGO (1 == scm_ilength (lst), badlst);
2186 scm_array_set_x (ra, SCM_CAR (lst), SCM_EOL);
2187 return ra;
2188 }
2189 if (!SCM_ARRAYP (ra))
2190 {
2191 for (k = 0; k < SCM_LENGTH (ra); k++, lst = SCM_CDR (lst))
2192 scm_array_set_x (ra, SCM_CAR (lst), SCM_MAKINUM (k));
2193 return ra;
2194 }
2195 if (l2ra (lst, ra, SCM_ARRAY_BASE (ra), 0))
2196 return ra;
2197 else
1bbd0b84 2198 badlst:scm_wta (lst, s_bad_ralst, FUNC_NAME);
0f2d19dd
JB
2199 return SCM_BOOL_F;
2200}
1bbd0b84 2201#undef FUNC_NAME
0f2d19dd 2202
0f2d19dd 2203static int
1bbd0b84 2204l2ra (SCM lst, SCM ra, scm_sizet base, scm_sizet k)
0f2d19dd
JB
2205{
2206 register long inc = SCM_ARRAY_DIMS (ra)[k].inc;
2207 register long n = (1 + SCM_ARRAY_DIMS (ra)[k].ubnd - SCM_ARRAY_DIMS (ra)[k].lbnd);
2208 int ok = 1;
2209 if (n <= 0)
2210 return (SCM_EOL == lst);
2211 if (k < SCM_ARRAY_NDIM (ra) - 1)
2212 {
2213 while (n--)
2214 {
2215 if (SCM_IMP (lst) || SCM_NCONSP (lst))
2216 return 0;
2217 ok = ok && l2ra (SCM_CAR (lst), ra, base, k + 1);
2218 base += inc;
2219 lst = SCM_CDR (lst);
2220 }
2221 if (SCM_NNULLP (lst))
2222 return 0;
2223 }
2224 else
2225 {
2226 while (n--)
2227 {
2228 if (SCM_IMP (lst) || SCM_NCONSP (lst))
2229 return 0;
2230 ok = ok && scm_array_set_x (SCM_ARRAY_V (ra), SCM_CAR (lst), SCM_MAKINUM (base));
2231 base += inc;
2232 lst = SCM_CDR (lst);
2233 }
2234 if (SCM_NNULLP (lst))
2235 return 0;
2236 }
2237 return ok;
2238}
2239
1cc91f1b 2240
0f2d19dd 2241static void
1bbd0b84 2242rapr1 (SCM ra,scm_sizet j,scm_sizet k,SCM port,scm_print_state *pstate)
0f2d19dd
JB
2243{
2244 long inc = 1;
2245 long n = SCM_LENGTH (ra);
2246 int enclosed = 0;
2247tail:
5c11cc9d 2248 switch SCM_TYP7 (ra)
0f2d19dd
JB
2249 {
2250 case scm_tc7_smob:
2251 if (enclosed++)
2252 {
2253 SCM_ARRAY_BASE (ra) = j;
2254 if (n-- > 0)
9882ea19 2255 scm_iprin1 (ra, port, pstate);
0f2d19dd
JB
2256 for (j += inc; n-- > 0; j += inc)
2257 {
b7f3516f 2258 scm_putc (' ', port);
0f2d19dd 2259 SCM_ARRAY_BASE (ra) = j;
9882ea19 2260 scm_iprin1 (ra, port, pstate);
0f2d19dd
JB
2261 }
2262 break;
2263 }
2264 if (k + 1 < SCM_ARRAY_NDIM (ra))
2265 {
2266 long i;
2267 inc = SCM_ARRAY_DIMS (ra)[k].inc;
2268 for (i = SCM_ARRAY_DIMS (ra)[k].lbnd; i < SCM_ARRAY_DIMS (ra)[k].ubnd; i++)
2269 {
b7f3516f 2270 scm_putc ('(', port);
9882ea19 2271 rapr1 (ra, j, k + 1, port, pstate);
b7f3516f 2272 scm_puts (") ", port);
0f2d19dd
JB
2273 j += inc;
2274 }
2275 if (i == SCM_ARRAY_DIMS (ra)[k].ubnd)
2276 { /* could be zero size. */
b7f3516f 2277 scm_putc ('(', port);
9882ea19 2278 rapr1 (ra, j, k + 1, port, pstate);
b7f3516f 2279 scm_putc (')', port);
0f2d19dd
JB
2280 }
2281 break;
2282 }
2283 if SCM_ARRAY_NDIM
2284 (ra)
2285 { /* Could be zero-dimensional */
2286 inc = SCM_ARRAY_DIMS (ra)[k].inc;
2287 n = (SCM_ARRAY_DIMS (ra)[k].ubnd - SCM_ARRAY_DIMS (ra)[k].lbnd + 1);
2288 }
2289 else
2290 n = 1;
2291 ra = SCM_ARRAY_V (ra);
2292 goto tail;
2293 default:
5c11cc9d 2294 /* scm_tc7_bvect and scm_tc7_llvect only? */
0f2d19dd 2295 if (n-- > 0)
9882ea19 2296 scm_iprin1 (scm_uniform_vector_ref (ra, SCM_MAKINUM (j)), port, pstate);
0f2d19dd
JB
2297 for (j += inc; n-- > 0; j += inc)
2298 {
b7f3516f 2299 scm_putc (' ', port);
9882ea19 2300 scm_iprin1 (scm_cvref (ra, j, SCM_UNDEFINED), port, pstate);
0f2d19dd
JB
2301 }
2302 break;
2303 case scm_tc7_string:
2304 if (n-- > 0)
fc1d67c4 2305 scm_iprin1 (SCM_MAKICHR (SCM_UCHARS (ra)[j]), port, pstate);
9882ea19 2306 if (SCM_WRITINGP (pstate))
0f2d19dd
JB
2307 for (j += inc; n-- > 0; j += inc)
2308 {
b7f3516f 2309 scm_putc (' ', port);
fc1d67c4 2310 scm_iprin1 (SCM_MAKICHR (SCM_UCHARS (ra)[j]), port, pstate);
0f2d19dd
JB
2311 }
2312 else
2313 for (j += inc; n-- > 0; j += inc)
b7f3516f 2314 scm_putc (SCM_CHARS (ra)[j], port);
0f2d19dd
JB
2315 break;
2316 case scm_tc7_byvect:
2317 if (n-- > 0)
2318 scm_intprint (((char *)SCM_CDR (ra))[j], 10, port);
2319 for (j += inc; n-- > 0; j += inc)
2320 {
b7f3516f 2321 scm_putc (' ', port);
0f2d19dd
JB
2322 scm_intprint (((char *)SCM_CDR (ra))[j], 10, port);
2323 }
2324 break;
2325
2326 case scm_tc7_uvect:
5c11cc9d
GH
2327 {
2328 char str[11];
2329
2330 if (n-- > 0)
2331 {
2332 /* intprint can't handle >= 2^31. */
2333 sprintf (str, "%lu", (unsigned long) SCM_VELTS (ra)[j]);
2334 scm_puts (str, port);
2335 }
2336 for (j += inc; n-- > 0; j += inc)
2337 {
2338 scm_putc (' ', port);
2339 sprintf (str, "%lu", (unsigned long) SCM_VELTS (ra)[j]);
2340 scm_puts (str, port);
2341 }
2342 }
0f2d19dd
JB
2343 case scm_tc7_ivect:
2344 if (n-- > 0)
2345 scm_intprint (SCM_VELTS (ra)[j], 10, port);
2346 for (j += inc; n-- > 0; j += inc)
2347 {
b7f3516f 2348 scm_putc (' ', port);
0f2d19dd
JB
2349 scm_intprint (SCM_VELTS (ra)[j], 10, port);
2350 }
2351 break;
2352
2353 case scm_tc7_svect:
2354 if (n-- > 0)
2355 scm_intprint (((short *)SCM_CDR (ra))[j], 10, port);
2356 for (j += inc; n-- > 0; j += inc)
2357 {
b7f3516f 2358 scm_putc (' ', port);
0f2d19dd
JB
2359 scm_intprint (((short *)SCM_CDR (ra))[j], 10, port);
2360 }
2361 break;
2362
2363#ifdef SCM_FLOATS
2364#ifdef SCM_SINGLES
2365 case scm_tc7_fvect:
2366 if (n-- > 0)
2367 {
2368 SCM z = scm_makflo (1.0);
2369 SCM_FLO (z) = ((float *) SCM_VELTS (ra))[j];
9882ea19 2370 scm_floprint (z, port, pstate);
0f2d19dd
JB
2371 for (j += inc; n-- > 0; j += inc)
2372 {
b7f3516f 2373 scm_putc (' ', port);
0f2d19dd 2374 SCM_FLO (z) = ((float *) SCM_VELTS (ra))[j];
9882ea19 2375 scm_floprint (z, port, pstate);
0f2d19dd
JB
2376 }
2377 }
2378 break;
2379#endif /*SCM_SINGLES*/
2380 case scm_tc7_dvect:
2381 if (n-- > 0)
2382 {
2383 SCM z = scm_makdbl (1.0 / 3.0, 0.0);
2384 SCM_REAL (z) = ((double *) SCM_VELTS (ra))[j];
9882ea19 2385 scm_floprint (z, port, pstate);
0f2d19dd
JB
2386 for (j += inc; n-- > 0; j += inc)
2387 {
b7f3516f 2388 scm_putc (' ', port);
0f2d19dd 2389 SCM_REAL (z) = ((double *) SCM_VELTS (ra))[j];
9882ea19 2390 scm_floprint (z, port, pstate);
0f2d19dd
JB
2391 }
2392 }
2393 break;
2394 case scm_tc7_cvect:
2395 if (n-- > 0)
2396 {
2397 SCM cz = scm_makdbl (0.0, 1.0), z = scm_makdbl (1.0 / 3.0, 0.0);
2398 SCM_REAL (z) = SCM_REAL (cz) = (((double *) SCM_VELTS (ra))[2 * j]);
2399 SCM_IMAG (cz) = ((double *) SCM_VELTS (ra))[2 * j + 1];
9882ea19 2400 scm_floprint ((0.0 == SCM_IMAG (cz) ? z : cz), port, pstate);
0f2d19dd
JB
2401 for (j += inc; n-- > 0; j += inc)
2402 {
b7f3516f 2403 scm_putc (' ', port);
0f2d19dd
JB
2404 SCM_REAL (z) = SCM_REAL (cz) = ((double *) SCM_VELTS (ra))[2 * j];
2405 SCM_IMAG (cz) = ((double *) SCM_VELTS (ra))[2 * j + 1];
9882ea19 2406 scm_floprint ((0.0 == SCM_IMAG (cz) ? z : cz), port, pstate);
0f2d19dd
JB
2407 }
2408 }
2409 break;
2410#endif /*SCM_FLOATS*/
2411 }
2412}
2413
2414
1cc91f1b 2415
0f2d19dd 2416int
1bbd0b84 2417scm_raprin1 (SCM exp, SCM port, scm_print_state *pstate)
0f2d19dd
JB
2418{
2419 SCM v = exp;
2420 scm_sizet base = 0;
b7f3516f 2421 scm_putc ('#', port);
0f2d19dd 2422tail:
5c11cc9d 2423 switch SCM_TYP7 (v)
0f2d19dd
JB
2424 {
2425 case scm_tc7_smob:
2426 {
2427 long ndim = SCM_ARRAY_NDIM (v);
2428 base = SCM_ARRAY_BASE (v);
2429 v = SCM_ARRAY_V (v);
2430 if (SCM_ARRAYP (v))
2431
2432 {
b7f3516f 2433 scm_puts ("<enclosed-array ", port);
9882ea19 2434 rapr1 (exp, base, 0, port, pstate);
b7f3516f 2435 scm_putc ('>', port);
0f2d19dd
JB
2436 return 1;
2437 }
2438 else
2439 {
2440 scm_intprint (ndim, 10, port);
2441 goto tail;
2442 }
2443 }
2444 case scm_tc7_bvect:
2445 if (exp == v)
2446 { /* a uve, not an scm_array */
2447 register long i, j, w;
b7f3516f 2448 scm_putc ('*', port);
0f2d19dd
JB
2449 for (i = 0; i < (SCM_LENGTH (exp)) / SCM_LONG_BIT; i++)
2450 {
2451 w = SCM_VELTS (exp)[i];
2452 for (j = SCM_LONG_BIT; j; j--)
2453 {
b7f3516f 2454 scm_putc (w & 1 ? '1' : '0', port);
0f2d19dd
JB
2455 w >>= 1;
2456 }
2457 }
2458 j = SCM_LENGTH (exp) % SCM_LONG_BIT;
2459 if (j)
2460 {
2461 w = SCM_VELTS (exp)[SCM_LENGTH (exp) / SCM_LONG_BIT];
2462 for (; j; j--)
2463 {
b7f3516f 2464 scm_putc (w & 1 ? '1' : '0', port);
0f2d19dd
JB
2465 w >>= 1;
2466 }
2467 }
2468 return 1;
2469 }
2470 else
b7f3516f 2471 scm_putc ('b', port);
0f2d19dd
JB
2472 break;
2473 case scm_tc7_string:
b7f3516f 2474 scm_putc ('a', port);
0f2d19dd
JB
2475 break;
2476 case scm_tc7_byvect:
05c33d09 2477 scm_putc ('y', port);
0f2d19dd
JB
2478 break;
2479 case scm_tc7_uvect:
b7f3516f 2480 scm_putc ('u', port);
0f2d19dd
JB
2481 break;
2482 case scm_tc7_ivect:
b7f3516f 2483 scm_putc ('e', port);
0f2d19dd
JB
2484 break;
2485 case scm_tc7_svect:
05c33d09 2486 scm_putc ('h', port);
0f2d19dd 2487 break;
5c11cc9d 2488#ifdef HAVE_LONG_LONGS
0f2d19dd 2489 case scm_tc7_llvect:
5c11cc9d 2490 scm_putc ('l', port);
0f2d19dd
JB
2491 break;
2492#endif
2493#ifdef SCM_FLOATS
2494#ifdef SCM_SINGLES
2495 case scm_tc7_fvect:
b7f3516f 2496 scm_putc ('s', port);
0f2d19dd
JB
2497 break;
2498#endif /*SCM_SINGLES*/
2499 case scm_tc7_dvect:
b7f3516f 2500 scm_putc ('i', port);
0f2d19dd
JB
2501 break;
2502 case scm_tc7_cvect:
b7f3516f 2503 scm_putc ('c', port);
0f2d19dd
JB
2504 break;
2505#endif /*SCM_FLOATS*/
2506 }
b7f3516f 2507 scm_putc ('(', port);
9882ea19 2508 rapr1 (exp, base, 0, port, pstate);
b7f3516f 2509 scm_putc (')', port);
0f2d19dd
JB
2510 return 1;
2511}
2512
3b3b36dd 2513SCM_DEFINE (scm_array_prototype, "array-prototype", 1, 0, 0,
1bbd0b84 2514 (SCM ra),
4079f87e
GB
2515"Returns an object that would produce an array of the same type as
2516@var{array}, if used as the @var{prototype} for
2517@code{make-uniform-array}.")
1bbd0b84 2518#define FUNC_NAME s_scm_array_prototype
0f2d19dd
JB
2519{
2520 int enclosed = 0;
2521 SCM_ASRTGO (SCM_NIMP (ra), badarg);
2522loop:
2523 switch SCM_TYP7
2524 (ra)
2525 {
2526 default:
1bbd0b84 2527 badarg:SCM_WTA (1,ra);
0f2d19dd
JB
2528 case scm_tc7_smob:
2529 SCM_ASRTGO (SCM_ARRAYP (ra), badarg);
2530 if (enclosed++)
2531 return SCM_UNSPECIFIED;
2532 ra = SCM_ARRAY_V (ra);
2533 goto loop;
2534 case scm_tc7_vector:
95f5b0f5 2535 case scm_tc7_wvect:
0f2d19dd
JB
2536 return SCM_EOL;
2537 case scm_tc7_bvect:
2538 return SCM_BOOL_T;
2539 case scm_tc7_string:
2540 return SCM_MAKICHR ('a');
2541 case scm_tc7_byvect:
2542 return SCM_MAKICHR ('\0');
2543 case scm_tc7_uvect:
2544 return SCM_MAKINUM (1L);
2545 case scm_tc7_ivect:
2546 return SCM_MAKINUM (-1L);
2547 case scm_tc7_svect:
2548 return SCM_CDR (scm_intern ("s", 1));
5c11cc9d 2549#ifdef HAVE_LONG_LONGS
0f2d19dd
JB
2550 case scm_tc7_llvect:
2551 return SCM_CDR (scm_intern ("l", 1));
2552#endif
2553#ifdef SCM_FLOATS
2554#ifdef SCM_SINGLES
2555 case scm_tc7_fvect:
2556 return scm_makflo (1.0);
2557#endif
2558 case scm_tc7_dvect:
2559 return scm_makdbl (1.0 / 3.0, 0.0);
2560 case scm_tc7_cvect:
2561 return scm_makdbl (0.0, 1.0);
2562#endif
2563 }
2564}
1bbd0b84 2565#undef FUNC_NAME
0f2d19dd 2566
1cc91f1b 2567
0f2d19dd 2568static SCM
1bbd0b84 2569markra (SCM ptr)
0f2d19dd 2570{
0f2d19dd
JB
2571 return SCM_ARRAY_V (ptr);
2572}
2573
1cc91f1b 2574
0f2d19dd 2575static scm_sizet
1bbd0b84 2576freera (SCM ptr)
0f2d19dd
JB
2577{
2578 scm_must_free (SCM_CHARS (ptr));
2579 return sizeof (scm_array) + SCM_ARRAY_NDIM (ptr) * sizeof (scm_array_dim);
2580}
2581
0f2d19dd
JB
2582void
2583scm_init_unif ()
0f2d19dd 2584{
23a62151
MD
2585 scm_tc16_array = scm_make_smob_type_mfpe ("array", 0,
2586 markra,
2587 freera,
2588 scm_raprin1,
2589 scm_array_equal_p);
0f2d19dd 2590 scm_add_feature ("array");
23a62151 2591#include "unif.x"
0f2d19dd 2592}