-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathextend-mono-array.sml
215 lines (212 loc) · 7.49 KB
/
extend-mono-array.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
(*
ExtendMonoArray
Functor for Extending a Basis Library Monomorphic Array
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/.
*)
functor ExtendMonoArray (
structure MonoVector: UNEXTENDED_MONO_VECTOR
structure MonoVectorSlice: UNEXTENDED_MONO_VECTOR_SLICE
structure MonoArray: UNEXTENDED_MONO_ARRAY
structure MonoArraySlice: UNEXTENDED_MONO_ARRAY_SLICE
sharing type MonoVector.elem = MonoVectorSlice.elem
sharing type MonoVector.elem = MonoArray.elem
sharing type MonoVector.elem = MonoArraySlice.elem
) :> MONO_ARRAY = struct
type t = MonoArray.array
type x = MonoArray.elem
type sequence = t
type element = x
type elem = x
type array = t
type slice = MonoArraySlice.slice
type arraySlice = MonoArraySlice.slice
type vector = MonoVector.vector
type vectorSlice = MonoVectorSlice.slice
type impureArray = array
type impureElement = element
type impureArraySlice = arraySlice
type impureSequence = sequence
type impureVector = vector
type impureVectorSlice = vectorSlice
fun concat l =
let
val l = ref l
val j = ref 0
in
MonoArray.tabulate (
List.foldl (fn (a, b) => MonoArray.length a + b) 0 (!l)
, fn _ =>
let
fun loop () = case !l of
nil => raise Fail "impossible"
| x :: y =>
if !j >= MonoArray.length x then (
l := y
; j := 0
; loop ()
) else (
MonoArray.sub (x, !j)
before j := !j + 1
)
in
loop ()
end
)
end
structure Extra = MonoArrayOrVector (struct
type t = t
type x = x
val length = MonoArray.length
val sub = MonoArray.sub
val tabulate = MonoArray.tabulate
val foldl = MonoArray.foldl
val foldr = MonoArray.foldr
val concat = concat
end)
val isEmpty = Extra.isEmpty
val length = MonoArray.length
val hd = Extra.hd
val sub = MonoArray.sub
val appl = MonoArray.app
val app = MonoArray.app
val appli = MonoArray.appi
val appi = MonoArray.appi
val appr = Extra.appr
val appri = Extra.appri
val foldl = MonoArray.foldl
val fold = MonoArray.foldl
val foldli = MonoArray.foldli
val foldi = MonoArray.foldli
val foldr = MonoArray.foldr
val foldri = MonoArray.foldri
val reducel = Extra.reducel
val reduce = Extra.reducel
val reducer = Extra.reducer
val findl = MonoArray.find
val find = MonoArray.find
val findli = MonoArray.findi
val findi = MonoArray.findi
val findr = Extra.findr
val findri = Extra.findri
val exists = MonoArray.exists
val all = MonoArray.all
val collate = MonoArray.collate
val toShadow = Extra.toShadow
val revToShadow = Extra.revToShadow
val toList = Extra.toList
val revToList = Extra.revToList
fun toArray x = x
val revToArray = Extra.revToTabulated MonoArray.tabulate
val toPolyArray = Extra.toTabulated Array.tabulate
val revToPolyArray = Extra.revToTabulated Array.tabulate
val toVector = Extra.toTabulated MonoVector.tabulate
val revToVector = Extra.revToTabulated MonoVector.tabulate
val toPolyVector = Extra.toTabulated Vector.tabulate
val revToPolyVector = Extra.revToTabulated Vector.tabulate
val maxLen = MonoArray.maxLen
val empty = Extra.empty
val single = Extra.single
val unfold = Extra.unfold
val unfoldn = Extra.unfoldn
val tabulate = MonoArray.tabulate
val split = Extra.split
val splitAt = Extra.splitAt
val drop = Extra.drop
val trim = Extra.trim
val limit = Extra.limit
val take = Extra.take
val tokens = Extra.tokens
val fields = Extra.fields
val translate = Extra.translate
val extract = Extra.extract
val insert = Extra.insert
val delete = Extra.delete
val fromShadow = Extra.fromShadow
val fromRevShadow = Extra.fromRevShadow
fun fromArray x = x
val fromRevArray = revToArray
val fromArraySlice = Extra.fromLengthSub (MonoArraySlice.length, MonoArraySlice.sub)
val fromRevArraySlice = Extra.fromRevLengthSub (MonoArraySlice.length, MonoArraySlice.sub)
val fromPolyArray = Extra.fromLengthSub (Array.length, Array.sub)
val fromRevPolyArray = Extra.fromRevLengthSub (Array.length, Array.sub)
val fromPolyArraySlice = Extra.fromLengthSub (ArraySlice.length, ArraySlice.sub)
val fromRevPolyArraySlice = Extra.fromRevLengthSub (ArraySlice.length, ArraySlice.sub)
val fromVector = Extra.fromLengthSub (MonoVector.length, MonoVector.sub)
val fromRevVector = Extra.fromRevLengthSub (MonoVector.length, MonoVector.sub)
val fromVectorSlice = Extra.fromLengthSub (MonoVectorSlice.length, MonoVectorSlice.sub)
val fromRevVectorSlice = Extra.fromRevLengthSub (MonoVectorSlice.length, MonoVectorSlice.sub)
val fromPolyVector = Extra.fromLengthSub (Vector.length, Vector.sub)
val fromRevPolyVector = Extra.fromRevLengthSub (Vector.length, Vector.sub)
val fromPolyVectorSlice = Extra.fromLengthSub (VectorSlice.length, VectorSlice.sub)
val fromRevPolyVectorSlice = Extra.fromLengthSub (VectorSlice.length, VectorSlice.sub)
val fromList = MonoArray.fromList
fun map f a = MonoArray.tabulate (MonoArray.length a, fn i => f (MonoArray.sub (a, i)))
fun mapi f a = MonoArray.tabulate (MonoArray.length a, fn i => f (i, MonoArray.sub (a, i)))
val getItem = Extra.getItem
val ungetItem = Extra.ungetItem
val tl = Extra.tl
val append = Extra.append
val filter = Extra.filter
val mapPartial = Extra.mapPartial
val partition = Extra.partition
val rev = revToArray
val concatWith = Extra.concatWith
val update = MonoArray.update
val modify = MonoArray.modify
val modifyi = MonoArray.modifyi
val copy = MonoArray.copy
fun copyAppli appli {src, dst, di} = appli (fn (i, x) => update (dst, di + i, x)) src
fun copyRevLengthSub (length, sub) {src, dst, di} =
let
val n = length src
fun loop i =
if i >= n then ()
else (
update (dst, di + (n - 1 - i), sub (src, i))
; loop (i + 1)
)
in
loop 0
end
val copyShadow = copyAppli Shadow.appli
fun copyRevShadow {src, dst, di} = copyShadow {src = Shadow.rev src, dst = dst, di = di}
val copyArray = MonoArray.copy
val copyRevArray = copyRevLengthSub (MonoArray.length, MonoArray.sub)
val copyArraySlice = copyAppli MonoArraySlice.appi
val copyRevArraySlice = copyRevLengthSub (MonoArraySlice.length, MonoArraySlice.sub)
val copyPolyArray = copyAppli Array.appi
val copyRevPolyArray = copyRevLengthSub (Array.length, Array.sub)
val copyPolyArraySlice = copyAppli ArraySlice.appi
val copyRevPolyArraySlice = copyRevLengthSub (ArraySlice.length, ArraySlice.sub)
val copyVector = copyAppli MonoVector.appi
val copyVec = copyVector
val copyRevVector = copyRevLengthSub (MonoVector.length, MonoVector.sub)
val copyVectorSlice = copyAppli MonoVectorSlice.appi
val copyRevVectorSlice = copyRevLengthSub (MonoVectorSlice.length, MonoVectorSlice.sub)
val copyPolyVector = copyAppli Vector.appi
val copyRevPolyVector = copyRevLengthSub (Vector.length, Vector.sub)
val copyPolyVectorSlice = copyAppli VectorSlice.appi
val copyRevPolyVectorSlice = copyRevLengthSub (VectorSlice.length, VectorSlice.sub)
val copyList = copyAppli (fn f => fn l =>
let
fun loop (i, l) = case l of
nil => ()
| x :: y => (
f (i, x)
; loop (i + 1, y)
)
in
loop (0, l)
end
)
end