25.ix-ctor.upstream.scm 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. ;;; Copyright (C) Jussi Piitulainen (2001). All Rights Reserved.
  2. ;;; Permission is hereby granted, free of charge, to any person obtaining a copy
  3. ;;; of this software and associated documentation files (the "Software"), to
  4. ;;; deal in the Software without restriction, including without limitation the
  5. ;;; rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  6. ;;; sell copies of the Software, and to permit persons to whom the Software is
  7. ;;; furnished to do so, subject to the following conditions:
  8. ;;; The above copyright notice and this permission notice shall be included in
  9. ;;; all copies or substantial portions of the Software.
  10. ;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  11. ;;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  12. ;;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  13. ;;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  14. ;;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  15. ;;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  16. ;;; IN THE SOFTWARE.
  17. (define (array-ref a . xs)
  18. (or (array:array? a)
  19. (error "not an array"))
  20. (let ((shape (array:shape a)))
  21. (if (null? xs)
  22. (array:check-indices "array-ref" xs shape)
  23. (let ((x (car xs)))
  24. (if (vector? x)
  25. (array:check-index-vector "array-ref" x shape)
  26. (if (integer? x)
  27. (array:check-indices "array-ref" xs shape)
  28. (if (array:array? x)
  29. (array:check-index-actor "array-ref" x shape)
  30. (error "not an index object"))))))
  31. (vector-ref
  32. (array:vector a)
  33. (if (null? xs)
  34. (vector-ref (array:index a) 0)
  35. (let ((x (car xs)))
  36. (if (vector? x)
  37. (array:index/vector
  38. (quotient (vector-length shape) 2)
  39. (array:index a)
  40. x)
  41. (if (integer? x)
  42. (array:vector-index (array:index a) xs)
  43. (if (array:array? x)
  44. (array:index/array
  45. (quotient (vector-length shape) 2)
  46. (array:index a)
  47. (array:vector x)
  48. (array:index x))
  49. (error "array-ref: bad index object")))))))))
  50. (define (array-set! a x . xs)
  51. (or (array:array? a)
  52. (error "array-set!: not an array"))
  53. (let ((shape (array:shape a)))
  54. (if (null? xs)
  55. (array:check-indices "array-set!" '() shape)
  56. (if (vector? x)
  57. (array:check-index-vector "array-set!" x shape)
  58. (if (integer? x)
  59. (array:check-indices.o "array-set!" (cons x xs) shape)
  60. (if (array:array? x)
  61. (array:check-index-actor "array-set!" x shape)
  62. (error "not an index object")))))
  63. (if (null? xs)
  64. (vector-set! (array:vector a) (vector-ref (array:index a) 0) x)
  65. (if (vector? x)
  66. (vector-set! (array:vector a)
  67. (array:index/vector
  68. (quotient (vector-length shape) 2)
  69. (array:index a)
  70. x)
  71. (car xs))
  72. (if (integer? x)
  73. (let ((v (array:vector a))
  74. (i (array:index a))
  75. (r (quotient (vector-length shape) 2)))
  76. (do ((sum (* (vector-ref i 0) x)
  77. (+ sum (* (vector-ref i k) (car ks))))
  78. (ks xs (cdr ks))
  79. (k 1 (+ k 1)))
  80. ((= k r)
  81. (vector-set! v (+ sum (vector-ref i k)) (car ks)))))
  82. (if (array:array? x)
  83. (vector-set! (array:vector a)
  84. (array:index/array
  85. (quotient (vector-length shape) 2)
  86. (array:index a)
  87. (array:vector x)
  88. (array:index x))
  89. (car xs))
  90. (error (string-append
  91. "array-set!: bad index object: "
  92. (array:thing->string x)))))))))