1;;; -*- mode: scheme; coding: utf-8; -*-
2;;;
3;;; Copyright (C) 1999, 2001, 2004, 2006, 2017 Free Software Foundation, Inc.
4;;;
5;;; This library is free software; you can redistribute it and/or
6;;; modify it under the terms of the GNU Lesser General Public
7;;; License as published by the Free Software Foundation; either
8;;; version 3 of the License, or (at your option) any later version.
9;;;
10;;; This library is distributed in the hope that it will be useful,
11;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13;;; Lesser General Public License for more details.
14;;;
15;;; You should have received a copy of the GNU Lesser General Public
16;;; License along with this library; if not, write to the Free Software
17;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
19(define-module (ice-9 arrays)
20  #:use-module (rnrs io ports)
21  #:use-module (srfi srfi-1)
22  #:export (array-copy))
23
24;; This is actually defined in boot-9.scm, apparently for backwards
25;; compatibility.
26
27;; (define (array-shape a)
28;;   (map (lambda (ind) (if (number? ind) (list 0 (+ -1 ind)) ind))
29;;        (array-dimensions a)))
30
31; FIXME writes over the array twice if (array-type) is #t
32(define (array-copy a)
33  (let ((b (apply make-typed-array (array-type a) *unspecified* (array-shape a))))
34    (array-copy! a b)
35    b))
36
37
38;; Printing arrays
39
40;; The dimensions aren't printed out unless they cannot be deduced from
41;; the content, which happens only when certain axes are empty. #:dims?
42;; can be used to force this printing. An array with all the dimensions
43;; printed out is still readable syntax, this can be useful for
44;; truncated-print.
45
46(define* (array-print-prefix a port #:key dims?)
47  (put-char port #\#)
48  (display (array-rank a) port)
49  (let ((t (array-type a)))
50    (unless (eq? #t t)
51      (display t port)))
52  (let ((ss (array-shape a)))
53    (let loop ((s ss) (slos? #f) (szero? #f) (slens? dims?))
54      (define lo caar)
55      (define hi cadar)
56      (if (null? s)
57        (when (or slos? slens?)
58          (pair-for-each (lambda (s)
59                           (when slos?
60                             (put-char port #\@)
61                             (display (lo s) port))
62                           (when slens?
63                             (put-char port #\:)
64                             (display (- (hi s) (lo s) -1) port)))
65                         ss))
66        (let ((zero-size? (zero? (- (hi s) (lo s) -1))))
67          (loop (cdr s)
68                (or slos? (not (zero? (lo s))))
69                (or szero? zero-size?)
70                (or slens? (and (not zero-size?) szero?))))))))
71