1 /* Copyright (C) 1995,1996,1997,1998,2000,2001,2002,2003,2004,2005,
2 * 2006, 2009, 2010, 2011, 2012, 2013, 2014 Free Software Foundation, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public License
6 * as published by the Free Software Foundation; either version 3 of
7 * the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
34 #include "libguile/_scm.h"
35 #include "libguile/__scm.h"
36 #include "libguile/eq.h"
37 #include "libguile/chars.h"
38 #include "libguile/eval.h"
39 #include "libguile/fports.h"
40 #include "libguile/feature.h"
41 #include "libguile/root.h"
42 #include "libguile/strings.h"
43 #include "libguile/srfi-13.h"
44 #include "libguile/srfi-4.h"
45 #include "libguile/vectors.h"
46 #include "libguile/bitvectors.h"
47 #include "libguile/bytevectors.h"
48 #include "libguile/list.h"
49 #include "libguile/dynwind.h"
50 #include "libguile/read.h"
52 #include "libguile/validate.h"
53 #include "libguile/arrays.h"
54 #include "libguile/array-map.h"
55 #include "libguile/generalized-vectors.h"
56 #include "libguile/generalized-arrays.h"
57 #include "libguile/uniform.h"
60 #define SCM_SET_ARRAY_CONTIGUOUS_FLAG(x) \
61 (SCM_SET_CELL_WORD_0 ((x), SCM_CELL_WORD_0 (x) | (SCM_I_ARRAY_FLAG_CONTIGUOUS << 16)))
62 #define SCM_CLR_ARRAY_CONTIGUOUS_FLAG(x) \
63 (SCM_SET_CELL_WORD_0 ((x), SCM_CELL_WORD_0 (x) & ~(SCM_I_ARRAY_FLAG_CONTIGUOUS << 16)))
66 SCM_DEFINE (scm_shared_array_root
, "shared-array-root", 1, 0, 0,
68 "Return the root vector of a shared array.")
69 #define FUNC_NAME s_scm_shared_array_root
71 if (SCM_I_ARRAYP (ra
))
72 return SCM_I_ARRAY_V (ra
);
73 else if (!scm_is_array (ra
))
74 scm_wrong_type_arg_msg (FUNC_NAME
, SCM_ARG1
, ra
, "array");
81 SCM_DEFINE (scm_shared_array_offset
, "shared-array-offset", 1, 0, 0,
83 "Return the root vector index of the first element in the array.")
84 #define FUNC_NAME s_scm_shared_array_offset
86 scm_t_array_handle handle
;
89 scm_array_get_handle (ra
, &handle
);
90 res
= scm_from_size_t (handle
.base
);
91 scm_array_handle_release (&handle
);
97 SCM_DEFINE (scm_shared_array_increments
, "shared-array-increments", 1, 0, 0,
99 "For each dimension, return the distance between elements in the root vector.")
100 #define FUNC_NAME s_scm_shared_array_increments
102 scm_t_array_handle handle
;
107 scm_array_get_handle (ra
, &handle
);
108 k
= scm_array_handle_rank (&handle
);
109 s
= scm_array_handle_dims (&handle
);
111 res
= scm_cons (scm_from_ssize_t (s
[k
].inc
), res
);
112 scm_array_handle_release (&handle
);
117 /* FIXME: to avoid this assumption, fix the accessors in arrays.h,
118 scm_i_make_array, and the array cases in system/vm/assembler.scm. */
120 verify (sizeof (scm_t_array_dim
) == 3*sizeof (scm_t_bits
));
122 /* Matching SCM_I_ARRAY accessors in arrays.h */
124 scm_i_make_array (int ndim
)
126 SCM ra
= scm_words (((scm_t_bits
) ndim
<< 17) + scm_tc7_array
, 3 + ndim
*3);
127 SCM_I_ARRAY_SET_V (ra
, SCM_BOOL_F
);
128 SCM_I_ARRAY_SET_BASE (ra
, 0);
129 /* dimensions are unset */
133 static char s_bad_spec
[] = "Bad scm_array dimension";
136 /* Increments will still need to be set. */
139 scm_i_shap2ra (SCM args
)
143 int ndim
= scm_ilength (args
);
145 scm_misc_error (NULL
, s_bad_spec
, SCM_EOL
);
147 ra
= scm_i_make_array (ndim
);
148 SCM_I_ARRAY_SET_BASE (ra
, 0);
149 s
= SCM_I_ARRAY_DIMS (ra
);
150 for (; !scm_is_null (args
); s
++, args
= SCM_CDR (args
))
152 spec
= SCM_CAR (args
);
153 if (scm_is_integer (spec
))
156 s
->ubnd
= scm_to_ssize_t (spec
);
158 scm_misc_error (NULL
, s_bad_spec
, SCM_EOL
);
163 if (!scm_is_pair (spec
) || !scm_is_integer (SCM_CAR (spec
)))
164 scm_misc_error (NULL
, s_bad_spec
, SCM_EOL
);
165 s
->lbnd
= scm_to_ssize_t (SCM_CAR (spec
));
166 spec
= SCM_CDR (spec
);
167 if (!scm_is_pair (spec
)
168 || !scm_is_integer (SCM_CAR (spec
))
169 || !scm_is_null (SCM_CDR (spec
)))
170 scm_misc_error (NULL
, s_bad_spec
, SCM_EOL
);
171 s
->ubnd
= scm_to_ssize_t (SCM_CAR (spec
));
172 if (s
->ubnd
- s
->lbnd
< -1)
173 scm_misc_error (NULL
, s_bad_spec
, SCM_EOL
);
180 SCM_DEFINE (scm_make_typed_array
, "make-typed-array", 2, 0, 1,
181 (SCM type
, SCM fill
, SCM bounds
),
182 "Create and return an array of type @var{type}.")
183 #define FUNC_NAME s_scm_make_typed_array
189 ra
= scm_i_shap2ra (bounds
);
190 SCM_SET_ARRAY_CONTIGUOUS_FLAG (ra
);
191 s
= SCM_I_ARRAY_DIMS (ra
);
192 k
= SCM_I_ARRAY_NDIM (ra
);
197 SCM_ASSERT_RANGE (1, bounds
, s
[k
].lbnd
<= s
[k
].ubnd
+ 1);
198 rlen
= (s
[k
].ubnd
- s
[k
].lbnd
+ 1) * s
[k
].inc
;
201 if (scm_is_eq (fill
, SCM_UNSPECIFIED
))
202 fill
= SCM_UNDEFINED
;
204 SCM_I_ARRAY_SET_V (ra
, scm_make_generalized_vector (type
, scm_from_size_t (rlen
), fill
));
206 if (1 == SCM_I_ARRAY_NDIM (ra
) && 0 == SCM_I_ARRAY_BASE (ra
))
208 return SCM_I_ARRAY_V (ra
);
215 scm_from_contiguous_typed_array (SCM type
, SCM bounds
, const void *bytes
,
217 #define FUNC_NAME "scm_from_contiguous_typed_array"
222 scm_t_array_handle h
;
226 ra
= scm_i_shap2ra (bounds
);
227 SCM_SET_ARRAY_CONTIGUOUS_FLAG (ra
);
228 s
= SCM_I_ARRAY_DIMS (ra
);
229 k
= SCM_I_ARRAY_NDIM (ra
);
234 SCM_ASSERT_RANGE (1, bounds
, s
[k
].lbnd
<= s
[k
].ubnd
+ 1);
235 rlen
= (s
[k
].ubnd
- s
[k
].lbnd
+ 1) * s
[k
].inc
;
237 SCM_I_ARRAY_SET_V (ra
, scm_make_generalized_vector (type
, scm_from_size_t (rlen
), SCM_UNDEFINED
));
240 scm_array_get_handle (ra
, &h
);
241 elts
= h
.writable_elements
;
242 sz
= scm_array_handle_uniform_element_bit_size (&h
);
243 scm_array_handle_release (&h
);
245 if (sz
>= 8 && ((sz
% 8) == 0))
247 if (byte_len
% (sz
/ 8))
248 SCM_MISC_ERROR ("byte length not a multiple of the unit size", SCM_EOL
);
249 if (byte_len
/ (sz
/ 8) != rlen
)
250 SCM_MISC_ERROR ("byte length and dimensions do not match", SCM_EOL
);
254 /* Elements of sub-byte size (bitvectors) are addressed in 32-bit
256 if (byte_len
!= ((rlen
* sz
+ 31) / 32) * 4)
257 SCM_MISC_ERROR ("byte length and dimensions do not match", SCM_EOL
);
260 /* an internal guile error, really */
261 SCM_MISC_ERROR ("uniform elements larger than 8 bits must fill whole bytes", SCM_EOL
);
263 memcpy (elts
, bytes
, byte_len
);
265 if (1 == SCM_I_ARRAY_NDIM (ra
) && 0 == SCM_I_ARRAY_BASE (ra
))
267 return SCM_I_ARRAY_V (ra
);
273 scm_from_contiguous_array (SCM bounds
, const SCM
*elts
, size_t len
)
274 #define FUNC_NAME "scm_from_contiguous_array"
279 scm_t_array_handle h
;
281 ra
= scm_i_shap2ra (bounds
);
282 SCM_SET_ARRAY_CONTIGUOUS_FLAG (ra
);
283 s
= SCM_I_ARRAY_DIMS (ra
);
284 k
= SCM_I_ARRAY_NDIM (ra
);
289 SCM_ASSERT_RANGE (1, bounds
, s
[k
].lbnd
<= s
[k
].ubnd
+ 1);
290 rlen
= (s
[k
].ubnd
- s
[k
].lbnd
+ 1) * s
[k
].inc
;
293 SCM_MISC_ERROR ("element length and dimensions do not match", SCM_EOL
);
295 SCM_I_ARRAY_SET_V (ra
, scm_c_make_vector (rlen
, SCM_UNDEFINED
));
296 scm_array_get_handle (ra
, &h
);
297 memcpy (h
.writable_elements
, elts
, rlen
* sizeof(SCM
));
298 scm_array_handle_release (&h
);
300 if (1 == SCM_I_ARRAY_NDIM (ra
) && 0 == SCM_I_ARRAY_BASE (ra
))
302 return SCM_I_ARRAY_V (ra
);
307 SCM_DEFINE (scm_make_array
, "make-array", 1, 0, 1,
308 (SCM fill
, SCM bounds
),
309 "Create and return an array.")
310 #define FUNC_NAME s_scm_make_array
312 return scm_make_typed_array (SCM_BOOL_T
, fill
, bounds
);
317 scm_i_ra_set_contp (SCM ra
)
319 size_t k
= SCM_I_ARRAY_NDIM (ra
);
322 ssize_t inc
= SCM_I_ARRAY_DIMS (ra
)[k
- 1].inc
;
325 if (inc
!= SCM_I_ARRAY_DIMS (ra
)[k
].inc
)
327 SCM_CLR_ARRAY_CONTIGUOUS_FLAG (ra
);
330 inc
*= (SCM_I_ARRAY_DIMS (ra
)[k
].ubnd
331 - SCM_I_ARRAY_DIMS (ra
)[k
].lbnd
+ 1);
334 SCM_SET_ARRAY_CONTIGUOUS_FLAG (ra
);
338 SCM_DEFINE (scm_make_shared_array
, "make-shared-array", 2, 0, 1,
339 (SCM oldra
, SCM mapfunc
, SCM dims
),
340 "@code{make-shared-array} can be used to create shared subarrays\n"
341 "of other arrays. The @var{mapfunc} is a function that\n"
342 "translates coordinates in the new array into coordinates in the\n"
343 "old array. A @var{mapfunc} must be linear, and its range must\n"
344 "stay within the bounds of the old array, but it can be\n"
345 "otherwise arbitrary. A simple example:\n"
347 "(define fred (make-array #f 8 8))\n"
348 "(define freds-diagonal\n"
349 " (make-shared-array fred (lambda (i) (list i i)) 8))\n"
350 "(array-set! freds-diagonal 'foo 3)\n"
351 "(array-ref fred 3 3) @result{} foo\n"
352 "(define freds-center\n"
353 " (make-shared-array fred (lambda (i j) (list (+ 3 i) (+ 3 j))) 2 2))\n"
354 "(array-ref freds-center 0 0) @result{} foo\n"
356 #define FUNC_NAME s_scm_make_shared_array
358 scm_t_array_handle old_handle
;
364 long old_base
, old_min
, new_min
, old_max
, new_max
;
367 SCM_VALIDATE_REST_ARGUMENT (dims
);
368 SCM_VALIDATE_PROC (2, mapfunc
);
369 ra
= scm_i_shap2ra (dims
);
371 scm_array_get_handle (oldra
, &old_handle
);
373 if (SCM_I_ARRAYP (oldra
))
375 SCM_I_ARRAY_SET_V (ra
, SCM_I_ARRAY_V (oldra
));
376 old_base
= old_min
= old_max
= SCM_I_ARRAY_BASE (oldra
);
377 s
= scm_array_handle_dims (&old_handle
);
378 k
= scm_array_handle_rank (&old_handle
);
382 old_max
+= (s
[k
].ubnd
- s
[k
].lbnd
) * s
[k
].inc
;
384 old_min
+= (s
[k
].ubnd
- s
[k
].lbnd
) * s
[k
].inc
;
389 SCM_I_ARRAY_SET_V (ra
, oldra
);
390 old_base
= old_min
= 0;
391 old_max
= scm_c_array_length (oldra
) - 1;
395 s
= SCM_I_ARRAY_DIMS (ra
);
396 for (k
= 0; k
< SCM_I_ARRAY_NDIM (ra
); k
++)
398 inds
= scm_cons (scm_from_ssize_t (s
[k
].lbnd
), inds
);
399 if (s
[k
].ubnd
< s
[k
].lbnd
)
401 if (1 == SCM_I_ARRAY_NDIM (ra
))
402 ra
= scm_make_generalized_vector (scm_array_type (ra
),
403 SCM_INUM0
, SCM_UNDEFINED
);
405 SCM_I_ARRAY_SET_V (ra
, scm_make_generalized_vector (scm_array_type (ra
),
406 SCM_INUM0
, SCM_UNDEFINED
));
407 scm_array_handle_release (&old_handle
);
412 imap
= scm_apply_0 (mapfunc
, scm_reverse (inds
));
413 i
= scm_array_handle_pos (&old_handle
, imap
);
414 new_min
= new_max
= i
+ old_base
;
415 SCM_I_ARRAY_SET_BASE (ra
, new_min
);
417 k
= SCM_I_ARRAY_NDIM (ra
);
420 if (s
[k
].ubnd
> s
[k
].lbnd
)
422 SCM_SETCAR (indptr
, scm_sum (SCM_CAR (indptr
), scm_from_int (1)));
423 imap
= scm_apply_0 (mapfunc
, scm_reverse (inds
));
424 s
[k
].inc
= scm_array_handle_pos (&old_handle
, imap
) - i
;
427 new_max
+= (s
[k
].ubnd
- s
[k
].lbnd
) * s
[k
].inc
;
429 new_min
+= (s
[k
].ubnd
- s
[k
].lbnd
) * s
[k
].inc
;
432 s
[k
].inc
= new_max
- new_min
+ 1; /* contiguous by default */
433 indptr
= SCM_CDR (indptr
);
436 scm_array_handle_release (&old_handle
);
438 if (old_min
> new_min
|| old_max
< new_max
)
439 SCM_MISC_ERROR ("mapping out of range", SCM_EOL
);
440 if (1 == SCM_I_ARRAY_NDIM (ra
) && 0 == SCM_I_ARRAY_BASE (ra
))
442 SCM v
= SCM_I_ARRAY_V (ra
);
443 size_t length
= scm_c_array_length (v
);
444 if (1 == s
->inc
&& 0 == s
->lbnd
&& length
== 1 + s
->ubnd
)
446 if (s
->ubnd
< s
->lbnd
)
447 return scm_make_generalized_vector (scm_array_type (ra
), SCM_INUM0
,
450 scm_i_ra_set_contp (ra
);
456 /* args are RA . DIMS */
457 SCM_DEFINE (scm_transpose_array
, "transpose-array", 1, 0, 1,
459 "Return an array sharing contents with @var{ra}, but with\n"
460 "dimensions arranged in a different order. There must be one\n"
461 "@var{dim} argument for each dimension of @var{ra}.\n"
462 "@var{dim0}, @var{dim1}, @dots{} should be integers between 0\n"
463 "and the rank of the array to be returned. Each integer in that\n"
464 "range must appear at least once in the argument list.\n"
466 "The values of @var{dim0}, @var{dim1}, @dots{} correspond to\n"
467 "dimensions in the array to be returned, their positions in the\n"
468 "argument list to dimensions of @var{ra}. Several @var{dim}s\n"
469 "may have the same value, in which case the returned array will\n"
470 "have smaller rank than @var{ra}.\n"
473 "(transpose-array '#2((a b) (c d)) 1 0) @result{} #2((a c) (b d))\n"
474 "(transpose-array '#2((a b) (c d)) 0 0) @result{} #1(a d)\n"
475 "(transpose-array '#3(((a b c) (d e f)) ((1 2 3) (4 5 6))) 1 1 0) @result{}\n"
476 " #2((a 4) (b 5) (c 6))\n"
478 #define FUNC_NAME s_scm_transpose_array
481 scm_t_array_dim
*s
, *r
;
484 SCM_VALIDATE_REST_ARGUMENT (args
);
485 SCM_ASSERT (SCM_HEAP_OBJECT_P (ra
), ra
, SCM_ARG1
, FUNC_NAME
);
487 switch (scm_c_array_rank (ra
))
490 if (!scm_is_null (args
))
491 SCM_WRONG_NUM_ARGS ();
494 /* Make sure that we are called with a single zero as
497 if (scm_is_null (args
) || !scm_is_null (SCM_CDR (args
)))
498 SCM_WRONG_NUM_ARGS ();
499 SCM_VALIDATE_INT_COPY (SCM_ARG2
, SCM_CAR (args
), i
);
500 SCM_ASSERT_RANGE (SCM_ARG2
, SCM_CAR (args
), i
== 0);
503 vargs
= scm_vector (args
);
504 if (SCM_SIMPLE_VECTOR_LENGTH (vargs
) != SCM_I_ARRAY_NDIM (ra
))
505 SCM_WRONG_NUM_ARGS ();
507 for (k
= 0; k
< SCM_I_ARRAY_NDIM (ra
); k
++)
509 i
= scm_to_signed_integer (SCM_SIMPLE_VECTOR_REF (vargs
, k
),
510 0, SCM_I_ARRAY_NDIM(ra
));
515 res
= scm_i_make_array (ndim
);
516 SCM_I_ARRAY_SET_V (res
, SCM_I_ARRAY_V (ra
));
517 SCM_I_ARRAY_SET_BASE (res
, SCM_I_ARRAY_BASE (ra
));
520 SCM_I_ARRAY_DIMS (res
)[k
].lbnd
= 0;
521 SCM_I_ARRAY_DIMS (res
)[k
].ubnd
= -1;
523 for (k
= SCM_I_ARRAY_NDIM (ra
); k
--;)
525 i
= scm_to_int (SCM_SIMPLE_VECTOR_REF (vargs
, k
));
526 s
= &(SCM_I_ARRAY_DIMS (ra
)[k
]);
527 r
= &(SCM_I_ARRAY_DIMS (res
)[i
]);
528 if (r
->ubnd
< r
->lbnd
)
537 if (r
->ubnd
> s
->ubnd
)
539 if (r
->lbnd
< s
->lbnd
)
541 SCM_I_ARRAY_SET_BASE (res
, SCM_I_ARRAY_BASE (res
) + (s
->lbnd
- r
->lbnd
) * r
->inc
);
548 SCM_MISC_ERROR ("bad argument list", SCM_EOL
);
549 scm_i_ra_set_contp (res
);
555 /* attempts to unroll an array into a one-dimensional array.
556 returns the unrolled array or #f if it can't be done. */
557 /* if strict is true, return #f if returned array
558 wouldn't have contiguous elements. */
559 SCM_DEFINE (scm_array_contents
, "array-contents", 1, 1, 0,
560 (SCM ra
, SCM strict
),
561 "If @var{ra} may be @dfn{unrolled} into a one dimensional shared\n"
562 "array without changing their order (last subscript changing\n"
563 "fastest), then @code{array-contents} returns that shared array,\n"
564 "otherwise it returns @code{#f}. All arrays made by\n"
565 "@code{make-array} and @code{make-uniform-array} may be unrolled,\n"
566 "some arrays made by @code{make-shared-array} may not be. If\n"
567 "the optional argument @var{strict} is provided, a shared array\n"
568 "will be returned only if its elements are stored internally\n"
569 "contiguous in memory.")
570 #define FUNC_NAME s_scm_array_contents
572 if (!scm_is_array (ra
))
573 scm_wrong_type_arg_msg (NULL
, 0, ra
, "array");
574 else if (SCM_I_ARRAYP (ra
))
577 size_t k
, ndim
= SCM_I_ARRAY_NDIM (ra
), len
= 1;
578 if (!SCM_I_ARRAY_CONTP (ra
))
580 for (k
= 0; k
< ndim
; k
++)
581 len
*= SCM_I_ARRAY_DIMS (ra
)[k
].ubnd
- SCM_I_ARRAY_DIMS (ra
)[k
].lbnd
+ 1;
582 if (!SCM_UNBNDP (strict
) && scm_is_true (strict
))
584 if (ndim
&& (1 != SCM_I_ARRAY_DIMS (ra
)[ndim
- 1].inc
))
586 if (scm_is_bitvector (SCM_I_ARRAY_V (ra
)))
588 if (len
!= scm_c_bitvector_length (SCM_I_ARRAY_V (ra
)) ||
589 SCM_I_ARRAY_BASE (ra
) % SCM_LONG_BIT
||
595 v
= SCM_I_ARRAY_V (ra
);
596 if ((len
== scm_c_array_length (v
)) && (0 == SCM_I_ARRAY_BASE (ra
)))
600 SCM sra
= scm_i_make_array (1);
601 SCM_I_ARRAY_DIMS (sra
)->lbnd
= 0;
602 SCM_I_ARRAY_DIMS (sra
)->ubnd
= len
- 1;
603 SCM_I_ARRAY_SET_V (sra
, v
);
604 SCM_I_ARRAY_SET_BASE (sra
, SCM_I_ARRAY_BASE (ra
));
605 SCM_I_ARRAY_DIMS (sra
)->inc
= (ndim
? SCM_I_ARRAY_DIMS (ra
)[ndim
- 1].inc
: 1);
616 list_to_array (SCM lst
, scm_t_array_handle
*handle
, ssize_t pos
, size_t k
)
618 if (k
== scm_array_handle_rank (handle
))
619 scm_array_handle_set (handle
, pos
, lst
);
622 scm_t_array_dim
*dim
= scm_array_handle_dims (handle
) + k
;
623 ssize_t inc
= dim
->inc
;
624 size_t len
= 1 + dim
->ubnd
- dim
->lbnd
, n
;
628 while (n
> 0 && scm_is_pair (lst
))
630 list_to_array (SCM_CAR (lst
), handle
, pos
, k
+ 1);
636 errmsg
= "too few elements for array dimension ~a, need ~a";
637 if (!scm_is_null (lst
))
638 errmsg
= "too many elements for array dimension ~a, want ~a";
640 scm_misc_error (NULL
, errmsg
, scm_list_2 (scm_from_size_t (k
),
641 scm_from_size_t (len
)));
646 SCM_DEFINE (scm_list_to_typed_array
, "list->typed-array", 3, 0, 0,
647 (SCM type
, SCM shape
, SCM lst
),
648 "Return an array of the type @var{type}\n"
649 "with elements the same as those of @var{lst}.\n"
651 "The argument @var{shape} determines the number of dimensions\n"
652 "of the array and their shape. It is either an exact integer,\n"
654 "number of dimensions directly, or a list whose length\n"
655 "specifies the number of dimensions and each element specified\n"
656 "the lower and optionally the upper bound of the corresponding\n"
658 "When the element is list of two elements, these elements\n"
659 "give the lower and upper bounds. When it is an exact\n"
660 "integer, it gives only the lower bound.")
661 #define FUNC_NAME s_scm_list_to_typed_array
665 scm_t_array_handle handle
;
668 if (scm_is_integer (shape
))
670 size_t k
= scm_to_size_t (shape
);
674 shape
= scm_cons (scm_length (row
), shape
);
675 if (k
> 0 && !scm_is_null (row
))
681 SCM shape_spec
= shape
;
685 SCM spec
= scm_car (shape_spec
);
686 if (scm_is_pair (spec
))
687 shape
= scm_cons (spec
, shape
);
689 shape
= scm_cons (scm_list_2 (spec
,
690 scm_sum (scm_sum (spec
,
694 shape_spec
= scm_cdr (shape_spec
);
695 if (scm_is_pair (shape_spec
))
697 if (!scm_is_null (row
))
705 ra
= scm_make_typed_array (type
, SCM_UNSPECIFIED
,
706 scm_reverse_x (shape
, SCM_EOL
));
708 scm_array_get_handle (ra
, &handle
);
709 list_to_array (lst
, &handle
, 0, 0);
710 scm_array_handle_release (&handle
);
716 SCM_DEFINE (scm_list_to_array
, "list->array", 2, 0, 0,
718 "Return an array with elements the same as those of @var{lst}.")
719 #define FUNC_NAME s_scm_list_to_array
721 return scm_list_to_typed_array (SCM_BOOL_T
, ndim
, lst
);
725 /* Print dimension DIM of ARRAY.
729 scm_i_print_array_dimension (scm_t_array_handle
*h
, int dim
, int pos
,
730 SCM port
, scm_print_state
*pstate
)
733 scm_iprin1 (scm_array_handle_ref (h
, pos
), port
, pstate
);
737 scm_putc_unlocked ('(', port
);
738 for (i
= h
->dims
[dim
].lbnd
; i
<= h
->dims
[dim
].ubnd
;
739 i
++, pos
+= h
->dims
[dim
].inc
)
741 scm_i_print_array_dimension (h
, dim
+1, pos
, port
, pstate
);
742 if (i
< h
->dims
[dim
].ubnd
)
743 scm_putc_unlocked (' ', port
);
745 scm_putc_unlocked (')', port
);
754 scm_i_print_array (SCM array
, SCM port
, scm_print_state
*pstate
)
756 scm_t_array_handle h
;
758 int print_lbnds
= 0, zero_size
= 0, print_lens
= 0;
760 scm_array_get_handle (array
, &h
);
762 scm_putc_unlocked ('#', port
);
763 if (h
.ndims
!= 1 || h
.dims
[0].lbnd
!= 0)
764 scm_intprint (h
.ndims
, 10, port
);
765 if (h
.element_type
!= SCM_ARRAY_ELEMENT_TYPE_SCM
)
766 scm_write (scm_array_handle_element_type (&h
), port
);
768 for (i
= 0; i
< h
.ndims
; i
++)
770 if (h
.dims
[i
].lbnd
!= 0)
772 if (h
.dims
[i
].ubnd
- h
.dims
[i
].lbnd
+ 1 == 0)
778 if (print_lbnds
|| print_lens
)
779 for (i
= 0; i
< h
.ndims
; i
++)
783 scm_putc_unlocked ('@', port
);
784 scm_intprint (h
.dims
[i
].lbnd
, 10, port
);
788 scm_putc_unlocked (':', port
);
789 scm_intprint (h
.dims
[i
].ubnd
- h
.dims
[i
].lbnd
+ 1,
796 /* Rank zero arrays, which are really just scalars, are printed
797 specially. The consequent way would be to print them as
801 where OBJ is the printed representation of the scalar, but we
802 print them instead as
806 to make them look less strange.
808 Just printing them as
812 would be correct in a way as well, but zero rank arrays are
813 not really the same as Scheme values since they are boxed and
814 can be modified with array-set!, say.
816 scm_putc_unlocked ('(', port
);
817 scm_i_print_array_dimension (&h
, 0, 0, port
, pstate
);
818 scm_putc_unlocked (')', port
);
822 return scm_i_print_array_dimension (&h
, 0, 0, port
, pstate
);
828 scm_add_feature ("array");
830 #include "libguile/arrays.x"