Debian Codegraph @

lib/srfi/179/generalized-arrays.scm

raw symbols SCIP
1 #|
2 SRFI 179: Nonempty Intervals and Generalized Arrays (Updated)
3
4 Copyright 2016, 2018, 2020 Bradley J Lucier.
5 All Rights Reserved.
6
7 Permission is hereby granted, free of charge,
8 to any person obtaining a copy of this software
9 and associated documentation files (the "Software"),
10 to deal in the Software without restriction,
11 including without limitation the rights to use, copy,
12 modify, merge, publish, distribute, sublicense,
13 and/or sell copies of the Software, and to permit
14 persons to whom the Software is furnished to do so,
15 subject to the following conditions:
16
17 The above copyright notice and this permission notice
18 (including the next paragraph) shall be included in
19 all copies or substantial portions of the Software.
20
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
22 ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
23 LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
24 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
25 EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
26 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
27 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
29 OTHER DEALINGS IN THE SOFTWARE.
30 |#
31
32 (##namespace ("srfi/179#"))
33 (##include "~~lib/gambit#.scm")
34
35 (##declare (standard-bindings)
36 (extended-bindings)
37 (block)
38 (mostly-fixnum)
39 (not safe))
40
41 ;;; Our naming convention prefixes %% to the names of internal procedures,
42
43 (define (c64vector-copy! to at from start end)
44 (f32vector-copy! to (fx* 2 at) from (fx* 2 start) (fx* 2 end)))
45 (define (c128vector-copy! to at from start end)
46 (f64vector-copy! to (fx* 2 at) from (fx* 2 start) (fx* 2 end)))
47
48 (declare (not inline))
49
50 ;;; We do not need a multi-argument every.
51
52 (define (%%every pred list)
53 ;; don't inline %%every, but unroll the loop if advantageous
54 (declare (inline))
55 (let loop ((list list))
56 (or (null? list)
57 (and (pred (car list))
58 (loop (cdr list))))))
59
60 ;;; the following is used in error checks.
61
62 (define %%vector-every
63 (case-lambda
64 ((pred vec)
65 (let loop ((i (fx- (vector-length vec) 1)))
66 (or (fx< i 0)
67 (and (pred (vector-ref vec i))
68 (loop (fx- i 1))))))
69 ((pred vec vec2)
70 (let loop ((i (fx- (vector-length vec) 1)))
71 (or (fx< i 0)
72 (and (pred (vector-ref vec i)
73 (vector-ref vec2 i))
74 (loop (fx- i 1))))))
75 ((pred vec vec2 . rest)
76 (let ((vecs (cons vec (cons vec2 rest))))
77 (let loop ((i (fx- (vector-length vec) 1)))
78 (or (fx< i 0)
79 (and (apply pred (map (lambda (vec) (vector-ref vec i)) vecs))
80 (loop (fx- i 1)))))))))
81
82
83
84 ;;; requires vector-map, vector-copy function
85
86 ;;; requires vector-concatenate function
87
88 ;;; requires exact-integer? function
89
90 ;;; requires iota, drop, take from SRFI-1
91
92 ;;; requires fixnum? and flonum?
93
94 (declare (inline))
95
96 ;;; An interval is a cross product of multi-indices
97
98 ;;; [l_0,u_0) x [l_1,u_1) x ... x [l_n-1,u_n-1)
99
100 ;;; where l_i < u_i for 0 <= i < n, and n > 0 is the dimension of the interval
101
102 (define-structure %%interval
103 dimension ;; a fixnum
104 %%volume ;; #f or an exact integer, calculated when needed
105 lower-bounds ;; a vector of exact integers l_0,...,l_n-1
106 upper-bounds ;; a vector of exact integers u_0,...,u_n-1
107 ) ;; end %%interval
108 (define (interval? x)
109 (%%interval? x))
110
111 (declare (not inline))
112
113 (define %%vector-of-zeros
114 '#(#()
115 #(0)
116 #(0 0)
117 #(0 0 0)
118 #(0 0 0 0)))
119
120 (define (%%finish-interval lower-bounds upper-bounds)
121 (make-%%interval (vector-length upper-bounds)
122 #f
123 (vector-copy lower-bounds)
124 (vector-copy upper-bounds)))
125
126 (define make-interval
127 (case-lambda
128 ((upper-bounds)
129 (cond ((not (and (vector? upper-bounds)
130 (fx< 0 (vector-length upper-bounds))
131 (%%vector-every (lambda (x) (exact-integer? x)) upper-bounds)
132 (%%vector-every (lambda (x) (positive? x)) upper-bounds)))
133 (error "make-interval: The argument is not a nonempty vector of positive exact integers: " upper-bounds))
134 (else
135 (let ((dimension (vector-length upper-bounds)))
136 (%%finish-interval (if (fx< dimension 5)
137 (vector-ref %%vector-of-zeros dimension)
138 (make-vector dimension 0))
139 (vector-copy upper-bounds))))))
140 ((lower-bounds upper-bounds)
141 (cond ((not (and (vector? lower-bounds)
142 (fx< 0 (vector-length lower-bounds))
143 (%%vector-every (lambda (x) (exact-integer? x)) lower-bounds)))
144 (error "make-interval: The first argument is not a nonempty vector of exact integers: " lower-bounds upper-bounds))
145 ((not (and (vector? upper-bounds)
146 (fx< 0 (vector-length upper-bounds))
147 (%%vector-every (lambda (x) (exact-integer? x)) upper-bounds)))
148 (error "make-interval: The second argument is not a nonempty vector of exact integers: " lower-bounds upper-bounds))
149 ((not (fx= (vector-length lower-bounds)
150 (vector-length upper-bounds)))
151 (error "make-interval: The first and second arguments are not the same length: " lower-bounds upper-bounds))
152 ((not (%%vector-every (lambda (x y) (< x y)) lower-bounds upper-bounds))
153 (error "make-interval: Each lower-bound must be less than the associated upper-bound: " lower-bounds upper-bounds))
154 (else
155 (%%finish-interval (vector-copy lower-bounds)
156 (vector-copy upper-bounds)))))))
157
158 (declare (inline))
159
160 #|
161
162 ;;; Now a cached field of %%interval
163
164 (define (%%interval-dimension interval)
165 (vector-length (%%interval-lower-bounds interval)))
166
167 |#
168
169 (define (%%interval-lower-bound interval i)
170 (vector-ref (%%interval-lower-bounds interval) i))
171
172 (define (%%interval-upper-bound interval i)
173 (vector-ref (%%interval-upper-bounds interval) i))
174
175 (define (%%interval-lower-bounds->vector interval)
176 (vector-copy (%%interval-lower-bounds interval)))
177
178 (define (%%interval-upper-bounds->vector interval)
179 (vector-copy (%%interval-upper-bounds interval)))
180
181 (define (%%interval-lower-bounds->list interval)
182 (vector->list (%%interval-lower-bounds interval)))
183
184 (define (%%interval-upper-bounds->list interval)
185 (vector->list (%%interval-upper-bounds interval)))
186
187 (declare (not inline))
188
189 (define (interval-dimension interval)
190 (cond ((not (interval? interval))
191 (error "interval-dimension: The argument is not an interval: " interval))
192 (else
193 (%%interval-dimension interval))))
194
195 (define (interval-lower-bound interval i)
196 (cond ((not (interval? interval))
197 (error "interval-lower-bound: The first argument is not an interval: " interval i))
198 ((not (and (fixnum? i)
199 (fx< -1 i (%%interval-dimension interval))))
200 (error "interval-lower-bound: The second argument is not an exact integer between 0 (inclusive) and (interval-dimension interval) (exclusive): " interval i))
201 (else
202 (%%interval-lower-bound interval i))))
203
204 (define (interval-upper-bound interval i)
205 (cond ((not (interval? interval))
206 (error "interval-upper-bound: The first argument is not an interval: " interval i))
207 ((not (and (fixnum? i)
208 (fx< -1 i (%%interval-dimension interval))))
209 (error "interval-upper-bound: The second argument is not an exact integer between 0 (inclusive) and (interval-dimension interval) (exclusive): " interval i))
210 (else
211 (%%interval-upper-bound interval i))))
212
213 (define (interval-lower-bounds->vector interval)
214 (cond ((not (interval? interval))
215 (error "interval-lower-bounds->vector: The argument is not an interval: " interval))
216 (else
217 (%%interval-lower-bounds->vector interval))))
218
219 (define (interval-upper-bounds->vector interval)
220 (cond ((not (interval? interval))
221 (error "interval-upper-bounds->vector: The argument is not an interval: " interval))
222 (else
223 (%%interval-upper-bounds->vector interval))))
224
225 (define (interval-lower-bounds->list interval)
226 (cond ((not (interval? interval))
227 (error "interval-lower-bounds->list: The argument is not an interval: " interval))
228 (else
229 (%%interval-lower-bounds->list interval))))
230
231 (define (interval-upper-bounds->list interval)
232 (cond ((not (interval? interval))
233 (error "interval-upper-bounds->list: The argument is not an interval: " interval))
234 (else
235 (%%interval-upper-bounds->list interval))))
236
237 (define (interval-projections interval right-dimension)
238 (cond ((not (interval? interval))
239 (error "interval-projections: The first argument is not an interval: " interval right-dimension))
240 ((not (fx< 1 (%%interval-dimension interval))) ;; redundant check, but useful error message
241 (error "interval-projections: The dimension of the first argument is not greater than 1: " interval right-dimension))
242 ((not (and (fixnum? right-dimension)
243 (fx< 0 right-dimension (%%interval-dimension interval))))
244 (error "interval-projections: The second argument is not an exact integer between 0 and the dimension of the first argument (exclusive): " interval right-dimension))
245 (else
246 (%%interval-projections interval right-dimension))))
247
248 (define (%%interval-projections interval right-dimension)
249 (let* ((n (%%interval-dimension interval))
250 (left-dimension (fx- n right-dimension))
251 (lower-bounds (%%interval-lower-bounds interval))
252 (upper-bounds (%%interval-upper-bounds interval))
253 (left-lower-bounds (make-vector left-dimension))
254 (left-upper-bounds (make-vector left-dimension))
255 (right-lower-bounds (make-vector (- n left-dimension)))
256 (right-upper-bounds (make-vector (- n left-dimension))))
257 (do ((i 0 (fx+ i 1)))
258 ((fx= i left-dimension)
259 (do ((i i (fx+ i 1)))
260 ((fx= i n)
261 (values (%%finish-interval left-lower-bounds
262 left-upper-bounds)
263 (%%finish-interval right-lower-bounds
264 right-upper-bounds)))
265 (vector-set! right-lower-bounds (fx- i left-dimension) (vector-ref lower-bounds i))
266 (vector-set! right-upper-bounds (fx- i left-dimension) (vector-ref upper-bounds i))))
267 (vector-set! left-lower-bounds i (vector-ref lower-bounds i))
268 (vector-set! left-upper-bounds i (vector-ref upper-bounds i)))))
269
270 (define (permutation? permutation)
271 (and (vector? permutation)
272 (let* ((n (vector-length permutation))
273 (permutation-range (make-vector n #f)))
274 ;; we'll write things into permutation-range
275 ;; each box should be written only once
276 (let loop ((i 0))
277 (or (fx= i n)
278 (let ((p_i (vector-ref permutation i)))
279 (and (fixnum? p_i) ;; a permutation index can't be a bignum
280 (fx< -1 p_i n)
281 (not (vector-ref permutation-range p_i))
282 (let ()
283 (vector-set! permutation-range p_i #t)
284 (loop (fx+ i 1))))))))))
285
286
287
288 (define (%%vector-permute vector permutation)
289 (let* ((n (vector-length vector))
290 (result (make-vector n)))
291 (do ((i 0 (fx+ i 1)))
292 ((fx= i n) result)
293 (vector-set! result i (vector-ref vector (vector-ref permutation i))))))
294
295 (define (%%vector-permute->list vector permutation)
296 (do ((i (fx- (vector-length vector) 1) (fx- i 1))
297 (result '() (cons (vector-ref vector (vector-ref permutation i))
298 result)))
299 ((fx< i 0) result)))
300
301 (define (%%permutation-invert permutation)
302 (let* ((n (vector-length permutation))
303 (result (make-vector n)))
304 (do ((i 0 (fx+ i 1)))
305 ((fx= i n) result)
306 (vector-set! result (vector-ref permutation i) i))))
307
308
309
310 (define (%%interval-permute interval permutation)
311 (%%finish-interval (%%vector-permute (%%interval-lower-bounds interval) permutation)
312 (%%vector-permute (%%interval-upper-bounds interval) permutation)))
313
314 (define (interval-permute interval permutation)
315 (cond ((not (interval? interval))
316 (error "interval-permute: The first argument is not an interval: " interval permutation))
317 ((not (permutation? permutation))
318 (error "interval-permute: The second argument is not a permutation: " interval permutation))
319 ((not (fx= (%%interval-dimension interval) (vector-length permutation)))
320 (error "interval-permute: The dimension of the first argument (an interval) does not equal the length of the second (a permutation): " interval permutation))
321 (else
322 (%%interval-permute interval permutation))))
323
324 (define (translation? translation)
325 (and (vector? translation)
326 (%%vector-every (lambda (x) (exact-integer? x)) translation)))
327
328 (define (interval-translate interval translation)
329 (cond ((not (interval? interval))
330 (error "interval-translate: The first argument is not an interval: " interval translation))
331 ((not (translation? translation))
332 (error "interval-translate: The second argument is not a vector of exact integers: " interval translation))
333 ((not (fx= (%%interval-dimension interval)
334 (vector-length translation)))
335 (error "interval-translate: The dimension of the first argument (an interval) does not equal the length of the second (a vector): " interval translation))
336 (else
337 (%%interval-translate interval translation))))
338
339 (define (%%interval-translate Interval translation)
340 (%%finish-interval (vector-map (lambda (x y) (+ x y)) (%%interval-lower-bounds Interval) translation)
341 (vector-map (lambda (x y) (+ x y)) (%%interval-upper-bounds Interval) translation)))
342
343 (define (%%interval-scale interval scales)
344 (let* ((uppers (%%interval-upper-bounds interval))
345 (lowers (%%interval-lower-bounds interval))
346 (new-uppers (vector-map (lambda (u s)
347 (quotient (+ u s -1) s))
348 uppers scales)))
349 (%%finish-interval lowers new-uppers)))
350
351 (define (interval-scale interval scales)
352 (cond ((not (and (interval? interval)
353 (%%vector-every (lambda (x) (eqv? 0 x)) (%%interval-lower-bounds interval))))
354 (error "interval-scale: The first argument is not an interval with all lower bounds zero: " interval scales))
355 ((not (and (vector? scales)
356 (%%vector-every (lambda (x) (exact-integer? x)) scales)
357 (%%vector-every (lambda (x) (positive? x)) scales)))
358 (error "interval-scale: The second argument is not a vector of positive, exact, integers: " interval scales))
359 ((not (fx= (vector-length scales) (%%interval-dimension interval)))
360 (error "interval-scale: The dimension of the first argument (an interval) is not equal to the length of the second (a vector): "
361 interval scales))
362 (else
363 (%%interval-scale interval scales))))
364
365 (define (%%interval-cartesian-product intervals)
366 (%%finish-interval (vector-concatenate (map %%interval-lower-bounds intervals))
367 (vector-concatenate (map %%interval-upper-bounds intervals))))
368
369 (define (interval-cartesian-product interval #!rest intervals)
370 (let ((intervals (cons interval intervals)))
371 (cond ((not (%%every interval? intervals))
372 (apply error "interval-cartesian-product: Not all arguments are intervals: " intervals))
373 (else
374 (%%interval-cartesian-product intervals)))))
375
376 (define (interval-dilate interval lower-diffs upper-diffs)
377 (cond ((not (interval? interval))
378 (error "interval-dilate: The first argument is not an interval: " interval lower-diffs upper-diffs))
379 ((not (and (vector? lower-diffs)
380 (%%vector-every (lambda (x) (exact-integer? x)) lower-diffs)))
381 (error "interval-dilate: The second argument is not a vector of exact integers: " interval lower-diffs upper-diffs))
382 ((not (and (vector? upper-diffs)
383 (%%vector-every (lambda (x) (exact-integer? x)) upper-diffs)))
384 (error "interval-dilate: The third argument is not a vector of exact integers: " interval lower-diffs upper-diffs))
385 ((not (fx= (vector-length lower-diffs)
386 (vector-length upper-diffs)
387 (%%interval-dimension interval)))
388 (error "interval-dilate: The second and third arguments must have the same length as the dimension of the first argument: " interval lower-diffs upper-diffs))
389 (else
390 (let ((new-lower-bounds (vector-map (lambda (x y) (+ x y)) (%%interval-lower-bounds interval) lower-diffs))
391 (new-upper-bounds (vector-map (lambda (x y) (+ x y)) (%%interval-upper-bounds interval) upper-diffs)))
392 (if (%%vector-every (lambda (x y) (< x y)) new-lower-bounds new-upper-bounds)
393 (%%finish-interval new-lower-bounds new-upper-bounds)
394 (error "interval-dilate: The resulting interval is empty: " interval lower-diffs upper-diffs))))))
395
396 (define (%%interval-volume interval)
397 (or (%%interval-%%volume interval)
398 (let* ((upper-bounds
399 (%%interval-upper-bounds interval))
400 (lower-bounds
401 (%%interval-lower-bounds interval))
402 (dimension
403 (%%interval-dimension interval))
404 (volume
405 (do ((i (fx- dimension 1) (fx- i 1))
406 (result 1 (* result (- (vector-ref upper-bounds i)
407 (vector-ref lower-bounds i)))))
408 ((fx< i 0) result))))
409 (%%interval-%%volume-set! interval volume)
410 volume)))
411
412 (define (interval-volume interval)
413 (cond ((not (interval? interval))
414 (error "interval-volume: The argument is not an interval: " interval))
415 (else
416 (%%interval-volume interval))))
417
418 (define (%%interval= interval1 interval2)
419 ;; This can be used a fair amount, so we open-code it
420 (or (eq? interval1 interval2)
421 (and (let ((upper1 (%%interval-upper-bounds interval1))
422 (upper2 (%%interval-upper-bounds interval2)))
423 (or (eq? upper1 upper2)
424 (and (fx= (vector-length upper1) (vector-length upper2))
425 (%%vector-every (lambda (x y) (= x y)) upper1 upper2))))
426 (let ((lower1 (%%interval-lower-bounds interval1))
427 (lower2 (%%interval-lower-bounds interval2)))
428 (or (eq? lower1 lower2)
429 ;; We don't need to check that the two lower bounds
430 ;; are the same length after checking the upper bounds
431 (%%vector-every (lambda (x y) (= x y)) lower1 lower2))))))
432
433 (define (interval= interval1 interval2)
434 (cond ((not (and (interval? interval1)
435 (interval? interval2)))
436 (error "interval=: Not all arguments are intervals: " interval1 interval2))
437 (else
438 (%%interval= interval1 interval2))))
439
440 (define (%%interval-subset? interval1 interval2)
441 (and (fx= (%%interval-dimension interval1) (%%interval-dimension interval2))
442 (%%vector-every (lambda (x y) (>= x y)) (%%interval-lower-bounds interval1) (%%interval-lower-bounds interval2))
443 (%%vector-every (lambda (x y) (<= x y)) (%%interval-upper-bounds interval1) (%%interval-upper-bounds interval2))))
444
445 (define (interval-subset? interval1 interval2)
446 (cond ((not (and (interval? interval1)
447 (interval? interval2)))
448 (error "interval-subset?: Not all arguments are intervals: " interval1 interval2))
449 ((not (fx= (%%interval-dimension interval1)
450 (%%interval-dimension interval2)))
451 (error "interval-subset?: The arguments do not have the same dimension: " interval1 interval2))
452 (else
453 (%%interval-subset? interval1 interval2))))
454
455 (define (%%interval-intersect intervals)
456 (let ((lower-bounds (apply vector-map max (map %%interval-lower-bounds intervals)))
457 (upper-bounds (apply vector-map min (map %%interval-upper-bounds intervals))))
458 (and (%%vector-every (lambda (x y) (< x y)) lower-bounds upper-bounds)
459 (%%finish-interval lower-bounds upper-bounds))))
460
461 (define (interval-intersect interval #!rest intervals)
462 (if (null? intervals)
463 (if (interval? interval)
464 interval
465 (error "interval-intersect: The argument is not an interval: " interval))
466 (let ((intervals (cons interval intervals)))
467 (cond ((not (%%every interval? intervals))
468 (apply error "interval-intersect: Not all arguments are intervals: " intervals))
469 ((let* ((dims (map %%interval-dimension intervals))
470 (dim1 (car dims)))
471 (not (%%every (lambda (dim) (fx= dim dim1)) (cdr dims))))
472 (apply error "interval-intersect: Not all arguments have the same dimension: " intervals))
473 (else
474 (%%interval-intersect intervals))))))
475
476 (declare (inline))
477
478 (define (%%interval-contains-multi-index?-1 interval i)
479 (and (<= (%%interval-lower-bound interval 0) i) (< i (%%interval-upper-bound interval 0))))
480
481 (define (%%interval-contains-multi-index?-2 interval i j)
482 (and (<= (%%interval-lower-bound interval 0) i) (< i (%%interval-upper-bound interval 0))
483 (<= (%%interval-lower-bound interval 1) j) (< j (%%interval-upper-bound interval 1))))
484
485 (define (%%interval-contains-multi-index?-3 interval i j k)
486 (and (<= (%%interval-lower-bound interval 0) i) (< i (%%interval-upper-bound interval 0))
487 (<= (%%interval-lower-bound interval 1) j) (< j (%%interval-upper-bound interval 1))
488 (<= (%%interval-lower-bound interval 2) k) (< k (%%interval-upper-bound interval 2))))
489
490 (define (%%interval-contains-multi-index?-4 interval i j k l)
491 (and (<= (%%interval-lower-bound interval 0) i) (< i (%%interval-upper-bound interval 0))
492 (<= (%%interval-lower-bound interval 1) j) (< j (%%interval-upper-bound interval 1))
493 (<= (%%interval-lower-bound interval 2) k) (< k (%%interval-upper-bound interval 2))
494 (<= (%%interval-lower-bound interval 3) l) (< l (%%interval-upper-bound interval 3))))
495
496 (declare (not inline))
497
498 (define (%%interval-contains-multi-index?-general interval multi-index)
499 (let loop ((i 0)
500 (multi-index multi-index))
501 (or (null? multi-index)
502 (let ((component (car multi-index)))
503 (and (<= (%%interval-lower-bound interval i) component)
504 (< component (%%interval-upper-bound interval i))
505 (loop (fx+ i 1)
506 (cdr multi-index)))))))
507
508 (define (interval-contains-multi-index? interval i #!rest multi-index-tail)
509
510 ;; this is relatively slow, but (a) I haven't seen a need to use it yet, and (b) this formulation
511 ;; significantly simplifies testing the error checking
512
513 (cond ((not (interval? interval))
514 (error "interval-contains-multi-index?: The first argument is not an interval: " interval))
515 (else
516 (let ((multi-index (cons i multi-index-tail)))
517 (cond ((not (fx= (%%interval-dimension interval)
518 (length multi-index)))
519 (apply error "interval-contains-multi-index?: The dimension of the first argument (an interval) does not match number of indices: " interval multi-index))
520 ((not (%%every (lambda (x) (exact-integer? x)) multi-index))
521 (apply error "interval-contains-multi-index?: At least one multi-index component is not an exact integer: " interval multi-index))
522 (else
523 (%%interval-contains-multi-index?-general interval multi-index)))))))
524
525 ;;; Applies f to every element of the domain; assumes that f is thread-safe,
526 ;;; the order of application is not specified
527
528 (define (interval-for-each f interval)
529 (cond ((not (interval? interval))
530 (error "interval-for-each: The second argument is not a interval: " interval))
531 ((not (procedure? f))
532 (error "interval-for-each: The first argument is not a procedure: " f))
533 (else
534 (%%interval-for-each f interval))))
535
536 (define (%%interval-for-each f interval)
537 (case (%%interval-dimension interval)
538 ((1) (let ((lower-i (%%interval-lower-bound interval 0))
539 (upper-i (%%interval-upper-bound interval 0)))
540 (let i-loop ((i lower-i))
541 (if (< i upper-i)
542 (begin
543 (f i)
544 (i-loop (+ i 1)))))))
545 ((2) (let ((lower-i (%%interval-lower-bound interval 0))
546 (lower-j (%%interval-lower-bound interval 1))
547 (upper-i (%%interval-upper-bound interval 0))
548 (upper-j (%%interval-upper-bound interval 1)))
549 (let i-loop ((i lower-i))
550 (if (< i upper-i)
551 (let j-loop ((j lower-j))
552 (if (< j upper-j)
553 (begin
554 (f i j)
555 (j-loop (+ j 1)))
556 (i-loop (+ i 1))))))))
557 ((3) (let ((lower-i (%%interval-lower-bound interval 0))
558 (lower-j (%%interval-lower-bound interval 1))
559 (lower-k (%%interval-lower-bound interval 2))
560 (upper-i (%%interval-upper-bound interval 0))
561 (upper-j (%%interval-upper-bound interval 1))
562 (upper-k (%%interval-upper-bound interval 2)))
563 (let i-loop ((i lower-i))
564 (if (< i upper-i)
565 (let j-loop ((j lower-j))
566 (if (< j upper-j)
567 (let k-loop ((k lower-k))
568 (if (< k upper-k)
569 (begin
570 (f i j k)
571 (k-loop (+ k 1)))
572 (j-loop (+ j 1))))
573 (i-loop (+ i 1))))))))
574 ((4) (let ((lower-i (%%interval-lower-bound interval 0))
575 (lower-j (%%interval-lower-bound interval 1))
576 (lower-k (%%interval-lower-bound interval 2))
577 (lower-l (%%interval-lower-bound interval 3))
578 (upper-i (%%interval-upper-bound interval 0))
579 (upper-j (%%interval-upper-bound interval 1))
580 (upper-k (%%interval-upper-bound interval 2))
581 (upper-l (%%interval-upper-bound interval 3)))
582 (let i-loop ((i lower-i))
583 (if (< i upper-i)
584 (let j-loop ((j lower-j))
585 (if (< j upper-j)
586 (let k-loop ((k lower-k))
587 (if (< k upper-k)
588 (let l-loop ((l lower-l))
589 (if (< l upper-l)
590 (begin
591 (f i j k l)
592 (l-loop (+ l 1)))
593 (k-loop (+ k 1))))
594 (j-loop (+ j 1))))
595 (i-loop (+ i 1))))))))
596 (else
597
598 (let* ((lower-bounds (%%interval-lower-bounds->list interval))
599 (upper-bounds (%%interval-upper-bounds->list interval))
600 (arg (map values lower-bounds))) ; copy lower-bounds
601
602 ;; I'm not particularly happy with set! here because f might capture the continuation
603 ;; and then funny things might pursue ...
604 ;; But it seems that the only way to have this work efficiently without the set
605 ;; is to have arrays with fortran-style numbering.
606 ;; blah
607
608 (define (iterate lower-bounds-tail
609 upper-bounds-tail
610 arg-tail)
611 (let ((lower-bound (car lower-bounds-tail))
612 (upper-bound (car upper-bounds-tail)))
613 (if (null? (cdr arg-tail))
614 (let loop ((i lower-bound))
615 (if (< i upper-bound)
616 (begin
617 (set-car! arg-tail i)
618 (apply f arg)
619 (loop (+ i 1)))))
620 (let loop ((i lower-bound))
621 (if (< i upper-bound)
622 (begin
623 (set-car! arg-tail i)
624 (iterate (cdr lower-bounds-tail)
625 (cdr upper-bounds-tail)
626 (cdr arg-tail))
627 (loop (+ i 1))))))))
628
629 (iterate lower-bounds
630 upper-bounds
631 arg)))))
632
633 ;;; Calculates
634 ;;;
635 ;;; (...(operator (operator (operator identity (f multi-index_1)) (f multi-index_2)) (f multi-index_3)) ...)
636 ;;;
637 ;;; where multi-index_1, multi-index_2, ... are the elements of interval in lexicographical order
638 ;;; This version assumes, and may use, that f is thread-safe and that operator is associative.
639 ;;; The order of application of f and operator is not specified.
640
641 (define (%%interval-fold f operator identity interval)
642 (case (%%interval-dimension interval)
643 ((1) (let ((lower-i (%%interval-lower-bound interval 0))
644 (upper-i (%%interval-upper-bound interval 0)))
645 (let i-loop ((i lower-i) (result identity))
646 (if (= i upper-i)
647 result
648 (i-loop (+ i 1) (operator (f i) result))))))
649 ((2) (let ((lower-i (%%interval-lower-bound interval 0))
650 (lower-j (%%interval-lower-bound interval 1))
651 (upper-i (%%interval-upper-bound interval 0))
652 (upper-j (%%interval-upper-bound interval 1)))
653 (let i-loop ((i lower-i) (result identity))
654 (if (= i upper-i)
655 result
656 (let j-loop ((j lower-j) (result result))
657 (if (= j upper-j)
658 (i-loop (+ i 1) result)
659 (j-loop (+ j 1) (operator (f i j) result))))))))
660 ((3) (let ((lower-i (%%interval-lower-bound interval 0))
661 (lower-j (%%interval-lower-bound interval 1))
662 (lower-k (%%interval-lower-bound interval 2))
663 (upper-i (%%interval-upper-bound interval 0))
664 (upper-j (%%interval-upper-bound interval 1))
665 (upper-k (%%interval-upper-bound interval 2)))
666 (let i-loop ((i lower-i) (result identity))
667 (if (= i upper-i)
668 result
669 (let j-loop ((j lower-j) (result result))
670 (if (= j upper-j)
671 (i-loop (+ i 1) result)
672 (let k-loop ((k lower-k) (result result))
673 (if (= k upper-k)
674 (j-loop (+ j 1) result)
675 (k-loop (+ k 1) (operator (f i j k) result))))))))))
676 ((4) (let ((lower-i (%%interval-lower-bound interval 0))
677 (lower-j (%%interval-lower-bound interval 1))
678 (lower-k (%%interval-lower-bound interval 2))
679 (lower-l (%%interval-lower-bound interval 3))
680 (upper-i (%%interval-upper-bound interval 0))
681 (upper-j (%%interval-upper-bound interval 1))
682 (upper-k (%%interval-upper-bound interval 2))
683 (upper-l (%%interval-upper-bound interval 3)))
684 (let i-loop ((i lower-i) (result identity))
685 (if (= i upper-i)
686 result
687 (let j-loop ((j lower-j) (result result))
688 (if (= j upper-j)
689 (i-loop (+ i 1) result)
690 (let k-loop ((k lower-k) (result result))
691 (if (= k upper-k)
692 (j-loop (+ j 1) result)
693 (let l-loop ((l lower-l) (result result))
694 (if (= l upper-l)
695 (k-loop (+ k 1) result)
696 (l-loop (+ l 1) (operator (f i j k l) result))))))))))))
697 (else
698 (let* ((lower-bounds (%%interval-lower-bounds->list interval))
699 (upper-bounds (%%interval-upper-bounds->list interval))
700 (arg (map values lower-bounds))) ; copy lower-bounds
701
702 ;; I'm not particularly happy with set! here because f or operator might capture
703 ;; the continuation and then funny things might pursue ...
704 ;; But it seems that the only way to have this work efficiently without the set~
705 ;; is to have arrays with fortran-style numbering.
706 ;; blah
707
708 (define (iterate lower-bounds-tail
709 upper-bounds-tail
710 arg-tail
711 result)
712 (let ((lower-bound (car lower-bounds-tail))
713 (upper-bound (car upper-bounds-tail)))
714 (if (null? (cdr arg-tail))
715 (let loop ((i lower-bound)
716 (result result))
717 (if (= i upper-bound)
718 result
719 (begin
720 (set-car! arg-tail i)
721 (loop (+ i 1)
722 (operator (apply f arg) result)))))
723 (let loop ((i lower-bound)
724 (result result))
725 (if (= i upper-bound)
726 result
727 (begin
728 (set-car! arg-tail i)
729 (loop (+ i 1)
730 (iterate (cdr lower-bounds-tail)
731 (cdr upper-bounds-tail)
732 (cdr arg-tail)
733 result))))))))
734
735 (iterate lower-bounds
736 upper-bounds
737 arg
738 identity)))))
739
740 ;; We'll use the same basic container for all types of arrays.
741
742 (declare (inline))
743
744 (define-structure %%array
745 ;; Part of all arrays
746 domain ;; an interval
747 getter ;; (lambda (i_0 ... i_n-1) ...) returns a value for (i_0,...,i_n-1) in (array-domain a)
748 ;; Part of mutable arrays
749 setter ;; (lambda (v i_0 ... i_n-1) ...) sets a value for (i_0,...,i_n-1) in (array-domain a)
750 ;; Part of specialized arrays
751 storage-class ;; a storage-class
752 body ;; the backing store for this array
753 indexer ;; see below
754 safe? ;; do we check whether bounds (in getters and setters) and values (in setters) are valid
755 in-order? ;; are the elements adjacent and in order?
756 copier: #f ;; We'll define our own %%array-copy
757 )
758
759 (define specialized-array-default-safe?
760 (let ((%%specialized-array-default-safe? #f))
761 (case-lambda
762 (() %%specialized-array-default-safe?)
763 ((bool)
764 (cond ((not (boolean? bool))
765 (error "specialized-array-default-safe?: The argument is not a boolean: " bool))
766 (else
767 (set! %%specialized-array-default-safe? bool)))))))
768
769 (define specialized-array-default-mutable?
770 (let ((%%specialized-array-default-mutable? #t))
771 (case-lambda
772 (()
773 %%specialized-array-default-mutable?)
774 ((bool)
775 (cond ((not (boolean? bool))
776 (error "specialized-array-default-mutable?: The argument is not a boolean: " bool))
777 (else
778 (set! %%specialized-array-default-mutable? bool)))))))
779
780
781 (declare (not inline))
782
783 ;; An array has a domain (which is an interval) and an getter that maps that domain into some type of
784 ;; Scheme objects
785
786 (define %%order-unknown 1) ;; can be any nonboolean
787
788 (define make-array
789 (case-lambda
790 ((domain getter)
791 (cond ((not (interval? domain))
792 (error "make-array: The first argument is not an interval: " domain getter))
793 ((not (procedure? getter))
794 (error "make-array: The second argument is not a procedure: " domain getter))
795 (else
796 (make-%%array domain
797 getter
798 #f ; no setter
799 #f ; storage-class
800 #f ; body
801 #f ; indexer
802 #f ; safe?
803 %%order-unknown ; in-order?
804 ))))
805 ((domain getter setter)
806 (cond ((not (interval? domain))
807 (error "make-array: The first argument is not an interval: " domain getter setter))
808 ((not (procedure? getter))
809 (error "make-array: The second argument is not a procedure: " domain getter setter))
810 ((not (procedure? setter))
811 (error "make-array: The third argument is not a procedure: " domain getter setter))
812 (else
813 (make-%%array domain
814 getter
815 setter
816 #f ; storage-class
817 #f ; body
818 #f ; indexer
819 #f ; safe?
820 %%order-unknown ; in-order?
821 ))))))
822
823 (define (array? x)
824 (%%array? x))
825
826 (define (array-domain obj)
827 (cond ((not (array? obj))
828 (error "array-domain: The argument is not an array: " obj))
829 (else
830 (%%array-domain obj))))
831
832 (define (array-getter obj)
833 (cond ((not (array? obj))
834 (error "array-getter: The argument is not an array: " obj))
835 (else
836 (%%array-getter obj))))
837
838 (define (%%array-dimension array)
839 (%%interval-dimension (%%array-domain array)))
840
841 (define (array-dimension array)
842 (cond ((not (array? array))
843 (error "array-dimension: The argument is not an array: " array))
844 (else
845 (%%array-dimension array))))
846
847
848 ;;;
849 ;;; A mutable array has, in addition a setter, that satisfies, roughly
850 ;;;
851 ;;; If (i_1, ..., i_n)\neq (j_1, ..., j_n) \in (array-domain a)
852 ;;;
853 ;;; and
854 ;;;
855 ;;; ((array-getter a) j_1 ... j_n) => x
856 ;;;
857 ;;; then "after"
858 ;;;
859 ;;; ((array-setter a) v i_1 ... i_n)
860 ;;;
861 ;;; we have
862 ;;;
863 ;;; ((array-getter a) j_1 ... j_n) => x
864 ;;;
865 ;;; and
866 ;;;
867 ;;; ((array-getter a) i_1 ... i_n) => v
868 ;;;
869
870 (define (mutable-array? obj)
871 (and (array? obj)
872 (not (eq? (%%array-setter obj) #f))))
873
874 (define (array-setter obj)
875 (cond ((not (mutable-array? obj))
876 (error "array-setter: The argument is not an mutable array: " obj))
877 (else
878 (%%array-setter obj))))
879
880 ;;;
881 ;;; A storage-class contains functions and objects to manipulate the
882 ;;; backing store of a specialized-array.
883 ;;;
884 ;;; getter: (lambda (body i) ...) returns the value of body at index i
885 ;;; setter: (lambda (body i v) ...) sets the value of body at index i to v
886 ;;; checker: (lambda (val) ...) checks that val is an appropriate value for storing in (maker n)
887 ;;; maker: (lambda (n val) ...) makes a body of length n with value val
888 ;;; length: (lambda (body) ...) returns the number of objects in body
889 ;;; default: object is the default value with which to fill body
890 ;;;
891
892 (define-structure storage-class getter setter checker maker copier length default)
893
894 ;;; We define specialized storage-classes for:
895 ;;;
896 ;;; 32- and 64-bit floating-point numbers,
897 ;;; complex numbers with real and imaginary parts of 32- and 64-bit floating-point numbers respectively
898 ;;; 8-, 16-, 32-, and 64-bit signed integers,
899 ;;; 8-, 16-, 32-, and 64-bit unsigned integers, and
900 ;;; 1-bit unsigned integers
901 ;;;
902 ;;; as well as generic objects.
903
904 (define-macro (srfi/179#make-standard-storage-classes)
905
906 (define (symbol-concatenate . symbols)
907 (string->symbol (apply string-append (map (lambda (s)
908 (if (string? s)
909 s
910 (symbol->string s)))
911 symbols))))
912
913 `(begin
914 ,@(map (lambda (name prefix default checker)
915 `(define ,(symbol-concatenate name '-storage-class)
916 (make-storage-class
917 ;; getter:
918 (lambda (v i)
919 (,(symbol-concatenate prefix 'vector-ref) v i))
920 ;; setter:
921 (lambda (v i val)
922 (,(symbol-concatenate prefix 'vector-set!) v i val))
923 ;; checker
924 ,checker
925 ;; maker:
926 ,(symbol-concatenate 'make- prefix 'vector)
927 ;; copier
928 ,(symbol-concatenate prefix 'vector-copy!)
929 ;; length:
930 ,(symbol-concatenate prefix 'vector-length)
931 ;; default:
932 ,default)))
933 '(generic s8 u8 s16 u16 s32 u32 s64 u64 f32 f64)
934 '("" s8 u8 s16 u16 s32 u32 s64 u64 f32 f64)
935 '(#f 0 0 0 0 0 0 0 0 0.0 0.0)
936 `((lambda (x) #t) ; generic
937 (lambda (x) ; s8
938 (and (fixnum? x)
939 (fx<= ,(- (expt 2 7))
940 x
941 ,(- (expt 2 7) 1))))
942 (lambda (x) ; u8
943 (and (fixnum? x)
944 (fx<= 0
945 x
946 ,(- (expt 2 8) 1))))
947 (lambda (x) ; s16
948 (and (fixnum? x)
949 (fx<= ,(- (expt 2 15))
950 x
951 ,(- (expt 2 15) 1))))
952 (lambda (x) ; u16
953 (and (fixnum? x)
954 (fx<= 0
955 x
956 ,(- (expt 2 16) 1))))
957 (lambda (x) ; s32
958 (declare (generic))
959 (and (exact-integer? x)
960 (<= ,(- (expt 2 31))
961 x
962 ,(- (expt 2 31) 1))))
963 (lambda (x) ; u32
964 (declare (generic))
965 (and (exact-integer? x)
966 (<= 0
967 x
968 ,(- (expt 2 32) 1))))
969 (lambda (x) ; s64
970 (declare (generic))
971 (and (exact-integer? x)
972 (<= ,(- (expt 2 63))
973 x
974 ,(- (expt 2 63) 1))))
975 (lambda (x) ; u64
976 (declare (generic))
977 (and (exact-integer? x)
978 (<= 0
979 x
980 ,(- (expt 2 64) 1))))
981 (lambda (x) (flonum? x)) ; f32
982 (lambda (x) (flonum? x)) ; f64
983 ))))
984
985 (make-standard-storage-classes)
986
987 ;;; This sample implementation does not implement the following.
988
989 (define f16-storage-class #f)
990 (define f8-storage-class #f)
991
992 ;;; for bit-arrays, body is a vector, the first element of which is the actual number of elements,
993 ;;; the second element of which is a u16vector that contains the bit string
994
995 (define u1-storage-class
996 (make-storage-class
997 ;; getter:
998 (lambda (v i)
999 (let ((index (fxarithmetic-shift-right i 4))
1000 (shift (fxand i 15))
1001 (bodyv (vector-ref v 1)))
1002 (fxand
1003 (fxarithmetic-shift-right
1004 (u16vector-ref bodyv index)
1005 shift)
1006 1)))
1007 ;; setter:
1008 (lambda (v i val)
1009 (let ((index (fxarithmetic-shift-right i 4))
1010 (shift (fxand i 15))
1011 (bodyv (vector-ref v 1)))
1012 (u16vector-set! bodyv index (fxior (fxarithmetic-shift-left val shift)
1013 (fxand (u16vector-ref bodyv index)
1014 (fxnot (fxarithmetic-shift-left 1 shift)))))))
1015 ;; checker
1016 (lambda (val)
1017 (and (fixnum? val)
1018 (eq? 0 (fxand -2 val))))
1019 ;; maker:
1020 (lambda (size initializer)
1021 (let ((u16-size (fxarithmetic-shift-right (+ size 15) 4)))
1022 (vector size (make-u16vector u16-size (if (eqv? 0 initializer) 0 65535)))))
1023 ;; no copier (for now)
1024 #f
1025 ;; length:
1026 (lambda (v)
1027 (vector-ref v 0))
1028 ;; default:
1029 0))
1030
1031 (define-macro (srfi/179#make-complex-storage-classes)
1032 (define (symbol-concatenate . symbols)
1033 (string->symbol (apply string-append (map (lambda (s)
1034 (if (string? s)
1035 s
1036 (symbol->string s)))
1037 symbols))))
1038 (define construct
1039 (lambda (size)
1040 (let ((prefix (string-append "c" (number->string (fx* 2 size))))
1041 (floating-point-prefix (string-append "f" (number->string size))))
1042 `(define ,(symbol-concatenate prefix '-storage-class)
1043 (make-storage-class
1044 ;; getter
1045 (lambda (body i)
1046 (make-rectangular (,(symbol-concatenate floating-point-prefix 'vector-ref) body (fx* 2 i))
1047 (,(symbol-concatenate floating-point-prefix 'vector-ref) body (fx+ (fx* 2 i) 1))))
1048 ;; setter
1049 (lambda (body i obj)
1050 (,(symbol-concatenate floating-point-prefix 'vector-set!) body (fx* 2 i) (real-part obj))
1051 (,(symbol-concatenate floating-point-prefix 'vector-set!) body (fx+ (fx* 2 i) 1) (imag-part obj)))
1052 ;; checker
1053 (lambda (obj)
1054 (and (complex? obj)
1055 (inexact? (real-part obj))
1056 (inexact? (imag-part obj))))
1057 ;; maker
1058 (lambda (n val)
1059 (let ((l (* 2 n))
1060 (re (real-part val))
1061 (im (imag-part val)))
1062 (let ((result (,(symbol-concatenate 'make-
1063 floating-point-prefix
1064 'vector)
1065 l)))
1066 (do ((i 0 (+ i 2)))
1067 ((= i l) result)
1068 (,(symbol-concatenate floating-point-prefix 'vector-set!) result i re)
1069 (,(symbol-concatenate floating-point-prefix 'vector-set!) result (fx+ i 1) im)))))
1070 ;; copier
1071 ,(symbol-concatenate prefix 'vector-copy!)
1072 ;; length
1073 (lambda (body)
1074 (fxquotient (,(symbol-concatenate floating-point-prefix 'vector-length) body) 2))
1075 ;; default
1076 0.+0.i)))))
1077 (let ((result
1078 `(begin
1079 ,@(map construct
1080 '(32 64)))))
1081 result))
1082
1083 (make-complex-storage-classes)
1084
1085 ;;;
1086 ;;; Conceptually, an indexer is itself a 1-1 array that maps one interval to another; thus, it is
1087 ;;; an example of an array that can return multiple values.
1088 ;;;
1089 ;;; Rather than trying to formalize this idea, and trying to get it to work with array-map,
1090 ;;; array-fold, ..., we'll just manipulate the getter functions of these conceptual arrays.
1091 ;;;
1092 ;;; Indexers are 1-1 affine maps from one interval to another.
1093 ;;;
1094 ;;; The indexer field of a specialized-array obj is a 1-1 mapping from
1095 ;;;
1096 ;;; (array-domain obj)
1097 ;;;
1098 ;;; to [0, top), where top is
1099 ;;;
1100 ;;; ((storage-class-length (array-storage-class obj)) (array-body obj))
1101 ;;;
1102
1103 ;; unfortunately, the next two functions were written by hand, so beware of bugs.
1104
1105 (define (%%indexer-1 base
1106 low-0
1107 increment-0)
1108 (if (eqv? base 0)
1109 (if (eqv? 0 low-0)
1110 (cond ((eqv? 1 increment-0) (lambda (i) i))
1111 ;;((eqv? -1 increment-0) (lambda (i) (- i))) ;; an impossible case
1112 (else (lambda (i) (* i increment-0))))
1113 (cond ((eqv? 1 increment-0) (lambda (i) (- i low-0)))
1114 ;;((eqv? -1 increment-0) (lambda (i) (- low-0 i))) ;; an impossible case
1115 (else (lambda (i) (* increment-0 (- i low-0))))))
1116 (if (eqv? 0 low-0)
1117 (cond ((eqv? 1 increment-0) (lambda (i) (+ base i)))
1118 ((eqv? -1 increment-0) (lambda (i) (- base i)))
1119 (else (lambda (i) (+ base (* increment-0 i)))))
1120 (cond ((eqv? 1 increment-0) (lambda (i) (+ base (- i low-0))))
1121 ((eqv? -1 increment-0) (lambda (i) (+ base (- low-0 i))))
1122 (else (lambda (i) (+ base (* increment-0 (- i low-0)))))))))
1123
1124 (define (%%indexer-2 base
1125 low-0 low-1
1126 increment-0 increment-1)
1127 (if (eqv? 0 base)
1128 (if (eqv? 0 low-0)
1129 (cond ((eqv? 1 increment-0)
1130 (if (eqv? 0 low-1)
1131 (cond ((eqv? 1 increment-1) (lambda (i j) (+ i j)))
1132 ((eqv? -1 increment-1) (lambda (i j) (+ i (- j))))
1133 (else (lambda (i j) (+ i (* increment-1 j)))))
1134 (cond ((eqv? 1 increment-1) (lambda (i j) (+ i (- j low-1))))
1135 ((eqv? -1 increment-1) (lambda (i j) (+ i (- low-1 j))))
1136 (else (lambda (i j) (+ i (* increment-1 (- j low-1))))))))
1137 #; ((eqv? -1 increment-0) ;; an impossible case
1138 (if (eqv? 0 low-1)
1139 (cond ((eqv? 1 increment-1) (lambda (i j) (- j i)))
1140 ((eqv? -1 increment-1) (lambda (i j) (- (- j) i)))
1141 (else (lambda (i j) (- (* increment-1 j) i))))
1142 (cond ((eqv? 1 increment-1) (lambda (i j) (- (- j low-1) i)))
1143 ((eqv? -1 increment-1) (lambda (i j) (- (- low-1 j) i)))
1144 (else (lambda (i j) (- (* increment-1 (- j low-1)) i))))))
1145 (else
1146 (if (eqv? 0 low-1)
1147 (cond ((eqv? 1 increment-1) (lambda (i j) (+ (* increment-0 i) j)))
1148 ((eqv? -1 increment-1) (lambda (i j) (+ (* increment-0 i) (- j))))
1149 (else (lambda (i j) (+ (* increment-0 i) (* increment-1 j)))))
1150 (cond ((eqv? 1 increment-1) (lambda (i j) (+ (* increment-0 i) (- j low-1))))
1151 ((eqv? -1 increment-1) (lambda (i j) (+ (* increment-0 i) (- low-1 j))))
1152 (else (lambda (i j) (+ (* increment-0 i) (* increment-1 (- j low-1)))))))))
1153 (cond ((eqv? 1 increment-0)
1154 (if (eqv? 0 low-1)
1155 (cond ((eqv? 1 increment-1) (lambda (i j) (+ (- i low-0) j)))
1156 ((eqv? -1 increment-1) (lambda (i j) (+ (- i low-0) (- j))))
1157 (else (lambda (i j) (+ (- i low-0) (* increment-1 j)))))
1158 (cond ((eqv? 1 increment-1) (lambda (i j) (+ (- i low-0) (- j low-1))))
1159 ((eqv? -1 increment-1) (lambda (i j) (+ (- i low-0) (- low-1 j))))
1160 (else (lambda (i j) (+ (- i low-0) (* increment-1 (- j low-1))))))))
1161 #;((eqv? -1 increment-0) ;; an impossible case
1162 (if (eqv? 0 low-1)
1163 (cond ((eqv? 1 increment-1) (lambda (i j) (- j (- i low-0))))
1164 ((eqv? -1 increment-1) (lambda (i j) (- (- j) (- i low-0))))
1165 (else (lambda (i j) (- (* increment-1 j) (- i low-0)))))
1166 (cond ((eqv? 1 increment-1) (lambda (i j) (- (- j low-1) (- i low-0))))
1167 ((eqv? -1 increment-1) (lambda (i j) (- (- low-1 j) (- i low-0))))
1168 (else (lambda (i j) (- (* increment-1 (- j low-1)) (- i low-0)))))))
1169 (else
1170 (if (eqv? 0 low-1)
1171 (cond ((eqv? 1 increment-1) (lambda (i j) (+ (* increment-0 (- i low-0)) j)))
1172 ((eqv? -1 increment-1) (lambda (i j) (+ (* increment-0 (- i low-0)) (- j))))
1173 (else (lambda (i j) (+ (* increment-0 (- i low-0)) (* increment-1 j)))))
1174 (cond ((eqv? 1 increment-1) (lambda (i j) (+ (* increment-0 (- i low-0)) (- j low-1))))
1175 ((eqv? -1 increment-1) (lambda (i j) (+ (* increment-0 (- i low-0)) (- low-1 j))))
1176 (else (lambda (i j) (+ (* increment-0 (- i low-0)) (* increment-1 (- j low-1))))))))))
1177 (if (eqv? 0 low-0)
1178 (cond ((eqv? 1 increment-0)
1179 (if (eqv? 0 low-1)
1180 (cond ((eqv? 1 increment-1) (lambda (i j) (+ base i j)))
1181 ((eqv? -1 increment-1) (lambda (i j) (+ base i (- j))))
1182 (else (lambda (i j) (+ base i (* increment-1 j)))))
1183 (cond ((eqv? 1 increment-1) (lambda (i j) (+ base i (- j low-1))))
1184 ((eqv? -1 increment-1) (lambda (i j) (+ base i (- low-1 j))))
1185 (else (lambda (i j) (+ base i (* increment-1 (- j low-1))))))))
1186 ((eqv? -1 increment-0)
1187 (if (eqv? 0 low-1)
1188 (cond ((eqv? 1 increment-1) (lambda (i j) (- (+ base j) i)))
1189 ((eqv? -1 increment-1) (lambda (i j) (- (- base j) i)))
1190 (else (lambda (i j) (- (+ base (* increment-1 j)) i))))
1191 (cond ((eqv? 1 increment-1) (lambda (i j) (- (+ base (- j low-1)) i)))
1192 ((eqv? -1 increment-1) (lambda (i j) (- (+ base (- low-1 j)) i)))
1193 (else (lambda (i j) (- (+ base (* increment-1 (- j low-1))) i))))))
1194 (else
1195 (if (eqv? 0 low-1)
1196 (cond ((eqv? 1 increment-1) (lambda (i j) (+ base (* increment-0 i) j)))
1197 ((eqv? -1 increment-1) (lambda (i j) (+ base (* increment-0 i) (- j))))
1198 (else (lambda (i j) (+ base (* increment-0 i) (* increment-1 j)))))
1199 (cond ((eqv? 1 increment-1) (lambda (i j) (+ base (* increment-0 i) (- j low-1))))
1200 ((eqv? -1 increment-1) (lambda (i j) (+ base (* increment-0 i) (- low-1 j))))
1201 (else (lambda (i j) (+ base (* increment-0 i) (* increment-1 (- j low-1)))))))))
1202 (cond ((eqv? 1 increment-0)
1203 (if (eqv? 0 low-1)
1204 (cond ((eqv? 1 increment-1) (lambda (i j) (+ base (- i low-0) j)))
1205 ((eqv? -1 increment-1) (lambda (i j) (+ base (- i low-0) (- j))))
1206 (else (lambda (i j) (+ base (- i low-0) (* increment-1 j)))))
1207 (cond ((eqv? 1 increment-1) (lambda (i j) (+ base (- i low-0) (- j low-1))))
1208 ((eqv? -1 increment-1) (lambda (i j) (+ base (- i low-0) (- low-1 j))))
1209 (else (lambda (i j) (+ base (- i low-0) (* increment-1 (- j low-1))))))))
1210 ((eqv? -1 increment-0)
1211 (if (eqv? 0 low-1)
1212 (cond ((eqv? 1 increment-1) (lambda (i j) (- (+ base j) (- i low-0))))
1213 ((eqv? -1 increment-1) (lambda (i j) (- (- base j) (- i low-0))))
1214 (else (lambda (i j) (- (+ base (* increment-1 j)) (- i low-0)))))
1215 (cond ((eqv? 1 increment-1) (lambda (i j) (- (+ base (- j low-1)) (- i low-0))))
1216 ((eqv? -1 increment-1) (lambda (i j) (- (+ base (- low-1 j)) (- i low-0))))
1217 (else (lambda (i j) (- (+ base (* increment-1 (- j low-1))) (- i low-0)))))))
1218 (else
1219 (if (eqv? 0 low-1)
1220 (cond ((eqv? 1 increment-1) (lambda (i j) (+ base (* increment-0 (- i low-0)) j)))
1221 ((eqv? -1 increment-1) (lambda (i j) (+ base (* increment-0 (- i low-0)) (- j))))
1222 (else (lambda (i j) (+ base (* increment-0 (- i low-0)) (* increment-1 j)))))
1223 (cond ((eqv? 1 increment-1) (lambda (i j) (+ base (* increment-0 (- i low-0)) (- j low-1))))
1224 ((eqv? -1 increment-1) (lambda (i j) (+ base (* increment-0 (- i low-0)) (- low-1 j))))
1225 (else (lambda (i j) (+ base (* increment-0 (- i low-0)) (* increment-1 (- j low-1))))))))))))
1226
1227 ;;; after this we basically punt
1228
1229 (define (%%indexer-3 base
1230 low-0 low-1 low-2
1231 increment-0 increment-1 increment-2)
1232 (if (and (eqv? 0 low-0)
1233 (eqv? 0 low-1)
1234 (eqv? 0 low-2))
1235 (if (eqv? base 0)
1236 (if (eqv? increment-2 1)
1237 (lambda (i j k)
1238 (+ (* increment-0 i)
1239 (* increment-1 j)
1240 k))
1241 (lambda (i j k)
1242 (+ (* increment-0 i)
1243 (* increment-1 j)
1244 (* increment-2 k))))
1245 (if (eqv? increment-2 1)
1246 (lambda (i j k)
1247 (+ base
1248 (* increment-0 i)
1249 (* increment-1 j)
1250 k))
1251 (lambda (i j k)
1252 (+ base
1253 (* increment-0 i)
1254 (* increment-1 j)
1255 (* increment-2 k)))))
1256 (if (eqv? base 0)
1257 (if (eqv? increment-2 1)
1258 (lambda (i j k)
1259 (+ (* increment-0 (- i low-0))
1260 (* increment-1 (- j low-1))
1261 (- k low-2)))
1262 (lambda (i j k)
1263 (+ (* increment-0 (- i low-0))
1264 (* increment-1 (- j low-1))
1265 (* increment-2 (- k low-2)))))
1266 (if (eqv? increment-2 1)
1267 (lambda (i j k)
1268 (+ base
1269 (* increment-0 (- i low-0))
1270 (* increment-1 (- j low-1))
1271 (- k low-2)))
1272 (lambda (i j k)
1273 (+ base
1274 (* increment-0 (- i low-0))
1275 (* increment-1 (- j low-1))
1276 (* increment-2 (- k low-2))))))))
1277
1278 (define (%%indexer-4 base
1279 low-0 low-1 low-2 low-3
1280 increment-0 increment-1 increment-2 increment-3)
1281 (if (and (eqv? 0 low-0)
1282 (eqv? 0 low-1)
1283 (eqv? 0 low-2)
1284 (eqv? 0 low-3))
1285 (if (eqv? base 0)
1286 (if (eqv? increment-3 1)
1287 (lambda (i j k l)
1288 (+ (* increment-0 i)
1289 (* increment-1 j)
1290 (* increment-2 k)
1291 l))
1292 (lambda (i j k l)
1293 (+ (* increment-0 i)
1294 (* increment-1 j)
1295 (* increment-2 k)
1296 (* increment-3 l))))
1297 (if (eqv? increment-3 1)
1298 (lambda (i j k l)
1299 (+ base
1300 (* increment-0 i)
1301 (* increment-1 j)
1302 (* increment-2 k)
1303 l))
1304 (lambda (i j k l)
1305 (+ base
1306 (* increment-0 i)
1307 (* increment-1 j)
1308 (* increment-2 k)
1309 (* increment-3 l)))))
1310 (if (eqv? base 0)
1311 (if (eqv? increment-3 1)
1312 (lambda (i j k l)
1313 (+ (* increment-0 (- i low-0))
1314 (* increment-1 (- j low-1))
1315 (* increment-2 (- k low-2))
1316 (- l low-3)))
1317 (lambda (i j k l)
1318 (+ (* increment-0 (- i low-0))
1319 (* increment-1 (- j low-1))
1320 (* increment-2 (- k low-2))
1321 (* increment-3 (- l low-3)))))
1322 (if (eqv? increment-3 1)
1323 (lambda (i j k l)
1324 (+ base
1325 (* increment-0 (- i low-0))
1326 (* increment-1 (- j low-1))
1327 (* increment-2 (- k low-2))
1328 (- l low-3)))
1329 (lambda (i j k l)
1330 (+ base
1331 (* increment-0 (- i low-0))
1332 (* increment-1 (- j low-1))
1333 (* increment-2 (- k low-2))
1334 (* increment-3 (- l low-3))))))))
1335
1336 (define (%%indexer-generic base lower-bounds increments)
1337 (let ((result
1338 (lambda multi-index
1339 (do ((multi-index multi-index (cdr multi-index))
1340 (lower-bounds lower-bounds (cdr lower-bounds))
1341 (increments increments (cdr increments))
1342 (result base (+ result (* (car increments)
1343 (- (car multi-index)
1344 (car lower-bounds))))))
1345 ((null? multi-index) result)))))
1346 result))
1347
1348
1349 ;;;
1350 ;;; The default getter and the setter of a specialized-array a are given by
1351 ;;;
1352 ;;; (lambda (i_0 ... i_n-1)
1353 ;;; ((storage-class-getter (array-storage-class a))
1354 ;;; (array-body a)
1355 ;;; ((array-indexer a) i_0 ... i_n-1)))
1356 ;;;
1357 ;;; (lambda (v i_0 ... i_n-1)
1358 ;;; ((storage-class-setter (array-storage-class a))
1359 ;;; (array-body a)
1360 ;;; ((array-indexer a) i_0 ... i_n-1)
1361 ;;; v))
1362 ;;;
1363 ;;; The default initializer-value is
1364 ;;;
1365 ;;; (storage-class-default (array-storage-class a))
1366 ;;;
1367 ;;; The default body is
1368 ;;;
1369 ;;; ((storage-class-maker (array-storage-class a))
1370 ;;; (interval-volume domain)
1371 ;;; initializer-value)
1372 ;;;
1373 ;;; The default indexer is the mapping of
1374 ;;; the domain to the natural numbers in lexicographical order.
1375 ;;;
1376
1377 (define (specialized-array? obj)
1378 (and (array? obj)
1379 (not (eq? (%%array-body obj) #f))))
1380
1381 (define (array-body obj)
1382 (cond ((not (specialized-array? obj))
1383 (error "array-body: The argument is not a specialized array: " obj))
1384 (else
1385 (%%array-body obj))))
1386
1387 (define (array-indexer obj)
1388 (cond ((not (specialized-array? obj))
1389 (error "array-indexer: The argument is not a specialized array: " obj))
1390 (else
1391 (%%array-indexer obj))))
1392
1393 (define (array-storage-class obj)
1394 (cond ((not (specialized-array? obj))
1395 (error "array-storage-class: The argument is not a specialized array: " obj))
1396 (else
1397 (%%array-storage-class obj))))
1398
1399 (define (array-safe? obj)
1400 (cond ((not (specialized-array? obj))
1401 (error "array-safe?: The argument is not a specialized array: " obj))
1402 (else
1403 (%%array-safe? obj))))
1404
1405 (define (%%compute-array-elements-in-order? domain indexer)
1406 (case (%%interval-dimension domain)
1407 ((1) (let ((lower-0 (%%interval-lower-bound domain 0))
1408 (upper-0 (%%interval-upper-bound domain 0)))
1409 (let ((increment 1))
1410 (or (eqv? 1 (- upper-0 lower-0))
1411 (= increment
1412 (- (indexer (+ lower-0 1))
1413 (indexer lower-0)))))))
1414 ((2) (let ((lower-0 (%%interval-lower-bound domain 0))
1415 (lower-1 (%%interval-lower-bound domain 1))
1416 (upper-0 (%%interval-upper-bound domain 0))
1417 (upper-1 (%%interval-upper-bound domain 1)))
1418 (let ((increment 1))
1419 (and (or (eqv? 1 (- upper-1 lower-1))
1420 (= increment
1421 (- (indexer lower-0 (+ lower-1 1))
1422 (indexer lower-0 lower-1))))
1423 (let ((increment (* increment (- upper-1 lower-1))))
1424 (or (eqv? 1 (- upper-0 lower-0))
1425 (= increment
1426 (- (indexer (+ lower-0 1) lower-1)
1427 (indexer lower-0 lower-1)))))))))
1428 ((3) (let ((lower-0 (%%interval-lower-bound domain 0))
1429 (lower-1 (%%interval-lower-bound domain 1))
1430 (lower-2 (%%interval-lower-bound domain 2))
1431 (upper-0 (%%interval-upper-bound domain 0))
1432 (upper-1 (%%interval-upper-bound domain 1))
1433 (upper-2 (%%interval-upper-bound domain 2)))
1434 (let ((increment 1))
1435 (and (or (eqv? 1 (- upper-2 lower-2))
1436 (= increment
1437 (- (indexer lower-0 lower-1 (+ lower-2 1))
1438 (indexer lower-0 lower-1 lower-2))))
1439 (let ((increment (* increment (- upper-2 lower-2))))
1440 (and (or (eqv? 1 (- upper-1 lower-1))
1441 (= increment
1442 (- (indexer lower-0 (+ lower-1 1) lower-2)
1443 (indexer lower-0 lower-1 lower-2))))
1444 (let ((increment (* increment (- upper-1 lower-1))))
1445 (or (eqv? 1 (- upper-0 lower-0))
1446 (= increment
1447 (- (indexer (+ lower-0 1) lower-1 lower-2)
1448 (indexer lower-0 lower-1 lower-2)))))))))))
1449 ((4) (let ((lower-0 (%%interval-lower-bound domain 0))
1450 (lower-1 (%%interval-lower-bound domain 1))
1451 (lower-2 (%%interval-lower-bound domain 2))
1452 (lower-3 (%%interval-lower-bound domain 3))
1453 (upper-0 (%%interval-upper-bound domain 0))
1454 (upper-1 (%%interval-upper-bound domain 1))
1455 (upper-2 (%%interval-upper-bound domain 2))
1456 (upper-3 (%%interval-upper-bound domain 3)))
1457 (let ((increment 1))
1458 (and (or (eqv? 1 (- upper-3 lower-3))
1459 (= increment
1460 (- (indexer lower-0 lower-1 lower-2 (+ lower-3 1))
1461 (indexer lower-0 lower-1 lower-2 lower-3))))
1462 (let ((increment (* increment (- upper-3 lower-3))))
1463 (and (or (eqv? 1 (- upper-2 lower-2))
1464 (= increment
1465 (- (indexer lower-0 lower-1 (+ lower-2 1) lower-3)
1466 (indexer lower-0 lower-1 lower-2 lower-3))))
1467 (let ((increment (* increment (- upper-2 lower-2))))
1468 (and (or (eqv? 1 (- upper-1 lower-1))
1469 (= increment
1470 (- (indexer lower-0 (+ lower-1 1) lower-2 lower-3)
1471 (indexer lower-0 lower-1 lower-2 lower-3))))
1472 (let ((increment (* increment (- upper-1 lower-1))))
1473 (or (eqv? 1 (- upper-0 lower-0))
1474 (= increment
1475 (- (indexer (+ lower-0 1) lower-1 lower-2 lower-3)
1476 (indexer lower-0 lower-1 lower-2 lower-3)))))))))))))
1477 (else (let ((global-lowers
1478 ;; will use as an argument list
1479 (%%interval-lower-bounds->list domain))
1480 (global-lowers+1
1481 ;; will modify and use as an argument list
1482 (%%interval-lower-bounds->list domain)))
1483 (and
1484 (let loop ((lowers global-lowers+1)
1485 (uppers (%%interval-upper-bounds->list domain)))
1486 ;; returns either #f or the increment
1487 ;; that the difference of indexers must equal.
1488 (if (null? lowers)
1489 1 ;; increment
1490 (let ((increment (loop (cdr lowers) (cdr uppers))))
1491 (and increment
1492 (or (and (eqv? 1 (- (car uppers) (car lowers)))
1493 ;; increment doesn't change
1494 increment)
1495 (begin
1496 ;; increment the correct index by 1
1497 (set-car! lowers (+ (car lowers) 1))
1498 (and (= (- (apply indexer global-lowers+1)
1499 (apply indexer global-lowers))
1500 increment)
1501 (begin
1502 ;; set it back
1503 (set-car! lowers (- (car lowers) 1))
1504 ;; multiply the increment by the difference in
1505 ;; the current upper and lower bounds and
1506 ;; return it.
1507 (* increment (- (car uppers) (car lowers)))))))))))
1508 ;; return a proper boolean instead of the volume of the domain
1509 #t)))))
1510
1511 (define (%%array-elements-in-order? array)
1512 (let ((in-order? (%%array-in-order? array)))
1513 (if (boolean? in-order?)
1514 in-order?
1515 (let ((in-order?
1516 (%%compute-array-elements-in-order?
1517 (%%array-domain array)
1518 (%%array-indexer array))))
1519 (%%array-in-order?-set! array in-order?)
1520 in-order?))))
1521
1522 (define (array-elements-in-order? array)
1523 (cond ((not (specialized-array? array))
1524 (error "array-elements-in-order?: The argument is not a specialized array: " array))
1525 (else
1526 (%%array-elements-in-order? array))))
1527
1528
1529 (define (%%finish-specialized-array domain storage-class body indexer mutable? safe? in-order?)
1530 (let ((storage-class-getter (storage-class-getter storage-class))
1531 (storage-class-setter (storage-class-setter storage-class))
1532 (checker (storage-class-checker storage-class))
1533 (indexer indexer)
1534 (body body))
1535
1536 ;;; we write the following three macros to specialize the setters and getters in the
1537 ;;; non-safe case to reduce one more function call.
1538
1539 (define-macro (expand-storage-class original-suffix replacement-suffix expr)
1540
1541 (define (symbol-append . args)
1542 (string->symbol (apply string-append (map (lambda (x) (if (symbol? x) (symbol->string x) x)) args))))
1543
1544 (define (replace old-symbol new-symbol expr)
1545 (let loop ((expr expr))
1546 (cond ((pair? expr) ;; we don't use map because of dotted argument list in general setter
1547 (cons (loop (car expr))
1548 (loop (cdr expr))))
1549 ((eq? expr old-symbol)
1550 new-symbol)
1551 (else
1552 expr))))
1553
1554 `(cond ,@(map (lambda (name prefix)
1555 `((eq? storage-class ,(symbol-append name '-storage-class))
1556 ,(replace (symbol-append 'storage-class original-suffix)
1557 (symbol-append prefix 'vector replacement-suffix)
1558 expr)))
1559 '(generic s8 u8 s16 u16 s32 u32 s64 u64 f32 f64)
1560 '("" s8 u8 s16 u16 s32 u32 s64 u64 f32 f64))
1561 (else
1562 ,expr)))
1563
1564 (define-macro (expand-getters expr)
1565 `(expand-storage-class -getter -ref ,expr))
1566
1567 (define-macro (expand-setters expr)
1568 `(expand-storage-class -setter -set! ,expr))
1569
1570 (let ((getter
1571 (if safe?
1572 (case (%%interval-dimension domain)
1573 ((1) (lambda (i)
1574 (cond ((not (exact-integer? i))
1575 (error "array-getter: multi-index component is not an exact integer: " i))
1576 ((not (%%interval-contains-multi-index?-1 domain i))
1577 (error "array-getter: domain does not contain multi-index: " domain i))
1578 (else
1579 (storage-class-getter body (indexer i))))))
1580 ((2) (lambda (i j)
1581 (cond ((not (and (exact-integer? i)
1582 (exact-integer? j)))
1583 (error "array-getter: multi-index component is not an exact integer: " i j))
1584 ((not (%%interval-contains-multi-index?-2 domain i j))
1585 (error "array-getter: domain does not contain multi-index: " domain i j))
1586 (else
1587 (storage-class-getter body (indexer i j))))))
1588 ((3) (lambda (i j k)
1589 (cond ((not (and (exact-integer? i)
1590 (exact-integer? j)
1591 (exact-integer? k)))
1592 (error "array-getter: multi-index component is not an exact integer: " i j k))
1593 ((not (%%interval-contains-multi-index?-3 domain i j k))
1594 (error "array-getter: domain does not contain multi-index: " domain i j k))
1595 (else
1596 (storage-class-getter body (indexer i j k))))))
1597 ((4) (lambda (i j k l)
1598 (cond ((not (and (exact-integer? i)
1599 (exact-integer? j)
1600 (exact-integer? k)
1601 (exact-integer? l)))
1602 (error "array-getter: multi-index component is not an exact integer: " i j k l))
1603 ((not (%%interval-contains-multi-index?-4 domain i j k l))
1604 (error "array-getter: domain does not contain multi-index: " domain i j k l))
1605 (else
1606 (storage-class-getter body (indexer i j k l))))))
1607 (else (lambda multi-index
1608 (cond ((not (%%every (lambda (x) (exact-integer? x)) multi-index))
1609 (apply error "array-getter: multi-index component is not an exact integer: " multi-index))
1610 ((not (fx= (%%interval-dimension domain) (length multi-index)))
1611 (apply error "array-getter: multi-index is not the correct dimension: " domain multi-index))
1612 ((not (%%interval-contains-multi-index?-general domain multi-index))
1613 (apply error "array-getter: domain does not contain multi-index: " domain multi-index))
1614 (else
1615 (storage-class-getter body (apply indexer multi-index)))))))
1616 (case (%%interval-dimension domain)
1617 ((1) (expand-getters (lambda (i) (storage-class-getter body (indexer i)))))
1618 ((2) (expand-getters (lambda (i j) (storage-class-getter body (indexer i j)))))
1619 ((3) (expand-getters (lambda (i j k) (storage-class-getter body (indexer i j k)))))
1620 ((4) (expand-getters (lambda (i j k l) (storage-class-getter body (indexer i j k l)))))
1621 (else (expand-getters (lambda multi-index (storage-class-getter body (apply indexer multi-index))))))))
1622 (setter
1623 (and mutable?
1624 (if safe?
1625 (case (%%interval-dimension domain)
1626 ((1) (lambda (value i)
1627 (cond ((not (exact-integer? i))
1628 (error "array-setter: multi-index component is not an exact integer: " i))
1629 ((not (%%interval-contains-multi-index?-1 domain i))
1630 (error "array-setter: domain does not contain multi-index: " domain i))
1631 ((not (checker value))
1632 (error "array-setter: value cannot be stored in body: " value))
1633 (else
1634 (storage-class-setter body (indexer i) value)))))
1635 ((2) (lambda (value i j)
1636 (cond ((not (and (exact-integer? i)
1637 (exact-integer? j)))
1638 (error "array-setter: multi-index component is not an exact integer: " i j))
1639 ((not (%%interval-contains-multi-index?-2 domain i j))
1640 (error "array-setter: domain does not contain multi-index: " domain i j))
1641 ((not (checker value))
1642 (error "array-setter: value cannot be stored in body: " value))
1643 (else
1644 (storage-class-setter body (indexer i j) value)))))
1645 ((3) (lambda (value i j k)
1646 (cond ((not (and (exact-integer? i)
1647 (exact-integer? j)
1648 (exact-integer? k)))
1649 (error "array-setter: multi-index component is not an exact integer: " i j k))
1650 ((not (%%interval-contains-multi-index?-3 domain i j k))
1651 (error "array-setter: domain does not contain multi-index: " domain i j k))
1652 ((not (checker value))
1653 (error "array-setter: value cannot be stored in body: " value))
1654 (else
1655 (storage-class-setter body (indexer i j k) value)))))
1656 ((4) (lambda (value i j k l)
1657 (cond ((not (and (exact-integer? i)
1658 (exact-integer? j)
1659 (exact-integer? k)
1660 (exact-integer? l)))
1661 (error "array-setter: multi-index component is not an exact integer: " i j k l))
1662 ((not (%%interval-contains-multi-index?-4 domain i j k l))
1663 (error "array-setter: domain does not contain multi-index: " domain i j k l))
1664 ((not (checker value))
1665 (error "array-setter: value cannot be stored in body: " value))
1666 (else
1667 (storage-class-setter body (indexer i j k l) value)))))
1668 (else (lambda (value . multi-index)
1669 (cond ((not (%%every (lambda (x) (exact-integer? x)) multi-index))
1670 (apply error "array-setter: multi-index component is not an exact integer: " multi-index))
1671 ((not (fx= (%%interval-dimension domain) (length multi-index)))
1672 (apply error "array-setter: multi-index is not the correct dimension: " domain multi-index))
1673 ((not (%%interval-contains-multi-index?-general domain multi-index))
1674 (apply error "array-setter: domain does not contain multi-index: " domain multi-index))
1675 ((not (checker value))
1676 (error "array-setter: value cannot be stored in body: " value))
1677 (else
1678 (storage-class-setter body (apply indexer multi-index) value))))))
1679 (case (%%interval-dimension domain)
1680 ((1) (expand-setters (lambda (value i) (storage-class-setter body (indexer i) value))))
1681 ((2) (expand-setters (lambda (value i j) (storage-class-setter body (indexer i j) value))))
1682 ((3) (expand-setters (lambda (value i j k) (storage-class-setter body (indexer i j k) value))))
1683 ((4) (expand-setters (lambda (value i j k l) (storage-class-setter body (indexer i j k l) value))))
1684 (else (expand-setters (lambda (value . multi-index) (storage-class-setter body (apply indexer multi-index) value)))))))))
1685 (make-%%array domain
1686 getter
1687 setter
1688 storage-class
1689 body
1690 indexer
1691 safe?
1692 in-order?))))
1693
1694 (define (%%interval->basic-indexer interval)
1695 (case (%%interval-dimension interval)
1696 ((1) (let ((low-0 (%%interval-lower-bound interval 0))
1697 (increment-0 1))
1698 (%%indexer-1 0 low-0 increment-0)))
1699 ((2) (let* ((low-0 (%%interval-lower-bound interval 0))
1700 (low-1 (%%interval-lower-bound interval 1))
1701 (increment-1 1)
1702 (increment-0 (* increment-1
1703 (- (%%interval-upper-bound interval 1)
1704 (%%interval-lower-bound interval 1)))))
1705 (%%indexer-2 0
1706 low-0 low-1
1707 increment-0 increment-1)))
1708 ((3) (let* ((low-0 (%%interval-lower-bound interval 0))
1709 (low-1 (%%interval-lower-bound interval 1))
1710 (low-2 (%%interval-lower-bound interval 2))
1711 (increment-2 1)
1712 (increment-1 (* increment-2
1713 (- (%%interval-upper-bound interval 2)
1714 (%%interval-lower-bound interval 2))))
1715 (increment-0 (* increment-1
1716 (- (%%interval-upper-bound interval 1)
1717 (%%interval-lower-bound interval 1)))))
1718 (%%indexer-3 0
1719 low-0 low-1 low-2
1720 increment-0 increment-1 increment-2)))
1721 ((4) (let* ((low-0 (%%interval-lower-bound interval 0))
1722 (low-1 (%%interval-lower-bound interval 1))
1723 (low-2 (%%interval-lower-bound interval 2))
1724 (low-3 (%%interval-lower-bound interval 3))
1725 (increment-3 1)
1726 (increment-2 (* increment-3
1727 (- (%%interval-upper-bound interval 3)
1728 (%%interval-lower-bound interval 3))))
1729 (increment-1 (* increment-2
1730 (- (%%interval-upper-bound interval 2)
1731 (%%interval-lower-bound interval 2))))
1732 (increment-0 (* increment-1
1733 (- (%%interval-upper-bound interval 1)
1734 (%%interval-lower-bound interval 1)))))
1735 (%%indexer-4 0
1736 low-0 low-1 low-2 low-3
1737 increment-0 increment-1 increment-2 increment-3)))
1738 (else
1739 (let ((lower-bounds (%%interval-lower-bounds->list interval))
1740 (upper-bounds (%%interval-upper-bounds->list interval)))
1741 (let ((ranges (map (lambda (u l) (- u l)) upper-bounds lower-bounds)))
1742 (do ((ranges (reverse ranges) (cdr ranges))
1743 (increments (list 1) (cons (* (car increments) (car ranges))
1744 increments)))
1745 ((null? (cdr ranges)) (%%indexer-generic 0 lower-bounds increments))))))))
1746
1747 (define (%%make-specialized-array interval
1748 storage-class
1749 ;; must be mutable
1750 safe?)
1751 (let* ((body ((storage-class-maker storage-class)
1752 (%%interval-volume interval)
1753 (storage-class-default storage-class)))
1754 (indexer (%%interval->basic-indexer interval)))
1755 (%%finish-specialized-array interval
1756 storage-class
1757 body
1758 indexer
1759 #t ;; mutable?
1760 safe?
1761 #t))) ;; new arrays are always in order
1762
1763
1764 (define (make-specialized-array interval
1765 #!optional
1766 (storage-class generic-storage-class)
1767 ;; must be mutable?
1768 (safe? (specialized-array-default-safe?)))
1769 ;; Returns a mutable specialized-array
1770 (cond ((not (interval? interval))
1771 (error "make-specialized-array: The first argument is not an interval: " interval))
1772 ((not (storage-class? storage-class))
1773 (error "make-specialized-array: The second argument is not a storage-class: " interval storage-class))
1774 ((not (boolean? safe?))
1775 (error "make-specialized-array: The third argument is not a boolean: " interval storage-class safe?))
1776 (else
1777 (%%make-specialized-array interval
1778 storage-class
1779 ;; must be mutable
1780 safe?))))
1781
1782 ;;; We consolidate all moving of array elements to the following procedure.
1783
1784 (define (%%move-array-elements destination source caller)
1785
1786 ;; Here's the logic:
1787 ;; We require the source and destination to have the same number of elements.
1788 ;; If destination is a specialized array
1789 ;; then
1790 ;; If its elements are in order
1791 ;; then
1792 ;; If the source is a specialized array
1793 ;; with the same storage class
1794 ;; for which a copier exists
1795 ;; and whose elements are also in order
1796 ;; then
1797 ;; do a block copy
1798 ;; else
1799 ;; if no checks are needed
1800 ;; then
1801 ;; step through the cells of the destination in order,
1802 ;; storing the source elements
1803 ;; else
1804 ;; step through the cells of the destination in order,
1805 ;; storing the source elements after testing they're OK
1806 ;; for the destination
1807 ;; else
1808 ;; we now require that the domains of destination and source are
1809 ;; the same.
1810 ;; If no checks are needed
1811 ;; then
1812 ;; Copy elements from the source to destination, without checks.
1813 ;; else
1814 ;; Copy elements from the source to destination, checking whether
1815 ;; they're OK
1816 ;; else
1817 ;; We require the domains of destination and source to be the same.
1818 ;; Copy elements of source to destination
1819
1820 ;; We check the whether the elements of the destination are in order to save
1821 ;; a bit of array indexing (or perhaps to a block copy, which is even better).
1822
1823 ;; We check that the elements we move to the destination are OK for the
1824 ;; destination because if we don't catch errors here they can be very tricky to find.
1825
1826 (if (not (= (%%interval-volume (%%array-domain source))
1827 (%%interval-volume (%%array-domain destination))))
1828 (error (string-append caller "Arrays must have the same volume: ")
1829 destination source))
1830
1831 (if (specialized-array? destination)
1832 (if (%%array-elements-in-order? destination)
1833 ;; Now we do not assume that the domains are the same
1834 ;; maybe we can do a block copy
1835 (if (and (specialized-array? source)
1836 (eq? (%%array-storage-class destination)
1837 (%%array-storage-class source))
1838 ;; does a copier for this storage-class exist?
1839 (storage-class-copier (%%array-storage-class destination))
1840 (%%array-elements-in-order? source))
1841 ;; do a block copy
1842 (let* ((source-indexer
1843 (%%array-indexer source))
1844 (destination-indexer
1845 (%%array-indexer destination))
1846 (copier
1847 (storage-class-copier (%%array-storage-class source)))
1848 (initial-destination-index
1849 (%%interval-lower-bounds->list (%%array-domain destination)))
1850 (destination-start
1851 (apply destination-indexer initial-destination-index))
1852 (initial-source-index
1853 (%%interval-lower-bounds->list (%%array-domain source)))
1854 (source-start
1855 (apply source-indexer initial-source-index))
1856 (source-end
1857 (fx+ source-start (%%interval-volume (%%array-domain source)))))
1858 (copier (%%array-body destination)
1859 destination-start
1860 (%%array-body source)
1861 source-start
1862 source-end)
1863 "Block copy")
1864 ;; we can step through the elements of destination in order.
1865 (let* ((domain
1866 (%%array-domain source))
1867 (getter
1868 (%%array-getter source))
1869 (destination-storage-class
1870 (%%array-storage-class destination))
1871 (initial-offset
1872 (apply (%%array-indexer destination)
1873 (%%interval-lower-bounds->list (%%array-domain destination)))))
1874 (cond ((eq? destination-storage-class generic-storage-class)
1875 ;; No checks needed, storage-class-setter is vector-set!
1876 (let ((body (%%array-body destination)))
1877 (%%interval-for-each
1878 (case (%%interval-dimension domain)
1879 ((1) (let ((index initial-offset))
1880 (lambda (i)
1881 (vector-set! body index (getter i))
1882 (set! index (fx+ index 1)))))
1883 ((2) (let ((index initial-offset))
1884 (lambda (i j)
1885 (vector-set! body index (getter i j))
1886 (set! index (fx+ index 1)))))
1887 ((3) (let ((index initial-offset))
1888 (lambda (i j k)
1889 (vector-set! body index (getter i j k))
1890 (set! index (fx+ index 1)))))
1891 ((4) (let ((index initial-offset))
1892 (lambda (i j k l)
1893 (vector-set! body index (getter i j k l))
1894 (set! index (fx+ index 1)))))
1895 (else (let ((index initial-offset))
1896 (lambda multi-index
1897 (vector-set! body index (apply getter multi-index))
1898 (set! index (fx+ index 1))))))
1899 domain))
1900 "In order, no checks needed, generic-storage-class")
1901 ((and (specialized-array? source)
1902 (eq? destination-storage-class
1903 (%%array-storage-class source)))
1904 ;; No checks needed
1905 (let ((setter (storage-class-setter destination-storage-class))
1906 (body (%%array-body destination)))
1907 (%%interval-for-each
1908 (case (%%interval-dimension domain)
1909 ((1) (let ((index initial-offset))
1910 (lambda (i)
1911 (setter body index (getter i))
1912 (set! index (fx+ index 1)))))
1913 ((2) (let ((index initial-offset))
1914 (lambda (i j)
1915 (setter body index (getter i j))
1916 (set! index (fx+ index 1)))))
1917 ((3) (let ((index initial-offset))
1918 (lambda (i j k)
1919 (setter body index (getter i j k))
1920 (set! index (fx+ index 1)))))
1921 ((4) (let ((index initial-offset))
1922 (lambda (i j k l)
1923 (setter body index (getter i j k l))
1924 (set! index (fx+ index 1)))))
1925 (else (let ((index initial-offset))
1926 (lambda multi-index
1927 (setter body index (apply getter multi-index))
1928 (set! index (fx+ index 1))))))
1929 domain))
1930 "In order, no checks needed")
1931 (else
1932 ;; checks needed
1933 (let ((checker
1934 (storage-class-checker destination-storage-class))
1935 (body
1936 (%%array-body destination))
1937 (setter
1938 (storage-class-setter destination-storage-class)))
1939 (%%interval-for-each
1940 (case (%%interval-dimension domain)
1941 ((1)
1942 (let ((index initial-offset))
1943 (lambda (i)
1944 (let ((item (getter i)))
1945 (if (checker item)
1946 (begin
1947 (setter body index item)
1948 (set! index (fx+ index 1)))
1949 (error
1950 (string-append
1951 caller
1952 "Not all elements of the source can be stored in destination: ")
1953 destination source i item))))))
1954 ((2)
1955 (let ((index initial-offset))
1956 (lambda (i j)
1957 (let ((item (getter i j)))
1958 (if (checker item)
1959 (begin
1960 (setter body index item)
1961 (set! index (fx+ index 1)))
1962 (error
1963 (string-append
1964 caller
1965 "Not all elements of the source can be stored in destination: ")
1966 destination source i j item))))))
1967 ((3)
1968 (let ((index initial-offset))
1969 (lambda (i j k)
1970 (let ((item (getter i j k)))
1971 (if (checker item)
1972 (begin
1973 (setter body index item)
1974 (set! index (fx+ index 1)))
1975 (error
1976 (string-append
1977 caller
1978 "Not all elements of the source can be stored in destination: ")
1979 destination source i j k item) )))))
1980 ((4)
1981 (let ((index initial-offset))
1982 (lambda (i j k l)
1983 (let ((item (getter i j k l)))
1984 (if (checker item)
1985 (begin
1986 (setter body index item)
1987 (set! index (fx+ index 1)))
1988 (error
1989 (string-append
1990 caller
1991 "Not all elements of the source can be stored in destination: ")
1992 destination source i j k l item))))))
1993 (else
1994 (let ((index initial-offset))
1995 (lambda multi-index
1996 (let ((item (apply getter multi-index)))
1997 (if (checker item)
1998 (begin
1999 (setter body index item)
2000 (set! index (fx+ index 1)))
2001 (error
2002 (string-append
2003 caller
2004 "Not all elements of the source can be stored in destination: ")
2005 destination source multi-index item)))))))
2006 domain))
2007 "In order, checks needed"))))
2008 ;; the elements of destination are not in order.
2009 ;; so we need the domains to be the same.
2010 (let* ((setter
2011 (%%array-setter destination))
2012 (getter
2013 (%%array-getter source))
2014 (checker
2015 (storage-class-checker (%%array-storage-class destination)))
2016 (domain
2017 (%%array-domain destination)))
2018 (cond ((not (%%interval= domain (%%array-domain source)))
2019 (error (string-append
2020 caller
2021 "Arrays must have the same domains: ")
2022 destination source))
2023 ((or (eq? (%%array-storage-class destination)
2024 generic-storage-class)
2025 (and (specialized-array? source)
2026 (eq? (%%array-storage-class destination)
2027 (%%array-storage-class source))))
2028 ;; no checks needed
2029 (%%interval-for-each
2030 (case (%%interval-dimension domain)
2031 ((1) (lambda (i)
2032 (setter (getter i) i)))
2033 ((2) (lambda (i j)
2034 (setter (getter i j) i j)))
2035 ((3) (lambda (i j k)
2036 (setter (getter i j k) i j k)))
2037 ((4) (lambda (i j k l)
2038 (setter (getter i j k l) i j k l)))
2039 (else
2040 (lambda multi-index
2041 (apply setter (apply getter multi-index) multi-index))))
2042 domain)
2043 "Out of order, no checks needed")
2044 (else
2045 ;; checks needed
2046 (%%interval-for-each
2047 (case (%%interval-dimension domain)
2048 ((1)
2049 (lambda (i)
2050 (let ((item (getter i)))
2051 (if (checker item)
2052 (setter item i)
2053 (error
2054 (string-append
2055 caller
2056 "Not all elements of the source can be stored in destination: ")
2057 destination source i item)))))
2058 ((2)
2059 (lambda (i j)
2060 (let ((item (getter i j)))
2061 (if (checker item)
2062 (setter item i j)
2063 (error
2064 (string-append
2065 caller
2066 "Not all elements of the source can be stored in destination: ")
2067 destination source i j item)))))
2068 ((3)
2069 (lambda (i j k)
2070 (let ((item (getter i j k)))
2071 (if (checker item)
2072 (setter item i j k)
2073 (error
2074 (string-append
2075 caller
2076 "Not all elements of the source can be stored in destination: ")
2077 destination source i j k item)))))
2078 ((4)
2079 (lambda (i j k l)
2080 (let ((item (getter i j k l)))
2081 (if (checker item)
2082 (setter item i j k l)
2083 (error
2084 (string-append
2085 caller
2086 "Not all elements of the source can be stored in destination: ")
2087 destination source i j k l item)))))
2088 (else
2089 (lambda multi-index
2090 (let ((item (apply getter multi-index)))
2091 (if (checker item)
2092 (apply setter item multi-index)
2093 (error
2094 (string-append
2095 caller
2096 "Not all elements of the source can be stored in destination: ")
2097 destination source multi-index item))))))
2098 domain)
2099 "Out of order, checks needed"))))
2100 ;; destination is not a specialized array, so checks,
2101 ;; if any, are built into the setter.
2102 (let ((domain (%%array-domain destination)))
2103 (if (not (%%interval= domain (%%array-domain source)))
2104 (error (string-append
2105 caller
2106 "Arrays must have the same domains: ")
2107 destination source)
2108 (let* ((setter
2109 (%%array-setter destination))
2110 (getter
2111 (%%array-getter source))
2112 (domain
2113 (%%array-domain destination)))
2114 (%%interval-for-each
2115 (case (%%interval-dimension domain)
2116 ((1) (lambda (i)
2117 (setter (getter i)
2118 i)))
2119 ((2) (lambda (i j)
2120 (setter (getter i j)
2121 i j)))
2122 ((3) (lambda (i j k)
2123 (setter (getter i j k)
2124 i j k)))
2125 ((4) (lambda (i j k l)
2126 (setter (getter i j k l)
2127 i j k l)))
2128 (else
2129 (lambda multi-index
2130 (apply setter
2131 (apply getter multi-index)
2132 multi-index))))
2133 domain)
2134 "Destination not specialized array"))))
2135 ;; %%move-array-elements returns a string that designates
2136 ;; the copying method it used.
2137 ;; Calling functions should return something useful.
2138 )
2139
2140 ;;;
2141 ;;; The domain of the result is the same as the domain of the argument.
2142 ;;;
2143 ;;; Builds a new specialized-array and populates the body of the result with
2144 ;;; (array-getter array) applied to the elements of (array-domain array)
2145
2146 (define (%%array-copy array
2147 result-storage-class
2148 domain
2149 mutable?
2150 safe?)
2151 (let ((result (%%make-specialized-array domain
2152 result-storage-class
2153 safe?)))
2154 (%%move-array-elements result array "array-copy: ")
2155 (if (not mutable?) ;; set the setter to #f if the final array is not mutable
2156 (%%array-setter-set! result #f))
2157 result))
2158
2159 (define (array-copy array
2160 #!optional
2161 (result-storage-class generic-storage-class)
2162 (new-domain #f)
2163 (mutable? (specialized-array-default-mutable?))
2164 (safe? (specialized-array-default-safe?)))
2165 (cond ((not (array? array))
2166 (error "array-copy: The first argument is not an array: " array))
2167 ((not (storage-class? result-storage-class))
2168 (error "array-copy: The second argument is not a storage-class: " result-storage-class))
2169 ((not (or (eq? new-domain #f) (%%interval? new-domain)))
2170 (error "array-copy: The third argument is neither #f nor an interval: " new-domain))
2171 ((and (%%interval? new-domain)
2172 (not (= (%%interval-volume new-domain)
2173 (%%interval-volume (%%array-domain array)))))
2174 (error
2175 "array-copy: The volume of the third argument is not the volume of the domain of the first argument: "
2176 array result-storage-class new-domain))
2177 ((not (boolean? mutable?))
2178 (error "array-copy: The fourth argument is not a boolean: " mutable?))
2179 ((not (boolean? safe?))
2180 (error "array-copy: The fifth argument is not a boolean: " safe?))
2181 (else
2182 (%%array-copy array
2183 result-storage-class
2184 (if new-domain new-domain (%%array-domain array))
2185 mutable?
2186 safe?))))
2187
2188 ;;;
2189 ;;; In the next function, old-indexer is an affine 1-1 mapping from an interval to [0,N), for some N.
2190 ;;;
2191 ;;; new-domain->old-domain is an affine 1-1 mapping from new-domain to the domain of old-indexer.
2192 ;;;
2193
2194 (define (%%compose-indexers old-indexer new-domain new-domain->old-domain)
2195 (case (%%interval-dimension new-domain)
2196 ((1) (let* ((lower-0 (%%interval-lower-bound new-domain 0))
2197 (upper-0 (%%interval-upper-bound new-domain 0))
2198 (base (call-with-values
2199 (lambda () (new-domain->old-domain lower-0))
2200 old-indexer))
2201 (increment-0 (if (< (+ lower-0 1) upper-0)
2202 (- (call-with-values
2203 (lambda () (new-domain->old-domain (+ lower-0 1)))
2204 old-indexer)
2205 base)
2206 0)))
2207 (%%indexer-1 base lower-0 increment-0)))
2208
2209 ((2) (let* ((lower-0 (%%interval-lower-bound new-domain 0))
2210 (lower-1 (%%interval-lower-bound new-domain 1))
2211 (upper-0 (%%interval-upper-bound new-domain 0))
2212 (upper-1 (%%interval-upper-bound new-domain 1))
2213 (base (call-with-values
2214 (lambda () (new-domain->old-domain lower-0 lower-1))
2215 old-indexer))
2216 (increment-0 (if (< (+ lower-0 1) upper-0)
2217 (- (call-with-values
2218 (lambda () (new-domain->old-domain (+ lower-0 1) lower-1))
2219 old-indexer)
2220 base)
2221 0))
2222 (increment-1 (if (< (+ lower-1 1) upper-1)
2223 (- (call-with-values
2224 (lambda () (new-domain->old-domain lower-0 (+ lower-1 1)))
2225 old-indexer)
2226 base)
2227 0)))
2228 (%%indexer-2 base lower-0 lower-1 increment-0 increment-1)))
2229 ((3) (let* ((lower-0 (%%interval-lower-bound new-domain 0))
2230 (lower-1 (%%interval-lower-bound new-domain 1))
2231 (lower-2 (%%interval-lower-bound new-domain 2))
2232 (upper-0 (%%interval-upper-bound new-domain 0))
2233 (upper-1 (%%interval-upper-bound new-domain 1))
2234 (upper-2 (%%interval-upper-bound new-domain 2))
2235 (base (call-with-values
2236 (lambda () (new-domain->old-domain lower-0 lower-1 lower-2))
2237 old-indexer))
2238 (increment-0 (if (< (+ lower-0 1) upper-0)
2239 (- (call-with-values
2240 (lambda () (new-domain->old-domain (+ lower-0 1) lower-1 lower-2))
2241 old-indexer)
2242 base)
2243 0))
2244 (increment-1 (if (< (+ lower-1 1) upper-1)
2245 (- (call-with-values
2246 (lambda () (new-domain->old-domain lower-0 (+ lower-1 1) lower-2))
2247 old-indexer)
2248 base)
2249 0))
2250 (increment-2 (if (< (+ lower-2 1) upper-2)
2251 (- (call-with-values
2252 (lambda () (new-domain->old-domain lower-0 lower-1 (+ lower-2 1)))
2253 old-indexer)
2254 base)
2255 0)))
2256 (%%indexer-3 base lower-0 lower-1 lower-2 increment-0 increment-1 increment-2)))
2257 ((4) (let* ((lower-0 (%%interval-lower-bound new-domain 0))
2258 (lower-1 (%%interval-lower-bound new-domain 1))
2259 (lower-2 (%%interval-lower-bound new-domain 2))
2260 (lower-3 (%%interval-lower-bound new-domain 3))
2261 (upper-0 (%%interval-upper-bound new-domain 0))
2262 (upper-1 (%%interval-upper-bound new-domain 1))
2263 (upper-2 (%%interval-upper-bound new-domain 2))
2264 (upper-3 (%%interval-upper-bound new-domain 3))
2265 (base (call-with-values
2266 (lambda () (new-domain->old-domain lower-0 lower-1 lower-2 lower-3))
2267 old-indexer))
2268 (increment-0 (if (< (+ lower-0 1) upper-0)
2269 (- (call-with-values
2270 (lambda () (new-domain->old-domain (+ lower-0 1) lower-1 lower-2 lower-3))
2271 old-indexer)
2272 base)
2273 0))
2274 (increment-1 (if (< (+ lower-1 1) upper-1)
2275 (- (call-with-values
2276 (lambda () (new-domain->old-domain lower-0 (+ lower-1 1) lower-2 lower-3))
2277 old-indexer)
2278 base)
2279 0))
2280 (increment-2 (if (< (+ lower-2 1) upper-2)
2281 (- (call-with-values
2282 (lambda () (new-domain->old-domain lower-0 lower-1 (+ lower-2 1) lower-3))
2283 old-indexer)
2284 base)
2285 0))
2286 (increment-3 (if (< (+ lower-3 1) upper-3)
2287 (- (call-with-values
2288 (lambda () (new-domain->old-domain lower-0 lower-1 lower-2 (+ lower-3 1)))
2289 old-indexer)
2290 base)
2291 0)))
2292 (%%indexer-4 base lower-0 lower-1 lower-2 lower-3 increment-0 increment-1 increment-2 increment-3)))
2293 (else
2294 (let* ((lower-bounds (%%interval-lower-bounds->list new-domain))
2295 (upper-bounds (%%interval-upper-bounds->list new-domain))
2296 (base (call-with-values
2297 (lambda () (apply new-domain->old-domain lower-bounds))
2298 old-indexer))
2299 (increments (let ((increments (map (lambda (x) 0) lower-bounds))
2300 (lower-bounds (map (lambda (x) x) lower-bounds)))
2301 (let loop ((l lower-bounds)
2302 (u upper-bounds)
2303 (i increments)
2304 (base base))
2305 (if (null? l)
2306 increments
2307 (let ((new-base
2308 (if (< (+ (car l) 1)
2309 (car u))
2310 (begin
2311 (set-car! l (+ (car l) 1))
2312 (let ((new-base (call-with-values
2313 (lambda () (apply new-domain->old-domain lower-bounds))
2314 old-indexer)))
2315 (set-car! i (- new-base base))
2316 new-base))
2317 base)))
2318 (loop (cdr l)
2319 (cdr u)
2320 (cdr i)
2321 new-base)))))))
2322 (%%indexer-generic base lower-bounds increments)))))
2323
2324 ;;; You want to share the backing store of array.
2325 ;;;
2326 ;;; So you specify a new domain and an affine 1-1 mapping from the new-domain to the old-domain.
2327
2328 (define (%%specialized-array-share array
2329 new-domain
2330 new-domain->old-domain
2331 #!optional
2332 (in-order? %%order-unknown))
2333 (let ((old-domain (%%array-domain array))
2334 (old-indexer (%%array-indexer array))
2335 (body (%%array-body array))
2336 (storage-class (%%array-storage-class array)))
2337 (%%finish-specialized-array new-domain
2338 storage-class
2339 body
2340 (%%compose-indexers old-indexer new-domain new-domain->old-domain)
2341 (mutable-array? array)
2342 (%%array-safe? array)
2343 in-order?)))
2344
2345
2346 (define (specialized-array-share array
2347 new-domain
2348 new-domain->old-domain)
2349 (cond ((not (specialized-array? array))
2350 (error "specialized-array-share: The first argument is not a specialized-array: "
2351 array new-domain new-domain->old-domain))
2352 ((not (interval? new-domain))
2353 (error "specialized-array-share: The second argument is not an interval: "
2354 array new-domain new-domain->old-domain))
2355 ((not (procedure? new-domain->old-domain))
2356 (error "specialized-array-share: The third argument is not a procedure: "
2357 array new-domain new-domain->old-domain))
2358 (else
2359 (%%specialized-array-share array
2360 new-domain
2361 new-domain->old-domain))))
2362
2363 (define (%%immutable-array-extract array new-domain)
2364 (make-array new-domain
2365 (%%array-getter array)))
2366
2367 (define (%%mutable-array-extract array new-domain)
2368 (make-array new-domain
2369 (%%array-getter array)
2370 (%%array-setter array)))
2371
2372 (define (%%specialized-array-extract array new-domain)
2373 (%%specialized-array-share array
2374 new-domain
2375 values))
2376
2377 (define (%%array-extract array new-domain)
2378 (cond ((specialized-array? array)
2379 (%%specialized-array-extract array new-domain))
2380 ((mutable-array? array)
2381 (%%mutable-array-extract array new-domain))
2382 (else
2383 (%%immutable-array-extract array new-domain))))
2384
2385 (define (array-extract array new-domain)
2386 (cond ((not (array? array))
2387 (error "array-extract: The first argument is not an array: " array new-domain))
2388 ((not (interval? new-domain))
2389 (error "array-extract: The second argument is not an interval: " array new-domain))
2390 ((not (fx= (%%array-dimension array)
2391 (%%interval-dimension new-domain)))
2392 (error "array-extract: The dimension of the second argument (an interval) does not equal the dimension of the domain of the first argument (an array): " array new-domain))
2393 ((not (%%interval-subset? new-domain (%%array-domain array)))
2394 (error "array-extract: The second argument (an interval) is not a subset of the domain of the first argument (an array): " array new-domain))
2395 (else
2396 (%%array-extract array new-domain))))
2397
2398 (define (array-tile array sides)
2399 (cond ((not (array? array))
2400 (error "array-tile: The first argument is not an array: " array sides))
2401 ((not (and (vector? sides)
2402 (%%vector-every (lambda (x) (and (exact-integer? x) (positive? x))) sides)))
2403 (error "array-tile: The second argument is not a vector of exact positive integers: " array sides))
2404 ((not (fx= (%%array-dimension array)
2405 (vector-length sides)))
2406 (error "array-tile: The dimension of the first argument (an array) does not equal the length of the second argument (a vector): " array sides))
2407 (else
2408 (let* ((n
2409 (vector-length sides))
2410 (domain
2411 (%%array-domain array))
2412 (lower-bounds
2413 (%%interval-lower-bounds domain))
2414 (upper-bounds
2415 (%%interval-upper-bounds domain))
2416 (result-lower-bounds
2417 (make-vector n 0))
2418 (result-upper-bounds
2419 (vector-map (lambda (l u s)
2420 (quotient (+ (- u l)
2421 (- s 1))
2422 s))
2423 lower-bounds upper-bounds sides))
2424 (result-domain
2425 (%%finish-interval result-lower-bounds result-upper-bounds)))
2426
2427 (define-macro (generate-result)
2428
2429 (define (symbol-append . args)
2430 (string->symbol
2431 (apply string-append (map (lambda (x)
2432 (cond ((symbol? x)
2433 (symbol->string x))
2434 ((number? x)
2435 (number->string x))
2436 ((string? x)
2437 x)
2438 (else
2439 (error "Arghh!"))))
2440 args))))
2441
2442 `(case n
2443 ,@(map (lambda (k)
2444 (let* ((indices
2445 (iota k))
2446 (args
2447 (map (lambda (j) (symbol-append 'i j)) indices))
2448 (lowers
2449 (map (lambda (j) (symbol-append 'l j)) indices))
2450 (uppers
2451 (map (lambda (j) (symbol-append 'u j)) indices))
2452 (sides
2453 (map (lambda (j) (symbol-append 's j)) indices)))
2454 `((,k)
2455 (lambda ,args
2456 (if (not (and ,@(map (lambda (arg) `(exact-integer? ,arg)) args)
2457 (,(symbol-append '%%interval-contains-multi-index?- k) result-domain ,@args)))
2458 (error "array-tile: Index to result array is not valid: " ,@args)
2459 (let* (,@(map (lambda (l j)
2460 `(,l (vector-ref lower-bounds ,j)))
2461 lowers indices)
2462 ,@(map (lambda (u j)
2463 `(,u (vector-ref upper-bounds ,j)))
2464 uppers indices)
2465 ,@(map (lambda (s j)
2466 `(,s (vector-ref sides ,j)))
2467 sides indices)
2468 (subdomain
2469 (%%finish-interval (vector ,@(map (lambda (l s i)
2470 `(+ ,l (* ,s ,i)))
2471 lowers sides args))
2472 (vector ,@(map (lambda (l u s i)
2473 `(min ,u (+ ,l (* ,s (+ ,i 1)))))
2474 lowers uppers sides args)))))
2475 (%%array-extract array subdomain)))))))
2476 '(1 2 3 4))
2477 (else
2478 (lambda i
2479 (if (not (and (fx= (length i) n)
2480 (%%every (lambda (x) (exact-integer? x)) i)
2481 (%%interval-contains-multi-index?-general result-domain i)))
2482 (apply error "array-tile: Index to result array is not valid: " i)
2483 (let* ((i (list->vector i))
2484 (subdomain (%%finish-interval
2485 (vector-map (lambda (l s i)
2486 (+ l (* s i)))
2487 lower-bounds sides i)
2488 (vector-map (lambda (l u s i)
2489 (min u (+ l (* s (+ i 1)))))
2490 lower-bounds upper-bounds sides i))))
2491 (%%array-extract array subdomain)))))))
2492
2493 (make-array result-domain (generate-result))))))
2494
2495
2496 (define (%%getter-translate getter translation)
2497 (case (vector-length translation)
2498 ((1) (lambda (i)
2499 (getter (- i (vector-ref translation 0)))))
2500 ((2) (lambda (i j)
2501 (getter (- i (vector-ref translation 0))
2502 (- j (vector-ref translation 1)))))
2503 ((3) (lambda (i j k)
2504 (getter (- i (vector-ref translation 0))
2505 (- j (vector-ref translation 1))
2506 (- k (vector-ref translation 2)))))
2507 ((4) (lambda (i j k l)
2508 (getter (- i (vector-ref translation 0))
2509 (- j (vector-ref translation 1))
2510 (- k (vector-ref translation 2))
2511 (- l (vector-ref translation 3)))))
2512 (else
2513 (let ((n (vector-length translation))
2514 (translation-list (vector->list translation)))
2515 (lambda indices
2516 (cond ((not (fx= (length indices) n))
2517 (error "The number of indices does not equal the array dimension: " indices))
2518 (else
2519 (apply getter (map - indices translation-list)))))))))
2520
2521 (define (%%setter-translate setter translation)
2522 (case (vector-length translation)
2523 ((1) (lambda (v i)
2524 (setter v
2525 (- i (vector-ref translation 0)))))
2526 ((2) (lambda (v i j)
2527 (setter v
2528 (- i (vector-ref translation 0))
2529 (- j (vector-ref translation 1)))))
2530 ((3) (lambda (v i j k)
2531 (setter v
2532 (- i (vector-ref translation 0))
2533 (- j (vector-ref translation 1))
2534 (- k (vector-ref translation 2)))))
2535 ((4) (lambda (v i j k l)
2536 (setter v
2537 (- i (vector-ref translation 0))
2538 (- j (vector-ref translation 1))
2539 (- k (vector-ref translation 2))
2540 (- l (vector-ref translation 3)))))
2541 (else
2542 (let ((n (vector-length translation))
2543 (translation-list (vector->list translation)))
2544 (lambda (v . indices)
2545 (cond ((not (fx= (length indices) n))
2546 (error "The number of indices does not equal the array dimension: " v indices))
2547 (else
2548 (apply setter v (map - indices translation-list)))))))))
2549
2550 (define (%%immutable-array-translate array translation)
2551 (make-array (%%interval-translate (%%array-domain array) translation)
2552 (%%getter-translate (%%array-getter array) translation)))
2553
2554 (define (%%mutable-array-translate array translation)
2555 (make-array (%%interval-translate (%%array-domain array) translation)
2556 (%%getter-translate (%%array-getter array) translation)
2557 (%%setter-translate (%%array-setter array) translation)))
2558
2559 (define (%%specialized-array-translate array translation)
2560 (%%specialized-array-share array
2561 (%%interval-translate (%%array-domain array) translation)
2562 (%%getter-translate values translation)
2563 (%%array-in-order? array)))
2564
2565 (define (array-translate array translation)
2566 (cond ((not (array? array))
2567 (error "array-translate: The first argument is not an array: " array translation))
2568 ((not (translation? translation))
2569 (error "array-translate: The second argument is not a vector of exact integers: " array translation))
2570 ((not (fx= (%%array-dimension array)
2571 (vector-length translation)))
2572 (error "array-translate: The dimension of the first argument (an array) does not equal the dimension of the second argument (a vector): " array translation))
2573 ((specialized-array? array)
2574 (%%specialized-array-translate array translation))
2575 ((mutable-array? array)
2576 (%%mutable-array-translate array translation))
2577 (else
2578 (%%immutable-array-translate array translation))))
2579
2580 (define-macro (srfi/179#setup-permuted-getters-and-setters)
2581
2582 (define (list-remove l i)
2583 ;; new list that removes (list-ref l i) from l
2584 (if (zero? i)
2585 (cdr l)
2586 (cons (car l)
2587 (list-remove (cdr l) (- i 1)))))
2588
2589 (define (permutations l)
2590 ;; generates list of all permutations of l
2591 (if (null? (cdr l))
2592 (list l)
2593 (apply append (map (lambda (i)
2594 (let ((x (list-ref l i))
2595 (rest (list-remove l i)))
2596 (map (lambda (tail)
2597 (cons x tail))
2598 (permutations rest))))
2599 (iota (length l))))))
2600
2601 (define (concat . args)
2602 (string->symbol (apply string-append (map (lambda (s) (if (string? s) s (symbol->string s ))) args))))
2603
2604 (define (permuter name transform-arguments)
2605 `(define (,(concat name '-permute) ,name permutation)
2606 (case (vector-length permutation)
2607 ,@(map (lambda (i)
2608 `((,i) (cond ,@(let ((args (take '(i j k l) i)))
2609 (map (lambda (perm permuted-args)
2610 `((equal? permutation ',(list->vector perm))
2611 (lambda ,(transform-arguments permuted-args)
2612 ,`(,name ,@(transform-arguments args)))))
2613 (permutations (take '(0 1 2 3) i))
2614 (permutations args))))))
2615 '(1 2 3 4))
2616 (else
2617 (let ((n (vector-length permutation))
2618 (permutation-inverse (%%permutation-invert permutation)))
2619 (lambda ,(transform-arguments 'indices)
2620 (if (not (fx= (length indices) n))
2621 (error "number of indices does not equal permutation dimension: " indices permutation)
2622 (apply ,name ,@(transform-arguments '((%%vector-permute->list (list->vector indices) permutation-inverse)))))))))))
2623
2624 (let ((result
2625 `(begin
2626 ,(permuter '%%getter values)
2627 ,(permuter '%%setter (lambda (args) (cons 'v args))))))
2628 result))
2629
2630 (setup-permuted-getters-and-setters)
2631
2632 (define (%%array-permute array permutation)
2633 (cond ((specialized-array? array)
2634 (%%specialized-array-share array
2635 (%%interval-permute (%%array-domain array) permutation)
2636 (%%getter-permute values permutation)))
2637 ((mutable-array? array)
2638 (make-array (%%interval-permute (%%array-domain array) permutation)
2639 (%%getter-permute (%%array-getter array) permutation)
2640 (%%setter-permute (%%array-setter array) permutation)))
2641 (else
2642 (make-array (%%interval-permute (%%array-domain array) permutation)
2643 (%%getter-permute (%%array-getter array) permutation)))))
2644
2645 (define (array-permute array permutation)
2646 (cond ((not (array? array))
2647 (error "array-permute: The first argument is not an array: " array permutation))
2648 ((not (permutation? permutation))
2649 (error "array-permute: The second argument is not a permutation: " array permutation))
2650 ((not (fx= (%%array-dimension array)
2651 (vector-length permutation)))
2652 (error "array-permute: The dimension of the first argument (an array) does not equal the dimension of the second argument (a permutation): " array permutation))
2653 (else
2654 (%%array-permute array permutation))))
2655
2656 (define (%%rotation->permutation k size)
2657
2658 ;; Generates a permutation that rotates
2659 ;; 0 1 ... size-1
2660 ;; left by k units.
2661
2662 (let ((result (make-vector size)))
2663 (let left-loop ((i 0)
2664 (j k))
2665 (if (fx< j size)
2666 (begin
2667 (vector-set! result i j)
2668 (left-loop (fx+ i 1)
2669 (fx+ j 1)))
2670 (let right-loop ((i i)
2671 (j 0))
2672 (if (fx< i size)
2673 (begin
2674 (vector-set! result i j)
2675 (right-loop (fx+ i 1)
2676 (fx+ j 1)))
2677 result))))))
2678
2679 (define (interval-rotate interval dim)
2680 (if (not (interval? interval))
2681 (error "interval-rotate: The first argument is not an interval: " interval dim)
2682 (let ((d (%%interval-dimension interval)))
2683 (if (not (and (fixnum? dim)
2684 (fx< -1 dim d)))
2685 (error "interval-rotate: The second argument is not an exact integer betweeen 0 (inclusive) and the interval-dimension of the first argument (exclusive): " interval dim)
2686 (%%interval-permute interval (%%rotation->permutation dim d))))))
2687
2688 (define (array-rotate array dim)
2689 (if (not (array? array))
2690 (error "array-rotate: The first argument is not an array: " array dim)
2691 (let ((d (%%array-dimension array)))
2692 (if (not (and (fixnum? dim)
2693 (fx< -1 dim d)))
2694 (error "array-rotate: The second argument is not an exact integer betweeen 0 (inclusive) and the array-dimension of the first argument (exclusive): " array dim)
2695 (%%array-permute array (%%rotation->permutation dim d))))))
2696
2697 (define-macro (srfi/179#setup-reversed-getters-and-setters)
2698
2699 (define (make-symbol . args)
2700 (string->symbol
2701 (apply string-append
2702 (map (lambda (x)
2703 (cond ((string? x) x)
2704 ((symbol? x) (symbol->string x))
2705 ((number? x) (number->string x))))
2706 args))))
2707
2708 (define (truth-table n) ;; generate all combinations of n #t and #f
2709 (if (zero? n)
2710 '(())
2711 (let ((subtable (truth-table (- n 1))))
2712 (apply append (map (lambda (value)
2713 (map (lambda (t)
2714 (cons value t))
2715 subtable))
2716 '(#t #f))))))
2717
2718 (define (generate-code-for-fixed-n name transformer n)
2719 (let ((zero-to-n-1
2720 (iota n))
2721 (table
2722 (truth-table n)))
2723 `((,n) (let (,@(map (lambda (k)
2724 `(,(make-symbol 'adjust_ k) (+ (%%interval-upper-bound interval ,k)
2725 (%%interval-lower-bound interval ,k)
2726 -1)))
2727 zero-to-n-1))
2728 (cond ,@(map (lambda (table-entry)
2729 `((equal? flip? ',(list->vector table-entry))
2730 (lambda ,(transformer (map (lambda (k)
2731 (make-symbol 'i_ k))
2732 zero-to-n-1))
2733 (,name ,@(transformer (map (lambda (flip? k)
2734 (if flip?
2735 `(- ,(make-symbol 'adjust_ k)
2736 ,(make-symbol 'i_ k))
2737 `,(make-symbol 'i_ k)))
2738 table-entry zero-to-n-1))))))
2739 table))))))
2740
2741 (define (reverser name transform-arguments)
2742 `(define (,(make-symbol name '-reverse) ,name flip? interval)
2743 (case (vector-length flip?)
2744 ,@(map (lambda (n)
2745 (generate-code-for-fixed-n name transform-arguments n))
2746 '(1 2 3 4))
2747 (else
2748 (let ((n
2749 (vector-length flip?))
2750 (flip?
2751 (vector->list flip?))
2752 (adjust
2753 (map (lambda (u_k l_k)
2754 (+ u_k l_k -1))
2755 (vector->list (%%interval-upper-bounds interval))
2756 (vector->list (%%interval-lower-bounds interval)))))
2757 (lambda ,(transform-arguments 'indices)
2758 (if (not (fx= (length indices) n))
2759 (error "number of indices does not equal array dimension: " indices)
2760 (apply ,name ,@(transform-arguments '((map (lambda (i adjust flip?)
2761 (if flip?
2762 (- adjust i)
2763 i))
2764 indices adjust flip?)))))))))))
2765 (let ((result
2766 `(begin
2767 ,(reverser '%%getter values)
2768 ,(reverser '%%setter (lambda (args) (cons 'v args))))))
2769 result))
2770
2771 (setup-reversed-getters-and-setters)
2772
2773 (define (%%array-reverse array flip?)
2774 (cond ((specialized-array? array)
2775 (%%specialized-array-share array
2776 (%%array-domain array)
2777 (%%getter-reverse values flip? (%%array-domain array))))
2778 ((mutable-array? array)
2779 (make-array (%%array-domain array)
2780 (%%getter-reverse (%%array-getter array) flip? (%%array-domain array))
2781 (%%setter-reverse (%%array-setter array) flip? (%%array-domain array))))
2782 (else
2783 (make-array (%%array-domain array)
2784 (%%getter-reverse (%%array-getter array) flip? (%%array-domain array))))))
2785
2786 (define %%vector-of-trues
2787 '#(#()
2788 #(#t)
2789 #(#t #t)
2790 #(#t #t #t)
2791 #(#t #t #t #t)))
2792
2793 (define array-reverse
2794 (case-lambda
2795 ((array)
2796 (cond ((not (array? array))
2797 (error "array-reverse: The argument is not an array: " array))
2798 (else
2799 (%%array-reverse array
2800 (let ((dim (%%array-dimension array)))
2801 (if (fx< dim 5)
2802 (vector-ref %%vector-of-trues dim)
2803 (make-vector dim #t)))))))
2804 ((array flip?)
2805 (cond ((not (array? array))
2806 (error "array-reverse: The first argument is not an array: " array flip?))
2807 ((not (and (vector? flip?)
2808 (%%vector-every (lambda (x) (boolean? x)) flip?)))
2809 (error "array-reverse: The second argument is not a vector of booleans: " array flip?))
2810 ((not (fx= (%%array-dimension array)
2811 (vector-length flip?)))
2812 (error "array-reverse: The dimension of the first argument (an array) does not equal the dimension of the second argument (a vector of booleans): " array flip?))
2813 (else
2814 (%%array-reverse array flip?))))))
2815
2816 (define-macro (srfi/179#macro-generate-sample)
2817
2818 (define (make-symbol . args)
2819 (string->symbol
2820 (apply string-append
2821 (map (lambda (x)
2822 (cond ((string? x) x)
2823 ((symbol? x) (symbol->string x))
2824 ((number? x) (number->string x))))
2825 args))))
2826
2827 (define (first-half l)
2828 (take l (quotient (length l) 2)))
2829
2830 (define (second-half l)
2831 (drop l (quotient (length l) 2)))
2832
2833 (define (arg-lists ks)
2834 (if (null? ks)
2835 '(())
2836 (let* ((k (car ks))
2837 (i_k (make-symbol 'i_ k))
2838 (s_k (make-symbol 's_ k))
2839 (sublists
2840 (arg-lists (cdr ks)))
2841 (plains
2842 (map (lambda (l)
2843 (cons i_k l))
2844 sublists))
2845 (scales
2846 (map (lambda (l)
2847 (cons `(* ,i_k ,s_k) l))
2848 sublists)))
2849 (append plains
2850 scales))))
2851
2852 (define (code-for-one-n name transformer n)
2853 (let* ((zero-to-n-1
2854 (iota n))
2855 (arg-list
2856 (map (lambda (k)
2857 (make-symbol 'i_ k))
2858 zero-to-n-1))
2859 (args
2860 (arg-lists zero-to-n-1)))
2861 (define (build-code args ks)
2862 (if (null? (cdr args))
2863 `(lambda ,(transformer arg-list)
2864 (,name ,@(transformer (car args))))
2865 (let* ((k (car ks))
2866 (s_k (make-symbol 's_ k))
2867 (plains (first-half args))
2868 (scales (second-half args)))
2869 `(if (= 1 ,s_k)
2870 ,(build-code plains (cdr ks))
2871 ,(build-code scales (cdr ks))))))
2872 `((,n)
2873 (let (,@(map (lambda (k)
2874 `(,(make-symbol 's_ k) (vector-ref scales ,k)))
2875 zero-to-n-1))
2876 ,(build-code args zero-to-n-1)))))
2877
2878 (define (sampler name transformer)
2879 `(define (,(make-symbol name '-sample) ,name scales interval)
2880 (case (vector-length scales)
2881 ,@(map (lambda (n)
2882 (code-for-one-n name transformer n))
2883 '(1 2 3 4))
2884 (else
2885 (let ((n
2886 (vector-length scales))
2887 (scales
2888 (vector->list scales)))
2889 (lambda ,(transformer 'indices)
2890 (if (not (fx= (length indices) n))
2891 (error "number of indices does not equal array dimension: " indices)
2892 (apply ,name ,@(transformer '((map (lambda (i s)
2893 (* s i))
2894 indices scales)))))))))))
2895
2896
2897
2898 (let ((result
2899 `(begin
2900 ,(sampler '%%getter values)
2901 ,(sampler '%%setter (lambda (args) (cons 'v args))))))
2902 result))
2903
2904 (macro-generate-sample)
2905
2906
2907 (define (%%immutable-array-sample array scales)
2908 (make-array (%%interval-scale (%%array-domain array) scales)
2909 (%%getter-sample (%%array-getter array) scales (%%array-domain array))))
2910
2911 (define (%%mutable-array-sample array scales)
2912 (make-array (%%interval-scale (%%array-domain array) scales)
2913 (%%getter-sample (%%array-getter array) scales (%%array-domain array))
2914 (%%setter-sample (%%array-setter array) scales (%%array-domain array))))
2915
2916 (define (%%specialized-array-sample array scales)
2917 (%%specialized-array-share array
2918 (%%interval-scale (%%array-domain array) scales)
2919 (%%getter-sample values scales (%%array-domain array))))
2920
2921 (define (array-sample array scales)
2922 (cond ((not (and (array? array)
2923 (%%vector-every (lambda (x) (eqv? x 0)) (%%interval-lower-bounds (%%array-domain array)))))
2924 (error "array-sample: The first argument is not an array whose domain has zero lower bounds: " array scales))
2925 ((not (and (vector? scales)
2926 (%%vector-every (lambda (x) (exact-integer? x)) scales)
2927 (%%vector-every (lambda (x) (positive? x)) scales)))
2928 (error "array-sample: The second argument is not a vector of positive, exact, integers: " array scales))
2929 ((not (fx= (vector-length scales) (%%array-dimension array)))
2930 (error "array-sample: The dimension of the first argument (an array) is not equal to the length of the second (a vector): "
2931 array scales))
2932 ((specialized-array? array)
2933 (%%specialized-array-sample array scales))
2934 ((mutable-array? array)
2935 (%%mutable-array-sample array scales))
2936 (else
2937 (%%immutable-array-sample array scales))))
2938
2939 (define (%%array-outer-product combiner array1 array2)
2940 (let* ((domain1 (%%array-domain array1))
2941 (domain2 (%%array-domain array2))
2942 (getter1 (%%array-getter array1))
2943 (getter2 (%%array-getter array2))
2944 (dimension1
2945 (%%interval-dimension domain1))
2946 (dimension2
2947 (%%interval-dimension domain2))
2948 (result-domain
2949 (%%interval-cartesian-product (list domain1 domain2)))
2950 (result-getter
2951 (case dimension1
2952 ((1)
2953 (case dimension2
2954 ((1)
2955 (lambda (i1 i2)
2956 (combiner (getter1 i1)
2957 (getter2 i2))))
2958 ((2)
2959 (lambda (i1 i2 j2)
2960 (combiner (getter1 i1)
2961 (getter2 i2 j2))))
2962 ((3)
2963 (lambda (i1 i2 j2 k2)
2964 (combiner (getter1 i1)
2965 (getter2 i2 j2 k2))))
2966 (else
2967 (lambda (i1 . rest)
2968 (combiner (getter1 i1)
2969 (apply getter2 rest))))))
2970 ((2)
2971 (case dimension2
2972 ((1)
2973 (lambda (i1 j1 i2)
2974 (combiner (getter1 i1 j1)
2975 (getter2 i2))))
2976 ((2)
2977 (lambda (i1 j1 i2 j2)
2978 (combiner (getter1 i1 j1)
2979 (getter2 i2 j2))))
2980 (else
2981 (lambda (i1 j1 . rest)
2982 (combiner (getter1 i1 j1)
2983 (apply getter2 rest))))))
2984 ((3)
2985 (case dimension2
2986 ((1)
2987 (lambda (i1 j1 k1 i2)
2988 (combiner (getter1 i1 j1 k1)
2989 (getter2 i2))))
2990 (else
2991 (lambda (i1 j1 k1 . rest)
2992 (combiner (getter1 i1 j1 k1)
2993 (apply getter2 rest))))))
2994 (else
2995 (lambda args
2996 (combiner (apply getter1 (take args dimension1))
2997 (apply getter2 (drop args dimension1))))))))
2998 (make-array result-domain result-getter)))
2999
3000 (define (array-outer-product combiner array1 array2)
3001 (cond ((not (array? array1))
3002 (error "array-outer-product: The second argument is not an array: " combiner array1 array2))
3003 ((not (array? array2))
3004 (error "array-outer-product: The third argument is not an array: " combiner array1 array2))
3005 ((not (procedure? combiner))
3006 (error "array-outer-product: The first argument is not a procedure: " combiner array1 array2))
3007 (else
3008 (%%array-outer-product combiner array1 array2))))
3009
3010 (define (%%immutable-array-curry array right-dimension)
3011 (call-with-values
3012 (lambda () (%%interval-projections (%%array-domain array) right-dimension))
3013 (lambda (left-interval right-interval)
3014 (let ((getter (%%array-getter array)))
3015 (make-array left-interval
3016 (case (%%interval-dimension left-interval)
3017 ((1) (case (%%interval-dimension right-interval)
3018 ((1) (lambda (i) (make-array right-interval (lambda (j) (getter i j)))))
3019 ((2) (lambda (i) (make-array right-interval (lambda (j k) (getter i j k)))))
3020 ((3) (lambda (i) (make-array right-interval (lambda (j k l) (getter i j k l)))))
3021 (else (lambda (i) (make-array right-interval (lambda multi-index (apply getter i multi-index)))))))
3022 ((2) (case (%%interval-dimension right-interval)
3023 ((1) (lambda (i j) (make-array right-interval (lambda (k) (getter i j k)))))
3024 ((2) (lambda (i j) (make-array right-interval (lambda (k l) (getter i j k l)))))
3025 (else (lambda (i j) (make-array right-interval (lambda multi-index (apply getter i j multi-index)))))))
3026 ((3) (case (%%interval-dimension right-interval)
3027 ((1) (lambda (i j k) (make-array right-interval (lambda (l) (getter i j k l)))))
3028 (else (lambda (i j k) (make-array right-interval (lambda multi-index (apply getter i j k multi-index)))))))
3029 (else (lambda left-multi-index
3030 (make-array right-interval
3031 (lambda right-multi-index
3032 (apply getter (append left-multi-index right-multi-index))))))))))))
3033
3034 (define (%%mutable-array-curry array right-dimension)
3035 (call-with-values
3036 (lambda () (%%interval-projections (%%array-domain array) right-dimension))
3037 (lambda (left-interval right-interval)
3038 (let ((getter (%%array-getter array))
3039 (setter (%%array-setter array)))
3040 (make-array left-interval
3041 (case (%%interval-dimension left-interval)
3042 ((1) (case (%%interval-dimension right-interval)
3043 ((1) (lambda (i) (make-array right-interval
3044 (lambda ( j) (getter i j))
3045 (lambda (v j) (setter v i j)))))
3046 ((2) (lambda (i) (make-array right-interval
3047 (lambda ( j k) (getter i j k))
3048 (lambda (v j k) (setter v i j k)))))
3049 ((3) (lambda (i) (make-array right-interval
3050 (lambda ( j k l) (getter i j k l))
3051 (lambda (v j k l) (setter v i j k l)))))
3052 (else (lambda (i) (make-array right-interval
3053 (lambda multi-index (apply getter i multi-index))
3054 (lambda (v . multi-index) (apply setter v i multi-index)))))))
3055 ((2) (case (%%interval-dimension right-interval)
3056 ((1) (lambda (i j) (make-array right-interval
3057 (lambda ( k) (getter i j k))
3058 (lambda (v k) (setter v i j k)))))
3059 ((2) (lambda (i j) (make-array right-interval
3060 (lambda ( k l) (getter i j k l))
3061 (lambda (v k l) (setter v i j k l)))))
3062 (else (lambda (i j) (make-array right-interval
3063 (lambda multi-index (apply getter i j multi-index))
3064 (lambda (v . multi-index) (apply setter v i j multi-index)))))))
3065 ((3) (case (%%interval-dimension right-interval)
3066 ((1) (lambda (i j k) (make-array right-interval
3067 (lambda ( l) (getter i j k l))
3068 (lambda (v l) (setter v i j k l)))))
3069 (else (lambda (i j k) (make-array right-interval
3070 (lambda multi-index (apply getter i j k multi-index))
3071 (lambda (v . multi-index) (apply setter v i j k multi-index)))))))
3072 (else (lambda left-multi-index
3073 (make-array right-interval
3074 (lambda right-multi-index (apply getter (append left-multi-index right-multi-index)))
3075 (lambda (v . right-multi-index) (apply setter v (append left-multi-index right-multi-index))))))))))))
3076
3077 (define (%%specialized-array-curry array right-dimension)
3078 (call-with-values
3079 (lambda () (%%interval-projections (%%array-domain array) right-dimension))
3080 (lambda (left-interval right-interval)
3081 (let* ((input-array-in-order?
3082 (%%array-in-order? array))
3083 (in-order?
3084 (or (and (boolean? input-array-in-order?) ;; we know whether the input array is in order
3085 input-array-in-order?) ;; and it is in order
3086 ;; But even if it isn't, the subarrays may be in order, so we
3087 ;; compute once whether all the subarrays are in order.
3088 ;; We do this without actually instantiating one of the subarrays.
3089 (%%compute-array-elements-in-order?
3090 ;; The same domain for all subarrays.
3091 right-interval
3092 ;; The new indexer computed using the general, nonspecialized new-domain->old-domain function,
3093 ;; applied specifically to the first multi-index in the left-interval.
3094 (%%compose-indexers (%%array-indexer array)
3095 right-interval
3096 (lambda right-multi-index
3097 (apply values
3098 (append (%%interval-lower-bounds->list left-interval)
3099 right-multi-index))))))))
3100 (make-array
3101 left-interval
3102 (case (%%interval-dimension left-interval)
3103 ((1) (case (%%interval-dimension right-interval)
3104 ((1) (lambda (i) (%%specialized-array-share array right-interval (lambda (j) (values i j )) in-order?)))
3105 ((2) (lambda (i) (%%specialized-array-share array right-interval (lambda (j k) (values i j k )) in-order?)))
3106 ((3) (lambda (i) (%%specialized-array-share array right-interval (lambda (j k l) (values i j k l)) in-order?)))
3107 (else (lambda (i) (%%specialized-array-share array right-interval (lambda multi-index (apply values i multi-index)) in-order?)))))
3108 ((2) (case (%%interval-dimension right-interval)
3109 ((1) (lambda (i j) (%%specialized-array-share array right-interval (lambda ( k) (values i j k )) in-order?)))
3110 ((2) (lambda (i j) (%%specialized-array-share array right-interval (lambda ( k l) (values i j k l)) in-order?)))
3111 (else (lambda (i j) (%%specialized-array-share array right-interval (lambda multi-index (apply values i j multi-index)) in-order?)))))
3112 ((3) (case (%%interval-dimension right-interval)
3113 ((1) (lambda (i j k) (%%specialized-array-share array right-interval (lambda ( l) (values i j k l)) in-order?)))
3114 (else (lambda (i j k) (%%specialized-array-share array right-interval (lambda multi-index (apply values i j k multi-index)) in-order?)))))
3115 (else (lambda left-multi-index
3116 (%%specialized-array-share array right-interval (lambda right-multi-index (apply values (append left-multi-index right-multi-index))) in-order?)))))))))
3117
3118 (define (array-curry array right-dimension)
3119 (cond ((not (array? array))
3120 (error "array-curry: The first argument is not an array: " array right-dimension))
3121 ((not (and (fixnum? right-dimension)
3122 (fx< 0 right-dimension (%%array-dimension array))))
3123 (error "array-curry: The second argument is not an exact integer between 0 and (interval-dimension (array-domain array)) (exclusive): " array right-dimension))
3124 ((specialized-array? array)
3125 (%%specialized-array-curry array right-dimension))
3126 ((mutable-array? array)
3127 (%%mutable-array-curry array right-dimension))
3128 (else ; immutable array
3129 (%%immutable-array-curry array right-dimension))))
3130
3131 ;;;
3132 ;;; array-map returns an array whose domain is the same as the common domain of (cons array arrays)
3133 ;;; and whose getter is
3134 ;;;
3135 ;;; (lambda multi-index
3136 ;;; (apply f (map (lambda (g) (apply g multi-index)) (map array-getter (cons array arrays)))))
3137 ;;;
3138 ;;; This function is also used in array-for-each, so we try to specialize the this
3139 ;;; function to speed things up a bit.
3140 ;;;
3141
3142 (define (%%specialize-function-applied-to-array-getters f array arrays)
3143 (let ((domain (%%array-domain array))
3144 (getter-0 (%%array-getter array)))
3145 (case (length arrays)
3146 ((0) (case (%%interval-dimension domain)
3147 ((1) (lambda (i) (f (getter-0 i))))
3148 ((2) (lambda (i j) (f (getter-0 i j))))
3149 ((3) (lambda (i j k) (f (getter-0 i j k))))
3150 ((4) (lambda (i j k l) (f (getter-0 i j k l))))
3151 (else (lambda multi-index (f (apply getter-0 multi-index))))))
3152
3153 ((1) (let ((getter-1 (%%array-getter (car arrays))))
3154 (case (%%interval-dimension domain)
3155 ((1) (lambda (i) (f (getter-0 i)
3156 (getter-1 i))))
3157 ((2) (lambda (i j) (f (getter-0 i j)
3158 (getter-1 i j))))
3159 ((3) (lambda (i j k) (f (getter-0 i j k)
3160 (getter-1 i j k))))
3161 ((4) (lambda (i j k l) (f (getter-0 i j k l)
3162 (getter-1 i j k l))))
3163 (else (lambda multi-index (f (apply getter-0 multi-index)
3164 (apply getter-1 multi-index)))))))
3165 ((2) (let ((getter-1 (%%array-getter (car arrays)))
3166 (getter-2 (%%array-getter (cadr arrays))))
3167 (case (%%interval-dimension domain)
3168 ((1) (lambda (i) (f (getter-0 i)
3169 (getter-1 i)
3170 (getter-2 i))))
3171 ((2) (lambda (i j) (f (getter-0 i j)
3172 (getter-1 i j)
3173 (getter-2 i j))))
3174 ((3) (lambda (i j k) (f (getter-0 i j k)
3175 (getter-1 i j k)
3176 (getter-2 i j k))))
3177 ((4) (lambda (i j k l) (f (getter-0 i j k l)
3178 (getter-1 i j k l)
3179 (getter-2 i j k l))))
3180 (else (lambda multi-index (f (apply getter-0 multi-index)
3181 (apply getter-1 multi-index)
3182 (apply getter-2 multi-index)))))))
3183 ((3) (let ((getter-1 (%%array-getter (car arrays)))
3184 (getter-2 (%%array-getter (cadr arrays)))
3185 (getter-3 (%%array-getter (caddr arrays))))
3186 (case (%%interval-dimension domain)
3187 ((1) (lambda (i) (f (getter-0 i)
3188 (getter-1 i)
3189 (getter-2 i)
3190 (getter-3 i))))
3191 ((2) (lambda (i j) (f (getter-0 i j)
3192 (getter-1 i j)
3193 (getter-2 i j)
3194 (getter-3 i j))))
3195 ((3) (lambda (i j k) (f (getter-0 i j k)
3196 (getter-1 i j k)
3197 (getter-2 i j k)
3198 (getter-3 i j k))))
3199 ((4) (lambda (i j k l) (f (getter-0 i j k l)
3200 (getter-1 i j k l)
3201 (getter-2 i j k l)
3202 (getter-3 i j k l))))
3203 (else (lambda multi-index (f (apply getter-0 multi-index)
3204 (apply getter-1 multi-index)
3205 (apply getter-2 multi-index)
3206 (apply getter-3 multi-index)))))))
3207 (else
3208 (let ((getters (cons getter-0 (map array-getter arrays))))
3209 (case (%%interval-dimension domain)
3210 ((1) (lambda (i) (apply f (map (lambda (g) (g i)) getters))))
3211 ((2) (lambda (i j) (apply f (map (lambda (g) (g i j)) getters))))
3212 ((3) (lambda (i j k) (apply f (map (lambda (g) (g i j k)) getters))))
3213 ((4) (lambda (i j k l) (apply f (map (lambda (g) (g i j k l)) getters))))
3214 (else (lambda multi-index (apply f (map (lambda (g) (apply g multi-index)) getters))))))))))
3215
3216 (define (array-map f array #!rest arrays)
3217 (cond ((not (procedure? f))
3218 (apply error "array-map: The first argument is not a procedure: " f array arrays))
3219 ((not (%%every array? (cons array arrays)))
3220 (apply error "array-map: Not all arguments after the first are arrays: " f array arrays))
3221 ((not (%%every (lambda (d) (%%interval= d (%%array-domain array))) (map %%array-domain arrays)))
3222 (apply error "array-map: Not all arguments after the first have the same domain: " f array arrays))
3223 (else
3224 (make-array (%%array-domain array)
3225 (%%specialize-function-applied-to-array-getters f array arrays)))))
3226
3227 ;;; applies f to the elements of the arrays in lexicographical order.
3228
3229 (define (array-for-each f array #!rest arrays)
3230 (cond ((not (procedure? f))
3231 (apply error "array-for-each: The first argument is not a procedure: " f array arrays))
3232 ((not (%%every array? (cons array arrays)))
3233 (apply error "array-for-each: Not all arguments after the first are arrays: " f array arrays))
3234 ((not (%%every (lambda (d) (%%interval= d (%%array-domain array))) (map %%array-domain arrays)))
3235 (apply error "array-for-each: Not all arguments after the first have the same domain: " f array arrays))
3236 (else
3237 (%%interval-for-each (%%specialize-function-applied-to-array-getters f array arrays)
3238 (%%array-domain array)))))
3239
3240 (define-macro (srfi/179#macro-make-predicates)
3241
3242 (define (concat . args)
3243 (string->symbol (apply string-append (map (lambda (s) (if (string? s) s (symbol->string s ))) args))))
3244
3245 (define (make-predicate name connector)
3246 `(define (,(concat '%%interval- name) f interval)
3247 (case (%%interval-dimension interval)
3248 ((1) (let ((lower-i (%%interval-lower-bound interval 0))
3249 (upper-i (%%interval-upper-bound interval 0))
3250 (index 0)
3251 (n (%%interval-volume interval)))
3252 (let i-loop ((i lower-i)
3253 (index (fx- n 1)))
3254 (cond ((eqv? 0 index)
3255 (f i))
3256 (else
3257 (,connector (f i)
3258 (i-loop (+ i 1)
3259 (fx- index 1))))))))
3260 ((2) (let ((lower-i (%%interval-lower-bound interval 0))
3261 (lower-j (%%interval-lower-bound interval 1))
3262 (upper-i (%%interval-upper-bound interval 0))
3263 (upper-j (%%interval-upper-bound interval 1))
3264 (n (%%interval-volume interval)))
3265 (let i-loop ((i lower-i)
3266 (index (fx- n 1)))
3267 ;; (< i upper-i) is always true because index is >= 0
3268 (let j-loop ((j lower-j)
3269 (index index))
3270 (cond ((= j upper-j)
3271 (i-loop (+ i 1)
3272 index))
3273 ((eqv? 0 index)
3274 (f i j))
3275 (else
3276 (,connector (f i j)
3277 (j-loop (+ j 1)
3278 (fx- index 1)))))))))
3279 ((3) (let ((lower-i (%%interval-lower-bound interval 0))
3280 (lower-j (%%interval-lower-bound interval 1))
3281 (lower-k (%%interval-lower-bound interval 2))
3282 (upper-i (%%interval-upper-bound interval 0))
3283 (upper-j (%%interval-upper-bound interval 1))
3284 (upper-k (%%interval-upper-bound interval 2))
3285 (n (%%interval-volume interval)))
3286 (let i-loop ((i lower-i)
3287 (index (fx- n 1)))
3288 ;; (< i upper-i) is always true because index is >= 0
3289 (let j-loop ((j lower-j)
3290 (index index))
3291 (if (< j upper-j)
3292 (let k-loop ((k lower-k)
3293 (index index))
3294 (cond ((= k upper-k)
3295 (j-loop (+ j 1)
3296 index))
3297 ((eqv? 0 index)
3298 (f i j k))
3299 (else
3300 (,connector (f i j k)
3301 (k-loop (+ k 1)
3302 (fx- index 1))))))
3303 (i-loop (+ i 1)
3304 index))))))
3305 ((4) (let ((lower-i (%%interval-lower-bound interval 0))
3306 (lower-j (%%interval-lower-bound interval 1))
3307 (lower-k (%%interval-lower-bound interval 2))
3308 (lower-l (%%interval-lower-bound interval 3))
3309 (upper-i (%%interval-upper-bound interval 0))
3310 (upper-j (%%interval-upper-bound interval 1))
3311 (upper-k (%%interval-upper-bound interval 2))
3312 (upper-l (%%interval-upper-bound interval 3))
3313 (n (%%interval-volume interval)))
3314 (let i-loop ((i lower-i)
3315 (index (fx- n 1)))
3316 (let j-loop ((j lower-j)
3317 (index index))
3318 (if (< j upper-j)
3319 (let k-loop ((k lower-k)
3320 (index index))
3321 (if (< k upper-k)
3322 (let l-loop ((l lower-l)
3323 (index index))
3324 (cond ((= l upper-l)
3325 (k-loop (+ k 1)
3326 index))
3327 ((eqv? 0 index)
3328 (f i j k l))
3329 (else
3330 (,connector (f i j k l)
3331 (l-loop (+ l 1)
3332 (fx- index 1))))))
3333 (j-loop (+ j 1)
3334 index)))
3335 (i-loop (+ i 1)
3336 index))))))
3337 (else
3338
3339 (let* ((lowers (%%interval-lower-bounds interval))
3340 (uppers (%%interval-upper-bounds interval))
3341 (dimensions (vector-length lowers))
3342 (arg (vector->list lowers)) ;; the argument to which f is applied
3343 (tails (let ((result (make-vector dimensions))) ;; the tails of the argument
3344 (do ((i 0 (fx+ i 1))
3345 (arg arg (cdr arg)))
3346 ((fx= i dimensions) result)
3347 (vector-set! result i arg)))))
3348 (let loop ((dimension 0)
3349 (total-index (fx- (%%interval-volume interval) 1)))
3350 (cond ((= (car (vector-ref tails dimension))
3351 (vector-ref uppers dimension))
3352 ;; We're done iterating in this dimension, set the arg index
3353 ;; at this dimension back to the lower bound, increment the
3354 ;; arg index at the previous dimension, and go back to the
3355 ;; previous dimension
3356 (let ((previous-tail (vector-ref tails (fx- dimension 1))))
3357 (set-car! (vector-ref tails dimension)
3358 (vector-ref lowers dimension))
3359 (set-car! previous-tail
3360 (+ 1 (car previous-tail)))
3361 (loop (fx- dimension 1)
3362 total-index)))
3363 ((fx< dimension (fx- dimensions 1))
3364 (loop (fx+ dimension 1)
3365 total-index))
3366 ;; Now we're at the final dimension
3367 ((eqv? 0 total-index)
3368 (apply f arg))
3369 (else
3370 (,connector (apply f arg)
3371 (let ((current-tail (vector-ref tails dimension)))
3372 (set-car! current-tail
3373 (+ (car current-tail) 1))
3374 (loop dimension
3375 (fx- total-index 1))))))))))))
3376
3377 (let ((result
3378 `(begin
3379 ,@(map make-predicate
3380 '(any every)
3381 '(or and)))))
3382 result))
3383
3384 (macro-make-predicates)
3385
3386 (define (array-every f array #!rest arrays)
3387 (cond ((not (procedure? f))
3388 (apply error "array-every: The first argument is not a procedure: " f array arrays))
3389 ((not (%%every array? (cons array arrays)))
3390 (apply error "array-every: Not all arguments after the first are arrays: " f array arrays))
3391 ((not (%%every (lambda (d) (%%interval= d (%%array-domain array))) (map %%array-domain arrays)))
3392 (apply error "array-every: Not all arguments after the first have the same domain: " f array arrays))
3393 (else
3394 (%%interval-every (%%specialize-function-applied-to-array-getters f array arrays)
3395 (%%array-domain array)))))
3396
3397 (define (array-any f array #!rest arrays)
3398 (cond ((not (procedure? f))
3399 (apply error "array-any: The first argument is not a procedure: " f array arrays))
3400 ((not (%%every array? (cons array arrays)))
3401 (apply error "array-any: Not all arguments after the first are arrays: " f array arrays))
3402 ((not (%%every (lambda (d) (%%interval= d (%%array-domain array))) (map %%array-domain arrays)))
3403 (apply error "array-any: Not all arguments after the first have the same domain: " f array arrays))
3404 (else
3405 (%%interval-any (%%specialize-function-applied-to-array-getters f array arrays)
3406 (%%array-domain array)))))
3407
3408
3409 (define (%%array-fold op id a)
3410 (%%interval-fold (%%array-getter a) op id (%%array-domain a)))
3411
3412 (define (array-fold op id a)
3413 (cond ((not (procedure? op))
3414 (error "array-fold: The first argument is not a procedure: " op id a))
3415 ((not (array? a))
3416 (error "array-fold: The third argument is not an array: " op id a))
3417 (else
3418 (%%array-fold op id a))))
3419
3420 (define (array-fold-right op id a)
3421 (cond ((not (procedure? op))
3422 (error "array-fold-right: The first argument is not a procedure: " op id a))
3423 ((not (array? a))
3424 (error "array-fold-right: The third argument is not an array: " op id a))
3425 (else
3426 ;; We let array-reverse do a redundant array? check to not generate
3427 ;; a new vector of #t's.
3428 (%%array-fold op id (array-reverse a)))))
3429
3430 (define (array-reduce sum A)
3431 (cond ((not (array? A))
3432 (error "array-reduce: The second argument is not an array: " sum A))
3433 ((not (procedure? sum))
3434 (error "array-reduce: The first argument is not a procedure: " sum A))
3435 (else
3436 (case (%%array-dimension A)
3437 ((1) (let ((box '())
3438 (A_ (%%array-getter A)))
3439 (%%interval-for-each
3440 (lambda (i)
3441 (if (null? box)
3442 (set! box (list (A_ i)))
3443 (set-car! box (sum (car box)
3444 (A_ i)))))
3445 (%%array-domain A))
3446 (car box)))
3447 ((2) (let ((box '())
3448 (A_ (%%array-getter A)))
3449 (%%interval-for-each
3450 (lambda (i j)
3451 (if (null? box)
3452 (set! box (list (A_ i j)))
3453 (set-car! box (sum (car box)
3454 (A_ i j)))))
3455 (%%array-domain A))
3456 (car box)))
3457 ((3) (let ((box '())
3458 (A_ (%%array-getter A)))
3459 (%%interval-for-each
3460 (lambda (i j k)
3461 (if (null? box)
3462 (set! box (list (A_ i j k)))
3463 (set-car! box (sum (car box)
3464 (A_ i j k)))))
3465 (%%array-domain A))
3466 (car box)))
3467 ((4) (let ((box '())
3468 (A_ (%%array-getter A)))
3469 (%%interval-for-each
3470 (lambda (i j k l)
3471 (if (null? box)
3472 (set! box (list (A_ i j k l)))
3473 (set-car! box (sum (car box)
3474 (A_ i j k l)))))
3475 (%%array-domain A))
3476 (car box)))
3477 (else (let ((box '())
3478 (A_ (%%array-getter A)))
3479 (%%interval-for-each
3480 (lambda args
3481 (if (null? box)
3482 (set! box (list (apply A_ args)))
3483 (set-car! box (sum (car box)
3484 (apply A_ args)))))
3485 (%%array-domain A))
3486 (car box)))))))
3487
3488 (define (array->list array)
3489 (cond ((not (array? array))
3490 (error "array->list: The argument is not an array: " array))
3491 (else
3492 (array-fold-right cons '() array))))
3493
3494 (define (list->array l
3495 interval
3496 #!optional
3497 (result-storage-class generic-storage-class)
3498 (mutable? (specialized-array-default-mutable?))
3499 (safe? (specialized-array-default-safe?)))
3500 (cond ((not (list? l))
3501 (error "list->array: The first argument is not a list: " l interval))
3502 ((not (interval? interval))
3503 (error "list->array: The second argument is not an interval: " l interval))
3504 ((not (storage-class? result-storage-class))
3505 (error "list->array: The third argument is not a storage-class: " l interval result-storage-class))
3506 ((not (boolean? mutable?))
3507 (error "list->array: The fourth argument is not a boolean: " l interval result-storage-class mutable?))
3508 ((not (boolean? safe?))
3509 (error "list->array: The fifth argument is not a boolean: " l interval result-storage-class mutable? safe?))
3510 (else
3511 (let* ((checker
3512 (storage-class-checker result-storage-class))
3513 (setter
3514 (storage-class-setter result-storage-class))
3515 (result
3516 (%%make-specialized-array interval
3517 result-storage-class
3518 safe?))
3519 (body
3520 (%%array-body result))
3521 (n
3522 (%%interval-volume interval)))
3523 (let loop ((i 0)
3524 (local l))
3525 (if (or (fx= i n) (null? local))
3526 (if (and (fx= i n) (null? local))
3527 (begin
3528 (if (not mutable?)
3529 (%%array-setter-set! result #f))
3530 result)
3531 (error "list->array: The length of the first argument does not equal the volume of the second: " l interval))
3532 (let ((item (car local)))
3533 (if (checker item)
3534 (begin
3535 (setter body i item)
3536 (loop (fx+ i 1)
3537 (cdr local)))
3538 (error "list->array: Not every element of the list can be stored in the body of the array: " l interval item)))))))))
3539
3540 (define (array-assign! destination source)
3541 (cond ((not (mutable-array? destination))
3542 (error "array-assign!: The destination is not a mutable array: " destination source))
3543 ((not (array? source))
3544 (error "array-assign!: The source is not an array: " destination source))
3545 ((interval= (%%array-domain destination)
3546 (%%array-domain source))
3547 (%%move-array-elements destination source "array-assign!: ")
3548 destination)
3549 ((not (fx= (%%interval-volume (%%array-domain destination))
3550 (%%interval-volume (%%array-domain source))))
3551 (error "array-assign!: The destination and source do not have the same number of elements: " destination source))
3552 ((not (specialized-array? destination))
3553 (error "array-assign!: The destination and source do not have the same domains, and the destination is not a specialized array: " destination source))
3554 ((not (%%array-elements-in-order? destination))
3555 (error "array-assign!: The destination and source do not have the same domains, and the elements of the destination are not stored adjacently and in order: "
3556 destination source))
3557 (else
3558 (%%move-array-elements destination source "array-assign!: ")
3559 destination)))
3560
3561 ;;; Because array-ref and array-set! have variable number of arguments, and
3562 ;;; they have to check on every call that the first argument is an array,
3563 ;;; compiled code using array-ref and array-set! can take up to three times
3564 ;;; as long as our usual notational convention:
3565 ;;;
3566 ;;; (let ((A_ (array-getter A))) ... (A_ i j) ...)
3567 ;;;
3568
3569 (define array-ref
3570 (case-lambda
3571 ((A i0)
3572 (if (not (array? A))
3573 (error "array-ref: The first argument is not an array: " A i0)
3574 ((%%array-getter A) i0)))
3575 ((A i0 i1)
3576 (if (not (array? A))
3577 (error "array-ref: The first argument is not an array: " A i0 i1)
3578 ((%%array-getter A) i0 i1)))
3579 ((A i0 i1 i2)
3580 (if (not (array? A))
3581 (error "array-ref: The first argument is not an array: " A i0 i1 i2)
3582 ((%%array-getter A) i0 i1 i2)))
3583 ((A i0 i1 i2 i3)
3584 (if (not (array? A))
3585 (error "array-ref: The first argument is not an array: " A i0 i1 i2 i3)
3586 ((%%array-getter A) i0 i1 i2 i3)))
3587 ((A i0 i1 i2 i3 . i-tail)
3588 (if (not (array? A))
3589 (apply error "array-ref: The first argument is not an array: " A i0 i1 i2 i3 i-tail)
3590 (apply (%%array-getter A) i0 i1 i2 i3 i-tail)))))
3591
3592 (define array-set!
3593 (case-lambda
3594 ((A v i0)
3595 (if (not (mutable-array? A))
3596 (error "array-set!: The first argument is not a mutable array: " A v i0)
3597 ((%%array-setter A) v i0)))
3598 ((A v i0 i1)
3599 (if (not (mutable-array? A))
3600 (error "array-set!: The first argument is not a mutable array: " A v i0 i1)
3601 ((%%array-setter A) v i0 i1)))
3602 ((A v i0 i1 i2)
3603 (if (not (mutable-array? A))
3604 (error "array-set!: The first argument is not a mutable array: " A v i0 i1 i2)
3605 ((%%array-setter A) v i0 i1 i2)))
3606 ((A v i0 i1 i2 i3)
3607 (if (not (mutable-array? A))
3608 (error "array-set!: The first argument is not a mutable array: " A v i0 i1 i2 i3)
3609 ((%%array-setter A) v i0 i1 i2 i3)))
3610 ((A v i0 i1 i2 i3 . i-tail)
3611 (if (not (mutable-array? A))
3612 (apply error "array-set!: The first argument is not a mutable array: " A v i0 i1 i2 i3 i-tail)
3613 (apply (%%array-setter A) v i0 i1 i2 i3 i-tail)))))
3614
3615 #|
3616
3617 The code for specialized-array-reshape is derived from _attempt_nocopy_reshape in
3618
3619 https://github.com/numpy/numpy/blob/7f836a9aca57de7fcae188b66ee1d8b60c6fc7b1/numpy/core/src/multiarray/shape.c
3620
3621 which is distributed under the following license:
3622
3623 Copyright (c) 2005-2020, NumPy Developers.
3624 All rights reserved.
3625
3626 Redistribution and use in source and binary forms, with or without
3627 modification, are permitted provided that the following conditions are
3628 met:
3629
3630 * Redistributions of source code must retain the above copyright
3631 notice, this list of conditions and the following disclaimer.
3632
3633 * Redistributions in binary form must reproduce the above
3634 copyright notice, this list of conditions and the following
3635 disclaimer in the documentation and/or other materials provided
3636 with the distribution.
3637
3638 * Neither the name of the NumPy Developers nor the names of any
3639 contributors may be used to endorse or promote products derived
3640 from this software without specific prior written permission.
3641
3642 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
3643 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3644 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
3645 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
3646 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3647 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3648 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3649 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3650 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3651 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3652 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3653
3654 |#
3655
3656 (define (specialized-array-reshape array new-domain #!optional (copy-on-failure? #f))
3657
3658 (define (vector-filter p v)
3659
3660 ;; Decides whether to include v(k) in the result vector
3661 ;; by testing p(k), not p(v(k)).
3662
3663 (let ((n (vector-length v)))
3664 (define (helper k i)
3665 (cond ((fx= k n)
3666 (make-vector i))
3667 ((p k)
3668 (let ((result (helper (fx+ k 1) (fx+ i 1))))
3669 (vector-set! result i (vector-ref v k))
3670 result))
3671 (else
3672 (helper (fx+ k 1) i))))
3673 (helper 0 0)))
3674
3675 (cond ((not (specialized-array? array))
3676 (error "specialized-array-reshape: The first argument is not a specialized array: " array new-domain))
3677 ((not (interval? new-domain))
3678 (error "specialized-array-reshape: The second argument is not an interval " array new-domain))
3679 ((not (fx= (%%interval-volume (%%array-domain array))
3680 (%%interval-volume new-domain)))
3681 (error "specialized-array-reshape: The volume of the domain of the first argument is not equal to the volume of the second argument: " array new-domain))
3682 ((not (boolean? copy-on-failure?))
3683 (error "specialized-array-reshape: The third argument is not a boolean: " array new-domain copy-on-failure?))
3684 (else
3685 (let* ((indexer
3686 (%%array-indexer array))
3687 (domain
3688 (%%array-domain array))
3689 (lowers
3690 (%%interval-lower-bounds domain))
3691 (uppers
3692 (%%interval-upper-bounds domain))
3693 (dims
3694 (vector-length lowers))
3695 (sides
3696 (vector-map (lambda (u l) (- u l)) uppers lowers))
3697 (args
3698 (vector->list lowers))
3699 (base
3700 (apply indexer args))
3701 (strides
3702 (let ((result (make-vector dims))
3703 (vec-args (let ((result (make-vector dims)))
3704 (do ((i 0 (fx+ i 1))
3705 (args-tail args (cdr args-tail)))
3706 ((fx= i dims) result)
3707 (vector-set! result i args-tail)))))
3708 (do ((i 0 (fx+ i 1)))
3709 ((fx= i dims) result)
3710 (let ((arg (vector-ref vec-args i)))
3711 ;; gives a nonsense result if (vector-ref sides i) is 1,
3712 ;; but doesn't matter.
3713 (set-car! arg (+ 1 (car arg)))
3714 (vector-set! result i (fx- (apply indexer args) base))
3715 (set-car! arg (+ -1 (car arg)))))))
3716 (filtered-strides
3717 (vector-filter (lambda (i)
3718 (not (eqv? 1 (vector-ref sides i))))
3719 strides))
3720 (filtered-sides
3721 (vector-filter (lambda (i)
3722 (not (eqv? 1 (vector-ref sides i))))
3723 sides))
3724 (new-sides
3725 (vector-map (lambda (u l) (- u l))
3726 (%%interval-upper-bounds new-domain)
3727 (%%interval-lower-bounds new-domain)))
3728 ;; Notation from the NumPy code
3729 (newdims
3730 new-sides)
3731 (olddims
3732 filtered-sides)
3733 (oldstrides
3734 filtered-strides)
3735 (newnd
3736 (vector-length new-sides))
3737 (newstrides
3738 (make-vector newnd 0))
3739 (oldnd
3740 (vector-length filtered-sides)))
3741 ;; In the following loops, the error call is in tail position
3742 ;; so it can be continued.
3743 ;; From this point on we're going to closely follow NumPy's code
3744 (let loop-1 ((oi 0)
3745 (oj 1)
3746 (ni 0)
3747 (nj 1))
3748 (if (and (fx< ni newnd)
3749 (fx< oi oldnd))
3750 ;; We find a minimal group of adjacent dimensions from left to right
3751 ;; on the old and new intervals with the same volume.
3752 ;; We then check to see that the elements in the old array of these
3753 ;; dimensions are evenly spaced, so an affine map can
3754 ;; cover them.
3755 (let loop-2 ((nj nj)
3756 (oj oj)
3757 (np (vector-ref newdims ni))
3758 (op (vector-ref olddims oi)))
3759 (if (not (fx= np op))
3760 (if (fx< np op)
3761 (loop-2 (fx+ nj 1)
3762 oj
3763 (fx* np (vector-ref newdims nj))
3764 op)
3765 (loop-2 nj
3766 (fx+ oj 1)
3767 np
3768 (fx* op (vector-ref olddims oj))))
3769 (let loop-3 ((ok oi))
3770 (if (fx< ok (fx- oj 1))
3771 (if (not (fx= (vector-ref oldstrides ok)
3772 (fx* (vector-ref olddims (fx+ ok 1))
3773 (vector-ref oldstrides (fx+ ok 1)))))
3774 (if copy-on-failure?
3775 (%%array-copy array
3776 (%%array-storage-class array)
3777 new-domain
3778 (mutable-array? array)
3779 (array-safe? array))
3780 (error "specialized-array-reshape: Requested reshaping is impossible: " array new-domain))
3781 (loop-3 (fx+ ok 1)))
3782 (begin
3783 (vector-set! newstrides (fx- nj 1) (vector-ref oldstrides (fx- oj 1)))
3784 (let loop-4 ((nk (fx- nj 1)))
3785 (if (fx< ni nk)
3786 (begin
3787 (vector-set! newstrides (fx- nk 1) (fx* (vector-ref newstrides nk)
3788 (vector-ref newdims nk)))
3789 (loop-4 (fx- nk 1)))
3790 (loop-1 oj
3791 (fx+ oj 1)
3792 nj
3793 (fx+ nj 1)))))))))
3794 ;; The NumPy code then sets the strides of the last
3795 ;; dimensions with side-length 1 to a value, we leave it zero.
3796 (let* ((new-lowers
3797 (%%interval-lower-bounds new-domain))
3798 (indexer
3799 (case newnd
3800 ((1) (%%indexer-1 base
3801 (vector-ref new-lowers 0)
3802 (vector-ref newstrides 0)))
3803 ((2) (%%indexer-2 base
3804 (vector-ref new-lowers 0)
3805 (vector-ref new-lowers 1)
3806 (vector-ref newstrides 0)
3807 (vector-ref newstrides 1)))
3808 ((3) (%%indexer-3 base
3809 (vector-ref new-lowers 0)
3810 (vector-ref new-lowers 1)
3811 (vector-ref new-lowers 2)
3812 (vector-ref newstrides 0)
3813 (vector-ref newstrides 1)
3814 (vector-ref newstrides 2)))
3815 ((4) (%%indexer-4 base
3816 (vector-ref new-lowers 0)
3817 (vector-ref new-lowers 1)
3818 (vector-ref new-lowers 2)
3819 (vector-ref new-lowers 3)
3820 (vector-ref newstrides 0)
3821 (vector-ref newstrides 1)
3822 (vector-ref newstrides 2)
3823 (vector-ref newstrides 3)))
3824 (else
3825 (%%indexer-generic base
3826 (vector->list new-lowers)
3827 (vector->list newstrides))))))
3828 (%%finish-specialized-array new-domain
3829 (%%array-storage-class array)
3830 (%%array-body array)
3831 indexer
3832 (mutable-array? array)
3833 (%%array-safe? array)
3834 (%%array-in-order? array)))))))))
3835
3836 (declare (inline))