srfi-25.scm 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381
  1. ;;; array
  2. ;;; 1997 - 2001 Jussi Piitulainen
  3. ;;; --- Intro ---
  4. ;;; This interface to arrays is based on Alan Bawden's array.scm of
  5. ;;; 1993 (earlier version in the Internet Repository and another
  6. ;;; version in SLIB). This is a complete rewrite, to be consistent
  7. ;;; with the rest of Scheme and to make arrays independent of lists.
  8. ;;; Some modifications are due to discussion in srfi-25 mailing list.
  9. ;;; (array? obj)
  10. ;;; (make-array shape [obj]) changed arguments
  11. ;;; (shape bound ...) new
  12. ;;; (array shape obj ...) new
  13. ;;; (array-rank array) changed name back
  14. ;;; (array-start array dimension) new
  15. ;;; (array-end array dimension) new
  16. ;;; (array-ref array k ...)
  17. ;;; (array-ref array index) new variant
  18. ;;; (array-set! array k ... obj) changed argument order
  19. ;;; (array-set! array index obj) new variant
  20. ;;; (share-array array shape proc) changed arguments
  21. ;;; All other variables in this file have names in "array:".
  22. ;;; Should there be a way to make arrays with initial values mapped
  23. ;;; from indices? Sure. The current "initial object" is lame.
  24. ;;;
  25. ;;; Removed (array-shape array) from here. There is a new version
  26. ;;; in arlib though.
  27. ;;; --- Representation type dependencies ---
  28. ;;; The mapping from array indices to the index to the underlying vector
  29. ;;; is whatever array:optimize returns. The file "opt" provides three
  30. ;;; representations:
  31. ;;;
  32. ;;; mbda) mapping is a procedure that allows an optional argument
  33. ;;; tter) mapping is two procedures that takes exactly the indices
  34. ;;; ctor) mapping is a vector of a constant term and coefficients
  35. ;;;
  36. ;;; Choose one in "opt" to make the optimizer. Then choose the matching
  37. ;;; implementation of array-ref and array-set!.
  38. ;;;
  39. ;;; These should be made macros to inline them. Or have a good compiler
  40. ;;; and plant the package as a module.
  41. ;;; 1. Pick an optimizer.
  42. ;;; 2. Pick matching index representation.
  43. ;;; 3. Pick a record implementation; as-procedure is generic; syntax inlines.
  44. ;;; 3. This file is otherwise portable.
  45. ;; Scheme 48 note: We picked the "ctor" representation
  46. (define-record-type array :array
  47. (array:make vec ind shp)
  48. array:array?
  49. (vec array:vector)
  50. (ind array:index)
  51. (shp array:shape))
  52. ;; Contents of ix-ctor.scm
  53. (define (array-ref a . xs)
  54. (or (array:array? a)
  55. (error "not an array"))
  56. (let ((shape (array:shape a)))
  57. (if (null? xs)
  58. (array:check-indices "array-ref" xs shape)
  59. (let ((x (car xs)))
  60. (if (vector? x)
  61. (array:check-index-vector "array-ref" x shape)
  62. (if (integer? x)
  63. (array:check-indices "array-ref" xs shape)
  64. (if (array:array? x)
  65. (array:check-index-actor "array-ref" x shape)
  66. (error "not an index object"))))))
  67. (vector-ref
  68. (array:vector a)
  69. (if (null? xs)
  70. (vector-ref (array:index a) 0)
  71. (let ((x (car xs)))
  72. (if (vector? x)
  73. (array:index/vector
  74. (quotient (vector-length shape) 2)
  75. (array:index a)
  76. x)
  77. (if (integer? x)
  78. (array:vector-index (array:index a) xs)
  79. (if (array:array? x)
  80. (array:index/array
  81. (quotient (vector-length shape) 2)
  82. (array:index a)
  83. (array:vector x)
  84. (array:index x))
  85. (error "array-ref: bad index object")))))))))
  86. (define (array-set! a x . xs)
  87. (or (array:array? a)
  88. (error "array-set!: not an array"))
  89. (let ((shape (array:shape a)))
  90. (if (null? xs)
  91. (array:check-indices "array-set!" '() shape)
  92. (if (vector? x)
  93. (array:check-index-vector "array-set!" x shape)
  94. (if (integer? x)
  95. (array:check-indices.o "array-set!" (cons x xs) shape)
  96. (if (array:array? x)
  97. (array:check-index-actor "array-set!" x shape)
  98. (error "not an index object")))))
  99. (if (null? xs)
  100. (vector-set! (array:vector a) (vector-ref (array:index a) 0) x)
  101. (if (vector? x)
  102. (vector-set! (array:vector a)
  103. (array:index/vector
  104. (quotient (vector-length shape) 2)
  105. (array:index a)
  106. x)
  107. (car xs))
  108. (if (integer? x)
  109. (let ((v (array:vector a))
  110. (i (array:index a))
  111. (r (quotient (vector-length shape) 2)))
  112. (do ((sum (* (vector-ref i 0) x)
  113. (+ sum (* (vector-ref i k) (car ks))))
  114. (ks xs (cdr ks))
  115. (k 1 (+ k 1)))
  116. ((= k r)
  117. (vector-set! v (+ sum (vector-ref i k)) (car ks)))))
  118. (if (array:array? x)
  119. (vector-set! (array:vector a)
  120. (array:index/array
  121. (quotient (vector-length shape) 2)
  122. (array:index a)
  123. (array:vector x)
  124. (array:index x))
  125. (car xs))
  126. (error (string-append
  127. "array-set!: bad index object: "
  128. (array:thing->string x)))))))))
  129. ;; Contents of op-ctor.scm
  130. (begin
  131. (define array:opt-args '(ctor (4)))
  132. (define (array:optimize f r)
  133. (case r
  134. ((0) (let ((n0 (f))) (array:0 n0)))
  135. ((1) (let ((n0 (f 0))) (array:1 n0 (- (f 1) n0))))
  136. ((2)
  137. (let ((n0 (f 0 0)))
  138. (array:2 n0 (- (f 1 0) n0) (- (f 0 1) n0))))
  139. ((3)
  140. (let ((n0 (f 0 0 0)))
  141. (array:3
  142. n0
  143. (- (f 1 0 0) n0)
  144. (- (f 0 1 0) n0)
  145. (- (f 0 0 1) n0))))
  146. (else
  147. (let ((v
  148. (do ((k 0 (+ k 1)) (v '() (cons 0 v)))
  149. ((= k r) v))))
  150. (let ((n0 (apply f v)))
  151. (apply
  152. array:n
  153. n0
  154. (array:coefficients f n0 v v)))))))
  155. (define (array:optimize-empty r)
  156. (let ((x (make-vector (+ r 1) 0)))
  157. (vector-set! x r -1)
  158. x))
  159. (define (array:coefficients f n0 vs vp)
  160. (case vp
  161. ((()) '())
  162. (else
  163. (set-car! vp 1)
  164. (let ((n (- (apply f vs) n0)))
  165. (set-car! vp 0)
  166. (cons n (array:coefficients f n0 vs (cdr vp)))))))
  167. (define (array:vector-index x ks)
  168. (do ((sum 0 (+ sum (* (vector-ref x k) (car ks))))
  169. (ks ks (cdr ks))
  170. (k 0 (+ k 1)))
  171. ((null? ks) (+ sum (vector-ref x k)))))
  172. (define (array:shape-index) '#(2 1 0))
  173. (define (array:empty-shape-index) '#(0 0 -1))
  174. (define (array:shape-vector-index x r k)
  175. (+
  176. (* (vector-ref x 0) r)
  177. (* (vector-ref x 1) k)
  178. (vector-ref x 2)))
  179. (define (array:actor-index x k)
  180. (+ (* (vector-ref x 0) k) (vector-ref x 1)))
  181. (define (array:0 n0) (vector n0))
  182. (define (array:1 n0 n1) (vector n1 n0))
  183. (define (array:2 n0 n1 n2) (vector n1 n2 n0))
  184. (define (array:3 n0 n1 n2 n3) (vector n1 n2 n3 n0))
  185. (define (array:n n0 n1 n2 n3 n4 . ns)
  186. (apply vector n1 n2 n3 n4 (append ns (list n0))))
  187. (define (array:maker r)
  188. (case r
  189. ((0) array:0)
  190. ((1) array:1)
  191. ((2) array:2)
  192. ((3) array:3)
  193. (else array:n)))
  194. (define array:indexer/vector
  195. (let ((em
  196. (vector
  197. (lambda (x i) (+ (vector-ref x 0)))
  198. (lambda (x i)
  199. (+
  200. (* (vector-ref x 0) (vector-ref i 0))
  201. (vector-ref x 1)))
  202. (lambda (x i)
  203. (+
  204. (* (vector-ref x 0) (vector-ref i 0))
  205. (* (vector-ref x 1) (vector-ref i 1))
  206. (vector-ref x 2)))
  207. (lambda (x i)
  208. (+
  209. (* (vector-ref x 0) (vector-ref i 0))
  210. (* (vector-ref x 1) (vector-ref i 1))
  211. (* (vector-ref x 2) (vector-ref i 2))
  212. (vector-ref x 3)))
  213. (lambda (x i)
  214. (+
  215. (* (vector-ref x 0) (vector-ref i 0))
  216. (* (vector-ref x 1) (vector-ref i 1))
  217. (* (vector-ref x 2) (vector-ref i 2))
  218. (* (vector-ref x 3) (vector-ref i 3))
  219. (vector-ref x 4)))
  220. (lambda (x i)
  221. (+
  222. (* (vector-ref x 0) (vector-ref i 0))
  223. (* (vector-ref x 1) (vector-ref i 1))
  224. (* (vector-ref x 2) (vector-ref i 2))
  225. (* (vector-ref x 3) (vector-ref i 3))
  226. (* (vector-ref x 4) (vector-ref i 4))
  227. (vector-ref x 5)))
  228. (lambda (x i)
  229. (+
  230. (* (vector-ref x 0) (vector-ref i 0))
  231. (* (vector-ref x 1) (vector-ref i 1))
  232. (* (vector-ref x 2) (vector-ref i 2))
  233. (* (vector-ref x 3) (vector-ref i 3))
  234. (* (vector-ref x 4) (vector-ref i 4))
  235. (* (vector-ref x 5) (vector-ref i 5))
  236. (vector-ref x 6)))
  237. (lambda (x i)
  238. (+
  239. (* (vector-ref x 0) (vector-ref i 0))
  240. (* (vector-ref x 1) (vector-ref i 1))
  241. (* (vector-ref x 2) (vector-ref i 2))
  242. (* (vector-ref x 3) (vector-ref i 3))
  243. (* (vector-ref x 4) (vector-ref i 4))
  244. (* (vector-ref x 5) (vector-ref i 5))
  245. (* (vector-ref x 6) (vector-ref i 6))
  246. (vector-ref x 7)))
  247. (lambda (x i)
  248. (+
  249. (* (vector-ref x 0) (vector-ref i 0))
  250. (* (vector-ref x 1) (vector-ref i 1))
  251. (* (vector-ref x 2) (vector-ref i 2))
  252. (* (vector-ref x 3) (vector-ref i 3))
  253. (* (vector-ref x 4) (vector-ref i 4))
  254. (* (vector-ref x 5) (vector-ref i 5))
  255. (* (vector-ref x 6) (vector-ref i 6))
  256. (* (vector-ref x 7) (vector-ref i 7))
  257. (vector-ref x 8)))
  258. (lambda (x i)
  259. (+
  260. (* (vector-ref x 0) (vector-ref i 0))
  261. (* (vector-ref x 1) (vector-ref i 1))
  262. (* (vector-ref x 2) (vector-ref i 2))
  263. (* (vector-ref x 3) (vector-ref i 3))
  264. (* (vector-ref x 4) (vector-ref i 4))
  265. (* (vector-ref x 5) (vector-ref i 5))
  266. (* (vector-ref x 6) (vector-ref i 6))
  267. (* (vector-ref x 7) (vector-ref i 7))
  268. (* (vector-ref x 8) (vector-ref i 8))
  269. (vector-ref x 9)))))
  270. (it
  271. (lambda (w)
  272. (lambda (x i)
  273. (+
  274. (* (vector-ref x 0) (vector-ref i 0))
  275. (* (vector-ref x 1) (vector-ref i 1))
  276. (* (vector-ref x 2) (vector-ref i 2))
  277. (* (vector-ref x 3) (vector-ref i 3))
  278. (* (vector-ref x 4) (vector-ref i 4))
  279. (* (vector-ref x 5) (vector-ref i 5))
  280. (* (vector-ref x 6) (vector-ref i 6))
  281. (* (vector-ref x 7) (vector-ref i 7))
  282. (* (vector-ref x 8) (vector-ref i 8))
  283. (* (vector-ref x 9) (vector-ref i 9))
  284. (do ((xi
  285. 0
  286. (+
  287. (* (vector-ref x u) (vector-ref i u))
  288. xi))
  289. (u (- w 1) (- u 1)))
  290. ((< u 10) xi))
  291. (vector-ref x w))))))
  292. (lambda (r) (if (< r 10) (vector-ref em r) (it r)))))
  293. (define array:indexer/array
  294. (let ((em
  295. (vector
  296. (lambda (x v i) (+ (vector-ref x 0)))
  297. (lambda (x v i)
  298. (+
  299. (*
  300. (vector-ref x 0)
  301. (vector-ref v (array:actor-index i 0)))
  302. (vector-ref x 1)))
  303. (lambda (x v i)
  304. (+
  305. (*
  306. (vector-ref x 0)
  307. (vector-ref v (array:actor-index i 0)))
  308. (*
  309. (vector-ref x 1)
  310. (vector-ref v (array:actor-index i 1)))
  311. (vector-ref x 2)))
  312. (lambda (x v i)
  313. (+
  314. (*
  315. (vector-ref x 0)
  316. (vector-ref v (array:actor-index i 0)))
  317. (*
  318. (vector-ref x 1)
  319. (vector-ref v (array:actor-index i 1)))
  320. (*
  321. (vector-ref x 2)
  322. (vector-ref v (array:actor-index i 2)))
  323. (vector-ref x 3)))
  324. (lambda (x v i)
  325. (+
  326. (*
  327. (vector-ref x 0)
  328. (vector-ref v (array:actor-index i 0)))
  329. (*
  330. (vector-ref x 1)
  331. (vector-ref v (array:actor-index i 1)))
  332. (*
  333. (vector-ref x 2)
  334. (vector-ref v (array:actor-index i 2)))
  335. (*
  336. (vector-ref x 3)
  337. (vector-ref v (array:actor-index i 3)))
  338. (vector-ref x 4)))
  339. (lambda (x v i)
  340. (+
  341. (*
  342. (vector-ref x 0)
  343. (vector-ref v (array:actor-index i 0)))
  344. (*
  345. (vector-ref x 1)
  346. (vector-ref v (array:actor-index i 1)))
  347. (*
  348. (vector-ref x 2)
  349. (vector-ref v (array:actor-index i 2)))
  350. (*
  351. (vector-ref x 3)
  352. (vector-ref v (array:actor-index i 3)))
  353. (*
  354. (vector-ref x 4)
  355. (vector-ref v (array:actor-index i 4)))
  356. (vector-ref x 5)))
  357. (lambda (x v i)
  358. (+
  359. (*
  360. (vector-ref x 0)
  361. (vector-ref v (array:actor-index i 0)))
  362. (*
  363. (vector-ref x 1)
  364. (vector-ref v (array:actor-index i 1)))
  365. (*
  366. (vector-ref x 2)
  367. (vector-ref v (array:actor-index i 2)))
  368. (*
  369. (vector-ref x 3)
  370. (vector-ref v (array:actor-index i 3)))
  371. (*
  372. (vector-ref x 4)
  373. (vector-ref v (array:actor-index i 4)))
  374. (*
  375. (vector-ref x 5)
  376. (vector-ref v (array:actor-index i 5)))
  377. (vector-ref x 6)))
  378. (lambda (x v i)
  379. (+
  380. (*
  381. (vector-ref x 0)
  382. (vector-ref v (array:actor-index i 0)))
  383. (*
  384. (vector-ref x 1)
  385. (vector-ref v (array:actor-index i 1)))
  386. (*
  387. (vector-ref x 2)
  388. (vector-ref v (array:actor-index i 2)))
  389. (*
  390. (vector-ref x 3)
  391. (vector-ref v (array:actor-index i 3)))
  392. (*
  393. (vector-ref x 4)
  394. (vector-ref v (array:actor-index i 4)))
  395. (*
  396. (vector-ref x 5)
  397. (vector-ref v (array:actor-index i 5)))
  398. (*
  399. (vector-ref x 6)
  400. (vector-ref v (array:actor-index i 6)))
  401. (vector-ref x 7)))
  402. (lambda (x v i)
  403. (+
  404. (*
  405. (vector-ref x 0)
  406. (vector-ref v (array:actor-index i 0)))
  407. (*
  408. (vector-ref x 1)
  409. (vector-ref v (array:actor-index i 1)))
  410. (*
  411. (vector-ref x 2)
  412. (vector-ref v (array:actor-index i 2)))
  413. (*
  414. (vector-ref x 3)
  415. (vector-ref v (array:actor-index i 3)))
  416. (*
  417. (vector-ref x 4)
  418. (vector-ref v (array:actor-index i 4)))
  419. (*
  420. (vector-ref x 5)
  421. (vector-ref v (array:actor-index i 5)))
  422. (*
  423. (vector-ref x 6)
  424. (vector-ref v (array:actor-index i 6)))
  425. (*
  426. (vector-ref x 7)
  427. (vector-ref v (array:actor-index i 7)))
  428. (vector-ref x 8)))
  429. (lambda (x v i)
  430. (+
  431. (*
  432. (vector-ref x 0)
  433. (vector-ref v (array:actor-index i 0)))
  434. (*
  435. (vector-ref x 1)
  436. (vector-ref v (array:actor-index i 1)))
  437. (*
  438. (vector-ref x 2)
  439. (vector-ref v (array:actor-index i 2)))
  440. (*
  441. (vector-ref x 3)
  442. (vector-ref v (array:actor-index i 3)))
  443. (*
  444. (vector-ref x 4)
  445. (vector-ref v (array:actor-index i 4)))
  446. (*
  447. (vector-ref x 5)
  448. (vector-ref v (array:actor-index i 5)))
  449. (*
  450. (vector-ref x 6)
  451. (vector-ref v (array:actor-index i 6)))
  452. (*
  453. (vector-ref x 7)
  454. (vector-ref v (array:actor-index i 7)))
  455. (*
  456. (vector-ref x 8)
  457. (vector-ref v (array:actor-index i 8)))
  458. (vector-ref x 9)))))
  459. (it
  460. (lambda (w)
  461. (lambda (x v i)
  462. (+
  463. (*
  464. (vector-ref x 0)
  465. (vector-ref v (array:actor-index i 0)))
  466. (*
  467. (vector-ref x 1)
  468. (vector-ref v (array:actor-index i 1)))
  469. (*
  470. (vector-ref x 2)
  471. (vector-ref v (array:actor-index i 2)))
  472. (*
  473. (vector-ref x 3)
  474. (vector-ref v (array:actor-index i 3)))
  475. (*
  476. (vector-ref x 4)
  477. (vector-ref v (array:actor-index i 4)))
  478. (*
  479. (vector-ref x 5)
  480. (vector-ref v (array:actor-index i 5)))
  481. (*
  482. (vector-ref x 6)
  483. (vector-ref v (array:actor-index i 6)))
  484. (*
  485. (vector-ref x 7)
  486. (vector-ref v (array:actor-index i 7)))
  487. (*
  488. (vector-ref x 8)
  489. (vector-ref v (array:actor-index i 8)))
  490. (*
  491. (vector-ref x 9)
  492. (vector-ref v (array:actor-index i 9)))
  493. (do ((xi
  494. 0
  495. (+
  496. (*
  497. (vector-ref x u)
  498. (vector-ref
  499. v
  500. (array:actor-index i u)))
  501. xi))
  502. (u (- w 1) (- u 1)))
  503. ((< u 10) xi))
  504. (vector-ref x w))))))
  505. (lambda (r) (if (< r 10) (vector-ref em r) (it r)))))
  506. (define array:applier-to-vector
  507. (let ((em
  508. (vector
  509. (lambda (p v) (p))
  510. (lambda (p v) (p (vector-ref v 0)))
  511. (lambda (p v)
  512. (p (vector-ref v 0) (vector-ref v 1)))
  513. (lambda (p v)
  514. (p
  515. (vector-ref v 0)
  516. (vector-ref v 1)
  517. (vector-ref v 2)))
  518. (lambda (p v)
  519. (p
  520. (vector-ref v 0)
  521. (vector-ref v 1)
  522. (vector-ref v 2)
  523. (vector-ref v 3)))
  524. (lambda (p v)
  525. (p
  526. (vector-ref v 0)
  527. (vector-ref v 1)
  528. (vector-ref v 2)
  529. (vector-ref v 3)
  530. (vector-ref v 4)))
  531. (lambda (p v)
  532. (p
  533. (vector-ref v 0)
  534. (vector-ref v 1)
  535. (vector-ref v 2)
  536. (vector-ref v 3)
  537. (vector-ref v 4)
  538. (vector-ref v 5)))
  539. (lambda (p v)
  540. (p
  541. (vector-ref v 0)
  542. (vector-ref v 1)
  543. (vector-ref v 2)
  544. (vector-ref v 3)
  545. (vector-ref v 4)
  546. (vector-ref v 5)
  547. (vector-ref v 6)))
  548. (lambda (p v)
  549. (p
  550. (vector-ref v 0)
  551. (vector-ref v 1)
  552. (vector-ref v 2)
  553. (vector-ref v 3)
  554. (vector-ref v 4)
  555. (vector-ref v 5)
  556. (vector-ref v 6)
  557. (vector-ref v 7)))
  558. (lambda (p v)
  559. (p
  560. (vector-ref v 0)
  561. (vector-ref v 1)
  562. (vector-ref v 2)
  563. (vector-ref v 3)
  564. (vector-ref v 4)
  565. (vector-ref v 5)
  566. (vector-ref v 6)
  567. (vector-ref v 7)
  568. (vector-ref v 8)))))
  569. (it
  570. (lambda (r)
  571. (lambda (p v)
  572. (apply
  573. p
  574. (vector-ref v 0)
  575. (vector-ref v 1)
  576. (vector-ref v 2)
  577. (vector-ref v 3)
  578. (vector-ref v 4)
  579. (vector-ref v 5)
  580. (vector-ref v 6)
  581. (vector-ref v 7)
  582. (vector-ref v 8)
  583. (vector-ref v 9)
  584. (do ((k r (- k 1))
  585. (r
  586. '()
  587. (cons (vector-ref v (- k 1)) r)))
  588. ((= k 10) r)))))))
  589. (lambda (r) (if (< r 10) (vector-ref em r) (it r)))))
  590. (define array:applier-to-actor
  591. (let ((em
  592. (vector
  593. (lambda (p a) (p))
  594. (lambda (p a) (p (array-ref a 0)))
  595. (lambda (p a)
  596. (p (array-ref a 0) (array-ref a 1)))
  597. (lambda (p a)
  598. (p
  599. (array-ref a 0)
  600. (array-ref a 1)
  601. (array-ref a 2)))
  602. (lambda (p a)
  603. (p
  604. (array-ref a 0)
  605. (array-ref a 1)
  606. (array-ref a 2)
  607. (array-ref a 3)))
  608. (lambda (p a)
  609. (p
  610. (array-ref a 0)
  611. (array-ref a 1)
  612. (array-ref a 2)
  613. (array-ref a 3)
  614. (array-ref a 4)))
  615. (lambda (p a)
  616. (p
  617. (array-ref a 0)
  618. (array-ref a 1)
  619. (array-ref a 2)
  620. (array-ref a 3)
  621. (array-ref a 4)
  622. (array-ref a 5)))
  623. (lambda (p a)
  624. (p
  625. (array-ref a 0)
  626. (array-ref a 1)
  627. (array-ref a 2)
  628. (array-ref a 3)
  629. (array-ref a 4)
  630. (array-ref a 5)
  631. (array-ref a 6)))
  632. (lambda (p a)
  633. (p
  634. (array-ref a 0)
  635. (array-ref a 1)
  636. (array-ref a 2)
  637. (array-ref a 3)
  638. (array-ref a 4)
  639. (array-ref a 5)
  640. (array-ref a 6)
  641. (array-ref a 7)))
  642. (lambda (p a)
  643. (p
  644. (array-ref a 0)
  645. (array-ref a 1)
  646. (array-ref a 2)
  647. (array-ref a 3)
  648. (array-ref a 4)
  649. (array-ref a 5)
  650. (array-ref a 6)
  651. (array-ref a 7)
  652. (array-ref a 8)))))
  653. (it
  654. (lambda (r)
  655. (lambda (p a)
  656. (apply
  657. a
  658. (array-ref a 0)
  659. (array-ref a 1)
  660. (array-ref a 2)
  661. (array-ref a 3)
  662. (array-ref a 4)
  663. (array-ref a 5)
  664. (array-ref a 6)
  665. (array-ref a 7)
  666. (array-ref a 8)
  667. (array-ref a 9)
  668. (do ((k r (- k 1))
  669. (r '() (cons (array-ref a (- k 1)) r)))
  670. ((= k 10) r)))))))
  671. (lambda (r)
  672. "These are high level, hiding implementation at call site."
  673. (if (< r 10) (vector-ref em r) (it r)))))
  674. (define array:applier-to-backing-vector
  675. (let ((em
  676. (vector
  677. (lambda (p ai av) (p))
  678. (lambda (p ai av)
  679. (p (vector-ref av (array:actor-index ai 0))))
  680. (lambda (p ai av)
  681. (p
  682. (vector-ref av (array:actor-index ai 0))
  683. (vector-ref av (array:actor-index ai 1))))
  684. (lambda (p ai av)
  685. (p
  686. (vector-ref av (array:actor-index ai 0))
  687. (vector-ref av (array:actor-index ai 1))
  688. (vector-ref av (array:actor-index ai 2))))
  689. (lambda (p ai av)
  690. (p
  691. (vector-ref av (array:actor-index ai 0))
  692. (vector-ref av (array:actor-index ai 1))
  693. (vector-ref av (array:actor-index ai 2))
  694. (vector-ref av (array:actor-index ai 3))))
  695. (lambda (p ai av)
  696. (p
  697. (vector-ref av (array:actor-index ai 0))
  698. (vector-ref av (array:actor-index ai 1))
  699. (vector-ref av (array:actor-index ai 2))
  700. (vector-ref av (array:actor-index ai 3))
  701. (vector-ref av (array:actor-index ai 4))))
  702. (lambda (p ai av)
  703. (p
  704. (vector-ref av (array:actor-index ai 0))
  705. (vector-ref av (array:actor-index ai 1))
  706. (vector-ref av (array:actor-index ai 2))
  707. (vector-ref av (array:actor-index ai 3))
  708. (vector-ref av (array:actor-index ai 4))
  709. (vector-ref av (array:actor-index ai 5))))
  710. (lambda (p ai av)
  711. (p
  712. (vector-ref av (array:actor-index ai 0))
  713. (vector-ref av (array:actor-index ai 1))
  714. (vector-ref av (array:actor-index ai 2))
  715. (vector-ref av (array:actor-index ai 3))
  716. (vector-ref av (array:actor-index ai 4))
  717. (vector-ref av (array:actor-index ai 5))
  718. (vector-ref av (array:actor-index ai 6))))
  719. (lambda (p ai av)
  720. (p
  721. (vector-ref av (array:actor-index ai 0))
  722. (vector-ref av (array:actor-index ai 1))
  723. (vector-ref av (array:actor-index ai 2))
  724. (vector-ref av (array:actor-index ai 3))
  725. (vector-ref av (array:actor-index ai 4))
  726. (vector-ref av (array:actor-index ai 5))
  727. (vector-ref av (array:actor-index ai 6))
  728. (vector-ref av (array:actor-index ai 7))))
  729. (lambda (p ai av)
  730. (p
  731. (vector-ref av (array:actor-index ai 0))
  732. (vector-ref av (array:actor-index ai 1))
  733. (vector-ref av (array:actor-index ai 2))
  734. (vector-ref av (array:actor-index ai 3))
  735. (vector-ref av (array:actor-index ai 4))
  736. (vector-ref av (array:actor-index ai 5))
  737. (vector-ref av (array:actor-index ai 6))
  738. (vector-ref av (array:actor-index ai 7))
  739. (vector-ref av (array:actor-index ai 8))))))
  740. (it
  741. (lambda (r)
  742. (lambda (p ai av)
  743. (apply
  744. p
  745. (vector-ref av (array:actor-index ai 0))
  746. (vector-ref av (array:actor-index ai 1))
  747. (vector-ref av (array:actor-index ai 2))
  748. (vector-ref av (array:actor-index ai 3))
  749. (vector-ref av (array:actor-index ai 4))
  750. (vector-ref av (array:actor-index ai 5))
  751. (vector-ref av (array:actor-index ai 6))
  752. (vector-ref av (array:actor-index ai 7))
  753. (vector-ref av (array:actor-index ai 8))
  754. (vector-ref av (array:actor-index ai 9))
  755. (do ((k r (- k 1))
  756. (r
  757. '()
  758. (cons
  759. (vector-ref
  760. av
  761. (array:actor-index ai (- k 1)))
  762. r)))
  763. ((= k 10) r)))))))
  764. (lambda (r)
  765. "These are low level, exposing implementation at call site."
  766. (if (< r 10) (vector-ref em r) (it r)))))
  767. (define (array:index/vector r x v)
  768. ((array:indexer/vector r) x v))
  769. (define (array:index/array r x av ai)
  770. ((array:indexer/array r) x av ai))
  771. (define (array:apply-to-vector r p v)
  772. ((array:applier-to-vector r) p v))
  773. (define (array:apply-to-actor r p a)
  774. ((array:applier-to-actor r) p a)))
  775. ;; Contents of array.scm
  776. ;;; --- Portable R5RS (R4RS and multiple values) ---
  777. ;;; (array? obj)
  778. ;;; returns #t if `obj' is an array and #t or #f otherwise.
  779. (define (array? obj)
  780. (array:array? obj))
  781. ;;; (make-array shape)
  782. ;;; (make-array shape obj)
  783. ;;; makes array of `shape' with each cell containing `obj' initially.
  784. (define (make-array shape . rest)
  785. (or (array:good-shape? shape)
  786. (error "make-array: shape is not a shape"))
  787. (apply array:make-array shape rest))
  788. (define (array:make-array shape . rest)
  789. (let ((size (array:size shape)))
  790. (array:make
  791. (if (pair? rest)
  792. (apply (lambda (o) (make-vector size o)) rest)
  793. (make-vector size))
  794. (if (= size 0)
  795. (array:optimize-empty
  796. (vector-ref (array:shape shape) 1))
  797. (array:optimize
  798. (array:make-index shape)
  799. (vector-ref (array:shape shape) 1)))
  800. (array:shape->vector shape))))
  801. ;;; (shape bound ...)
  802. ;;; makes a shape. Bounds must be an even number of exact, pairwise
  803. ;;; non-decreasing integers. Note that any such array can be a shape.
  804. (define (shape . bounds)
  805. (let ((v (list->vector bounds)))
  806. (or (even? (vector-length v))
  807. (error (string-append "shape: uneven number of bounds: "
  808. (array:list->string bounds))))
  809. (let ((shp (array:make
  810. v
  811. (if (pair? bounds)
  812. (array:shape-index)
  813. (array:empty-shape-index))
  814. (vector 0 (quotient (vector-length v) 2)
  815. 0 2))))
  816. (or (array:good-shape? shp)
  817. (error (string-append "shape: bounds are not pairwise "
  818. "non-decreasing exact integers: "
  819. (array:list->string bounds))))
  820. shp)))
  821. ;;; (array shape obj ...)
  822. ;;; is analogous to `vector'.
  823. (define (array shape . elts)
  824. (or (array:good-shape? shape)
  825. (error (string-append "array: shape " (array:thing->string shape)
  826. " is not a shape")))
  827. (let ((size (array:size shape)))
  828. (let ((vector (list->vector elts)))
  829. (or (= (vector-length vector) size)
  830. (error (string-append "array: an array of shape "
  831. (array:shape-vector->string
  832. (array:vector shape))
  833. " has "
  834. (number->string size)
  835. " elements but got "
  836. (number->string (vector-length vector))
  837. " values: "
  838. (array:list->string elts))))
  839. (array:make
  840. vector
  841. (if (= size 0)
  842. (array:optimize-empty
  843. (vector-ref (array:shape shape) 1))
  844. (array:optimize
  845. (array:make-index shape)
  846. (vector-ref (array:shape shape) 1)))
  847. (array:shape->vector shape)))))
  848. ;;; (array-rank array)
  849. ;;; returns the number of dimensions of `array'.
  850. (define (array-rank array)
  851. (quotient (vector-length (array:shape array)) 2))
  852. ;;; (array-start array k)
  853. ;;; returns the lower bound index of array along dimension k. This is
  854. ;;; the least valid index along that dimension if the dimension is not
  855. ;;; empty.
  856. (define (array-start array d)
  857. (vector-ref (array:shape array) (+ d d)))
  858. ;;; (array-end array k)
  859. ;;; returns the upper bound index of array along dimension k. This is
  860. ;;; not a valid index. If the dimension is empty, this is the same as
  861. ;;; the lower bound along it.
  862. (define (array-end array d)
  863. (vector-ref (array:shape array) (+ d d 1)))
  864. ;;; (share-array array shape proc)
  865. ;;; makes an array that shares elements of `array' at shape `shape'.
  866. ;;; The arguments to `proc' are indices of the result. The values of
  867. ;;; `proc' are indices of `array'.
  868. ;;; Todo: in the error message, should recognise the mapping and show it.
  869. (define (share-array array subshape f)
  870. (or (array:good-shape? subshape)
  871. (error (string-append "share-array: shape "
  872. (array:thing->string subshape)
  873. " is not a shape")))
  874. (let ((subsize (array:size subshape)))
  875. (or (array:good-share? subshape subsize f (array:shape array))
  876. (error (string-append "share-array: subshape "
  877. (array:shape-vector->string
  878. (array:vector subshape))
  879. " does not map into supershape "
  880. (array:shape-vector->string
  881. (array:shape array))
  882. " under mapping "
  883. (array:map->string
  884. f
  885. (vector-ref (array:shape subshape) 1)))))
  886. (let ((g (array:index array)))
  887. (array:make
  888. (array:vector array)
  889. (if (= subsize 0)
  890. (array:optimize-empty
  891. (vector-ref (array:shape subshape) 1))
  892. (array:optimize
  893. (lambda ks
  894. (call-with-values
  895. (lambda () (apply f ks))
  896. (lambda ks (array:vector-index g ks))))
  897. (vector-ref (array:shape subshape) 1)))
  898. (array:shape->vector subshape)))))
  899. ;;; --- Hrmph ---
  900. ;;; (array:share/index! ...)
  901. ;;; reuses a user supplied index object when recognising the
  902. ;;; mapping. The mind balks at the very nasty side effect that
  903. ;;; exposes the implementation. So this is not in the spec.
  904. ;;; But letting index objects in at all creates a pressure
  905. ;;; to go the whole hog. Arf.
  906. ;;; Use array:optimize-empty for an empty array to get a
  907. ;;; clearly invalid vector index.
  908. ;;; Surely it's perverse to use an actor for index here? But
  909. ;;; the possibility is provided for completeness.
  910. (define (array:share/index! array subshape proc index)
  911. (array:make
  912. (array:vector array)
  913. (if (= (array:size subshape) 0)
  914. (array:optimize-empty
  915. (quotient (vector-length (array:shape array)) 2))
  916. ((if (vector? index)
  917. array:optimize/vector
  918. array:optimize/actor)
  919. (lambda (subindex)
  920. (let ((superindex (proc subindex)))
  921. (if (vector? superindex)
  922. (array:index/vector
  923. (quotient (vector-length (array:shape array)) 2)
  924. (array:index array)
  925. superindex)
  926. (array:index/array
  927. (quotient (vector-length (array:shape array)) 2)
  928. (array:index array)
  929. (array:vector superindex)
  930. (array:index superindex)))))
  931. index))
  932. (array:shape->vector subshape)))
  933. (define (array:optimize/vector f v)
  934. (let ((r (vector-length v)))
  935. (do ((k 0 (+ k 1)))
  936. ((= k r))
  937. (vector-set! v k 0))
  938. (let ((n0 (f v))
  939. (cs (make-vector (+ r 1)))
  940. (apply (array:applier-to-vector (+ r 1))))
  941. (vector-set! cs 0 n0)
  942. (let wok ((k 0))
  943. (if (< k r)
  944. (let ((k1 (+ k 1)))
  945. (vector-set! v k 1)
  946. (let ((nk (- (f v) n0)))
  947. (vector-set! v k 0)
  948. (vector-set! cs k1 nk)
  949. (wok k1)))))
  950. (apply (array:maker r) cs))))
  951. (define (array:optimize/actor f a)
  952. (let ((r (array-end a 0))
  953. (v (array:vector a))
  954. (i (array:index a)))
  955. (do ((k 0 (+ k 1)))
  956. ((= k r))
  957. (vector-set! v (array:actor-index i k) 0))
  958. (let ((n0 (f a))
  959. (cs (make-vector (+ r 1)))
  960. (apply (array:applier-to-vector (+ r 1))))
  961. (vector-set! cs 0 n0)
  962. (let wok ((k 0))
  963. (if (< k r)
  964. (let ((k1 (+ k 1))
  965. (t (array:actor-index i k)))
  966. (vector-set! v t 1)
  967. (let ((nk (- (f a) n0)))
  968. (vector-set! v t 0)
  969. (vector-set! cs k1 nk)
  970. (wok k1)))))
  971. (apply (array:maker r) cs))))
  972. ;;; --- Internals ---
  973. (define (array:shape->vector shape)
  974. (let ((idx (array:index shape))
  975. (shv (array:vector shape))
  976. (rnk (vector-ref (array:shape shape) 1)))
  977. (let ((vec (make-vector (* rnk 2))))
  978. (do ((k 0 (+ k 1)))
  979. ((= k rnk)
  980. vec)
  981. (vector-set! vec (+ k k)
  982. (vector-ref shv (array:shape-vector-index idx k 0)))
  983. (vector-set! vec (+ k k 1)
  984. (vector-ref shv (array:shape-vector-index idx k 1)))))))
  985. ;;; (array:size shape)
  986. ;;; returns the number of elements in arrays of shape `shape'.
  987. (define (array:size shape)
  988. (let ((idx (array:index shape))
  989. (shv (array:vector shape))
  990. (rnk (vector-ref (array:shape shape) 1)))
  991. (do ((k 0 (+ k 1))
  992. (s 1 (* s
  993. (- (vector-ref shv (array:shape-vector-index idx k 1))
  994. (vector-ref shv (array:shape-vector-index idx k 0))))))
  995. ((= k rnk) s))))
  996. ;;; (array:make-index shape)
  997. ;;; returns an index function for arrays of shape `shape'. This is a
  998. ;;; runtime composition of several variable arity procedures, to be
  999. ;;; passed to array:optimize for recognition as an affine function of
  1000. ;;; as many variables as there are dimensions in arrays of this shape.
  1001. (define (array:make-index shape)
  1002. (let ((idx (array:index shape))
  1003. (shv (array:vector shape))
  1004. (rnk (vector-ref (array:shape shape) 1)))
  1005. (do ((f (lambda () 0)
  1006. (lambda (k . ks)
  1007. (+ (* s (- k (vector-ref
  1008. shv
  1009. (array:shape-vector-index idx (- j 1) 0))))
  1010. (apply f ks))))
  1011. (s 1 (* s (- (vector-ref
  1012. shv
  1013. (array:shape-vector-index idx (- j 1) 1))
  1014. (vector-ref
  1015. shv
  1016. (array:shape-vector-index idx (- j 1) 0)))))
  1017. (j rnk (- j 1)))
  1018. ((= j 0)
  1019. f))))
  1020. ;;; --- Error checking ---
  1021. ;;; (array:good-shape? shape)
  1022. ;;; returns true if `shape' is an array of the right shape and its
  1023. ;;; elements are exact integers that pairwise bound intervals `[lo..hi)´.
  1024. (define (array:good-shape? shape)
  1025. (and (array:array? shape)
  1026. (let ((u (array:shape shape))
  1027. (v (array:vector shape))
  1028. (x (array:index shape)))
  1029. (and (= (vector-length u) 4)
  1030. (= (vector-ref u 0) 0)
  1031. (= (vector-ref u 2) 0)
  1032. (= (vector-ref u 3) 2))
  1033. (let ((p (vector-ref u 1)))
  1034. (do ((k 0 (+ k 1))
  1035. (true #t (let ((lo (vector-ref
  1036. v
  1037. (array:shape-vector-index x k 0)))
  1038. (hi (vector-ref
  1039. v
  1040. (array:shape-vector-index x k 1))))
  1041. (and true
  1042. (integer? lo)
  1043. (exact? lo)
  1044. (integer? hi)
  1045. (exact? hi)
  1046. (<= lo hi)))))
  1047. ((= k p) true))))))
  1048. ;;; (array:good-share? subv subsize mapping superv)
  1049. ;;; returns true if the extreme indices in the subshape vector map
  1050. ;;; into the bounds in the supershape vector.
  1051. ;;; If some interval in `subv' is empty, then `subv' is empty and its
  1052. ;;; image under `f' is empty and it is trivially alright. One must
  1053. ;;; not call `f', though.
  1054. (define (array:good-share? subshape subsize f super)
  1055. (or (zero? subsize)
  1056. (letrec
  1057. ((sub (array:vector subshape))
  1058. (dex (array:index subshape))
  1059. (ck (lambda (k ks)
  1060. (if (zero? k)
  1061. (call-with-values
  1062. (lambda () (apply f ks))
  1063. (lambda qs (array:good-indices? qs super)))
  1064. (and (ck (- k 1)
  1065. (cons (vector-ref
  1066. sub
  1067. (array:shape-vector-index
  1068. dex
  1069. (- k 1)
  1070. 0))
  1071. ks))
  1072. (ck (- k 1)
  1073. (cons (- (vector-ref
  1074. sub
  1075. (array:shape-vector-index
  1076. dex
  1077. (- k 1)
  1078. 1))
  1079. 1)
  1080. ks)))))))
  1081. (let ((rnk (vector-ref (array:shape subshape) 1)))
  1082. (or (array:unchecked-share-depth? rnk)
  1083. (ck rnk '()))))))
  1084. ;;; Check good-share on 10 dimensions at most. The trouble is,
  1085. ;;; the cost of this check is exponential in the number of dimensions.
  1086. (define (array:unchecked-share-depth? rank)
  1087. (if (> rank 10)
  1088. (begin
  1089. (display `(warning: unchecked depth in share:
  1090. ,rank subdimensions))
  1091. (newline)
  1092. #t)
  1093. #f))
  1094. ;;; (array:check-indices caller indices shape-vector)
  1095. ;;; (array:check-indices.o caller indices shape-vector)
  1096. ;;; (array:check-index-vector caller index-vector shape-vector)
  1097. ;;; return if the index is in bounds, else signal error.
  1098. ;;;
  1099. ;;; Shape-vector is the internal representation, with
  1100. ;;; b and e for dimension k at 2k and 2k + 1.
  1101. (define (array:check-indices who ks shv)
  1102. (or (array:good-indices? ks shv)
  1103. (error (array:not-in who ks shv))))
  1104. (define (array:check-indices.o who ks shv)
  1105. (or (array:good-indices.o? ks shv)
  1106. (error (array:not-in who (reverse (cdr (reverse ks))) shv))))
  1107. (define (array:check-index-vector who ks shv)
  1108. (or (array:good-index-vector? ks shv)
  1109. (error (array:not-in who (vector->list ks) shv))))
  1110. (define (array:check-index-actor who ks shv)
  1111. (let ((shape (array:shape ks)))
  1112. (or (and (= (vector-length shape) 2)
  1113. (= (vector-ref shape 0) 0))
  1114. (error "not an actor"))
  1115. (or (array:good-index-actor?
  1116. (vector-ref shape 1)
  1117. (array:vector ks)
  1118. (array:index ks)
  1119. shv)
  1120. (array:not-in who (do ((k (vector-ref shape 1) (- k 1))
  1121. (m '() (cons (vector-ref
  1122. (array:vector ks)
  1123. (array:actor-index
  1124. (array:index ks)
  1125. (- k 1)))
  1126. m)))
  1127. ((= k 0) m))
  1128. shv))))
  1129. (define (array:good-indices? ks shv)
  1130. (let ((d2 (vector-length shv)))
  1131. (do ((kp ks (if (pair? kp)
  1132. (cdr kp)))
  1133. (k 0 (+ k 2))
  1134. (true #t (and true (pair? kp)
  1135. (array:good-index? (car kp) shv k))))
  1136. ((= k d2)
  1137. (and true (null? kp))))))
  1138. (define (array:good-indices.o? ks.o shv)
  1139. (let ((d2 (vector-length shv)))
  1140. (do ((kp ks.o (if (pair? kp)
  1141. (cdr kp)))
  1142. (k 0 (+ k 2))
  1143. (true #t (and true (pair? kp)
  1144. (array:good-index? (car kp) shv k))))
  1145. ((= k d2)
  1146. (and true (pair? kp) (null? (cdr kp)))))))
  1147. (define (array:good-index-vector? ks shv)
  1148. (let ((r2 (vector-length shv)))
  1149. (and (= (* 2 (vector-length ks)) r2)
  1150. (do ((j 0 (+ j 1))
  1151. (k 0 (+ k 2))
  1152. (true #t (and true
  1153. (array:good-index? (vector-ref ks j) shv k))))
  1154. ((= k r2) true)))))
  1155. (define (array:good-index-actor? r v i shv)
  1156. (and (= (* 2 r) (vector-length shv))
  1157. (do ((j 0 (+ j 1))
  1158. (k 0 (+ k 2))
  1159. (true #t (and true
  1160. (array:good-index? (vector-ref
  1161. v
  1162. (array:actor-index i j))
  1163. shv
  1164. k))))
  1165. ((= j r) true))))
  1166. ;;; (array:good-index? index shape-vector 2d)
  1167. ;;; returns true if index is within bounds for dimension 2d/2.
  1168. (define (array:good-index? w shv k)
  1169. (and (integer? w)
  1170. (exact? w)
  1171. (<= (vector-ref shv k) w)
  1172. (< w (vector-ref shv (+ k 1)))))
  1173. (define (array:not-in who ks shv)
  1174. (let ((index (array:list->string ks))
  1175. (bounds (array:shape-vector->string shv)))
  1176. (error (string-append who
  1177. ": index " index
  1178. " not in bounds " bounds))))
  1179. (define (array:list->string ks)
  1180. (do ((index "" (string-append index (array:thing->string (car ks)) " "))
  1181. (ks ks (cdr ks)))
  1182. ((null? ks) index)))
  1183. (define (array:shape-vector->string shv)
  1184. (do ((bounds "" (string-append bounds
  1185. "["
  1186. (number->string (vector-ref shv t))
  1187. ".."
  1188. (number->string (vector-ref shv (+ t 1)))
  1189. ")"
  1190. " "))
  1191. (t 0 (+ t 2)))
  1192. ((= t (vector-length shv)) bounds)))
  1193. (define (array:thing->string thing)
  1194. (cond
  1195. ((number? thing) (number->string thing))
  1196. ((symbol? thing) (string-append "#<symbol>" (symbol->string thing)))
  1197. ((char? thing) "#<char>")
  1198. ((string? thing) "#<string>")
  1199. ((list? thing) (string-append "#" (number->string (length thing))
  1200. "<list>"))
  1201. ((pair? thing) "#<pair>")
  1202. ((array? thing) "#<array>")
  1203. ((vector? thing) (string-append "#" (number->string
  1204. (vector-length thing))
  1205. "<vector>"))
  1206. ((procedure? thing) "#<procedure>")
  1207. (else
  1208. (case thing
  1209. ((()) "()")
  1210. ((#t) "#t")
  1211. ((#f) "#f")
  1212. (else
  1213. "#<whatsit>")))))
  1214. ;;; And to grok an affine map, vector->vector type. Column k of arr
  1215. ;;; will contain coefficients n0 ... nm of 1 k1 ... km for kth value.
  1216. ;;;
  1217. ;;; These are for the error message when share fails.
  1218. (define (array:index-ref ind k)
  1219. (if (vector? ind)
  1220. (vector-ref ind k)
  1221. (vector-ref
  1222. (array:vector ind)
  1223. (array:actor-index (array:index ind) k))))
  1224. (define (array:index-set! ind k o)
  1225. (if (vector? ind)
  1226. (vector-set! ind k o)
  1227. (vector-set!
  1228. (array:vector ind)
  1229. (array:actor-index (array:index ind) k)
  1230. o)))
  1231. (define (array:index-length ind)
  1232. (if (vector? ind)
  1233. (vector-length ind)
  1234. (vector-ref (array:shape ind) 1)))
  1235. (define (array:map->string proc r)
  1236. (let* ((m (array:grok/arguments proc r))
  1237. (s (vector-ref (array:shape m) 3)))
  1238. (do ((i "" (string-append i c "k" (number->string k)))
  1239. (c "" ", ")
  1240. (k 1 (+ k 1)))
  1241. ((< r k)
  1242. (do ((o "" (string-append o c (array:map-column->string m r k)))
  1243. (c "" ", ")
  1244. (k 0 (+ k 1)))
  1245. ((= k s)
  1246. (string-append i " => " o)))))))
  1247. (define (array:map-column->string m r k)
  1248. (let ((v (array:vector m))
  1249. (i (array:index m)))
  1250. (let ((n0 (vector-ref v (array:vector-index i (list 0 k)))))
  1251. (let wok ((j 1)
  1252. (e (if (= n0 0) "" (number->string n0))))
  1253. (if (<= j r)
  1254. (let ((nj (vector-ref v (array:vector-index i (list j k)))))
  1255. (if (= nj 0)
  1256. (wok (+ j 1) e)
  1257. (let* ((nj (if (= nj 1) ""
  1258. (if (= nj -1) "-"
  1259. (string-append (number->string nj)
  1260. " "))))
  1261. (njkj (string-append nj "k" (number->string j))))
  1262. (if (string=? e "")
  1263. (wok (+ j 1) njkj)
  1264. (wok (+ j 1) (string-append e " + " njkj))))))
  1265. (if (string=? e "") "0" e))))))
  1266. (define (array:grok/arguments proc r)
  1267. (array:grok/index!
  1268. (lambda (vec)
  1269. (call-with-values
  1270. (lambda ()
  1271. (array:apply-to-vector r proc vec))
  1272. vector))
  1273. (make-vector r)))
  1274. (define (array:grok/index! proc in)
  1275. (let ((m (array:index-length in)))
  1276. (do ((k 0 (+ k 1)))
  1277. ((= k m))
  1278. (array:index-set! in k 0))
  1279. (let* ((n0 (proc in))
  1280. (n (array:index-length n0)))
  1281. (let ((arr (make-array (shape 0 (+ m 1) 0 n)))) ; (*)
  1282. (do ((k 0 (+ k 1)))
  1283. ((= k n))
  1284. (array-set! arr 0 k (array:index-ref n0 k))) ; (**)
  1285. (do ((j 0 (+ j 1)))
  1286. ((= j m))
  1287. (array:index-set! in j 1)
  1288. (let ((nj (proc in)))
  1289. (array:index-set! in j 0)
  1290. (do ((k 0 (+ k 1)))
  1291. ((= k n))
  1292. (array-set! arr (+ j 1) k (- (array:index-ref nj k) ; (**)
  1293. (array:index-ref n0 k))))))
  1294. arr))))
  1295. ;; (*) Should not use `make-array' and `shape' here
  1296. ;; (**) Should not use `array-set!' here
  1297. ;; Should use something internal to the library instead: either lower
  1298. ;; level code (preferable but complex) or alternative names to these same.