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