arrays.scm 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. ;;; installed-scm-file
  2. ;;;; Copyright (C) 1999, 2001 Free Software Foundation, Inc.
  3. ;;;;
  4. ;;;; This program is free software; you can redistribute it and/or modify
  5. ;;;; it under the terms of the GNU General Public License as published by
  6. ;;;; the Free Software Foundation; either version 2, or (at your option)
  7. ;;;; any later version.
  8. ;;;;
  9. ;;;; This program is distributed in the hope that it will be useful,
  10. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;;;; GNU General Public License for more details.
  13. ;;;;
  14. ;;;; You should have received a copy of the GNU General Public License
  15. ;;;; along with this software; see the file COPYING. If not, write to
  16. ;;;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17. ;;;; Boston, MA 02110-1301 USA
  18. ;;;;
  19. (define uniform-vector? array?)
  20. (define make-uniform-vector dimensions->uniform-array)
  21. ;; (define uniform-vector-ref array-ref)
  22. (define (uniform-vector-set! u i o)
  23. (uniform-array-set1! u o i))
  24. (define uniform-vector-fill! array-fill!)
  25. (define uniform-vector-read! uniform-array-read!)
  26. (define uniform-vector-write uniform-array-write)
  27. (define (make-array fill . args)
  28. (dimensions->uniform-array args '() fill))
  29. (define (make-uniform-array prot . args)
  30. (dimensions->uniform-array args prot))
  31. (define (list->array ndim lst)
  32. (list->uniform-array ndim '() lst))
  33. (define (list->uniform-vector prot lst)
  34. (list->uniform-array 1 prot lst))
  35. (define (array-shape a)
  36. (map (lambda (ind) (if (number? ind) (list 0 (+ -1 ind)) ind))
  37. (array-dimensions a)))
  38. (let ((make-array-proc (lambda (template)
  39. (lambda (c port)
  40. (read:uniform-vector template port)))))
  41. (for-each (lambda (char template)
  42. (read-hash-extend char
  43. (make-array-proc template)))
  44. '(#\a #\u #\e #\s #\i #\c #\y #\h #\l)
  45. '(#\a 1 -1 1.0 1/3 0+i #\nul s l)))
  46. (let ((array-proc (lambda (c port)
  47. (read:array c port))))
  48. (for-each (lambda (char) (read-hash-extend char array-proc))
  49. '(#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9)))
  50. (define (read:array digit port)
  51. (define chr0 (char->integer #\0))
  52. (let ((rank (let readnum ((val (- (char->integer digit) chr0)))
  53. (if (char-numeric? (peek-char port))
  54. (readnum (+ (* 10 val)
  55. (- (char->integer (read-char port)) chr0)))
  56. val)))
  57. (prot (if (eq? #\( (peek-char port))
  58. '()
  59. (let ((c (read-char port)))
  60. (case c ((#\b) #t)
  61. ((#\a) #\a)
  62. ((#\u) 1)
  63. ((#\e) -1)
  64. ((#\s) 1.0)
  65. ((#\i) 1/3)
  66. ((#\c) 0+i)
  67. (else (error "read:array unknown option " c)))))))
  68. (if (eq? (peek-char port) #\()
  69. (list->uniform-array rank prot (read port))
  70. (error "read:array list not found"))))
  71. (define (read:uniform-vector proto port)
  72. (if (eq? #\( (peek-char port))
  73. (list->uniform-array 1 proto (read port))))