-
Notifications
You must be signed in to change notification settings - Fork 4
/
generic-arrays.scm
3853 lines (3570 loc) · 188 KB
/
generic-arrays.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#|
SRFI 179: Nonempty Intervals and Generalized Arrays (Updated)
Copyright 2016, 2018, 2020 Bradley J Lucier.
All Rights Reserved.
Permission is hereby granted, free of charge,
to any person obtaining a copy of this software
and associated documentation files (the "Software"),
to deal in the Software without restriction,
including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice
(including the next paragraph) shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
|#
;;; declarations to reduce the size of the .o file in Gambit
(declare (standard-bindings)
(extended-bindings)
(block)
(mostly-fixnum)
(not safe))
;;; Our naming convention prefixes %% to the names of internal procedures,
(cond-expand
((or gambit r7rs)
(begin
(define (c64vector-copy! to at from start end)
(f32vector-copy! to (fx* 2 at) from (fx* 2 start) (fx* 2 end)))
(define (c128vector-copy! to at from start end)
(f64vector-copy! to (fx* 2 at) from (fx* 2 start) (fx* 2 end)))))
(else
;; Punt
(begin
(define vector-copy! #f)
(define s8vector-copy! #f)
(define s16vector-copy! #f)
(define s32vector-copy! #f)
(define s64vector-copy! #f)
(define u8vector-copy! #f)
(define u16vector-copy! #f)
(define u32vector-copy! #f)
(define u64vector-copy! #f)
(define c64vector-copy! #f)
(define c128vector-copy! #f))))
;; Inlining the following routines causes too much code bloat
;; after compilation.
(declare (not inline))
;;; We do not need a multi-argument every.
(define (%%every pred list)
;; don't inline %%every, but unroll the loop if advantageous
(declare (inline))
(let loop ((list list))
(or (null? list)
(and (pred (car list))
(loop (cdr list))))))
;;; the following is used in error checks.
(define %%vector-every
(case-lambda
((pred vec)
(let loop ((i (fx- (vector-length vec) 1)))
(or (fx< i 0)
(and (pred (vector-ref vec i))
(loop (fx- i 1))))))
((pred vec vec2)
(let loop ((i (fx- (vector-length vec) 1)))
(or (fx< i 0)
(and (pred (vector-ref vec i)
(vector-ref vec2 i))
(loop (fx- i 1))))))
((pred vec vec2 . rest)
(let ((vecs (cons vec (cons vec2 rest))))
(let loop ((i (fx- (vector-length vec) 1)))
(or (fx< i 0)
(and (apply pred (map (lambda (vec) (vector-ref vec i)) vecs))
(loop (fx- i 1)))))))))
;;; requires vector-map, vector-copy function
;;; requires vector-concatenate function
;;; requires exact-integer? function
;;; requires iota, drop, take from SRFI-1
;;; requires fixnum? and flonum?
(declare (inline))
;;; An interval is a cross product of multi-indices
;;; [l_0,u_0) x [l_1,u_1) x ... x [l_n-1,u_n-1)
;;; where l_i < u_i for 0 <= i < n, and n > 0 is the dimension of the interval
(define-structure %%interval
dimension ;; a fixnum
%%volume ;; #f or an exact integer, calculated when needed
lower-bounds ;; a vector of exact integers l_0,...,l_n-1
upper-bounds ;; a vector of exact integers u_0,...,u_n-1
) ;; end %%interval
(define (interval? x)
(%%interval? x))
(declare (not inline))
(define %%vector-of-zeros
'#(#()
#(0)
#(0 0)
#(0 0 0)
#(0 0 0 0)))
(define (%%finish-interval lower-bounds upper-bounds)
(make-%%interval (vector-length upper-bounds)
#f
(vector-copy lower-bounds)
(vector-copy upper-bounds)))
(define make-interval
(case-lambda
((upper-bounds)
(cond ((not (and (vector? upper-bounds)
(fx< 0 (vector-length upper-bounds))
(%%vector-every (lambda (x) (exact-integer? x)) upper-bounds)
(%%vector-every (lambda (x) (positive? x)) upper-bounds)))
(error "make-interval: The argument is not a nonempty vector of positive exact integers: " upper-bounds))
(else
(let ((dimension (vector-length upper-bounds)))
(%%finish-interval (if (fx< dimension 5)
(vector-ref %%vector-of-zeros dimension)
(make-vector dimension 0))
(vector-copy upper-bounds))))))
((lower-bounds upper-bounds)
(cond ((not (and (vector? lower-bounds)
(fx< 0 (vector-length lower-bounds))
(%%vector-every (lambda (x) (exact-integer? x)) lower-bounds)))
(error "make-interval: The first argument is not a nonempty vector of exact integers: " lower-bounds upper-bounds))
((not (and (vector? upper-bounds)
(fx< 0 (vector-length upper-bounds))
(%%vector-every (lambda (x) (exact-integer? x)) upper-bounds)))
(error "make-interval: The second argument is not a nonempty vector of exact integers: " lower-bounds upper-bounds))
((not (fx= (vector-length lower-bounds)
(vector-length upper-bounds)))
(error "make-interval: The first and second arguments are not the same length: " lower-bounds upper-bounds))
((not (%%vector-every (lambda (x y) (< x y)) lower-bounds upper-bounds))
(error "make-interval: Each lower-bound must be less than the associated upper-bound: " lower-bounds upper-bounds))
(else
(%%finish-interval (vector-copy lower-bounds)
(vector-copy upper-bounds)))))))
(declare (inline))
#|
;;; Now a cached field of %%interval
(define (%%interval-dimension interval)
(vector-length (%%interval-lower-bounds interval)))
|#
(define (%%interval-lower-bound interval i)
(vector-ref (%%interval-lower-bounds interval) i))
(define (%%interval-upper-bound interval i)
(vector-ref (%%interval-upper-bounds interval) i))
(define (%%interval-lower-bounds->vector interval)
(vector-copy (%%interval-lower-bounds interval)))
(define (%%interval-upper-bounds->vector interval)
(vector-copy (%%interval-upper-bounds interval)))
(define (%%interval-lower-bounds->list interval)
(vector->list (%%interval-lower-bounds interval)))
(define (%%interval-upper-bounds->list interval)
(vector->list (%%interval-upper-bounds interval)))
(declare (not inline))
(define (interval-dimension interval)
(cond ((not (interval? interval))
(error "interval-dimension: The argument is not an interval: " interval))
(else
(%%interval-dimension interval))))
(define (interval-lower-bound interval i)
(cond ((not (interval? interval))
(error "interval-lower-bound: The first argument is not an interval: " interval i))
((not (and (fixnum? i)
(fx< -1 i (%%interval-dimension interval))))
(error "interval-lower-bound: The second argument is not an exact integer between 0 (inclusive) and (interval-dimension interval) (exclusive): " interval i))
(else
(%%interval-lower-bound interval i))))
(define (interval-upper-bound interval i)
(cond ((not (interval? interval))
(error "interval-upper-bound: The first argument is not an interval: " interval i))
((not (and (fixnum? i)
(fx< -1 i (%%interval-dimension interval))))
(error "interval-upper-bound: The second argument is not an exact integer between 0 (inclusive) and (interval-dimension interval) (exclusive): " interval i))
(else
(%%interval-upper-bound interval i))))
(define (interval-lower-bounds->vector interval)
(cond ((not (interval? interval))
(error "interval-lower-bounds->vector: The argument is not an interval: " interval))
(else
(%%interval-lower-bounds->vector interval))))
(define (interval-upper-bounds->vector interval)
(cond ((not (interval? interval))
(error "interval-upper-bounds->vector: The argument is not an interval: " interval))
(else
(%%interval-upper-bounds->vector interval))))
(define (interval-lower-bounds->list interval)
(cond ((not (interval? interval))
(error "interval-lower-bounds->list: The argument is not an interval: " interval))
(else
(%%interval-lower-bounds->list interval))))
(define (interval-upper-bounds->list interval)
(cond ((not (interval? interval))
(error "interval-upper-bounds->list: The argument is not an interval: " interval))
(else
(%%interval-upper-bounds->list interval))))
(define (interval-projections interval right-dimension)
(cond ((not (interval? interval))
(error "interval-projections: The first argument is not an interval: " interval right-dimension))
((not (fx< 1 (%%interval-dimension interval))) ;; redundant check, but useful error message
(error "interval-projections: The dimension of the first argument is not greater than 1: " interval right-dimension))
((not (and (fixnum? right-dimension)
(fx< 0 right-dimension (%%interval-dimension interval))))
(error "interval-projections: The second argument is not an exact integer between 0 and the dimension of the first argument (exclusive): " interval right-dimension))
(else
(%%interval-projections interval right-dimension))))
(define (%%interval-projections interval right-dimension)
(let* ((n (%%interval-dimension interval))
(left-dimension (fx- n right-dimension))
(lower-bounds (%%interval-lower-bounds interval))
(upper-bounds (%%interval-upper-bounds interval))
(left-lower-bounds (make-vector left-dimension))
(left-upper-bounds (make-vector left-dimension))
(right-lower-bounds (make-vector (- n left-dimension)))
(right-upper-bounds (make-vector (- n left-dimension))))
(do ((i 0 (fx+ i 1)))
((fx= i left-dimension)
(do ((i i (fx+ i 1)))
((fx= i n)
(values (%%finish-interval left-lower-bounds
left-upper-bounds)
(%%finish-interval right-lower-bounds
right-upper-bounds)))
(vector-set! right-lower-bounds (fx- i left-dimension) (vector-ref lower-bounds i))
(vector-set! right-upper-bounds (fx- i left-dimension) (vector-ref upper-bounds i))))
(vector-set! left-lower-bounds i (vector-ref lower-bounds i))
(vector-set! left-upper-bounds i (vector-ref upper-bounds i)))))
(define (permutation? permutation)
(and (vector? permutation)
(let* ((n (vector-length permutation))
(permutation-range (make-vector n #f)))
;; we'll write things into permutation-range
;; each box should be written only once
(let loop ((i 0))
(or (fx= i n)
(let ((p_i (vector-ref permutation i)))
(and (fixnum? p_i) ;; a permutation index can't be a bignum
(fx< -1 p_i n)
(not (vector-ref permutation-range p_i))
(let ()
(vector-set! permutation-range p_i #t)
(loop (fx+ i 1))))))))))
(define (%%vector-permute vector permutation)
(let* ((n (vector-length vector))
(result (make-vector n)))
(do ((i 0 (fx+ i 1)))
((fx= i n) result)
(vector-set! result i (vector-ref vector (vector-ref permutation i))))))
(define (%%vector-permute->list vector permutation)
(do ((i (fx- (vector-length vector) 1) (fx- i 1))
(result '() (cons (vector-ref vector (vector-ref permutation i))
result)))
((fx< i 0) result)))
(define (%%permutation-invert permutation)
(let* ((n (vector-length permutation))
(result (make-vector n)))
(do ((i 0 (fx+ i 1)))
((fx= i n) result)
(vector-set! result (vector-ref permutation i) i))))
(define (%%interval-permute interval permutation)
(%%finish-interval (%%vector-permute (%%interval-lower-bounds interval) permutation)
(%%vector-permute (%%interval-upper-bounds interval) permutation)))
(define (interval-permute interval permutation)
(cond ((not (interval? interval))
(error "interval-permute: The first argument is not an interval: " interval permutation))
((not (permutation? permutation))
(error "interval-permute: The second argument is not a permutation: " interval permutation))
((not (fx= (%%interval-dimension interval) (vector-length permutation)))
(error "interval-permute: The dimension of the first argument (an interval) does not equal the length of the second (a permutation): " interval permutation))
(else
(%%interval-permute interval permutation))))
(define (translation? translation)
(and (vector? translation)
(%%vector-every (lambda (x) (exact-integer? x)) translation)))
(define (interval-translate interval translation)
(cond ((not (interval? interval))
(error "interval-translate: The first argument is not an interval: " interval translation))
((not (translation? translation))
(error "interval-translate: The second argument is not a vector of exact integers: " interval translation))
((not (fx= (%%interval-dimension interval)
(vector-length translation)))
(error "interval-translate: The dimension of the first argument (an interval) does not equal the length of the second (a vector): " interval translation))
(else
(%%interval-translate interval translation))))
(define (%%interval-translate Interval translation)
(%%finish-interval (vector-map (lambda (x y) (+ x y)) (%%interval-lower-bounds Interval) translation)
(vector-map (lambda (x y) (+ x y)) (%%interval-upper-bounds Interval) translation)))
(define (%%interval-scale interval scales)
(let* ((uppers (%%interval-upper-bounds interval))
(lowers (%%interval-lower-bounds interval))
(new-uppers (vector-map (lambda (u s)
(quotient (+ u s -1) s))
uppers scales)))
(%%finish-interval lowers new-uppers)))
(define (interval-scale interval scales)
(cond ((not (and (interval? interval)
(%%vector-every (lambda (x) (eqv? 0 x)) (%%interval-lower-bounds interval))))
(error "interval-scale: The first argument is not an interval with all lower bounds zero: " interval scales))
((not (and (vector? scales)
(%%vector-every (lambda (x) (exact-integer? x)) scales)
(%%vector-every (lambda (x) (positive? x)) scales)))
(error "interval-scale: The second argument is not a vector of positive, exact, integers: " interval scales))
((not (fx= (vector-length scales) (%%interval-dimension interval)))
(error "interval-scale: The dimension of the first argument (an interval) is not equal to the length of the second (a vector): "
interval scales))
(else
(%%interval-scale interval scales))))
(define (%%interval-cartesian-product intervals)
(%%finish-interval (vector-concatenate (map %%interval-lower-bounds intervals))
(vector-concatenate (map %%interval-upper-bounds intervals))))
(define (interval-cartesian-product interval #!rest intervals)
(let ((intervals (cons interval intervals)))
(cond ((not (%%every interval? intervals))
(apply error "interval-cartesian-product: Not all arguments are intervals: " intervals))
(else
(%%interval-cartesian-product intervals)))))
(define (interval-dilate interval lower-diffs upper-diffs)
(cond ((not (interval? interval))
(error "interval-dilate: The first argument is not an interval: " interval lower-diffs upper-diffs))
((not (and (vector? lower-diffs)
(%%vector-every (lambda (x) (exact-integer? x)) lower-diffs)))
(error "interval-dilate: The second argument is not a vector of exact integers: " interval lower-diffs upper-diffs))
((not (and (vector? upper-diffs)
(%%vector-every (lambda (x) (exact-integer? x)) upper-diffs)))
(error "interval-dilate: The third argument is not a vector of exact integers: " interval lower-diffs upper-diffs))
((not (fx= (vector-length lower-diffs)
(vector-length upper-diffs)
(%%interval-dimension interval)))
(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))
(else
(let ((new-lower-bounds (vector-map (lambda (x y) (+ x y)) (%%interval-lower-bounds interval) lower-diffs))
(new-upper-bounds (vector-map (lambda (x y) (+ x y)) (%%interval-upper-bounds interval) upper-diffs)))
(if (%%vector-every (lambda (x y) (< x y)) new-lower-bounds new-upper-bounds)
(%%finish-interval new-lower-bounds new-upper-bounds)
(error "interval-dilate: The resulting interval is empty: " interval lower-diffs upper-diffs))))))
(define (%%interval-volume interval)
(or (%%interval-%%volume interval)
(let* ((upper-bounds
(%%interval-upper-bounds interval))
(lower-bounds
(%%interval-lower-bounds interval))
(dimension
(%%interval-dimension interval))
(volume
(do ((i (fx- dimension 1) (fx- i 1))
(result 1 (* result (- (vector-ref upper-bounds i)
(vector-ref lower-bounds i)))))
((fx< i 0) result))))
(%%interval-%%volume-set! interval volume)
volume)))
(define (interval-volume interval)
(cond ((not (interval? interval))
(error "interval-volume: The argument is not an interval: " interval))
(else
(%%interval-volume interval))))
(define (%%interval= interval1 interval2)
;; This can be used a fair amount, so we open-code it
(or (eq? interval1 interval2)
(and (let ((upper1 (%%interval-upper-bounds interval1))
(upper2 (%%interval-upper-bounds interval2)))
(or (eq? upper1 upper2)
(and (fx= (vector-length upper1) (vector-length upper2))
(%%vector-every (lambda (x y) (= x y)) upper1 upper2))))
(let ((lower1 (%%interval-lower-bounds interval1))
(lower2 (%%interval-lower-bounds interval2)))
(or (eq? lower1 lower2)
;; We don't need to check that the two lower bounds
;; are the same length after checking the upper bounds
(%%vector-every (lambda (x y) (= x y)) lower1 lower2))))))
(define (interval= interval1 interval2)
(cond ((not (and (interval? interval1)
(interval? interval2)))
(error "interval=: Not all arguments are intervals: " interval1 interval2))
(else
(%%interval= interval1 interval2))))
(define (%%interval-subset? interval1 interval2)
(and (fx= (%%interval-dimension interval1) (%%interval-dimension interval2))
(%%vector-every (lambda (x y) (>= x y)) (%%interval-lower-bounds interval1) (%%interval-lower-bounds interval2))
(%%vector-every (lambda (x y) (<= x y)) (%%interval-upper-bounds interval1) (%%interval-upper-bounds interval2))))
(define (interval-subset? interval1 interval2)
(cond ((not (and (interval? interval1)
(interval? interval2)))
(error "interval-subset?: Not all arguments are intervals: " interval1 interval2))
((not (fx= (%%interval-dimension interval1)
(%%interval-dimension interval2)))
(error "interval-subset?: The arguments do not have the same dimension: " interval1 interval2))
(else
(%%interval-subset? interval1 interval2))))
(define (%%interval-intersect intervals)
(let ((lower-bounds (apply vector-map max (map %%interval-lower-bounds intervals)))
(upper-bounds (apply vector-map min (map %%interval-upper-bounds intervals))))
(and (%%vector-every (lambda (x y) (< x y)) lower-bounds upper-bounds)
(%%finish-interval lower-bounds upper-bounds))))
(define (interval-intersect interval #!rest intervals)
(if (null? intervals)
(if (interval? interval)
interval
(error "interval-intersect: The argument is not an interval: " interval))
(let ((intervals (cons interval intervals)))
(cond ((not (%%every interval? intervals))
(apply error "interval-intersect: Not all arguments are intervals: " intervals))
((let* ((dims (map %%interval-dimension intervals))
(dim1 (car dims)))
(not (%%every (lambda (dim) (fx= dim dim1)) (cdr dims))))
(apply error "interval-intersect: Not all arguments have the same dimension: " intervals))
(else
(%%interval-intersect intervals))))))
(declare (inline))
(define (%%interval-contains-multi-index?-1 interval i)
(and (<= (%%interval-lower-bound interval 0) i) (< i (%%interval-upper-bound interval 0))))
(define (%%interval-contains-multi-index?-2 interval i j)
(and (<= (%%interval-lower-bound interval 0) i) (< i (%%interval-upper-bound interval 0))
(<= (%%interval-lower-bound interval 1) j) (< j (%%interval-upper-bound interval 1))))
(define (%%interval-contains-multi-index?-3 interval i j k)
(and (<= (%%interval-lower-bound interval 0) i) (< i (%%interval-upper-bound interval 0))
(<= (%%interval-lower-bound interval 1) j) (< j (%%interval-upper-bound interval 1))
(<= (%%interval-lower-bound interval 2) k) (< k (%%interval-upper-bound interval 2))))
(define (%%interval-contains-multi-index?-4 interval i j k l)
(and (<= (%%interval-lower-bound interval 0) i) (< i (%%interval-upper-bound interval 0))
(<= (%%interval-lower-bound interval 1) j) (< j (%%interval-upper-bound interval 1))
(<= (%%interval-lower-bound interval 2) k) (< k (%%interval-upper-bound interval 2))
(<= (%%interval-lower-bound interval 3) l) (< l (%%interval-upper-bound interval 3))))
(declare (not inline))
(define (%%interval-contains-multi-index?-general interval multi-index)
(let loop ((i 0)
(multi-index multi-index))
(or (null? multi-index)
(let ((component (car multi-index)))
(and (<= (%%interval-lower-bound interval i) component)
(< component (%%interval-upper-bound interval i))
(loop (fx+ i 1)
(cdr multi-index)))))))
(define (interval-contains-multi-index? interval i #!rest multi-index-tail)
;; this is relatively slow, but (a) I haven't seen a need to use it yet, and (b) this formulation
;; significantly simplifies testing the error checking
(cond ((not (interval? interval))
(error "interval-contains-multi-index?: The first argument is not an interval: " interval))
(else
(let ((multi-index (cons i multi-index-tail)))
(cond ((not (fx= (%%interval-dimension interval)
(length multi-index)))
(apply error "interval-contains-multi-index?: The dimension of the first argument (an interval) does not match number of indices: " interval multi-index))
((not (%%every (lambda (x) (exact-integer? x)) multi-index))
(apply error "interval-contains-multi-index?: At least one multi-index component is not an exact integer: " interval multi-index))
(else
(%%interval-contains-multi-index?-general interval multi-index)))))))
;;; Applies f to every element of the domain; assumes that f is thread-safe,
;;; the order of application is not specified
(define (interval-for-each f interval)
(cond ((not (interval? interval))
(error "interval-for-each: The second argument is not a interval: " interval))
((not (procedure? f))
(error "interval-for-each: The first argument is not a procedure: " f))
(else
(%%interval-for-each f interval))))
(define (%%interval-for-each f interval)
(case (%%interval-dimension interval)
((1) (let ((lower-i (%%interval-lower-bound interval 0))
(upper-i (%%interval-upper-bound interval 0)))
(let i-loop ((i lower-i))
(if (< i upper-i)
(begin
(f i)
(i-loop (+ i 1)))))))
((2) (let ((lower-i (%%interval-lower-bound interval 0))
(lower-j (%%interval-lower-bound interval 1))
(upper-i (%%interval-upper-bound interval 0))
(upper-j (%%interval-upper-bound interval 1)))
(let i-loop ((i lower-i))
(if (< i upper-i)
(let j-loop ((j lower-j))
(if (< j upper-j)
(begin
(f i j)
(j-loop (+ j 1)))
(i-loop (+ i 1))))))))
((3) (let ((lower-i (%%interval-lower-bound interval 0))
(lower-j (%%interval-lower-bound interval 1))
(lower-k (%%interval-lower-bound interval 2))
(upper-i (%%interval-upper-bound interval 0))
(upper-j (%%interval-upper-bound interval 1))
(upper-k (%%interval-upper-bound interval 2)))
(let i-loop ((i lower-i))
(if (< i upper-i)
(let j-loop ((j lower-j))
(if (< j upper-j)
(let k-loop ((k lower-k))
(if (< k upper-k)
(begin
(f i j k)
(k-loop (+ k 1)))
(j-loop (+ j 1))))
(i-loop (+ i 1))))))))
((4) (let ((lower-i (%%interval-lower-bound interval 0))
(lower-j (%%interval-lower-bound interval 1))
(lower-k (%%interval-lower-bound interval 2))
(lower-l (%%interval-lower-bound interval 3))
(upper-i (%%interval-upper-bound interval 0))
(upper-j (%%interval-upper-bound interval 1))
(upper-k (%%interval-upper-bound interval 2))
(upper-l (%%interval-upper-bound interval 3)))
(let i-loop ((i lower-i))
(if (< i upper-i)
(let j-loop ((j lower-j))
(if (< j upper-j)
(let k-loop ((k lower-k))
(if (< k upper-k)
(let l-loop ((l lower-l))
(if (< l upper-l)
(begin
(f i j k l)
(l-loop (+ l 1)))
(k-loop (+ k 1))))
(j-loop (+ j 1))))
(i-loop (+ i 1))))))))
(else
(let* ((lower-bounds (%%interval-lower-bounds->list interval))
(upper-bounds (%%interval-upper-bounds->list interval))
(arg (map values lower-bounds))) ; copy lower-bounds
;; I'm not particularly happy with set! here because f might capture the continuation
;; and then funny things might pursue ...
;; But it seems that the only way to have this work efficiently without the set
;; is to have arrays with fortran-style numbering.
;; blah
(define (iterate lower-bounds-tail
upper-bounds-tail
arg-tail)
(let ((lower-bound (car lower-bounds-tail))
(upper-bound (car upper-bounds-tail)))
(if (null? (cdr arg-tail))
(let loop ((i lower-bound))
(if (< i upper-bound)
(begin
(set-car! arg-tail i)
(apply f arg)
(loop (+ i 1)))))
(let loop ((i lower-bound))
(if (< i upper-bound)
(begin
(set-car! arg-tail i)
(iterate (cdr lower-bounds-tail)
(cdr upper-bounds-tail)
(cdr arg-tail))
(loop (+ i 1))))))))
(iterate lower-bounds
upper-bounds
arg)))))
;;; Calculates
;;;
;;; (...(operator (operator (operator identity (f multi-index_1)) (f multi-index_2)) (f multi-index_3)) ...)
;;;
;;; where multi-index_1, multi-index_2, ... are the elements of interval in lexicographical order
;;; This version assumes, and may use, that f is thread-safe and that operator is associative.
;;; The order of application of f and operator is not specified.
(define (%%interval-fold f operator identity interval)
(case (%%interval-dimension interval)
((1) (let ((lower-i (%%interval-lower-bound interval 0))
(upper-i (%%interval-upper-bound interval 0)))
(let i-loop ((i lower-i) (result identity))
(if (= i upper-i)
result
(i-loop (+ i 1) (operator (f i) result))))))
((2) (let ((lower-i (%%interval-lower-bound interval 0))
(lower-j (%%interval-lower-bound interval 1))
(upper-i (%%interval-upper-bound interval 0))
(upper-j (%%interval-upper-bound interval 1)))
(let i-loop ((i lower-i) (result identity))
(if (= i upper-i)
result
(let j-loop ((j lower-j) (result result))
(if (= j upper-j)
(i-loop (+ i 1) result)
(j-loop (+ j 1) (operator (f i j) result))))))))
((3) (let ((lower-i (%%interval-lower-bound interval 0))
(lower-j (%%interval-lower-bound interval 1))
(lower-k (%%interval-lower-bound interval 2))
(upper-i (%%interval-upper-bound interval 0))
(upper-j (%%interval-upper-bound interval 1))
(upper-k (%%interval-upper-bound interval 2)))
(let i-loop ((i lower-i) (result identity))
(if (= i upper-i)
result
(let j-loop ((j lower-j) (result result))
(if (= j upper-j)
(i-loop (+ i 1) result)
(let k-loop ((k lower-k) (result result))
(if (= k upper-k)
(j-loop (+ j 1) result)
(k-loop (+ k 1) (operator (f i j k) result))))))))))
((4) (let ((lower-i (%%interval-lower-bound interval 0))
(lower-j (%%interval-lower-bound interval 1))
(lower-k (%%interval-lower-bound interval 2))
(lower-l (%%interval-lower-bound interval 3))
(upper-i (%%interval-upper-bound interval 0))
(upper-j (%%interval-upper-bound interval 1))
(upper-k (%%interval-upper-bound interval 2))
(upper-l (%%interval-upper-bound interval 3)))
(let i-loop ((i lower-i) (result identity))
(if (= i upper-i)
result
(let j-loop ((j lower-j) (result result))
(if (= j upper-j)
(i-loop (+ i 1) result)
(let k-loop ((k lower-k) (result result))
(if (= k upper-k)
(j-loop (+ j 1) result)
(let l-loop ((l lower-l) (result result))
(if (= l upper-l)
(k-loop (+ k 1) result)
(l-loop (+ l 1) (operator (f i j k l) result))))))))))))
(else
(let* ((lower-bounds (%%interval-lower-bounds->list interval))
(upper-bounds (%%interval-upper-bounds->list interval))
(arg (map values lower-bounds))) ; copy lower-bounds
;; I'm not particularly happy with set! here because f or operator might capture
;; the continuation and then funny things might pursue ...
;; But it seems that the only way to have this work efficiently without the set~
;; is to have arrays with fortran-style numbering.
;; blah
(define (iterate lower-bounds-tail
upper-bounds-tail
arg-tail
result)
(let ((lower-bound (car lower-bounds-tail))
(upper-bound (car upper-bounds-tail)))
(if (null? (cdr arg-tail))
(let loop ((i lower-bound)
(result result))
(if (= i upper-bound)
result
(begin
(set-car! arg-tail i)
(loop (+ i 1)
(operator (apply f arg) result)))))
(let loop ((i lower-bound)
(result result))
(if (= i upper-bound)
result
(begin
(set-car! arg-tail i)
(loop (+ i 1)
(iterate (cdr lower-bounds-tail)
(cdr upper-bounds-tail)
(cdr arg-tail)
result))))))))
(iterate lower-bounds
upper-bounds
arg
identity)))))
;; We'll use the same basic container for all types of arrays.
(declare (inline))
(define-structure %%array
;; Part of all arrays
domain ;; an interval
getter ;; (lambda (i_0 ... i_n-1) ...) returns a value for (i_0,...,i_n-1) in (array-domain a)
;; Part of mutable arrays
setter ;; (lambda (v i_0 ... i_n-1) ...) sets a value for (i_0,...,i_n-1) in (array-domain a)
;; Part of specialized arrays
storage-class ;; a storage-class
body ;; the backing store for this array
indexer ;; see below
safe? ;; do we check whether bounds (in getters and setters) and values (in setters) are valid
in-order? ;; are the elements adjacent and in order?
)
(define specialized-array-default-safe?
(let ((%%specialized-array-default-safe? #f))
(case-lambda
(() %%specialized-array-default-safe?)
((bool)
(cond ((not (boolean? bool))
(error "specialized-array-default-safe?: The argument is not a boolean: " bool))
(else
(set! %%specialized-array-default-safe? bool)))))))
(define specialized-array-default-mutable?
(let ((%%specialized-array-default-mutable? #t))
(case-lambda
(()
%%specialized-array-default-mutable?)
((bool)
(cond ((not (boolean? bool))
(error "specialized-array-default-mutable?: The argument is not a boolean: " bool))
(else
(set! %%specialized-array-default-mutable? bool)))))))
(declare (not inline))
;; An array has a domain (which is an interval) and an getter that maps that domain into some type of
;; Scheme objects
(define %%order-unknown 1) ;; can be any nonboolean
(define make-array
(case-lambda
((domain getter)
(cond ((not (interval? domain))
(error "make-array: The first argument is not an interval: " domain getter))
((not (procedure? getter))
(error "make-array: The second argument is not a procedure: " domain getter))
(else
(make-%%array domain
getter
#f ; no setter
#f ; storage-class
#f ; body
#f ; indexer
#f ; safe?
%%order-unknown ; in-order?
))))
((domain getter setter)
(cond ((not (interval? domain))
(error "make-array: The first argument is not an interval: " domain getter setter))
((not (procedure? getter))
(error "make-array: The second argument is not a procedure: " domain getter setter))
((not (procedure? setter))
(error "make-array: The third argument is not a procedure: " domain getter setter))
(else
(make-%%array domain
getter
setter
#f ; storage-class
#f ; body
#f ; indexer
#f ; safe?
%%order-unknown ; in-order?
))))))
(define (array? x)
(%%array? x))
(define (array-domain obj)
(cond ((not (array? obj))
(error "array-domain: The argument is not an array: " obj))
(else
(%%array-domain obj))))
(define (array-getter obj)
(cond ((not (array? obj))
(error "array-getter: The argument is not an array: " obj))
(else
(%%array-getter obj))))
(define (%%array-dimension array)
(%%interval-dimension (%%array-domain array)))
(define (array-dimension array)
(cond ((not (array? array))
(error "array-dimension: The argument is not an array: " array))
(else
(%%array-dimension array))))
;;;
;;; A mutable array has, in addition a setter, that satisfies, roughly
;;;
;;; If (i_1, ..., i_n)\neq (j_1, ..., j_n) \in (array-domain a)
;;;
;;; and
;;;
;;; ((array-getter a) j_1 ... j_n) => x
;;;
;;; then "after"
;;;
;;; ((array-setter a) v i_1 ... i_n)
;;;
;;; we have
;;;
;;; ((array-getter a) j_1 ... j_n) => x
;;;
;;; and
;;;
;;; ((array-getter a) i_1 ... i_n) => v
;;;
(define (mutable-array? obj)
(and (array? obj)
(not (eq? (%%array-setter obj) #f))))
(define (array-setter obj)
(cond ((not (mutable-array? obj))
(error "array-setter: The argument is not an mutable array: " obj))
(else
(%%array-setter obj))))
;;;
;;; A storage-class contains functions and objects to manipulate the
;;; backing store of a specialized-array.
;;;
;;; getter: (lambda (body i) ...) returns the value of body at index i
;;; setter: (lambda (body i v) ...) sets the value of body at index i to v
;;; checker: (lambda (val) ...) checks that val is an appropriate value for storing in (maker n)
;;; maker: (lambda (n val) ...) makes a body of length n with value val
;;; length: (lambda (body) ...) returns the number of objects in body
;;; default: object is the default value with which to fill body
;;;
(define-structure storage-class getter setter checker maker copier length default)
;;; We define specialized storage-classes for:
;;;
;;; 32- and 64-bit floating-point numbers,
;;; complex numbers with real and imaginary parts of 32- and 64-bit floating-point numbers respectively
;;; 8-, 16-, 32-, and 64-bit signed integers,
;;; 8-, 16-, 32-, and 64-bit unsigned integers, and
;;; 1-bit unsigned integers
;;;
;;; as well as generic objects.
(define-macro (make-standard-storage-classes)
(define (symbol-concatenate . symbols)
(string->symbol (apply string-append (map (lambda (s)
(if (string? s)
s
(symbol->string s)))
symbols))))
`(begin
,@(map (lambda (name prefix default checker)
`(define ,(symbol-concatenate name '-storage-class)
(make-storage-class
;; getter:
(lambda (v i)
(,(symbol-concatenate prefix 'vector-ref) v i))
;; setter:
(lambda (v i val)
(,(symbol-concatenate prefix 'vector-set!) v i val))
;; checker
,checker
;; maker:
,(symbol-concatenate 'make- prefix 'vector)
;; copier
,(symbol-concatenate prefix 'vector-copy!)
;; length:
,(symbol-concatenate prefix 'vector-length)
;; default:
,default)))
'(generic s8 u8 s16 u16 s32 u32 s64 u64 f32 f64)
'("" s8 u8 s16 u16 s32 u32 s64 u64 f32 f64)
'(#f 0 0 0 0 0 0 0 0 0.0 0.0)
`((lambda (x) #t) ; generic
(lambda (x) ; s8
(and (fixnum? x)
(fx<= ,(- (expt 2 7))
x
,(- (expt 2 7) 1))))
(lambda (x) ; u8
(and (fixnum? x)
(fx<= 0
x
,(- (expt 2 8) 1))))
(lambda (x) ; s16
(and (fixnum? x)
(fx<= ,(- (expt 2 15))
x
,(- (expt 2 15) 1))))
(lambda (x) ; u16
(and (fixnum? x)
(fx<= 0
x
,(- (expt 2 16) 1))))
(lambda (x) ; s32
(declare (generic))
(and (exact-integer? x)
(<= ,(- (expt 2 31))
x
,(- (expt 2 31) 1))))
(lambda (x) ; u32
(declare (generic))
(and (exact-integer? x)
(<= 0
x
,(- (expt 2 32) 1))))
(lambda (x) ; s64
(declare (generic))
(and (exact-integer? x)
(<= ,(- (expt 2 63))
x
,(- (expt 2 63) 1))))
(lambda (x) ; u64
(declare (generic))
(and (exact-integer? x)
(<= 0
x
,(- (expt 2 64) 1))))
(lambda (x) (flonum? x)) ; f32
(lambda (x) (flonum? x)) ; f64
))))