srfi-25.scm 49 KB

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