srfi-63.scm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. ;; SRFI 63: Homogeneous and Heterogeneous Arrays
  2. ; Copyright (C) 2005 Aubrey Jaffer
  3. ;
  4. ; Permission is hereby granted, free of charge, to any person
  5. ; obtaining a copy of this software and associated documentation files
  6. ; (the "Software"), to deal in the Software without restriction,
  7. ; including without limitation the rights to use, copy, modify, merge,
  8. ; publish, distribute, sublicense, and/or sell copies of the Software,
  9. ; and to permit persons to whom the Software is furnished to do so,
  10. ; subject to the following conditions:
  11. ;
  12. ; The above copyright notice and this permission notice shall be
  13. ; included in all copies or substantial portions of the Software.
  14. ;
  15. ; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. ; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. ; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. ; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  19. ; BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  20. ; ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. ; CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. ; SOFTWARE.
  23. (define-syntax enumerate
  24. (syntax-rules ()
  25. ((enumerate name (const val) ...)
  26. (define-syntax name
  27. (syntax-rules (const ...)
  28. ((name const) val) ...)))))
  29. (enumerate a:
  30. (vector 0)
  31. (floc128b 1) (floc64b 2) (floc32b 3) (floc16b 4)
  32. (flor128b 5) (flor64b 6) (flor32b 7) (flor16b 8)
  33. (floq128d 9) (floq64d 10) (floq32d 11)
  34. (fixz64b 12) (fixz32b 13) (fixz16b 14) (fixz8b 15)
  35. (fixn64b 16) (fixn32b 17) (fixn16b 18) (fixn8b 19)
  36. (bool 20)
  37. (string 21))
  38. (define (srfi-4-vector? x)
  39. (or (s8vector? x) (u8vector? x)
  40. (s16vector? x) (u16vector? x)
  41. (s32vector? x) (u32vector? x)
  42. (s64vector? x) (u64vector? x)
  43. (f32vector? x) (f64vector? x)))
  44. ;; This implementation uses SRFI-4 vectors as the store for
  45. ;; several of the homogeneous array types, but several types
  46. ;; are implemented using plain vectors. To improve the
  47. ;; implementation, simply update the appropriate entry in
  48. ;; this table.
  49. (define implementation-list
  50. (let ((ls list))
  51. (ls (ls (a: vector) make-vector vector-ref vector-set!)
  52. (ls (a: floc128b) make-vector vector-ref vector-set!)
  53. (ls (a: floc64b) make-f64vector f64vector-ref f64vector-set!)
  54. (ls (a: floc32b) make-f32vector f32vector-ref f32vector-set!)
  55. (ls (a: floc16b) make-vector vector-ref vector-set!)
  56. (ls (a: flor128b) make-vector vector-ref vector-set!)
  57. (ls (a: flor64b) make-vector vector-ref vector-set!)
  58. (ls (a: flor32b) make-vector vector-ref vector-set!)
  59. (ls (a: flor16b) make-vector vector-ref vector-set!)
  60. (ls (a: floq128d) make-vector vector-ref vector-set!)
  61. (ls (a: floq64d) make-vector vector-ref vector-set!)
  62. (ls (a: floq32d) make-vector vector-ref vector-set!)
  63. (ls (a: fixz64b) make-s64vector s64vector-ref s64vector-set!)
  64. (ls (a: fixz32b) make-s32vector s32vector-ref s32vector-set!)
  65. (ls (a: fixz16b) make-s16vector s16vector-ref s16vector-set!)
  66. (ls (a: fixz8b) make-s8vector s8vector-ref s8vector-set!)
  67. (ls (a: fixn64b) make-u64vector u64vector-ref u64vector-set!)
  68. (ls (a: fixn32b) make-u32vector u32vector-ref u32vector-set!)
  69. (ls (a: fixn16b) make-u16vector u16vector-ref u16vector-set!)
  70. (ls (a: fixn8b) make-u8vector u8vector-ref u8vector-set!)
  71. (ls (a: bool) make-vector vector-ref vector-set!)
  72. (ls (a: string) make-string string-ref string-set!))))
  73. (define (add1 i) (+ i 1))
  74. (define (sub1 i) (- i 1))
  75. (define-record-type strict-array :strict-array
  76. (make-strict-array dimensions scales offset store store-type)
  77. strict-array?
  78. (dimensions strict-array-dimensions)
  79. (scales strict-array-scales)
  80. (offset strict-array-offset)
  81. (store strict-array-store)
  82. (store-type strict-array-store-type))
  83. (define (array-dimensions array)
  84. (if (strict-array? array)
  85. (strict-array-dimensions array)
  86. (list
  87. (cond ((vector? array) (vector-length array))
  88. ((string? array) (string-length array))
  89. ((s8vector? array) (s8vector-length array))
  90. ((u8vector? array) (u8vector-length array))
  91. ((s16vector? array) (s16vector-length array))
  92. ((u16vector? array) (u16vector-length array))
  93. ((s32vector? array) (s32vector-length array))
  94. ((u32vector? array) (u32vector-length array))
  95. ((s64vector? array) (s64vector-length array))
  96. ((u64vector? array) (u64vector-length array))
  97. ((f32vector? array) (f32vector-length array))
  98. ((f64vector? array) (f64vector-length array))))))
  99. (define (array-scales array)
  100. (if (strict-array? array)
  101. (strict-array-scales array)
  102. '(1)))
  103. (define (array-store array)
  104. (if (strict-array? array)
  105. (strict-array-store array)
  106. array))
  107. (define store-makers
  108. (apply vector
  109. (map (lambda (item) (list-ref item 1)) implementation-list)))
  110. (define store-reffers
  111. (apply vector
  112. (map (lambda (item) (list-ref item 2)) implementation-list)))
  113. (define store-setters
  114. (apply vector
  115. (map (lambda (item) (list-ref item 3)) implementation-list)))
  116. (define (array-store-type array)
  117. (if (strict-array? array)
  118. (strict-array-store-type array)
  119. (cond ((string? array) (a: string))
  120. ((vector? array) (a: vector))
  121. ((s8vector? array) (a: fixz8b))
  122. ((u8vector? array) (a: fixn8b))
  123. ((s16vector? array) (a: fixz16b))
  124. ((u16vector? array) (a: fixn16b))
  125. ((s32vector? array) (a: fixz32b))
  126. ((u32vector? array) (a: fixn32b))
  127. ((s64vector? array) (a: fixz64b))
  128. ((u64vector? array) (a: fixn64b))
  129. ((f32vector? array) (a: floc32b))
  130. ((f64vector? array) (a: floc64b)))))
  131. (define (array-store-ref array)
  132. (vector-ref store-reffers (array-store-type array)))
  133. (define (array-store-set array)
  134. (vector-ref store-setters (array-store-type array)))
  135. (define (array-store-maker array-type)
  136. (vector-ref store-makers array-type))
  137. (define (array-offset array)
  138. (if (strict-array? array)
  139. (strict-array-offset array)
  140. 0))
  141. (define (array? obj)
  142. (or (strict-array? obj)
  143. (string? obj)
  144. (vector? obj)
  145. (srfi-4-vector? obj)))
  146. (define (equal? obj1 obj2)
  147. (cond ((eqv? obj1 obj2) #t)
  148. ((pair? obj1)
  149. (and (pair? obj2)
  150. (equal? (car obj1) (car obj2))
  151. (equal? (cdr obj1) (cdr obj2))))
  152. ;; The logic in these two cases were rearranged from s48's
  153. ;; original equal? to handle things like (equal? "" '#()).
  154. ((and (string? obj1) (string? obj2))
  155. (string=? obj1 obj2))
  156. ((and (vector? obj1) (vector? obj2))
  157. (let ((z (vector-length obj1)))
  158. (and (= z (vector-length obj2))
  159. (let loop ((i 0))
  160. (cond ((= i z) #t)
  161. ((equal? (vector-ref obj1 i) (vector-ref obj2 i))
  162. (loop (add1 i)))
  163. (else #f))))))
  164. ((array? obj1)
  165. (and (array? obj2)
  166. (equal? (array-dimensions obj1)
  167. (array-dimensions obj2))
  168. (equal? (array->vector obj1)
  169. (array->vector obj2))))
  170. (else #f)))
  171. (define (array-rank x)
  172. (if (array? x)
  173. (length (array-dimensions x))
  174. 0))
  175. (define (make-array prototype . dimensions)
  176. (let ((prot (array-store prototype))
  177. (pdims (array-dimensions prototype))
  178. (onedim? (eqv? 1 (length dimensions)))
  179. (tcnt (apply * dimensions)))
  180. (let ((initializer
  181. (if (zero? (apply * pdims)) '()
  182. (list ;; a list with single element at origin
  183. (apply array-ref prototype
  184. (map (lambda (x) 0) pdims))))))
  185. (if (and onedim? (or (string? prot) (srfi-4-vector? prot)))
  186. (apply (array-store-maker (array-store-type prot))
  187. (car dimensions) initializer)
  188. (let* ((store-type (array-store-type prototype))
  189. (store (apply (array-store-maker store-type)
  190. tcnt initializer)))
  191. (let loop ((dims (reverse dimensions)) (scales '(1)))
  192. (if (null? dims)
  193. (make-strict-array dimensions (cdr scales) 0
  194. store
  195. store-type)
  196. (loop (cdr dims)
  197. (cons (* (car dims) (car scales)) scales)))))))))
  198. (define (make-shared-array array mapper . dimensions)
  199. (define odl (array-scales array))
  200. (define rank (length dimensions))
  201. (define shape
  202. (map (lambda (dim) (if (list? dim) dim (list 0 (sub1 dim)))) dimensions))
  203. (do ((idx (sub1 rank) (sub1 idx))
  204. (uvt (if (zero? rank)
  205. '()
  206. (append (cdr (vector->list (make-vector rank 0))) '(1)))
  207. (append (cdr uvt) '(0)))
  208. (uvts '() (cons uvt uvts)))
  209. ((negative? idx)
  210. (let ((ker0 (apply + (map * odl (apply mapper uvt)))))
  211. (make-strict-array
  212. (map (lambda (dim) (add1 (- (cadr dim) (car dim)))) shape)
  213. (map (lambda (uvt) (- (apply + (map * odl (apply mapper uvt))) ker0))
  214. uvts)
  215. (apply +
  216. (array-offset array)
  217. (map * odl (apply mapper (map car shape))))
  218. (array-store array)
  219. (array-store-type array))))))
  220. (define (list->array rank proto lst)
  221. (define dimensions
  222. (do ((shp '() (cons (length row) shp))
  223. (row lst (car lst))
  224. (rnk (sub1 rank) (sub1 rnk)))
  225. ((negative? rnk) (reverse shp))))
  226. (let ((nra (apply make-array proto dimensions)))
  227. (define (l2ra dims idxs row)
  228. (cond ((null? dims)
  229. (apply array-set! nra row (reverse idxs)))
  230. (;; ERROR CHECKING
  231. (if (not (eqv? (car dims) (length row)))
  232. (assertion-violation 'list->array "non-rectangular array"
  233. dims dimensions))
  234. (do ((idx 0 (add1 idx))
  235. (row row (cdr row)))
  236. ((>= idx (car dims)))
  237. (l2ra (cdr dims) (cons idx idxs) (car row))))))
  238. (l2ra dimensions '() lst)
  239. nra))
  240. (define (array->list ra)
  241. (define (ra2l dims idxs)
  242. (if (null? dims)
  243. (apply array-ref ra (reverse idxs))
  244. (do ((lst '() (cons (ra2l (cdr dims) (cons idx idxs)) lst))
  245. (idx (sub1 (car dims)) (sub1 idx)))
  246. ((negative? idx) lst))))
  247. (ra2l (array-dimensions ra) '()))
  248. (define (vector->array vect prototype . dimensions)
  249. (let ((vdx (vector-length vect))
  250. (ra (apply make-array prototype dimensions)))
  251. (define (v2ra dims idxs)
  252. (cond ((null? dims)
  253. (set! vdx (sub1 vdx))
  254. (apply array-set! ra (vector-ref vect vdx) (reverse idxs)))
  255. (else
  256. (do ((idx (sub1 (car dims)) (sub1 idx)))
  257. ((negative? idx) vect)
  258. (v2ra (cdr dims) (cons idx idxs))))))
  259. (v2ra dimensions '())
  260. ra))
  261. (define (array->vector ra)
  262. (define dims (array-dimensions ra))
  263. (let* ((vdx (apply * dims))
  264. (vect (make-vector vdx)))
  265. (define (ra2v dims idxs)
  266. (if (null? dims)
  267. (let ((val (apply array-ref ra (reverse idxs))))
  268. (set! vdx (sub1 vdx))
  269. (vector-set! vect vdx val))
  270. (do ((idx (sub1 (car dims)) (sub1 idx)))
  271. ((negative? idx) vect)
  272. (ra2v (cdr dims) (cons idx idxs)))))
  273. (ra2v dims '())
  274. vect))
  275. (define (array-in-bounds? array . indices)
  276. (do ((bnds (array-dimensions array) (cdr bnds))
  277. (idxs indices (cdr idxs)))
  278. ((or (null? bnds)
  279. (null? idxs)
  280. (not (integer? (car idxs)))
  281. (not (< -1 (car idxs) (car bnds))))
  282. (and (null? bnds) (null? idxs)))))
  283. (define (array-ref array . indices)
  284. ((array-store-ref array)
  285. (array-store array)
  286. (apply + (array-offset array) (map * (array-scales array) indices))))
  287. (define (array-set! array obj . indices)
  288. ((array-store-set array)
  289. (array-store array)
  290. (apply + (array-offset array) (map * (array-scales array) indices))
  291. obj))
  292. (define (tag-maker array-type)
  293. (case-lambda
  294. (() (make-strict-array
  295. '(0) '(1) 0
  296. ((array-store-maker array-type) 0)
  297. array-type))
  298. ((x) (make-strict-array
  299. '(1) '(1) 0
  300. ((array-store-maker array-type) 1 x)
  301. array-type))))
  302. (define a:floc128b (tag-maker (a: floc128b)))
  303. (define a:floc16b (tag-maker (a: floc16b)))
  304. (define a:flor128b (tag-maker (a: flor128b)))
  305. (define a:flor64b (tag-maker (a: flor64b)))
  306. (define a:flor32b (tag-maker (a: flor32b)))
  307. (define a:flor16b (tag-maker (a: flor16b)))
  308. (define a:floq128d (tag-maker (a: floq128d)))
  309. (define a:floq64d (tag-maker (a: floq64d)))
  310. (define a:floq32d (tag-maker (a: floq32d)))
  311. (define a:bool (tag-maker (a: bool)))
  312. (define (srfi-4-proto f)
  313. (case-lambda
  314. (() (f))
  315. ((x) (f x))))
  316. (define a:floc64b (srfi-4-proto f64vector))
  317. (define a:floc32b (srfi-4-proto f32vector))
  318. (define a:fixz64b (srfi-4-proto s64vector))
  319. (define a:fixz32b (srfi-4-proto s32vector))
  320. (define a:fixz16b (srfi-4-proto s16vector))
  321. (define a:fixz8b (srfi-4-proto s8vector))
  322. (define a:fixn64b (srfi-4-proto u64vector))
  323. (define a:fixn32b (srfi-4-proto u32vector))
  324. (define a:fixn16b (srfi-4-proto u16vector))
  325. (define a:fixn8b (srfi-4-proto u8vector))