-
Notifications
You must be signed in to change notification settings - Fork 2
/
shadow.sml
1443 lines (1423 loc) · 38.6 KB
/
shadow.sml
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
(*
Shadows - Abstract, lazy, functional sequences
Copyright 2011 Christopher Cramer
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/.
*)
signature SHADOW = sig
(*
'a t is the type of a shadow - a sequence containing elements of
type 'a. Retrieval is generally lazy, meaning that an
element is not retrieved unless and until it is requested.
For shadows returned from map, filter, etc. this also
means that the given function is not applied to an element
unless and until the result of that application is requested.
Shadows can be based on either random or sequential access,
which will determine the performance, but not the availability,
of operations.
*)
type 'a t = 'a Shadow.t
(*
Shadows are createable and pure.
*)
include CREATEABLE_SEQUENCE where type 'a sequence = 'a t
include PURE_SEQUENCE where type 'a pureSequence = 'a t
(*
This exception is raised when a shadow returns an erroneous
length, that is, the number of elements returned is not
the same as the purported length.
*)
exception BadLength
(*
This exception is raised when the length of a shadow is
requested, but the shadow does not have a finite length.
*)
exception InfiniteLength
datatype access = datatype Shadow.access
(*
access t returns the access characteristics of t.
*)
val access: 'a t -> access
(*
create {access, getItem, sub, length} creates a sequence from
an access indicator and three functions.
access indicates whether the underlying structure of a shadow
is random or sequential.
getItem is a function which returns either NONE if the
sequence is empty, or SOME (x, t) in which x is the next
element in the sequence and t is a sequence containing
the remaining elements. next must be idempotent, that
is, repeated calls to next must have the same effect as
one call.
sub is a function which returns an element by index. If
the given index is greater than or equal to the length of
the shadow, sub should raise the Subscript exception.
length is a function which returns the number of
elements in the sequence. If the sequence does not have
a finite number of elements, then length should raise
InfiniteLength.
*)
val create: {
access: access
, getItem: unit -> ('a * 'a t) option
, sub: int -> 'a
, length: unit -> int
} -> 'a t
(*
fromRandom {sub, length} creates a shadow from two functions.
This is equivalent to create above, except that access is
assumed to be Random and getItem is computed using sub and length.
*)
val fromRandom: {sub: int -> 'a, length: unit -> int} -> 'a t
(*
fromIdempotentFunction f creates a shadow from a function.
This is equivalent to create above, giving f as getItem, except
that access is assumed to be Sequential, and sub and length are
computed by calling f.
*)
val fromIdempotentFunction: (unit -> ('a * 'a t) option) -> 'a t
(*
fromFunction f creates a sequence from a function.
This is equivalent to fromIdempotentFunction above, except
that f is not required to be idempotent; f will only ever
be called zero or one times.
*)
val fromFunction: (unit -> ('a * 'a t) option) -> 'a t
(*
for {start, test, step} returns a sequence which
goes through a sequence of values. The first value
is given by start. On each iteration, test is applied,
and the value is yielded only if test returns true.
If test start returns false, then the sequence is empty.
The next value is given by applying step to the previous
value. This behavior mimics the for loops in C, C++, and
Java.
*)
val for: {
start: 'a
, test: 'a -> bool
, step: 'a -> 'a
} -> 'a t
(*
numbers {from, to, step} returns a sequence of numbers
starting at from, ending inclusively at to, with an
increment of step. step may be negative, or even zero. If
the difference between from and to is not a multiple of
step, the last element in the sequence will not be to.
If step is zero, requesting the length of the sequence
will result in raising InfiniteLength. If step is
negative and to is greather than from, or step is positive
and to is less than from, the sequence will be empty.
*)
val numbers: {from: int, to: int, step: int} -> int t
(*
fromReader f a returns a sequence based on applying
f to a. The result of f a should be either NONE, indicating
the end of the sequence, or SOME (x, b), in which x
is the next element of the sequence and f b is the next
application. f must be idempotent, that is, multiple calls
to f a, f b, etc. should be the same as one call. fromReader
could be used with many functions from the Standard Basis,
including TextIO.StreamIO.input1, Substring.getc,
VectorSlice.getItem, List.getItem, etc., although
conversion functions have been provided, which in some cases
give a faster length.
*)
val fromReader: ('a -> ('b * 'a) option) -> 'a -> 'b t
(*
pairs t returns a sequence of pairs of the elements of
t. The first pair contains the first and second element,
the second pair contains the third and fourth element,
and so on. If t contains an odd number of elements,
the last element is ignored.
*)
val pairs: 'a t -> ('a * 'a) t
(*
group (t, n) returns a sequence of subsequences,
in which each subsequence has a length of n elements,
taken in succession from t.
*)
val group: ('a t * int) -> 'a t t
end
structure Shadow :> SHADOW = struct
(*
Because the SHADOW signature includes CREATEABLE_SEQUENCE etc.
which themselves require the Shadow.t type, Shadow.t (and Shadow.access,
upon which it depends) is defined elsewhere and then opened here.
*)
open Shadow
type 'a sequence = 'a t
type 'a pureSequence = 'a t
val maxLen = valOf Int.maxInt
exception BadLength
exception InfiniteLength
fun create x = T x
fun access (T {access, ...}) = access
fun sub (T {sub, ...}, i) = sub i
fun getItem (T {getItem, ...}) = getItem ()
fun length (T {length, ...}) = length ()
fun empty () = create {
access = Random
, getItem = fn () => NONE
, length = fn () => 0
, sub = fn _ => raise Subscript
}
fun single x = create {
access = Random
, getItem = fn () => SOME (
x
, empty ()
), length = fn () => 1
, sub = fn
0 => x
| _ => raise Subscript
}
fun hd t = case getItem t of
NONE => raise Empty
| SOME (x, _) => x
fun tl t = case getItem t of
NONE => raise Empty
| SOME (_, x) => x
fun fromRandom {sub, length} =
let
fun length' offset () = length () - offset
fun sub' offset index = sub (offset + index)
fun getItem' offset () =
if offset >= length () then NONE
else SOME (
sub offset
, create' (offset + 1)
)
and create' offset = create {
access = Random
, sub = sub' offset
, length = length' offset
, getItem = getItem' offset
}
in
create' 0
end
fun fromIdempotentFunction f =
let
fun length' () = case f () of
NONE => 0
| SOME (_, x) => 1 + length x
fun sub' i = case (i, f ()) of
(_, NONE) => raise Subscript
| (0, SOME (x, _)) => x
| (_, SOME (_, x)) => sub (x, i - 1)
in
create {
access = Sequential
, getItem = f
, length = length'
, sub = sub'
}
end
fun lazy f =
let
val x = ref NONE
in
fn () => (case !x of
NONE =>
let
val y = f ()
in
x := SOME y
; y
end
| SOME y => y
)
end
fun fromFunction f = fromIdempotentFunction (lazy f)
fun unfold f a = fromFunction (fn () =>
case f a of
NONE => NONE
| SOME (b, a) => SOME (
b
, unfold f a
)
)
fun unfoldn f (n, a) = fromFunction (fn () =>
if n <= 0 then NONE
else
let
val (b, a) = f a
in
SOME (b, unfoldn f (n - 1, a))
end
)
fun for {start, test, step} = fromFunction (fn () =>
if test start then SOME (
start
, for {start = step start, test = test, step = step}
) else NONE
)
fun numbers {from, to, step} =
let
fun zero () = create {
access = Random
, getItem = fn () => SOME (from, zero ())
, length = fn () => raise InfiniteLength
, sub = fn _ => from
}
fun length current () =
Int.max ((to - current) div step + 1, 0)
fun nonzero compare current = create {
access = Random
, length = length current
, getItem = fn () =>
if compare (current, to) then SOME (
current
, nonzero compare (step + current)
) else NONE
, sub = fn i =>
let
val target = current + step * i
in
if compare (target, to) then target
else raise Subscript
end
}
in
case Int.compare (step, 0) of
EQUAL =>
if from = to then single from
else zero ()
| GREATER => nonzero (op <=) from
| LESS => nonzero (op >=) from
end
fun fromShadow t = t
fun fromReader f a = fromIdempotentFunction (fn () =>
(case f a of
NONE => NONE
| SOME (b, a) => SOME (b, fromReader f a)
)
)
fun fromSlice (x as (length, getItem, sub)) slice = create {
access = Random
, length = fn () => length slice
, getItem = fn () => (
case getItem slice of
NONE => NONE
| SOME (y, slice) => SOME (
y
, fromSlice x slice
)
), sub = fn i => sub (slice, i)
}
fun fromRevSlice (x as (length, sub, subslice)) slice = create {
access = Random
, length = fn () => length slice
, getItem = fn () =>
let
val length' = length slice
in
if length' = 0 then NONE
else SOME (
sub (slice, length' - 1)
, fromRevSlice x (subslice (slice, 0, SOME (length' - 1)))
)
end
, sub = fn index => sub (slice, index)
}
fun fromArraySlice slice =
fromSlice (
ArraySlice.length
, ArraySlice.getItem
, ArraySlice.sub
) slice
fun fromRevArraySlice slice =
fromRevSlice (
ArraySlice.length
, ArraySlice.sub
, ArraySlice.subslice
) slice
val fromCharArraySlice =
fromSlice (
CharArraySlice.length
, CharArraySlice.getItem
, CharArraySlice.sub
)
val fromRevCharArraySlice =
fromRevSlice (
CharArraySlice.length
, CharArraySlice.sub
, CharArraySlice.subslice
)
val fromBoolArraySlice =
fromSlice (
BoolArraySlice.length
, BoolArraySlice.getItem
, BoolArraySlice.sub
)
val fromRevBoolArraySlice =
fromRevSlice (
BoolArraySlice.length
, BoolArraySlice.sub
, BoolArraySlice.subslice
)
val fromIntArraySlice =
fromSlice (
IntArraySlice.length
, IntArraySlice.getItem
, IntArraySlice.sub
)
val fromRevIntArraySlice =
fromRevSlice (
IntArraySlice.length
, IntArraySlice.sub
, IntArraySlice.subslice
)
val fromWordArraySlice =
fromSlice (
WordArraySlice.length
, WordArraySlice.getItem
, WordArraySlice.sub
)
val fromRevWordArraySlice =
fromRevSlice (
WordArraySlice.length
, WordArraySlice.sub
, WordArraySlice.subslice
)
val fromRealArraySlice =
fromSlice (
RealArraySlice.length
, RealArraySlice.getItem
, RealArraySlice.sub
)
val fromRevRealArraySlice =
fromRevSlice (
RealArraySlice.length
, RealArraySlice.sub
, RealArraySlice.subslice
)
val fromLargeIntArraySlice =
fromSlice (
LargeIntArraySlice.length
, LargeIntArraySlice.getItem
, LargeIntArraySlice.sub
)
val fromRevLargeIntArraySlice =
fromRevSlice (
LargeIntArraySlice.length
, LargeIntArraySlice.sub
, LargeIntArraySlice.subslice
)
val fromLargeWordArraySlice =
fromSlice (
LargeWordArraySlice.length
, LargeWordArraySlice.getItem
, LargeWordArraySlice.sub
)
val fromRevLargeWordArraySlice =
fromRevSlice (
LargeWordArraySlice.length
, LargeWordArraySlice.sub
, LargeWordArraySlice.subslice
)
val fromInt8ArraySlice =
fromSlice (
Int8ArraySlice.length
, Int8ArraySlice.getItem
, Int8ArraySlice.sub
)
val fromRevInt8ArraySlice =
fromRevSlice (
Int8ArraySlice.length
, Int8ArraySlice.sub
, Int8ArraySlice.subslice
)
val fromInt16ArraySlice =
fromSlice (
Int16ArraySlice.length
, Int16ArraySlice.getItem
, Int16ArraySlice.sub
)
val fromRevInt16ArraySlice =
fromRevSlice (
Int16ArraySlice.length
, Int16ArraySlice.sub
, Int16ArraySlice.subslice
)
val fromInt32ArraySlice =
fromSlice (
Int32ArraySlice.length
, Int32ArraySlice.getItem
, Int32ArraySlice.sub
)
val fromRevInt32ArraySlice =
fromRevSlice (
Int32ArraySlice.length
, Int32ArraySlice.sub
, Int32ArraySlice.subslice
)
val fromInt64ArraySlice =
fromSlice (
Int64ArraySlice.length
, Int64ArraySlice.getItem
, Int64ArraySlice.sub
)
val fromRevInt64ArraySlice =
fromRevSlice (
Int64ArraySlice.length
, Int64ArraySlice.sub
, Int64ArraySlice.subslice
)
val fromWord8ArraySlice =
fromSlice (
Word8ArraySlice.length
, Word8ArraySlice.getItem
, Word8ArraySlice.sub
)
val fromRevWord8ArraySlice =
fromRevSlice (
Word8ArraySlice.length
, Word8ArraySlice.sub
, Word8ArraySlice.subslice
)
val fromWord16ArraySlice =
fromSlice (
Word16ArraySlice.length
, Word16ArraySlice.getItem
, Word16ArraySlice.sub
)
val fromRevWord16ArraySlice =
fromRevSlice (
Word16ArraySlice.length
, Word16ArraySlice.sub
, Word16ArraySlice.subslice
)
val fromWord32ArraySlice =
fromSlice (
Word32ArraySlice.length
, Word32ArraySlice.getItem
, Word32ArraySlice.sub
)
val fromRevWord32ArraySlice =
fromRevSlice (
Word32ArraySlice.length
, Word32ArraySlice.sub
, Word32ArraySlice.subslice
)
val fromWord64ArraySlice =
fromSlice (
Word64ArraySlice.length
, Word64ArraySlice.getItem
, Word64ArraySlice.sub
)
val fromRevWord64ArraySlice =
fromRevSlice (
Word64ArraySlice.length
, Word64ArraySlice.sub
, Word64ArraySlice.subslice
)
val fromReal32ArraySlice =
fromSlice (
Real32ArraySlice.length
, Real32ArraySlice.getItem
, Real32ArraySlice.sub
)
val fromRevReal32ArraySlice =
fromRevSlice (
Real32ArraySlice.length
, Real32ArraySlice.sub
, Real32ArraySlice.subslice
)
val fromReal64ArraySlice =
fromSlice (
Real64ArraySlice.length
, Real64ArraySlice.getItem
, Real64ArraySlice.sub
)
val fromRevReal64ArraySlice =
fromRevSlice (
Real64ArraySlice.length
, Real64ArraySlice.sub
, Real64ArraySlice.subslice
)
fun fromArray array = fromArraySlice (ArraySlice.full array)
fun fromRevArray array = fromRevArraySlice (ArraySlice.full array)
val fromCharArray = fromCharArraySlice o CharArraySlice.full
val fromRevCharArray = fromRevCharArraySlice o CharArraySlice.full
val fromBoolArray = fromBoolArraySlice o BoolArraySlice.full
val fromRevBoolArray = fromRevBoolArraySlice o BoolArraySlice.full
val fromIntArray = fromIntArraySlice o IntArraySlice.full
val fromRevIntArray = fromRevIntArraySlice o IntArraySlice.full
val fromWordArray = fromWordArraySlice o WordArraySlice.full
val fromRevWordArray = fromRevWordArraySlice o WordArraySlice.full
val fromRealArray = fromRealArraySlice o RealArraySlice.full
val fromRevRealArray = fromRevRealArraySlice o RealArraySlice.full
val fromLargeIntArray = fromLargeIntArraySlice o LargeIntArraySlice.full
val fromRevLargeIntArray = fromRevLargeIntArraySlice o LargeIntArraySlice.full
val fromLargeWordArray = fromLargeWordArraySlice o LargeWordArraySlice.full
val fromRevLargeWordArray = fromRevLargeWordArraySlice o LargeWordArraySlice.full
val fromInt8Array = fromInt8ArraySlice o Int8ArraySlice.full
val fromRevInt8Array = fromRevInt8ArraySlice o Int8ArraySlice.full
val fromInt16Array = fromInt16ArraySlice o Int16ArraySlice.full
val fromRevInt16Array = fromRevInt16ArraySlice o Int16ArraySlice.full
val fromInt32Array = fromInt32ArraySlice o Int32ArraySlice.full
val fromRevInt32Array = fromRevInt32ArraySlice o Int32ArraySlice.full
val fromInt64Array = fromInt64ArraySlice o Int64ArraySlice.full
val fromRevInt64Array = fromRevInt64ArraySlice o Int64ArraySlice.full
val fromWord8Array = fromWord8ArraySlice o Word8ArraySlice.full
val fromRevWord8Array = fromRevWord8ArraySlice o Word8ArraySlice.full
val fromWord16Array = fromWord16ArraySlice o Word16ArraySlice.full
val fromRevWord16Array = fromRevWord16ArraySlice o Word16ArraySlice.full
val fromWord32Array = fromWord32ArraySlice o Word32ArraySlice.full
val fromRevWord32Array = fromRevWord32ArraySlice o Word32ArraySlice.full
val fromWord64Array = fromWord64ArraySlice o Word64ArraySlice.full
val fromRevWord64Array = fromRevWord64ArraySlice o Word64ArraySlice.full
val fromReal32Array = fromReal32ArraySlice o Real32ArraySlice.full
val fromRevReal32Array = fromRevReal32ArraySlice o Real32ArraySlice.full
val fromReal64Array = fromReal64ArraySlice o Real64ArraySlice.full
val fromRevReal64Array = fromRevReal64ArraySlice o Real64ArraySlice.full
fun fromVectorSlice slice =
fromSlice (
VectorSlice.length
, VectorSlice.getItem
, VectorSlice.sub
) slice
fun fromRevVectorSlice slice =
fromRevSlice (
VectorSlice.length
, VectorSlice.sub
, VectorSlice.subslice
) slice
val fromCharVectorSlice =
fromSlice (
CharVectorSlice.length
, CharVectorSlice.getItem
, CharVectorSlice.sub
)
val fromRevCharVectorSlice =
fromRevSlice (
CharVectorSlice.length
, CharVectorSlice.sub
, CharVectorSlice.subslice
)
val fromBoolVectorSlice =
fromSlice (
BoolVectorSlice.length
, BoolVectorSlice.getItem
, BoolVectorSlice.sub
)
val fromRevBoolVectorSlice =
fromRevSlice (
BoolVectorSlice.length
, BoolVectorSlice.sub
, BoolVectorSlice.subslice
)
val fromIntVectorSlice =
fromSlice (
IntVectorSlice.length
, IntVectorSlice.getItem
, IntVectorSlice.sub
)
val fromRevIntVectorSlice =
fromRevSlice (
IntVectorSlice.length
, IntVectorSlice.sub
, IntVectorSlice.subslice
)
val fromWordVectorSlice =
fromSlice (
WordVectorSlice.length
, WordVectorSlice.getItem
, WordVectorSlice.sub
)
val fromRevWordVectorSlice =
fromRevSlice (
WordVectorSlice.length
, WordVectorSlice.sub
, WordVectorSlice.subslice
)
val fromRealVectorSlice =
fromSlice (
RealVectorSlice.length
, RealVectorSlice.getItem
, RealVectorSlice.sub
)
val fromRevRealVectorSlice =
fromRevSlice (
RealVectorSlice.length
, RealVectorSlice.sub
, RealVectorSlice.subslice
)
val fromLargeIntVectorSlice =
fromSlice (
LargeIntVectorSlice.length
, LargeIntVectorSlice.getItem
, LargeIntVectorSlice.sub
)
val fromRevLargeIntVectorSlice =
fromRevSlice (
LargeIntVectorSlice.length
, LargeIntVectorSlice.sub
, LargeIntVectorSlice.subslice
)
val fromLargeWordVectorSlice =
fromSlice (
LargeWordVectorSlice.length
, LargeWordVectorSlice.getItem
, LargeWordVectorSlice.sub
)
val fromRevLargeWordVectorSlice =
fromRevSlice (
LargeWordVectorSlice.length
, LargeWordVectorSlice.sub
, LargeWordVectorSlice.subslice
)
val fromInt8VectorSlice =
fromSlice (
Int8VectorSlice.length
, Int8VectorSlice.getItem
, Int8VectorSlice.sub
)
val fromRevInt8VectorSlice =
fromRevSlice (
Int8VectorSlice.length
, Int8VectorSlice.sub
, Int8VectorSlice.subslice
)
val fromInt16VectorSlice =
fromSlice (
Int16VectorSlice.length
, Int16VectorSlice.getItem
, Int16VectorSlice.sub
)
val fromRevInt16VectorSlice =
fromRevSlice (
Int16VectorSlice.length
, Int16VectorSlice.sub
, Int16VectorSlice.subslice
)
val fromInt32VectorSlice =
fromSlice (
Int32VectorSlice.length
, Int32VectorSlice.getItem
, Int32VectorSlice.sub
)
val fromRevInt32VectorSlice =
fromRevSlice (
Int32VectorSlice.length
, Int32VectorSlice.sub
, Int32VectorSlice.subslice
)
val fromInt64VectorSlice =
fromSlice (
Int64VectorSlice.length
, Int64VectorSlice.getItem
, Int64VectorSlice.sub
)
val fromRevInt64VectorSlice =
fromRevSlice (
Int64VectorSlice.length
, Int64VectorSlice.sub
, Int64VectorSlice.subslice
)
val fromWord8VectorSlice =
fromSlice (
Word8VectorSlice.length
, Word8VectorSlice.getItem
, Word8VectorSlice.sub
)
val fromRevWord8VectorSlice =
fromRevSlice (
Word8VectorSlice.length
, Word8VectorSlice.sub
, Word8VectorSlice.subslice
)
val fromWord16VectorSlice =
fromSlice (
Word16VectorSlice.length
, Word16VectorSlice.getItem
, Word16VectorSlice.sub
)
val fromRevWord16VectorSlice =
fromRevSlice (
Word16VectorSlice.length
, Word16VectorSlice.sub
, Word16VectorSlice.subslice
)
val fromWord32VectorSlice =
fromSlice (
Word32VectorSlice.length
, Word32VectorSlice.getItem
, Word32VectorSlice.sub
)
val fromRevWord32VectorSlice =
fromRevSlice (
Word32VectorSlice.length
, Word32VectorSlice.sub
, Word32VectorSlice.subslice
)
val fromWord64VectorSlice =
fromSlice (
Word64VectorSlice.length
, Word64VectorSlice.getItem
, Word64VectorSlice.sub
)
val fromRevWord64VectorSlice =
fromRevSlice (
Word64VectorSlice.length
, Word64VectorSlice.sub
, Word64VectorSlice.subslice
)
val fromReal32VectorSlice =
fromSlice (
Real32VectorSlice.length
, Real32VectorSlice.getItem
, Real32VectorSlice.sub
)
val fromRevReal32VectorSlice =
fromRevSlice (
Real32VectorSlice.length
, Real32VectorSlice.sub
, Real32VectorSlice.subslice
)
val fromReal64VectorSlice =
fromSlice (
Real64VectorSlice.length
, Real64VectorSlice.getItem
, Real64VectorSlice.sub
)
val fromRevReal64VectorSlice =
fromRevSlice (
Real64VectorSlice.length
, Real64VectorSlice.sub
, Real64VectorSlice.subslice
)
fun fromVector vector = fromVectorSlice (VectorSlice.full vector)
fun fromRevVector vector = fromRevVectorSlice (VectorSlice.full vector)
val fromCharVector = fromCharVectorSlice o CharVectorSlice.full
val fromRevCharVector = fromRevCharVectorSlice o CharVectorSlice.full
val fromBoolVector = fromBoolVectorSlice o BoolVectorSlice.full
val fromRevBoolVector = fromRevBoolVectorSlice o BoolVectorSlice.full
val fromIntVector = fromIntVectorSlice o IntVectorSlice.full
val fromRevIntVector = fromRevIntVectorSlice o IntVectorSlice.full
val fromWordVector = fromWordVectorSlice o WordVectorSlice.full
val fromRevWordVector = fromRevWordVectorSlice o WordVectorSlice.full
val fromRealVector = fromRealVectorSlice o RealVectorSlice.full
val fromRevRealVector = fromRevRealVectorSlice o RealVectorSlice.full
val fromLargeIntVector = fromLargeIntVectorSlice o LargeIntVectorSlice.full
val fromRevLargeIntVector = fromRevLargeIntVectorSlice o LargeIntVectorSlice.full
val fromLargeWordVector = fromLargeWordVectorSlice o LargeWordVectorSlice.full
val fromRevLargeWordVector = fromRevLargeWordVectorSlice o LargeWordVectorSlice.full
val fromInt8Vector = fromInt8VectorSlice o Int8VectorSlice.full
val fromRevInt8Vector = fromRevInt8VectorSlice o Int8VectorSlice.full
val fromInt16Vector = fromInt16VectorSlice o Int16VectorSlice.full
val fromRevInt16Vector = fromRevInt16VectorSlice o Int16VectorSlice.full
val fromInt32Vector = fromInt32VectorSlice o Int32VectorSlice.full
val fromRevInt32Vector = fromRevInt32VectorSlice o Int32VectorSlice.full
val fromInt64Vector = fromInt64VectorSlice o Int64VectorSlice.full
val fromRevInt64Vector = fromRevInt64VectorSlice o Int64VectorSlice.full
val fromWord8Vector = fromWord8VectorSlice o Word8VectorSlice.full
val fromRevWord8Vector = fromRevWord8VectorSlice o Word8VectorSlice.full
val fromWord16Vector = fromWord16VectorSlice o Word16VectorSlice.full
val fromRevWord16Vector = fromRevWord16VectorSlice o Word16VectorSlice.full
val fromWord32Vector = fromWord32VectorSlice o Word32VectorSlice.full
val fromRevWord32Vector = fromRevWord32VectorSlice o Word32VectorSlice.full
val fromWord64Vector = fromWord64VectorSlice o Word64VectorSlice.full
val fromRevWord64Vector = fromRevWord64VectorSlice o Word64VectorSlice.full
val fromReal32Vector = fromReal32VectorSlice o Real32VectorSlice.full
val fromRevReal32Vector = fromRevReal32VectorSlice o Real32VectorSlice.full
val fromReal64Vector = fromReal64VectorSlice o Real64VectorSlice.full
val fromRevReal64Vector = fromRevReal64VectorSlice o Real64VectorSlice.full
val fromString = fromCharVector
val fromRevString = fromRevCharVector
val fromSubstring = fromCharVectorSlice
val fromRevSubstring = fromRevCharVectorSlice
fun fromList list = fromReader List.getItem list
fun fromTextIO instream = fromFunction (
fn () => (case TextIO.input1 instream of
NONE => NONE
| SOME x => SOME (
x
, fromTextIO instream
)
)
)
fun fromTextIOLines instream = fromFunction (
fn () => (case TextIO.inputLine instream of
NONE => NONE
| SOME x => SOME (
x
, fromTextIOLines instream
)
)
)
fun fromBinIO instream = fromFunction (
fn () => (case BinIO.input1 instream of
NONE => NONE
| SOME x => SOME (
x
, fromBinIO instream
)
)
)
fun toTabulated tabulate t =
let
val t = ref t
in
tabulate (
length (!t)
, fn _ => (case getItem (!t) of
NONE => raise BadLength
| SOME (x, u) => (
t := u
; x
)
)
)
end
fun toArray t = toTabulated Array.tabulate t
val toCharArray = toTabulated CharArray.tabulate
val toBoolArray = toTabulated BoolArray.tabulate
val toIntArray = toTabulated IntArray.tabulate
val toWordArray = toTabulated WordArray.tabulate
val toRealArray = toTabulated RealArray.tabulate
val toLargeIntArray = toTabulated LargeIntArray.tabulate
val toLargeWordArray = toTabulated LargeWordArray.tabulate
val toInt8Array = toTabulated Int8Array.tabulate
val toInt16Array = toTabulated Int16Array.tabulate
val toInt32Array = toTabulated Int32Array.tabulate
val toInt64Array = toTabulated Int64Array.tabulate
val toWord8Array = toTabulated Word8Array.tabulate
val toWord16Array = toTabulated Word16Array.tabulate
val toWord32Array = toTabulated Word32Array.tabulate
val toWord64Array = toTabulated Word64Array.tabulate
val toReal32Array = toTabulated Real32Array.tabulate
val toReal64Array = toTabulated Real64Array.tabulate
fun toVector t = toTabulated Vector.tabulate t
val toCharVector = toTabulated CharVector.tabulate
val toBoolVector = toTabulated BoolVector.tabulate
val toIntVector = toTabulated IntVector.tabulate
val toWordVector = toTabulated WordVector.tabulate
val toRealVector = toTabulated RealVector.tabulate
val toLargeIntVector = toTabulated LargeIntVector.tabulate
val toLargeWordVector = toTabulated LargeWordVector.tabulate
val toInt8Vector = toTabulated Int8Vector.tabulate
val toInt16Vector = toTabulated Int16Vector.tabulate
val toInt32Vector = toTabulated Int32Vector.tabulate
val toInt64Vector = toTabulated Int64Vector.tabulate
val toWord8Vector = toTabulated Word8Vector.tabulate
val toWord16Vector = toTabulated Word16Vector.tabulate
val toWord32Vector = toTabulated Word32Vector.tabulate
val toWord64Vector = toTabulated Word64Vector.tabulate
val toReal32Vector = toTabulated Real32Vector.tabulate
val toReal64Vector = toTabulated Real64Vector.tabulate
val toString = toCharVector
fun rev t = case access t of
Sequential => fromRevVector (toVector t)
| Random =>
let
fun length' index () = index + 1
fun sub' index revIndex =
if revIndex < 0 then raise Subscript
else sub (t, index - revIndex)
fun getItem' index () =
if index < 0 then NONE
else SOME (
sub (t, index)
, create' (index - 1)
)
and create' index = create {
access = Random
, length = length' index
, sub = sub' index
, getItem = getItem' index
}
in
create' (length t - 1)
end
fun fromRevShadow t = rev t
fun toShadow t = t
fun revToShadow t = rev t
fun revToList t =
let
fun loop (t, l) = case getItem t of
NONE => l
| SOME (x, t) => loop (t, x :: l)
in
loop (t, nil)
end
fun toList t = case access t of
Random => revToList (rev t)
| Sequential => (case getItem t of
NONE => nil
| SOME (x, t) => x :: toList t
)
fun revToTabulated tabulate t = case access t of
Sequential => toTabulated tabulate (rev t)
| Random =>
let
val n = length t
in
tabulate (n, fn i => sub (t, n - 1 - i))
end
fun revToArray t = revToTabulated Array.tabulate t
val revToBoolArray = revToTabulated BoolArray.tabulate
val revToCharArray = revToTabulated CharArray.tabulate
val revToIntArray = revToTabulated IntArray.tabulate
val revToWordArray = revToTabulated WordArray.tabulate
val revToRealArray = revToTabulated RealArray.tabulate
val revToLargeIntArray = revToTabulated LargeIntArray.tabulate
val revToLargeWordArray = revToTabulated LargeWordArray.tabulate
val revToInt8Array = revToTabulated Int8Array.tabulate
val revToInt16Array = revToTabulated Int16Array.tabulate
val revToInt32Array = revToTabulated Int32Array.tabulate
val revToInt64Array = revToTabulated Int64Array.tabulate
val revToWord8Array = revToTabulated Word8Array.tabulate