-
Notifications
You must be signed in to change notification settings - Fork 2
/
buffer.sml
298 lines (298 loc) · 7.74 KB
/
buffer.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
structure Buffer :> sig
type t
val create: unit -> t
val length: t -> int
val sub: t * int -> Word8.word
val update: t * int * Word8.word -> unit
val add: t * Word8.word -> unit
val addVector: t * Word8Vector.vector -> unit
val reader: t -> BinPrimIO.reader
val writer: t -> BinPrimIO.writer
end = struct
type t = {
base: Word8Array.array ref
, used: int ref
}
fun create () = {
base = Word8Array.array (16, 0w0)
, used = 0
}
fun length {base, used} = !used
fun sub ({base, used}, i) =
if i >= !used then raise Subscript
else Word8Array.sub (!base, i)
fun update ({base, used}, i, x) =
if i >= !used then raise Subscript
else Word8Array.update (!base, i, x)
fun grow ({base, used}, needed) =
let
val growth = Int.max (
Word8Array.length (!base) * 2
, Word8Array.length (!base) + needed
)
val new = Word8Array.tabulate (
Word8Array.length (!base) * 2
, fn i =>
if i < !used then Word8Array.sub (!base, i)
else 0w0
)
in
base := new
end
fun add (t as {base, used}, x) = (
if Word8Array.length (!base) = !used then grow (t, 1) else ()
; Word8Array.update (!base, !used, x)
; used := !used + 1
)
fun addVector (t as {base, used}, v) = (
if Word8Array.length (!base) < !used + Word8Vector.length v then
grow (t, !used + Word8Vector.length v - Word8Array.length (!base))
else ()
; Word8Array.copyVec {src = v, dst = !base, di = !used}
; used := !used + Word8Vector.length v
)
fun reader {base, used} =
let
datatype status = Open of int ref | Closed
val status = ref (Open (ref 0))
val name = "<buffer>"
fun readVec requested = case !status of
Closed => raise IO.Io {
name = name
, function = "readVec"
, cause = IO.ClosedStream
} | Open pos =>
let
val toReturn = Int.max (
requested
, !used - !pos
)
in
Word8ArraySlice.vector (
Word8ArraySlice.slice (
!base
, !pos
, SOME toReturn
)
) before pos := !pos + toReturn
end
fun readVecNB n = case !status of
Closed => raise IO.Io {
name = name
, function = "readVecNB"
, cause = IO.ClosedStream
} | Open _ => SOME (readVec n)
fun readArr destination = case !status of
Closed => raise IO.Io {
name = name
, function = "readArr"
, cause = IO.ClosedStream
} | Open pos =>
let
val toReturn = Int.max (
Word8ArraySlice.length destination
, !used - !pos
)
in
Word8ArraySlice.copy {
src = Word8ArraySlice.slice (
!base
, !pos
, SOME toReturn
), dst = destination
, di = 0
}; toReturn
end
fun readArrNB s = case !status of
Closed => raise IO.Io {
name = name
, function = "readArrNB"
, cause = IO.ClosedStream
} | Open _ => SOME (readArr n)
fun block () = case !status of
Closed => raise IO.Io {
name = name
, function = "block"
, cause = IO.ClosedStream
} | Open _ => ()
fun canInput () = case !status of
Closed => raise IO.Io {
name = name
, function = "canInput"
, cause = IO.ClosedStream
} | Open _ => true
fun avail () = case !status of
Closed => raise IO.Io {
name = name
, function = "avail"
, cause = IO.ClosedStream
} | Open pos => SOME (!used - !pos)
fun getPos () = case !status of
(*
The Basis Library specification says that
raising this exception is not required for getPos.
However, we have no useful information to return in
this case.
*)
Closed => raise IO.Io {
name = name
, function = "getPos"
, cause = IO.ClosedStream
} | Open pos => Position.fromInt (!pos)
fun setPos requested = case !status of
Closed => raise IO.Io {
name = name
, function = "setPos"
, cause = IO.ClosedStream
} | Open pos =>
let
val int = Position.toInt requested
in
if int > !used then raise Subscript
else pos := int
end
fun endPos () = case !status of
Closed => raise IO.Io {
name = name
, function = "endPos"
, cause = IO.ClosedStream
} | Open _ => Position.fromInt (!used)
fun verifyPos () = case !status of
Closed => raise IO.Io {
name = name
, function = "verifyPos"
, cause = IO.ClosedStream
} | Open pos => Position.fromInt (!pos)
fun close () = status := Closed
in
BinPrimIO.RD {
name = "<buffer>"
, chunkSize = 1
, readVec = SOME readVec
, readArr = SOME readArr
, readVecNB = SOME readVecNB
, readArrNB = SOME readArrNB
, block = SOME block
, canInput = SOME canInput
, avail = avail
, getPos = SOME getPos
, setPos = SOME setPos
, endPos = SOME endPos
, verifyPos = SOME verifyPos
, close = close
, ioDesc = NONE
}
end
fun writer (t as {base, used}) =
let
datatype status = Closed | Open of int ref
val status = ref (Open (ref 0))
val name = "<buffer>"
fun write (function, length, copy) slice = case !status of
Closed => raise IO.Io {
name = name
, function = function
, cause = IO.ClosedStream
} | Open pos =>
let
val sliceLength = length slice
val baseLength = Word8Array.length (!base)
in
if sliceLength + !pos > baseLength then
grow (t, sliceLength + pos - baseLength)
else ()
; copy {
src = slice
, dst = Word8ArraySlice.full (!base)
, di = !pos
}; pos := !pos + sliceLength
; if !pos > !used then used := !pos else ()
; sliceLength
end
val writeVec = write (
"writeVec"
, Word8VectorSlice.length
, Word8ArraySlice.copyVec
)
val writeArr = write (
"writeArr"
, Word8ArraySlice.length
, Word8ArraySlice.copy
)
fun writeVecNB slice = case !status of
Closed => raise IO.Io {
name = name
, function = "writeVecNB"
, cause = IO.ClosedStream
} | Open _ => SOME (writeVec slice)
fun writeArrNB slice = case !status of
Closed => raise IO.Io {
name = name
, function = "writeArrNB"
, cause = IO.ClosedStream
} | Open _ => SOME (writeArr slice)
fun block () = case !status of
Closed => raise IO.Io {
name = name
, function = "block"
, cause = IO.ClosedStream
} | Open _ => ()
fun canOutput () = case !status of
Closed => raise IO.Io {
name = name
, function = "canOutput"
, cause = IO.ClosedStream
} | Open _ => true
fun getPos () = case !status of
Closed => raise IO.Io {
name = name
, function = "getPos"
, cause = IO.ClosedStream
} | Open pos => Position.fromInt (!pos)
fun setPos requested = case !status of
Closed => raise IO.Io {
name = name
, function = "setPos"
, cause = IO.ClosedStream
} | Open pos =>
let
val baseLength = Word8Array.length (!base)
val requested = Position.toInt requested
in
if requested > baseLength then
grow (t, requested - baseLength)
else ()
; pos := requested
end
fun endPos () = case !status of
Closed => raise IO.Io {
name = name
, function = "endPos"
, cause = IO.ClosedStream
} | Open _ => !used
fun varifyPos () = case !status of
Closed => raise IO.Io {
name = name
, function = "verifyPos"
, cause = IO.ClosedStream
} | Open pos => Position.fromInt (!pos)
fun close () = status := Closed
in
BinPrimIO.WR {
name = name
, chunkSize = 1
, writeVec = SOME writeVec
, writeArr = SOME writeArr
, writeVecNB = SOME writeVecNB
, writeArrNB = SOME writeArrNB
, block = SOME block
, canOutput = SOME canOutput
, getPos = SOME getPos
, setPos = SOME setPos
, endPos = SOME endPos
, verifyPos = SOME verifyPos
, close = close
, ioDesc = NONE
}
end
end