srfi-63.scm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. ;; SRFI 63: Homogeneous and Heterogeneous Arrays
  2. (define-syntax enumerate
  3. (syntax-rules ()
  4. ((enumerate name (const val) ...)
  5. (define-syntax name
  6. (syntax-rules (const ...)
  7. ((name const) val) ...)))))
  8. (enumerate a:
  9. (vector 0)
  10. (floc128b 1) (floc64b 2) (floc32b 3) (floc16b 4)
  11. (flor128b 5) (flor64b 6) (flor32b 7) (flor16b 8)
  12. (floq128d 9) (floq64d 10) (floq32d 11)
  13. (fixz64b 12) (fixz32b 13) (fixz16b 14) (fixz8b 15)
  14. (fixn64b 16) (fixn32b 17) (fixn16b 18) (fixn8b 19)
  15. (bool 20)
  16. (string 21))
  17. (define (srfi-4-vector? x)
  18. (or (s8vector? x) (u8vector? x)
  19. (s16vector? x) (u16vector? x)
  20. (s32vector? x) (u32vector? x)
  21. (s64vector? x) (u64vector? x)
  22. (f32vector? x) (f64vector? x)))
  23. ;; This implementation uses SRFI-4 vectors as the store for
  24. ;; several of the homogeneous array types, but several types
  25. ;; are implemented using plain vectors. To improve the
  26. ;; implementation, simply update the appropriate entry in
  27. ;; this table.
  28. (define implementation-list
  29. (let ((ls list))
  30. (ls (ls (a: vector) make-vector vector-ref vector-set!)
  31. (ls (a: floc128b) make-vector vector-ref vector-set!)
  32. (ls (a: floc64b) make-f64vector f64vector-ref f64vector-set!)
  33. (ls (a: floc32b) make-f32vector f32vector-ref f32vector-set!)
  34. (ls (a: floc16b) make-vector vector-ref vector-set!)
  35. (ls (a: flor128b) make-vector vector-ref vector-set!)
  36. (ls (a: flor64b) make-vector vector-ref vector-set!)
  37. (ls (a: flor32b) make-vector vector-ref vector-set!)
  38. (ls (a: flor16b) make-vector vector-ref vector-set!)
  39. (ls (a: floq128d) make-vector vector-ref vector-set!)
  40. (ls (a: floq64d) make-vector vector-ref vector-set!)
  41. (ls (a: floq32d) make-vector vector-ref vector-set!)
  42. (ls (a: fixz64b) make-s64vector s64vector-ref s64vector-set!)
  43. (ls (a: fixz32b) make-s32vector s32vector-ref s32vector-set!)
  44. (ls (a: fixz16b) make-s16vector s16vector-ref s16vector-set!)
  45. (ls (a: fixz8b) make-s8vector s8vector-ref s8vector-set!)
  46. (ls (a: fixn64b) make-u64vector u64vector-ref u64vector-set!)
  47. (ls (a: fixn32b) make-u32vector u32vector-ref u32vector-set!)
  48. (ls (a: fixn16b) make-u16vector u16vector-ref u16vector-set!)
  49. (ls (a: fixn8b) make-u8vector u8vector-ref u8vector-set!)
  50. (ls (a: bool) make-vector vector-ref vector-set!)
  51. (ls (a: string) make-string string-ref string-set!))))
  52. (define (add1 i) (+ i 1))
  53. (define (sub1 i) (- i 1))
  54. (define-record-type strict-array :strict-array
  55. (make-strict-array dimensions scales offset store store-type)
  56. strict-array?
  57. (dimensions strict-array-dimensions)
  58. (scales strict-array-scales)
  59. (offset strict-array-offset)
  60. (store strict-array-store)
  61. (store-type strict-array-store-type))
  62. (define (array-dimensions array)
  63. (if (strict-array? array)
  64. (strict-array-dimensions array)
  65. (list
  66. (cond ((vector? array) (vector-length array))
  67. ((string? array) (string-length array))
  68. ((s8vector? array) (s8vector-length array))
  69. ((u8vector? array) (u8vector-length array))
  70. ((s16vector? array) (s16vector-length array))
  71. ((u16vector? array) (u16vector-length array))
  72. ((s32vector? array) (s32vector-length array))
  73. ((u32vector? array) (u32vector-length array))
  74. ((s64vector? array) (s64vector-length array))
  75. ((u64vector? array) (u64vector-length array))
  76. ((f32vector? array) (f32vector-length array))
  77. ((f64vector? array) (f64vector-length array))))))
  78. (define (array-scales array)
  79. (if (strict-array? array)
  80. (strict-array-scales array)
  81. '(1)))
  82. (define (array-store array)
  83. (if (strict-array? array)
  84. (strict-array-store array)
  85. array))
  86. (define store-makers
  87. (apply vector
  88. (map (lambda (item) (list-ref item 1)) implementation-list)))
  89. (define store-reffers
  90. (apply vector
  91. (map (lambda (item) (list-ref item 2)) implementation-list)))
  92. (define store-setters
  93. (apply vector
  94. (map (lambda (item) (list-ref item 3)) implementation-list)))
  95. (define (array-store-type array)
  96. (if (strict-array? array)
  97. (strict-array-store-type array)
  98. (cond ((string? array) (a: string))
  99. ((vector? array) (a: vector))
  100. ((s8vector? array) (a: fixz8b))
  101. ((u8vector? array) (a: fixn8b))
  102. ((s16vector? array) (a: fixz16b))
  103. ((u16vector? array) (a: fixn16b))
  104. ((s32vector? array) (a: fixz32b))
  105. ((u32vector? array) (a: fixn32b))
  106. ((s64vector? array) (a: fixz64b))
  107. ((u64vector? array) (a: fixn64b))
  108. ((f32vector? array) (a: floc32b))
  109. ((f64vector? array) (a: floc64b)))))
  110. (define (array-store-ref array)
  111. (vector-ref store-reffers (array-store-type array)))
  112. (define (array-store-set array)
  113. (vector-ref store-setters (array-store-type array)))
  114. (define (array-store-maker array-type)
  115. (vector-ref store-makers array-type))
  116. (define (array-offset array)
  117. (if (strict-array? array)
  118. (strict-array-offset array)
  119. 0))
  120. (define (array? obj)
  121. (or (strict-array? obj)
  122. (string? obj)
  123. (vector? obj)
  124. (srfi-4-vector? obj)))
  125. (define (equal? obj1 obj2)
  126. (cond ((eqv? obj1 obj2) #t)
  127. ((pair? obj1)
  128. (and (pair? obj2)
  129. (equal? (car obj1) (car obj2))
  130. (equal? (cdr obj1) (cdr obj2))))
  131. ;; The logic in these two cases were rearranged from s48's
  132. ;; original equal? to handle things like (equal? "" '#()).
  133. ((and (string? obj1) (string? obj2))
  134. (string=? obj1 obj2))
  135. ((and (vector? obj1) (vector? obj2))
  136. (let ((z (vector-length obj1)))
  137. (and (= z (vector-length obj2))
  138. (let loop ((i 0))
  139. (cond ((= i z) #t)
  140. ((equal? (vector-ref obj1 i) (vector-ref obj2 i))
  141. (loop (add1 i)))
  142. (else #f))))))
  143. ((array? obj1)
  144. (and (array? obj2)
  145. (equal? (array-dimensions obj1)
  146. (array-dimensions obj2))
  147. (equal? (array->vector obj1)
  148. (array->vector obj2))))
  149. (else #f)))
  150. (define (array-rank x)
  151. (if (array? x)
  152. (length (array-dimensions x))
  153. 0))
  154. (define (make-array prototype . dimensions)
  155. (let ((prot (array-store prototype))
  156. (pdims (array-dimensions prototype))
  157. (onedim? (eqv? 1 (length dimensions)))
  158. (tcnt (apply * dimensions)))
  159. (let ((initializer
  160. (if (zero? (apply * pdims)) '()
  161. (list ;; a list with single element at origin
  162. (apply array-ref prototype
  163. (map (lambda (x) 0) pdims))))))
  164. (if (and onedim? (or (string? prot) (srfi-4-vector? prot)))
  165. (apply (array-store-maker (array-store-type prot))
  166. (car dimensions) initializer)
  167. (let* ((store-type (array-store-type prototype))
  168. (store (apply (array-store-maker store-type)
  169. tcnt initializer)))
  170. (let loop ((dims (reverse dimensions)) (scales '(1)))
  171. (if (null? dims)
  172. (make-strict-array dimensions (cdr scales) 0
  173. store
  174. store-type)
  175. (loop (cdr dims)
  176. (cons (* (car dims) (car scales)) scales)))))))))
  177. (define (make-shared-array array mapper . dimensions)
  178. (define odl (array-scales array))
  179. (define rank (length dimensions))
  180. (define shape
  181. (map (lambda (dim) (if (list? dim) dim (list 0 (sub1 dim)))) dimensions))
  182. (do ((idx (sub1 rank) (sub1 idx))
  183. (uvt (if (zero? rank)
  184. '()
  185. (append (cdr (vector->list (make-vector rank 0))) '(1)))
  186. (append (cdr uvt) '(0)))
  187. (uvts '() (cons uvt uvts)))
  188. ((negative? idx)
  189. (let ((ker0 (apply + (map * odl (apply mapper uvt)))))
  190. (make-strict-array
  191. (map (lambda (dim) (add1 (- (cadr dim) (car dim)))) shape)
  192. (map (lambda (uvt) (- (apply + (map * odl (apply mapper uvt))) ker0))
  193. uvts)
  194. (apply +
  195. (array-offset array)
  196. (map * odl (apply mapper (map car shape))))
  197. (array-store array)
  198. (array-store-type array))))))
  199. (define (list->array rank proto lst)
  200. (define dimensions
  201. (do ((shp '() (cons (length row) shp))
  202. (row lst (car lst))
  203. (rnk (sub1 rank) (sub1 rnk)))
  204. ((negative? rnk) (reverse shp))))
  205. (let ((nra (apply make-array proto dimensions)))
  206. (define (l2ra dims idxs row)
  207. (cond ((null? dims)
  208. (apply array-set! nra row (reverse idxs)))
  209. (;; ERROR CHECKING
  210. (if (not (eqv? (car dims) (length row)))
  211. (assertion-violation 'list->array "non-rectangular array"
  212. dims dimensions))
  213. (do ((idx 0 (add1 idx))
  214. (row row (cdr row)))
  215. ((>= idx (car dims)))
  216. (l2ra (cdr dims) (cons idx idxs) (car row))))))
  217. (l2ra dimensions '() lst)
  218. nra))
  219. (define (array->list ra)
  220. (define (ra2l dims idxs)
  221. (if (null? dims)
  222. (apply array-ref ra (reverse idxs))
  223. (do ((lst '() (cons (ra2l (cdr dims) (cons idx idxs)) lst))
  224. (idx (sub1 (car dims)) (sub1 idx)))
  225. ((negative? idx) lst))))
  226. (ra2l (array-dimensions ra) '()))
  227. (define (vector->array vect prototype . dimensions)
  228. (let ((vdx (vector-length vect))
  229. (ra (apply make-array prototype dimensions)))
  230. (define (v2ra dims idxs)
  231. (cond ((null? dims)
  232. (set! vdx (sub1 vdx))
  233. (apply array-set! ra (vector-ref vect vdx) (reverse idxs)))
  234. (else
  235. (do ((idx (sub1 (car dims)) (sub1 idx)))
  236. ((negative? idx) vect)
  237. (v2ra (cdr dims) (cons idx idxs))))))
  238. (v2ra dimensions '())
  239. ra))
  240. (define (array->vector ra)
  241. (define dims (array-dimensions ra))
  242. (let* ((vdx (apply * dims))
  243. (vect (make-vector vdx)))
  244. (define (ra2v dims idxs)
  245. (if (null? dims)
  246. (let ((val (apply array-ref ra (reverse idxs))))
  247. (set! vdx (sub1 vdx))
  248. (vector-set! vect vdx val))
  249. (do ((idx (sub1 (car dims)) (sub1 idx)))
  250. ((negative? idx) vect)
  251. (ra2v (cdr dims) (cons idx idxs)))))
  252. (ra2v dims '())
  253. vect))
  254. (define (array-in-bounds? array . indices)
  255. (do ((bnds (array-dimensions array) (cdr bnds))
  256. (idxs indices (cdr idxs)))
  257. ((or (null? bnds)
  258. (null? idxs)
  259. (not (integer? (car idxs)))
  260. (not (< -1 (car idxs) (car bnds))))
  261. (and (null? bnds) (null? idxs)))))
  262. (define (array-ref array . indices)
  263. ((array-store-ref array)
  264. (array-store array)
  265. (apply + (array-offset array) (map * (array-scales array) indices))))
  266. (define (array-set! array obj . indices)
  267. ((array-store-set array)
  268. (array-store array)
  269. (apply + (array-offset array) (map * (array-scales array) indices))
  270. obj))
  271. (define (tag-maker array-type)
  272. (case-lambda
  273. (() (make-strict-array
  274. '(0) '(1) 0
  275. ((array-store-maker array-type) 0)
  276. array-type))
  277. ((x) (make-strict-array
  278. '(1) '(1) 0
  279. ((array-store-maker array-type) 1 x)
  280. array-type))))
  281. (define a:floc128b (tag-maker (a: floc128b)))
  282. (define a:floc16b (tag-maker (a: floc16b)))
  283. (define a:flor128b (tag-maker (a: flor128b)))
  284. (define a:flor64b (tag-maker (a: flor64b)))
  285. (define a:flor32b (tag-maker (a: flor32b)))
  286. (define a:flor16b (tag-maker (a: flor16b)))
  287. (define a:floq128d (tag-maker (a: floq128d)))
  288. (define a:floq64d (tag-maker (a: floq64d)))
  289. (define a:floq32d (tag-maker (a: floq32d)))
  290. (define a:bool (tag-maker (a: bool)))
  291. (define (srfi-4-proto f)
  292. (case-lambda
  293. (() (f))
  294. ((x) (f x))))
  295. (define a:floc64b (srfi-4-proto f64vector))
  296. (define a:floc32b (srfi-4-proto f32vector))
  297. (define a:fixz64b (srfi-4-proto s64vector))
  298. (define a:fixz32b (srfi-4-proto s32vector))
  299. (define a:fixz16b (srfi-4-proto s16vector))
  300. (define a:fixz8b (srfi-4-proto s8vector))
  301. (define a:fixn64b (srfi-4-proto u64vector))
  302. (define a:fixn32b (srfi-4-proto u32vector))
  303. (define a:fixn16b (srfi-4-proto u16vector))
  304. (define a:fixn8b (srfi-4-proto u8vector))