arrays.scm 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. (define-module (ice-9 arrays)
  19. #:use-module (rnrs io ports)
  20. #:use-module (srfi srfi-1)
  21. #:export (array-copy))
  22. ;; This is actually defined in boot-9.scm, apparently for backwards
  23. ;; compatibility.
  24. ;; (define (array-shape a)
  25. ;; (map (lambda (ind) (if (number? ind) (list 0 (+ -1 ind)) ind))
  26. ;; (array-dimensions a)))
  27. ; FIXME writes over the array twice if (array-type) is #t
  28. (define (array-copy a)
  29. (let ((b (apply make-typed-array (array-type a) *unspecified* (array-shape a))))
  30. (array-copy! a b)
  31. b))
  32. ;; Printing arrays
  33. ;; The dimensions aren't printed out unless they cannot be deduced from
  34. ;; the content, which happens only when certain axes are empty. #:dims?
  35. ;; can be used to force this printing. An array with all the dimensions
  36. ;; printed out is still readable syntax, this can be useful for
  37. ;; truncated-print.
  38. (define* (array-print-prefix a port #:key dims?)
  39. (put-char port #\#)
  40. (display (array-rank a) port)
  41. (let ((t (array-type a)))
  42. (unless (eq? #t t)
  43. (display t port)))
  44. (let ((ss (array-shape a)))
  45. (let loop ((s ss) (slos? #f) (szero? #f) (slens? dims?))
  46. (define lo caar)
  47. (define hi cadar)
  48. (if (null? s)
  49. (when (or slos? slens?)
  50. (pair-for-each (lambda (s)
  51. (when slos?
  52. (put-char port #\@)
  53. (display (lo s) port))
  54. (when slens?
  55. (put-char port #\:)
  56. (display (- (hi s) (lo s) -1) port)))
  57. ss))
  58. (let ((zero-size? (zero? (- (hi s) (lo s) -1))))
  59. (loop (cdr s)
  60. (or slos? (not (zero? (lo s))))
  61. (or szero? zero-size?)
  62. (or slens? (and (not zero-size?) szero?))))))))