-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTextIOFragment.drv
430 lines (379 loc) · 17 KB
/
TextIOFragment.drv
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
/*
* Copyright (C) 2004-2024 Sebastiano Vigna
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/** Loads elements from a given fast buffered reader, storing them in a given array fragment.
*
* @param reader a buffered reader.
* @param array an array which will be filled with data from {@code reader}.
* @param offset the index of the first element of {@code array} to be filled.
* @param length the number of elements of {@code array} to be filled.
* @return the number of elements actually read from {@code reader} (it might be less than {@code length} if {@code reader} ends).
*/
public static int LOAD_KEYS(final BufferedReader reader, final KEY_TYPE[] array, final int offset, final int length) throws IOException {
PACKAGE.ARRAYS.ensureOffsetLength(array, offset, length);
int i = 0;
String s;
try {
for(i = 0; i < length; i++)
if ((s = reader.readLine()) != null) array[i + offset] = KEY_CLASS.PARSE_KEY(s.trim());
else break;
}
catch(EOFException itsOk) {}
return i;
}
/** Loads elements from a given buffered reader, storing them in a given array.
*
* @param reader a buffered reader.
* @param array an array which will be filled with data from {@code reader}.
* @return the number of elements actually read from {@code reader} (it might be less than the array length if {@code reader} ends).
*/
public static int LOAD_KEYS(final BufferedReader reader, final KEY_TYPE[] array) throws IOException {
return LOAD_KEYS(reader, array, 0, array.length);
}
/** Loads elements from a file given by a {@link File} object, storing them in a given array fragment.
*
* @param file a file.
* @param array an array which will be filled with data from the specified file.
* @param offset the index of the first element of {@code array} to be filled.
* @param length the number of elements of {@code array} to be filled.
* @return the number of elements actually read from the given file (it might be less than {@code length} if the file is too short).
*/
public static int LOAD_KEYS(final File file, final KEY_TYPE[] array, final int offset, final int length) throws IOException {
final BufferedReader reader = new BufferedReader(new FileReader(file));
final int result = LOAD_KEYS(reader, array, offset, length);
reader.close();
return result;
}
/** Loads elements from a file given by a filename, storing them in a given array fragment.
*
* @param filename a filename.
* @param array an array which will be filled with data from the specified file.
* @param offset the index of the first element of {@code array} to be filled.
* @param length the number of elements of {@code array} to be filled.
* @return the number of elements actually read from the given file (it might be less than {@code length} if the file is too short).
*/
public static int LOAD_KEYS(final CharSequence filename, final KEY_TYPE[] array, final int offset, final int length) throws IOException {
return LOAD_KEYS(new File(filename.toString()), array, offset, length);
}
/** Loads elements from a file given by a {@link File} object, storing them in a given array.
*
* @param file a file.
* @param array an array which will be filled with data from the specified file.
* @return the number of elements actually read from the given file (it might be less than the array length if the file is too short).
*/
public static int LOAD_KEYS(final File file, final KEY_TYPE[] array) throws IOException {
return LOAD_KEYS(file, array, 0, array.length);
}
/** Loads elements from a file given by a filename, storing them in a given array.
*
* @param filename a filename.
* @param array an array which will be filled with data from the specified file.
* @return the number of elements actually read from the given file (it might be less than the array length if the file is too short).
*/
public static int LOAD_KEYS(final CharSequence filename, final KEY_TYPE[] array) throws IOException {
return LOAD_KEYS(filename, array, 0, array.length);
}
/** Stores an array fragment to a given print stream.
*
* @param array an array whose elements will be written to {@code stream}.
* @param offset the index of the first element of {@code array} to be written.
* @param length the number of elements of {@code array} to be written.
* @param stream a print stream.
*/
public static void STORE_KEYS(final KEY_TYPE array[], final int offset, final int length, final PrintStream stream) {
PACKAGE.ARRAYS.ensureOffsetLength(array, offset, length);
for(int i = 0; i < length; i++) stream.println(array[offset + i]);
}
/** Stores an array to a given print stream.
*
* @param array an array whose elements will be written to {@code stream}.
* @param stream a print stream.
*/
public static void STORE_KEYS(final KEY_TYPE array[], final PrintStream stream) {
STORE_KEYS(array, 0, array.length, stream);
}
/** Stores an array fragment to a file given by a {@link File} object.
*
* @param array an array whose elements will be written to {@code filename}.
* @param offset the index of the first element of {@code array} to be written.
* @param length the number of elements of {@code array} to be written.
* @param file a file.
*/
public static void STORE_KEYS(final KEY_TYPE array[], final int offset, final int length, final File file) throws IOException {
final PrintStream stream = new PrintStream(new FastBufferedOutputStream(new FileOutputStream(file)));
STORE_KEYS(array, offset, length, stream);
stream.close();
}
/** Stores an array fragment to a file given by a pathname.
*
* @param array an array whose elements will be written to {@code filename}.
* @param offset the index of the first element of {@code array} to be written.
* @param length the number of elements of {@code array} to be written.
* @param filename a filename.
*/
public static void STORE_KEYS(final KEY_TYPE array[], final int offset, final int length, final CharSequence filename) throws IOException {
STORE_KEYS(array, offset, length, new File(filename.toString()));
}
/** Stores an array to a file given by a {@link File} object.
*
* @param array an array whose elements will be written to {@code filename}.
* @param file a file.
*/
public static void STORE_KEYS(final KEY_TYPE array[], final File file) throws IOException {
STORE_KEYS(array, 0, array.length, file);
}
/** Stores an array to a file given by a pathname.
*
* @param array an array whose elements will be written to {@code filename}.
* @param filename a filename.
*/
public static void STORE_KEYS(final KEY_TYPE array[], final CharSequence filename) throws IOException {
STORE_KEYS(array, 0, array.length, filename);
}
/** Stores the element returned by an iterator to a given print stream.
*
* @param i an iterator whose output will be written to {@code stream}.
* @param stream a print stream.
*/
public static void STORE_KEYS(final KEY_ITERATOR i, final PrintStream stream) {
while(i.hasNext()) stream.println(i.NEXT_KEY());
}
/** Stores the element returned by an iterator to a file given by a {@link File} object.
*
* @param i an iterator whose output will be written to {@code filename}.
* @param file a file.
*/
public static void STORE_KEYS(final KEY_ITERATOR i, final File file) throws IOException {
final PrintStream stream = new PrintStream(new FastBufferedOutputStream(new FileOutputStream(file)));
STORE_KEYS(i, stream);
stream.close();
}
/** Stores the element returned by an iterator to a file given by a pathname.
*
* @param i an iterator whose output will be written to {@code filename}.
* @param filename a filename.
*/
public static void STORE_KEYS(final KEY_ITERATOR i, final CharSequence filename) throws IOException {
STORE_KEYS(i, new File(filename.toString()));
}
/** Loads elements from a given fast buffered reader, storing them in a given big-array fragment.
*
* @param reader a buffered reader.
* @param array a big array which will be filled with data from {@code reader}.
* @param offset the index of the first element of {@code array} to be filled.
* @param length the number of elements of {@code array} to be filled.
* @return the number of elements actually read from {@code reader} (it might be less than {@code length} if {@code reader} ends).
*/
public static long LOAD_KEYS(final BufferedReader reader, final KEY_TYPE[][] array, final long offset, final long length) throws IOException {
ensureOffsetLength(array, offset, length);
long c = 0;
String s;
try {
for(int i = segment(offset); i < segment(offset + length + SEGMENT_MASK); i++) {
final KEY_TYPE[] t = array[i];
final int l = (int)Math.min(t.length, offset + length - start(i));
for(int d = (int)Math.max(0, offset - start(i)); d < l; d++) {
if ((s = reader.readLine()) != null) t[d] = KEY_CLASS.PARSE_KEY(s.trim());
else return c;
c++;
}
}
}
catch(EOFException itsOk) {}
return c;
}
/** Loads elements from a given buffered reader, storing them in a given array.
*
* @param reader a buffered reader.
* @param array a big array which will be filled with data from {@code reader}.
* @return the number of elements actually read from {@code reader} (it might be less than the array length if {@code reader} ends).
*/
public static long LOAD_KEYS(final BufferedReader reader, final KEY_TYPE[][] array) throws IOException {
return LOAD_KEYS(reader, array, 0, length(array));
}
/** Loads elements from a file given by a {@link File} object, storing them in a given big-array fragment.
*
* @param file a file.
* @param array a big array which will be filled with data from the specified file.
* @param offset the index of the first element of {@code array} to be filled.
* @param length the number of elements of {@code array} to be filled.
* @return the number of elements actually read from the given file (it might be less than {@code length} if the file is too short).
*/
public static long LOAD_KEYS(final File file, final KEY_TYPE[][] array, final long offset, final long length) throws IOException {
final BufferedReader reader = new BufferedReader(new FileReader(file));
final long result = LOAD_KEYS(reader, array, offset, length);
reader.close();
return result;
}
/** Loads elements from a file given by a filename, storing them in a given big-array fragment.
*
* @param filename a filename.
* @param array a big array which will be filled with data from the specified file.
* @param offset the index of the first element of {@code array} to be filled.
* @param length the number of elements of {@code array} to be filled.
* @return the number of elements actually read from the given file (it might be less than {@code length} if the file is too short).
*/
public static long LOAD_KEYS(final CharSequence filename, final KEY_TYPE[][] array, final long offset, final long length) throws IOException {
return LOAD_KEYS(new File(filename.toString()), array, offset, length);
}
/** Loads elements from a file given by a {@link File} object, storing them in a given array.
*
* @param file a file.
* @param array a big array which will be filled with data from the specified file.
* @return the number of elements actually read from the given file (it might be less than the array length if the file is too short).
*/
public static long LOAD_KEYS(final File file, final KEY_TYPE[][] array) throws IOException {
return LOAD_KEYS(file, array, 0, length(array));
}
/** Loads elements from a file given by a filename, storing them in a given array.
*
* @param filename a filename.
* @param array a big array which will be filled with data from the specified file.
* @return the number of elements actually read from the given file (it might be less than the array length if the file is too short).
*/
public static long LOAD_KEYS(final CharSequence filename, final KEY_TYPE[][] array) throws IOException {
return LOAD_KEYS(filename, array, 0, length(array));
}
/** Stores a big-array fragment to a given print stream.
*
* @param array a big array whose elements will be written to {@code stream}.
* @param offset the index of the first element of {@code array} to be written.
* @param length the number of elements of {@code array} to be written.
* @param stream a print stream.
*/
public static void STORE_KEYS(final KEY_TYPE array[][], final long offset, final long length, final PrintStream stream) {
ensureOffsetLength(array, offset, length);
for(int i = segment(offset); i < segment(offset + length + SEGMENT_MASK); i++) {
final KEY_TYPE[] t = array[i];
final int l = (int)Math.min(t.length, offset + length - start(i));
for(int d = (int)Math.max(0, offset - start(i)); d < l; d++) stream.println(t[d]);
}
}
/** Stores a big array to a given print stream.
*
* @param array a big array whose elements will be written to {@code stream}.
* @param stream a print stream.
*/
public static void STORE_KEYS(final KEY_TYPE array[][], final PrintStream stream) {
STORE_KEYS(array, 0, length(array), stream);
}
/** Stores a big-array fragment to a file given by a {@link File} object.
*
* @param array a big array whose elements will be written to {@code filename}.
* @param offset the index of the first element of {@code array} to be written.
* @param length the number of elements of {@code array} to be written.
* @param file a file.
*/
public static void STORE_KEYS(final KEY_TYPE array[][], final long offset, final long length, final File file) throws IOException {
final PrintStream stream = new PrintStream(new FastBufferedOutputStream(new FileOutputStream(file)));
STORE_KEYS(array, offset, length, stream);
stream.close();
}
/** Stores a big-array fragment to a file given by a pathname.
*
* @param array a big array whose elements will be written to {@code filename}.
* @param offset the index of the first element of {@code array} to be written.
* @param length the number of elements of {@code array} to be written.
* @param filename a filename.
*/
public static void STORE_KEYS(final KEY_TYPE array[][], final long offset, final long length, final CharSequence filename) throws IOException {
STORE_KEYS(array, offset, length, new File(filename.toString()));
}
/** Stores a big array to a file given by a {@link File} object.
*
* @param array a big array whose elements will be written to {@code filename}.
* @param file a file.
*/
public static void STORE_KEYS(final KEY_TYPE array[][], final File file) throws IOException {
STORE_KEYS(array, 0, length(array), file);
}
/** Stores a big array to a file given by a pathname.
*
* @param array a big array whose elements will be written to {@code filename}.
* @param filename a filename.
*/
public static void STORE_KEYS(final KEY_TYPE array[][], final CharSequence filename) throws IOException {
STORE_KEYS(array, 0, length(array), filename);
}
/** A wrapper that exhibits the content of a reader as a type-specific iterator. */
private static final class KEY_READER_WRAPPER implements KEY_ITERATOR {
private final BufferedReader reader;
private boolean toAdvance = true;
private String s;
private KEY_TYPE next;
public KEY_READER_WRAPPER(final BufferedReader reader) {
this.reader = reader;
}
@Override
public boolean hasNext() {
if (! toAdvance) return s != null;
toAdvance = false;
try {
s = reader.readLine();
}
catch(EOFException itsOk) {}
catch(IOException rethrow) { throw new RuntimeException(rethrow); }
if (s == null) return false;
next = KEY_CLASS.PARSE_KEY(s.trim());
return true;
}
@Override
public KEY_TYPE NEXT_KEY() {
if (! hasNext()) throw new NoSuchElementException();
toAdvance = true;
return next;
}
}
/** Wraps the given buffered reader into an iterator.
*
* @param reader a buffered reader.
*/
public static KEY_ITERATOR AS_KEY_ITERATOR(final BufferedReader reader) {
return new KEY_READER_WRAPPER(reader);
}
/** Wraps a file given by a {@link File} object into an iterator.
*
* @param file a file.
*/
public static KEY_ITERATOR AS_KEY_ITERATOR(final File file) throws IOException {
return new KEY_READER_WRAPPER(new BufferedReader(new FileReader(file)));
}
/** Wraps a file given by a pathname into an iterator.
*
* @param filename a filename.
*/
public static KEY_ITERATOR AS_KEY_ITERATOR(final CharSequence filename) throws IOException {
return AS_KEY_ITERATOR(new File(filename.toString()));
}
/** Wraps a file given by a {@link File} object into an iterable object.
*
* @param file a file.
*/
public static KEY_ITERABLE AS_KEY_ITERABLE(final File file) {
return () -> {
try { return AS_KEY_ITERATOR(file); }
catch(IOException e) { throw new RuntimeException(e); }
};
}
/** Wraps a file given by a pathname into an iterable object.
*
* @param filename a filename.
*/
public static KEY_ITERABLE AS_KEY_ITERABLE(final CharSequence filename) {
return () -> {
try { return AS_KEY_ITERATOR(filename); }
catch(IOException e) { throw new RuntimeException(e); }
};
}