-
Notifications
You must be signed in to change notification settings - Fork 1
/
sequence-stream.dylan
518 lines (408 loc) · 14.9 KB
/
sequence-stream.dylan
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
module: sequence-stream
author: Dustin Voss
define open primary class <sequence-stream> (<basic-stream>, <positionable-stream>)
// Storage
slot stream-storage :: <vector>;
slot stream-start :: <integer>;
slot stream-end :: <integer>;
constant slot stream-element-type :: <type>, init-keyword: element-type:;
constant slot stream-fill :: <object>, init-keyword: fill:;
// State
slot stream-open? :: <boolean> = #t;
slot stream-position :: <integer> = 0;
slot stream-unread-from :: false-or(<integer>) = #f;
constant slot stream-direction :: one-of(#"input", #"output", #"input-output"),
init-keyword: direction:;
// Manual offset
constant slot stream-position-offset :: <integer>, init-keyword: position-offset:;
// Keywords
keyword contents: = make(<vector>);
keyword start:;
keyword end:;
keyword position-offset: = 0;
keyword element-type: = <object>;
keyword fill: = #f;
keyword direction: = #"input";
keyword outer-stream:;
end class;
define open primary class <string-stream> (<sequence-stream>)
keyword contents: = make(<string>), type: <string>;
keyword element-type: = <character>, type: <type>;
keyword fill: = ' ', type: <character>;
end class;
define sealed primary class <byte-string-stream> (<string-stream>)
keyword contents: = make(<byte-string>), type: <byte-string>;
keyword element-type: = <byte-character>, type: <type>;
keyword fill: = as(<byte-character>, ' '), type: <byte-character>;
end class;
define method make
(class == <sequence-stream>, #rest keys, #key contents, #all-keys)
=> (inst :: <sequence-stream>)
select (contents by instance?)
<string> => apply(make, <string-stream>, keys);
otherwise => next-method();
end select
end method;
define method make
(class == <string-stream>, #rest keys, #key contents, #all-keys)
=> (inst :: <sequence-stream>)
select (contents by instance?)
<byte-string> => apply(make, <byte-string-stream>, keys);
otherwise => next-method();
end select
end method;
define method initialize
(stream :: <sequence-stream>, #key
direction :: <symbol>, contents :: <sequence>,
start: start-pos :: <integer> = 0,
end: end-pos :: <integer> = contents.size)
=> ()
next-method();
stream.stream-storage :=
if (direction = #"input")
if (instance?(contents, <vector>) &
~instance?(contents, <stretchy-vector>))
contents
else
let (storage, new-start-pos, new-end-pos)
= make-storage(stream, <vector>, contents, start-pos, end-pos);
start-pos := new-start-pos;
end-pos := new-end-pos;
storage
end if;
else
check-fill-type(stream);
let (storage, new-start-pos, new-end-pos)
= make-storage(stream, <stretchy-vector>, contents, start-pos, end-pos);
start-pos := new-start-pos;
end-pos := new-end-pos;
storage
end if;
stream.stream-start := start-pos;
stream.stream-end := end-pos;
end method;
define sealed domain make (singleton(<byte-string-stream>));
define sealed domain initialize (<byte-string-stream>);
//
// Client methods
//
define method ext-stream-open? (stream :: <sequence-stream>)
=> (open? :: <boolean>)
stream.stream-open?
end method;
define method ext-close (stream :: <sequence-stream>, #key, #all-keys) => ()
stream.stream-open? := #f;
stream.stream-storage := #[];
end method;
define method ext-stream-at-end? (stream :: <sequence-stream>)
=> (at-end? :: <boolean>)
check-stream-open(stream);
stream.stream-at-end?
end method;
define method ext-stream-contents
(stream :: <sequence-stream>, #key clear-contents? :: <boolean> = #t)
=> (contents :: <sequence>)
check-stream-open(stream);
let copy = copy-sequence(stream.stream-storage,
start: stream.stream-start,
end: stream.stream-end);
when (clear-contents? &
stream.stream-direction ~= #"input")
reset-stream(stream);
end when;
copy
end method;
define method ext-stream-contents-as
(type :: subclass(<sequence>), stream :: <sequence-stream>,
#key clear-contents? :: <boolean> = #t)
=> (contents :: <sequence>)
as(type, ext-stream-contents(stream.outer-stream, clear-contents?: clear-contents?))
end method;
define method ext-stream-element-type (stream :: <sequence-stream>)
=> (type :: <type>)
check-stream-open(stream);
stream.stream-element-type
end method;
//
// Client position methods
//
define method ext-stream-size (stream :: <sequence-stream>)
=> (size :: <integer>)
check-stream-open(stream);
stream.stream-size + stream.stream-position-offset
end method;
define inline method ext-stream-limit (stream :: <sequence-stream>)
=> (limit :: <integer>)
stream.outer-stream.ext-stream-size
end method;
define method ext-adjust-stream-position
(stream :: <sequence-stream>, delta :: <integer>,
#key from :: one-of(#"current", #"start", #"end") = #"current")
=> (new-position :: <integer>)
check-stream-open(stream);
adjust-stream-position(stream, delta, from: from) + stream.stream-position-offset
end method;
define method ext-stream-position (stream :: <sequence-stream>)
=> (position :: <integer>)
check-stream-open(stream);
stream.stream-position + stream.stream-position-offset
end method;
define method ext-stream-position-setter
(position :: <integer>, stream :: <sequence-stream>)
=> (position :: <integer>)
check-stream-open(stream);
let position = position - stream.stream-position-offset;
if (position < 0 | position > stream.stream-size)
position-range-error(stream);
elseif (position = stream.stream-size)
adjust-stream-position(stream, 0, from: #"end", grow: #f);
else
adjust-stream-position(stream, position, from: #"start", grow: #f);
end if;
end method;
define method ext-stream-position-setter
(position == #"start", stream :: <sequence-stream>)
=> (position :: <integer>)
check-stream-open(stream);
adjust-stream-position(stream, 0, from: #"start", grow: #f);
end method;
define method ext-stream-position-setter
(position == #"end", stream :: <sequence-stream>)
=> (position :: <integer>)
check-stream-open(stream);
adjust-stream-position(stream, 0, from: #"end", grow: #f);
end method;
//
// Internal methods
//
define method adjust-stream-position
(stream :: <sequence-stream>, delta :: <integer>,
#key from :: one-of(#"current", #"start", #"end") = #"current",
grow: grow? :: <boolean> = #t)
=> (new-position :: <integer>)
// Compute position.
let base-pos :: <integer> =
select (from)
#"current" => stream.stream-position;
#"start" => 0;
#"end" => stream.stream-size;
end select;
let new-pos = base-pos + delta;
// Grow stream if necessary.
if (grow? & new-pos >= stream.stream-size)
if (stream.stream-direction = #"input")
cannot-grow-error(stream);
else
let grow-data = make(<vector>, size: new-pos - stream.stream-size + 1,
fill: stream.stream-fill);
replace-stream-elements(stream, grow-data);
end if;
end if;
// Set position and return.
stream.stream-unread-from := #f;
stream.stream-position := max(0, new-pos);
end method;
define inline method stream-size (stream :: <sequence-stream>)
=> (size :: <integer>)
stream.stream-end - stream.stream-start
end method;
define inline method stream-at-end? (stream :: <sequence-stream>)
=> (at-end? :: <boolean>)
stream.stream-position >= stream.stream-size
end method;
define method peek
(stream :: <sequence-stream>, #key on-end-of-stream = unsupplied())
=> (elem-or-eof :: <object>)
case
~stream.stream-at-end? =>
stream.stream-storage[stream.stream-position + stream.stream-start];
on-end-of-stream.supplied? =>
on-end-of-stream;
otherwise =>
eos-error(stream);
end case
end method;
define method read-element
(stream :: <sequence-stream>, #key on-end-of-stream = unsupplied())
=> (elem-or-eof :: <object>)
case
~stream.stream-at-end? =>
let pos = stream.stream-position;
let elem = stream.stream-storage[pos + stream.stream-start];
stream.stream-unread-from := adjust-stream-position(stream, +1, grow: #f);
elem;
on-end-of-stream.supplied? =>
on-end-of-stream;
otherwise =>
eos-error(stream);
end case
end method;
define method read-through
(stream :: <sequence-stream>, to-elem :: <object>,
#key on-end-of-stream = unsupplied(), test :: <function> = \==,
keep-term :: <boolean> = #t)
=> (elements-or-eof :: <object>, found? :: <boolean>)
if (stream.stream-at-end?)
case
on-end-of-stream.supplied? => values(on-end-of-stream, #f);
otherwise => eos-error(stream);
end case
else
let start-idx = stream.stream-position + stream.stream-start;
let found-idx = #f;
let end-idx =
for (idx from start-idx below stream.stream-end, until: found-idx)
if (test(stream.stream-storage[idx], to-elem))
found-idx := idx
end if
finally
idx
end for;
// Compute new stream position.
if (end-idx > start-idx)
stream.stream-position := end-idx - stream.stream-start;
stream.stream-unread-from := stream.stream-position;
end if;
// Copy and return read elements.
if (found-idx & ~keep-term)
end-idx := max(start-idx, end-idx - 1)
end if;
values(copy-sequence(stream.stream-storage, start: start-idx, end: end-idx),
found-idx.true?)
end if;
end method;
define method read-stream-elements
(stream :: <sequence-stream>,
#key start: start-idx :: <integer>, end: end-idx :: <integer>)
=> (elems :: <sequence>)
stream.stream-position := end-idx - stream.stream-start;
if (end-idx > start-idx)
stream.stream-unread-from := stream.stream-position;
end if;
copy-sequence(stream.stream-storage, start: start-idx, end: end-idx)
end method;
define method skip-through
(stream :: <sequence-stream>, to-elem :: <object>, #key test :: <function> = \==)
=> (found-idx :: false-or(<integer>), next-idx :: <integer>)
let start-idx = stream.stream-position + stream.stream-start;
let found-idx = #f;
let end-idx =
for (idx from start-idx below stream.stream-end, until: found-idx)
if (test(stream.stream-storage[idx], to-elem))
found-idx := idx
end if
finally
idx
end for;
if (end-idx > start-idx)
stream.stream-position := end-idx - stream.stream-start;
stream.stream-unread-from := stream.stream-position;
end if;
values(found-idx, end-idx)
end method;
define method replace-stream-elements
(stream :: <sequence-stream>, insert-seq :: <sequence>,
#key start: start-idx :: <integer> = stream.stream-end,
end: end-idx :: <integer> = stream.stream-end)
=> ()
let start-idx = min(start-idx, stream.stream-end);
let end-idx = min(end-idx, stream.stream-end);
// Trim down size of sequence to make any later resizing faster.
when (stream.stream-end < stream.stream-storage.size)
stream.stream-storage :=
replace-subsequence!(stream.stream-storage, #[], start: stream.stream-end);
end when;
when (stream.stream-start > 0)
stream.stream-storage :=
replace-subsequence!(stream.stream-storage, #[], end: stream.stream-end);
let adjustment = stream.stream-start;
stream.stream-position := stream.stream-position - adjustment;
stream.stream-end := stream.stream-end - adjustment;
start-idx := start-idx - adjustment;
end-idx := end-idx - adjustment;
end when;
// Replace and resize if necessary.
stream.stream-storage :=
replace-subsequence!(stream.stream-storage, insert-seq,
start: start-idx, end: end-idx);
stream.stream-end := stream.stream-storage.size;
end method;
define method make-storage
(stream :: <sequence-stream>, class :: <class>, original :: <sequence>,
start-pos :: <integer>, end-pos :: <integer>)
=> (storage :: <vector>, new-start-pos :: <integer>, new-end-pos :: <integer>)
// Set up storage of a usable class, for the part of the original sequence
// that we want to work with.
let storage = make(class, size: end-pos - start-pos, fill: stream.stream-fill);
let (iter-start, iter-limit, iter-next, iter-done?, iter-key, iter-elem) =
forward-iteration-protocol(original);
// Skip to what we want from original sequence.
let iter-start-wanted =
for (skip-count from 0 below start-pos,
state = iter-start then iter-next(original, state),
until: iter-done?(original, state, iter-limit))
finally
state
end for;
// Copy those elements into storage.
for (storage-index from 0 below storage.size,
state = iter-start-wanted then iter-next(original, state),
until: iter-done?(original, state, iter-limit))
storage[storage-index] := iter-elem(original, state)
end for;
values(storage, 0, storage.size)
end method;
define method reset-stream (stream :: <sequence-stream>) => ()
replace-stream-elements(stream, #[], start: 0, end: stream.stream-size);
adjust-stream-position(stream, 0, from: #"start", grow: #f);
end method;
//
// Checks and errors
//
define method check-stream-open (stream :: <sequence-stream>) => ()
unless (stream.stream-open?)
error(make(<stream-closed-error>, stream: stream))
end unless;
end method;
define method check-stream-readable (stream :: <sequence-stream>) => ()
unless (stream.stream-direction = #"input" |
stream.stream-direction = #"input-output")
error(make(<stream-not-readable>, stream: stream))
end unless;
end method;
define method check-stream-writable (stream :: <sequence-stream>) => ()
unless (stream.stream-direction = #"output" |
stream.stream-direction = #"input-output")
error(make(<stream-not-writable>, stream: stream))
end unless;
end method;
define method check-element-type (stream :: <sequence-stream>, elem :: <object>)
=> ()
unless (instance?(elem, stream.stream-element-type))
error("Stream does not accept element type %=", elem.object-class)
end unless;
end method;
define method check-fill-type (stream :: <sequence-stream>)
=> ()
unless (instance?(stream.stream-fill, stream.stream-element-type))
error("Stream filler %= is not correct element type", stream.stream-fill)
end unless;
end method;
define inline function eos-error (stream :: <sequence-stream>) => ()
error(make(<end-of-stream-error>, stream: stream))
end function;
define inline function incomplete-error
(stream :: <sequence-stream>, partial :: <sequence>)
=> ()
error(make(<incomplete-read-error>, stream: stream, count: partial.size,
sequence: partial));
end function;
define inline function cannot-grow-error (stream :: <sequence-stream>) => ()
error("Cannot grow input stream");
end function;
define inline function position-range-error (stream :: <sequence-stream>) => ()
error("Stream position past end of stream");
end function;
define inline function cannot-unread-error (stream :: <sequence-stream>) => ()
error("Cannot unread element")
end function;