joinAppend

joinAppend performs a join operation on an input range, appending the results to an output range.

Note: The main uses of joinAppend have been replaced by BufferedOutputRange, which has its own joinAppend method.

joinAppend was written as a performance enhancement over using std.algorithm.joiner or std.array.join with writeln. Using joiner with writeln is quite slow, 3-4x slower than std.array.join with writeln. The joiner performance may be due to interaction with writeln, this was not investigated. Using joiner with stdout.lockingTextWriter is better, but still substantially slower than join. Using join works reasonably well, but is allocating memory unnecessarily.

Using joinAppend with Appender is a bit faster than join, and allocates less memory. The Appender re-uses the underlying data buffer, saving memory. The example below illustrates. It is a modification of the InputFieldReordering example. The role Appender plus joinAppend are playing is to buffer the output. BufferedOutputRange uses a similar technique to buffer multiple lines.

1 int main(string[] args)
2 {
3     import tsvutil;
4     import std.algorithm, std.array, std.range, std.stdio;
5     size_t[] fieldIndicies = [3, 0, 2];
6     auto fieldReordering = new InputFieldReordering!char(fieldIndicies);
7     auto outputBuffer = appender!(char[]);
8     foreach (line; stdin.byLine)
9     {
10         fieldReordering.initNewLine;
11         foreach(fieldIndex, fieldValue; line.splitter('\t').enumerate)
12         {
13             fieldReordering.processNextField(fieldIndex, fieldValue);
14             if (fieldReordering.allFieldsFilled) break;
15         }
16         if (fieldReordering.allFieldsFilled)
17         {
18             outputBuffer.clear;
19             writeln(fieldReordering.outputFields.joinAppend(outputBuffer, ('\t')));
20         }
21         else
22         {
23             writeln("Error: Insufficient number of field on the line.");
24         }
25     }
26     return 0;
27 }
OutputRange
joinAppend
(
InputRange
OutputRange
E
)
(
InputRange inputRange
,
ref OutputRange outputRange
,
)
if (
isInputRange!InputRange &&
(
is(ElementType!InputRange : const E[]) &&
isOutputRange!(OutputRange, E[])
)
||
(
is(ElementType!InputRange : const E) &&
isOutputRange!(OutputRange, E)
)
)

Meta