Stream merge: Difference between revisions

15,661 bytes added ,  2 months ago
Added FreeBASIC
m (→‎{{header|Sidef}}: Fix link: Perl 6 --> Raku)
(Added FreeBASIC)
 
(11 intermediate revisions by 7 users not shown)
Line 14:
=={{header|360 Assembly}}==
No usage of tricks such as forbiden records in the streams.
<langsyntaxhighlight lang="360asm">* Stream Merge 07/02/2017
STRMERGE CSECT
USING STRMERGE,R13 base register
Line 130:
PG DS CL64
YREGS
END STRMERGE</langsyntaxhighlight>
{{in}}
<pre style="height:20ex">
Line 165:
Line 055
</pre>
 
=={{header|Ada}}==
<syntaxhighlight lang="ada">with Ada.Text_Io;
with Ada.Command_Line;
with Ada.Containers.Indefinite_Holders;
 
procedure Stream_Merge is
 
package String_Holders
is new Ada.Containers.Indefinite_Holders (String);
 
use Ada.Text_Io, String_Holders;
 
type Stream_Type is
record
File : File_Type;
Value : Holder;
end record;
 
subtype Index_Type is Positive range 1 .. Ada.Command_Line.Argument_Count;
Streams : array (Index_Type) of Stream_Type;
 
procedure Fetch (Stream : in out Stream_Type) is
begin
Stream.Value := (if End_Of_File (Stream.File)
then Empty_Holder
else To_Holder (Get_Line (Stream.File)));
end Fetch;
 
function Next_Stream return Index_Type is
Index : Index_Type := Index_Type'First;
Value : Holder;
begin
for I in Streams'Range loop
if Value.Is_Empty and not Streams (I).Value.Is_Empty then
Value := Streams (I).Value;
Index := I;
elsif not Streams (I).Value.Is_Empty and then Streams (I).Value.Element < Value.Element then
Value := Streams (I).Value;
Index := I;
end if;
end loop;
if Value.Is_Empty then
raise Program_Error;
end if;
return Index;
end Next_Stream;
 
function More_Data return Boolean
is (for some Stream of Streams => not Stream.Value.Is_Empty);
 
begin
 
if Ada.Command_Line.Argument_Count = 0 then
Put_Line ("Usage: prog <file1> <file2> ...");
Put_Line ("Merge the sorted files file1, file2...");
return;
end if;
 
for I in Streams'Range loop
Open (Streams (I).File, In_File, Ada.Command_Line.Argument (I));
Fetch (Streams (I));
end loop;
 
while More_Data loop
declare
Stream : Stream_Type renames Streams (Next_Stream);
begin
Put_Line (Stream.Value.Element);
Fetch (Stream);
end;
end loop;
 
end Stream_Merge;</syntaxhighlight>
 
=={{header|ALGOL 68}}==
NB, all the files (including the output files) must exist before running this. The output files are overwritten with the merged records.
<langsyntaxhighlight lang="algol68"># merge a number of input files to an output file #
PROC mergenf = ( []REF FILE inf, REF FILE out )VOID:
BEGIN
Line 270 ⟶ 344:
# test the file merge #
merge2( "in1.txt", "in2.txt", "out2.txt" );
mergen( ( "in1.txt", "in2.txt", "in3.txt", "in4.txt" ), "outn.txt" )</langsyntaxhighlight>
{{out}}
<pre>
Line 276 ⟶ 350:
 
=={{header|ATS}}==
<syntaxhighlight lang="ats">
<lang ATS>
(* ****** ****** *)
//
Line 465 ⟶ 539:
//
} (* end of [main0] *)
</syntaxhighlight>
</lang>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f STREAM_MERGE.AWK filename(s) >output
# handles 1 .. N files
Line 534 ⟶ 608:
errors++
}
</syntaxhighlight>
</lang>
 
=={{header|C}}==
<syntaxhighlight lang="c">/*
<lang C>/*
* Rosetta Code - stream merge in C.
*
Line 580 ⟶ 654:
return EXIT_SUCCESS;
}
</syntaxhighlight>
</lang>
 
=={{header|C sharp|C#}}==
<syntaxhighlight lang="csharp">
{{trans|Java}}
<lang csharp>using System;
using System.Collections.Generic;
using System.Linq;
 
namespace StreamMerge {RosettaCode
{
class Program {
static class StreamMerge
static void Merge2<T>(IEnumerable<T> i1, IEnumerable<T> i2, Action<T> output) where T : IComparable {
{
IEnumerator<T> e1 = i1.GetEnumerator();
static IEnumerable<T> Merge2<T>(IEnumerable<T> source1, IEnumerable<T> source2) where T : IComparable
IEnumerator<T> e2 = i2.GetEnumerator();
{
 
boolvar hasAq1 = e1.MoveNextnew Queue<T>(source1);
boolvar hasBq2 = e2.MoveNextnew Queue<T>(source2);
while (hasAq1.Any() ||&& hasBq2.Any()) {
if (hasA) {
var c = if q1.Peek(hasB) {.CompareTo(q2.Peek());
if (c <= 0) yield return q1.Dequeue(); else IComparableyield a =return e1q2.CurrentDequeue();
IComparable b = e2.Current;
if (a.CompareTo(b) < 0) {
output.Invoke(e1.Current);
hasA = e1.MoveNext();
}
else {
output.Invoke(e2.Current);
hasB = e2.MoveNext();
}
}
else {
output.Invoke(e1.Current);
hasA = e1.MoveNext();
}
}
else if (hasB) {
output.Invoke(e2.Current);
hasB = e2.MoveNext();
}
}
while (q1.Any()) yield return q1.Dequeue();
while (q2.Any()) yield return q2.Dequeue();
}
 
static voidIEnumerable<T> MergeN<T>(Action<T> output, params IEnumerable<T>[] enumerablessources) where T : IComparable {
if (enumerables.Length == 0) {
var queues = sources.Select(e => new Queue<T>(e)).Where(q => q.Any()).ToList();
return;
var headComparer = Comparer<Queue<T>>.Create((x, y) => x.Peek().CompareTo(y.Peek()));
}
if (enumerablesqueues.Length == 1Sort(headComparer) {;
IEnumerator<T> e = enumerables[0].GetEnumerator();
while (equeues.MoveNextAny()) {
output.Invoke(e.Current);{
var q = queues.First();
queues.RemoveAt(0);
yield return q.Dequeue();
if (q.Any())
{
var index = queues.BinarySearch(q, headComparer);
queues.Insert(index < 0 ? ~index : index, q);
}
return;
}
 
int count = enumerables.Length;
IEnumerator<T>[] eArr = new IEnumerator<T>[count];
bool[] hasN = new bool[count];
for (int i = 0; i < count; i++) {
eArr[i] = enumerables[i].GetEnumerator();
hasN[i] = eArr[i].MoveNext();
}
 
while (hasN.Aggregate(false, (a, b) => a || b)) {
int index = -1;
T value = default(T);
for (int i = 0; i < count; i++) {
if (hasN[i]) {
if (index == -1) {
value = eArr[i].Current;
index = i;
}
else if (eArr[i].Current.CompareTo(value) < 0) {
value = eArr[i].Current;
index = i;
}
}
}
 
output.Invoke(value);
hasN[index] = eArr[index].MoveNext();
}
}
 
static void Main(string[] args) {
{
List<int> a = new List<int>() { 1, 4, 7, 10 };
List<int>var ba = new List<int>()[] { 21, 54, 87, 1110 };
List<int>var cb = new List<int>()[] { 32, 65, 98, 1211 };
var c = new[] { 3, 6, 9, 12 };
 
foreach (var i in Merge2(a, b, m =>)) Console.Write($"{0i} ", m));
Console.WriteLine();
 
MergeN(m => Console.Write("{0} ", m), a, b, c);
foreach (var i in MergeN(a, b, c)) Console.Write($"{i} ");
Console.WriteLine();
}
}
}</langsyntaxhighlight>
{{out}}
<pre>1 2 4 5 7 8 10 11
Line 681 ⟶ 718:
=={{header|C++}}==
{{trans|C#}}
<langsyntaxhighlight lang="cpp">//#include <functional>
#include <iostream>
#include <vector>
Line 776 ⟶ 813:
mergeN(display, { v3, v2, v1 });
std::cout << '\n';
}</langsyntaxhighlight>
{{out}}
<pre>0 1 3 4 6 7
Line 783 ⟶ 820:
 
=={{header|D}}==
<langsyntaxhighlight Dlang="d">import std.range.primitives;
import std.stdio;
 
Line 855 ⟶ 892:
}
} while (!done);
}</langsyntaxhighlight>
 
{{out}}
Line 865 ⟶ 902:
 
=={{header|Elixir}}==
<langsyntaxhighlight lang="elixir">defmodule StreamMerge do
def merge2(file1, file2), do: mergeN([file1, file2])
Line 893 ⟶ 930:
StreamMerge.merge2("temp1.dat", "temp2.dat")
IO.puts "\nN-stream merge:"
StreamMerge.mergeN(filenames)</langsyntaxhighlight>
 
{{out}}
Line 943 ⟶ 980:
 
=={{header|Fortran}}==
This is a classic problem, but even so, Fortran does not supply a library routine for this. So...<langsyntaxhighlight Fortranlang="fortran"> SUBROUTINE FILEMERGE(N,INF,OUTF) !Merge multiple inputs into one output.
INTEGER N !The number of input files.
INTEGER INF(*) !Their unit numbers.
Line 1,010 ⟶ 1,047:
CALL FILEMERGE(MANY,FI,F) !E pluribus unum.
 
END !That was easy.</langsyntaxhighlight>
Obviously, there would be variations according to the nature of the data streams being merged, and whatever sort key was involved. For this example, input from disc files will do and the sort key is the entire record's text. This means there is no need to worry over the case where, having written a record from stream S and obtained the next record from stream S, it proves to have equal precedence with the waiting record for some other stream. Which now should take precedence? With entirely-equal records it obviously doesn't matter but if the sort key is only partial then different record content could be deemed equal and then a choice has an effect.
 
Line 1,020 ⟶ 1,057:
 
The source for subroutine GRAB is within subroutine FILEMERGE for the convenience in sharing and messing with variables important to both, but not to outsiders. This facility is standard in Algol-following languages but often omitted and was not added to Fortran until F90. In its absence, either more parameters are required for the separate routines, or there will be messing with COMMON storage areas.
 
=={{header|FreeBASIC}}==
{{trans|C++}}
<syntaxhighlight lang="vbnet">Sub Merge2(c1() As Integer, c2() As Integer)
Dim As Integer i1 = Lbound(c1)
Dim As Integer i2 = Lbound(c2)
While i1 <= Ubound(c1) And i2 <= Ubound(c2)
If c1(i1) <= c2(i2) Then
Print c1(i1);
i1 += 1
Else
Print c2(i2);
i2 += 1
End If
Wend
While i1 <= Ubound(c1)
Print c1(i1);
i1 += 1
Wend
While i2 <= Ubound(c2)
Print c2(i2);
i2 += 1
Wend
Print
End Sub
 
Sub MergeN(all() As Integer)
Dim As Integer i = Lbound(all)
While i <= Ubound(all)
Print all(i);
i += 1
Wend
Print
End Sub
 
Dim As Integer v1(2) = {0, 3, 6}
Dim As Integer v2(2) = {1, 4, 7}
Dim As Integer v3(2) = {2, 5, 8}
Merge2(v2(), v1())
MergeN(v1())
 
Dim As Integer all(8) = {v1(0), v2(0), v3(0), v1(1), v2(1), v3(1), v1(2), v2(2), v3(2)}
MergeN(all())
 
Sleep</syntaxhighlight>
{{out}}
<pre> 0 1 3 4 6 7
0 3 6
0 1 2 3 4 5 6 7 8</pre>
 
=={{header|Go}}==
'''Using standard library binary heap for mergeN:'''
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,117 ⟶ 1,206:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,124 ⟶ 1,213:
</pre>
'''MergeN using package from [[Fibonacci heap]] task:'''
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,183 ⟶ 1,272:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,189 ⟶ 1,278:
</pre>
 
== {{header|Haskell}} ==
 
There is no built-in iterator or stream type for file operations in Haskell. But several such libraries exist.
Line 1,195 ⟶ 1,284:
=== conduit ===
 
<langsyntaxhighlight lang="haskell">-- stack runhaskell --package=conduit-extra --package=conduit-merge
 
import Control.Monad.Trans.Resource (runResourceT)
Line 1,213 ⟶ 1,302:
runResourceT $ mergeSources inputs $$ sinkStdoutLn
where
sinkStdoutLn = Conduit.map (`BS.snoc` '\n') =$= sinkHandle stdout</langsyntaxhighlight>
 
See implementation in https://github.com/cblp/conduit-merge/blob/master/src/Data/Conduit/Merge.hs
Line 1,219 ⟶ 1,308:
=== pipes ===
 
<langsyntaxhighlight lang="haskell">-- stack runhaskell --package=pipes-safe --package=pipes-interleave
 
import Pipes (runEffect, (>->))
Line 1,233 ⟶ 1,322:
sourceFileNames <- getArgs
let sources = map readFile sourceFileNames
runSafeT . runEffect $ interleave compare sources >-> stdoutLn</langsyntaxhighlight>
 
See implementation in https://github.com/bgamari/pipes-interleave/blob/master/Pipes/Interleave.hs
 
=={{header|Java}}==
<langsyntaxhighlight Javalang="java">import java.util.Iterator;
import java.util.List;
import java.util.Objects;
Line 1,337 ⟶ 1,426:
System.out.flush();
}
}</langsyntaxhighlight>
{{out}}
<pre>1245781011
Line 1,345 ⟶ 1,434:
{{trans|C}}
The IOStream type in Julia encompasses any data stream, including file I/O and TCP/IP. The IOBuffer used here maps a stream to a buffer in memory, and so allows an easy simulation of two streams without opening files.
<syntaxhighlight lang="julia">
<lang Julia>
function merge(stream1, stream2, T=Char)
if !eof(stream1) && !eof(stream2)
Line 1,384 ⟶ 1,473:
println("\nDone.")
 
</langsyntaxhighlight>{{output}}<pre>
abcdefghijklmnopqrstuvwyxz
Done.
Line 1,391 ⟶ 1,480:
=={{header|Kotlin}}==
Uses the same data as the REXX entry. As Kotlin lacks a Heap class, when merging N files, we use a nullable MutableList instead. All comparisons are text based even when the files contain nothing but numbers.
<langsyntaxhighlight lang="scala">// version 1.2.21
 
import java.io.File
Line 1,450 ⟶ 1,539:
println(File("merged2.txt").readText())
println(File("mergedN.txt").readText())
}</langsyntaxhighlight>
 
{{out}}
Line 1,473 ⟶ 1,562:
8
</pre>
=={{header|Nim}}==
===Merge two streams===
Optimized for clarity and simplicity, not performance.
assumes two files containing sorted integers separated by newlines
<syntaxhighlight lang="nim">import streams,strutils
let
stream1 = newFileStream("file1")
stream2 = newFileStream("file2")
while not stream1.atEnd and not stream2.atEnd:
echo (if stream1.peekLine.parseInt > stream2.peekLine.parseInt: stream2.readLine else: stream1.readLine)
 
for line in stream1.lines:
echo line
for line in stream2.lines:
echo line</syntaxhighlight>
 
===Merge N streams===
{{trans|Phix}}
Of course, as Phix and Nim are very different languages, the code is quite different, but as Phix, we use a priority queue (which is provided by the standard module <code>heapqueue</code>. We work with files built from the “Data” constant, but we destroy them after usage. We have also put the whole merging code in an procedure.
 
<syntaxhighlight lang="nim">import heapqueue, os, sequtils, streams
 
type
Source = tuple[stream: Stream; line: string]
SourceHeap = HeapQueue[Source]
 
 
# Comparison of sources. Needed for the heap to sort the sources by line contents.
proc `<`(a, b: Source): bool = a.line < b.line
 
 
proc add(heap: var SourceHeap; stream: Stream) =
## Add a stream to the heap.
if stream.atEnd:
stream.close()
else:
heap.push((stream, stream.readLine()))
 
 
proc merge(inStreams: seq[Stream]; outStream: Stream) =
## Merge the input streams into an output stream.
 
# Initialize the heap.
var heap: SourceHeap
for stream in inStreams:
heap.add(stream)
 
# Merging loop.
while heap.len > 0:
let (stream, line) = heap.pop()
outStream.writeLine line
heap.add(stream)
 
 
when isMainModule:
 
const
Data = ["Line 001\nLine 008\nLine 017\n",
"Line 019\nLine 033\nLine 044\nLine 055\n",
"Line 019\nLine 029\nLine 039\n",
"Line 023\nLine 030\n"]
Filenames = ["file1.txt", "file2.txt", "file3.txt", "file4.txt"]
 
# Create files.
for i, name in Filenames:
writeFile(name, Data[i])
 
# Create input and output streams.
let inStreams = Filenames.mapIt(Stream(newFileStream(it)))
let outStream = Stream(newFileStream(stdout))
 
# Merge the streams.
merge(inStreams, outStream)
 
# Clean-up: delete the files.
for name in Filenames:
removeFile(name)</syntaxhighlight>
 
{{out}}
<pre>Line 001
Line 008
Line 017
Line 019
Line 019
Line 023
Line 029
Line 030
Line 033
Line 039
Line 044
Line 055</pre>
 
=={{header|Perl}}==
We make use of an iterator interface which String::Tokenizer provides. Credit: we obtained all the sample text from http://www.lipsum.com/.
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use English;
Line 1,601 ⟶ 1,781:
# At this point every iterator has been exhausted.
return;
}</langsyntaxhighlight>
{{out}}
<pre>Merge of 2 streams:
Line 1,611 ⟶ 1,791:
=={{header|Phix}}==
Using a priority queue
<!--<syntaxhighlight lang="phix">(notonline)-->
<lang Phix>include builtins/pqueue.e
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- file i/o</span>
 
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins</span><span style="color: #0000FF;">/</span><span style="color: #000000;">pqueue</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
procedure add(integer fn, pq)
object line = gets(fn)
<span style="color: #008080;">procedure</span> <span style="color: #000000;">add</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">fn</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">pq</span><span style="color: #0000FF;">)</span>
if line=-1 then
<span style="color: #004080;">object</span> <span style="color: #000000;">line</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">gets</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">)</span>
close(fn)
<span style="color: #008080;">if</span> <span style="color: #000000;">line</span><span style="color: #0000FF;">=-</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
else
<span style="color: #7060A8;">close</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">)</span>
pq_add({fn,line}, pq)
<span style="color: #008080;">else</span>
end if
<span style="color: #7060A8;">pq_add</span><span style="color: #0000FF;">({</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">,</span><span style="color: #000000;">line</span><span style="color: #0000FF;">},</span> <span style="color: #000000;">pq</span><span style="color: #0000FF;">)</span>
end procedure
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
-- setup (optional/remove if files already exist)
constant data = {"Line 001\nLine 008\nLine 017\n",
<span style="color: #000080;font-style:italic;">-- setup (optional/remove if files already exist)</span>
"Line 019\nLine 033\nLine 044\nLine 055\n",
<span style="color: #008080;">constant</span> <span style="color: #000000;">data</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"Line 001\nLine 008\nLine 017\n"</span><span style="color: #0000FF;">,</span>
"Line 019\nLine 029\nLine 039\n",
<span style="color: #008000;">"Line 023019\nLine 030033\nLine 044\nLine 055\n"}</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"Line 019\nLine 029\nLine 039\n"</span><span style="color: #0000FF;">,</span>
filenames = {"file1.txt","file2.txt","file3.txt","file4.txt"}
<span style="color: --#008000;">"Line (or023\nLine command_line()[3..$]030\n"</span><span ifstyle="color: you prefer)#0000FF;">},</span>
<span style="color: #000000;">filenames</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"file1.txt"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"file2.txt"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"file3.txt"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"file4.txt"</span><span style="color: #0000FF;">}</span>
 
<span style="color: #000080;font-style:italic;">-- (or command_line()[3..$] if you prefer)</span>
for i=1 to length(filenames) do
integer fn = open(filenames[i], "w")
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">filenames</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
if fn<0 then crash("cannot open file") end if
<span style="color: #004080;">integer</span> <span style="color: #000000;">fn</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">open</span><span style="color: #0000FF;">(</span><span style="color: #000000;">filenames</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span> <span style="color: #008000;">"w"</span><span style="color: #0000FF;">)</span>
puts(fn, data[i])
<span style="color: #008080;">if</span> <span style="color: #000000;">fn</span><span style="color: #0000FF;"><</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #7060A8;">crash</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"cannot open file"</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
close(fn)
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">data</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
end for
<span style="color: #7060A8;">close</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">)</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
-- initilisation
integer pq = pq_new()
<span style="color: #000080;font-style:italic;">-- initilisation</span>
for i=1 to length(filenames) do
<span style="color: #004080;">integer</span> <span style="color: #000000;">pq</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">pq_new</span><span style="color: #0000FF;">()</span>
integer fn = open(filenames[i], "r")
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">filenames</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
if fn<0 then crash("cannot open file") end if
<span style="color: #004080;">integer</span> <span style="color: #000000;">fn</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">open</span><span style="color: #0000FF;">(</span><span style="color: #000000;">filenames</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span> <span style="color: #008000;">"r"</span><span style="color: #0000FF;">)</span>
add(fn,pq)
<span style="color: #008080;">if</span> <span style="color: #000000;">fn</span><span style="color: #0000FF;"><</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #7060A8;">crash</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"cannot open file"</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end for
<span style="color: #000000;">add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">,</span><span style="color: #000000;">pq</span><span style="color: #0000FF;">)</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
-- main loop
while not pq_empty(pq) do
<span style="color: #000080;font-style:italic;">-- main loop</span>
{integer fn, string line} = pq_pop(pq)
<span style="color: #008080;">while</span> <span style="color: #008080;">not</span> <span style="color: #7060A8;">pq_empty</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pq</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
puts(1,line)
<span style="color: #0000FF;">{</span><span style="color: #004080;">integer</span> <span style="color: #000000;">fn</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">string</span> <span style="color: #000000;">line</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">pq_pop</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pq</span><span style="color: #0000FF;">)</span>
add(fn, pq)
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">line</span><span style="color: #0000FF;">)</span>
end while
<span style="color: #000000;">add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">pq</span><span style="color: #0000FF;">)</span>
pq_destroy(pq)
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
 
<span style="color: #7060A8;">pq_destroy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pq</span><span style="color: #0000FF;">)</span>
-- cleanup (optional/remove if files already exist)
for i=1 to length(filenames) do
<span style="color: #000080;font-style:italic;">-- cleanup (optional/remove if files already exist)</span>
{} = delete_file(filenames[i])
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">filenames</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
end for</lang>
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">delete_file</span><span style="color: #0000FF;">(</span><span style="color: #000000;">filenames</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,674 ⟶ 1,857:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de streamMerge @
(let Heap
(make
Line 1,687 ⟶ 1,870:
(if (in (cdar Heap) (read))
(set (car Heap) @)
(close (cdr (pop 'Heap))) ) ) ) ) )</langsyntaxhighlight>
<pre>$ cat a
3 14 15
Line 1,699 ⟶ 1,882:
2 3 5 7</pre>
Test:
<langsyntaxhighlight PicoLisplang="picolisp">(test (2 3 14 15 17 18)
(streamMerge
(open "a")
Line 1,709 ⟶ 1,892:
(open "b")
(open "c")
(open "d") ) )</langsyntaxhighlight>
'streamMerge' works with non-numeric data as well, and also - instead of calling
'open' on a file or named pipe - with the results of 'connect' or 'listen' (i.e.
sockets).
 
== {{header|Python}} ==
 
Built-in function <code>open</code> opens a file for reading and returns a line-by-line iterator (stream) over the file.
Line 1,720 ⟶ 1,903:
There exists a standard library function <code>heapq.merge</code> that takes any number of sorted stream iterators and merges them into one sorted iterator, using a [[heap]].
 
<langsyntaxhighlight lang="python">import heapq
import sys
 
sources = sys.argv[1:]
for item in heapq.merge(open(source) for source in sources):
print(item)</langsyntaxhighlight>
 
=={{header|Racket}}==
 
<langsyntaxhighlight lang="racket">;; This module produces a sequence that merges streams in order (by <)
#lang racket/base
(require racket/stream)
Line 1,801 ⟶ 1,984:
'(1 2 3 4 5 6 7 8 9 10))
(check-equal? (for/list ((i (merge-sequences/< '(2 4 6 7 8 9 10) '(1 3 5)))) i)
'(1 2 3 4 5 6 7 8 9 10)))</langsyntaxhighlight>
 
{{out}}
Line 1,815 ⟶ 1,998:
monkey</pre>
 
== {{header|REXX}} ==
===version 1===
<langsyntaxhighlight lang="rexx">/* REXX ***************************************************************
* Merge 1.txt ... n.txt into all.txt
* 1.txt 2.txt 3.txt 4.txt
Line 1,896 ⟶ 2,079:
Return
 
o: Return lineout(oid,arg(1))</langsyntaxhighlight>
{{out}}
<pre>1
Line 1,919 ⟶ 2,102:
 
No &nbsp; ''heap'' &nbsp; is needed to keep track of which record was written, nor needs replenishing from its input file.
<langsyntaxhighlight lang="rexx">/*REXX pgm reads sorted files (1.TXT, 2.TXT, ···), and writes sorted data ───► ALL.TXT */
@.=copies('ff'x, 1e4); call lineout 'ALL.TXT',,1 /*no value should be larger than this. */
do n=1 until @.n==@.; call rdr n; end /*read any number of appropriate files.*/
Line 1,932 ⟶ 2,115:
end /*forever*/ /*keep reading/merging until exhausted.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
rdr: arg z; @.z= @.; f= z'.TXT'; if lines(f)\==0 then @.z= linein(f); return</langsyntaxhighlight>
{{out|output|text=&nbsp; is the same as the 1<sup>st</sup> REXX version when using identical input files, &nbsp; except the output file is named &nbsp; '''ALL.TXT'''}} <br><br>
 
Line 1,939 ⟶ 2,122:
{{works with|Rakudo|2018.02}}
 
<syntaxhighlight lang="raku" perl6line>sub merge_streams ( @streams ) {
my @s = @streams.map({ hash( STREAM => $_, HEAD => .get ) })\
.grep({ .<HEAD>.defined });
Line 1,951 ⟶ 2,134:
}
 
say merge_streams([ @*ARGS».&open ]);</langsyntaxhighlight>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">def stream_merge(*files)
fio = files.map{|fname| open(fname)}
merge(fio.map{|io| [io, io.gets]})
Line 1,978 ⟶ 2,161:
puts "#{fname}: #{data}"
end
stream_merge(*files)</langsyntaxhighlight>
 
{{out}}
Line 2,008 ⟶ 2,191:
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">def mergeN[A : Ordering](is: Iterator[A]*): Iterator[A] = is.reduce((a, b) => merge2(a, b))
 
def merge2[A : Ordering](i1: Iterator[A], i2: Iterator[A]): Iterator[A] = {
Line 2,027 ⟶ 2,210:
nextHead ++ merge2Buffered(i1, i2)
}
}</langsyntaxhighlight>
 
Example usage, demonstrating lazyness:
 
<langsyntaxhighlight lang="scala">val i1 = Iterator.tabulate(5) { i =>
val x = i * 3
println(s"generating $x")
Line 2,054 ⟶ 2,237:
val x = merged.next
println(s"output: $x")
}</langsyntaxhighlight>
 
{{out}}
Line 2,090 ⟶ 2,273:
=={{header|Sidef}}==
{{trans|Raku}}
<langsyntaxhighlight lang="ruby">func merge_streams(streams) {
var s = streams.map { |stream|
Pair(stream, stream.readline)
Line 2,104 ⟶ 2,287:
}
 
say merge_streams(ARGV.map {|f| File(f).open_r }).join("\n")</langsyntaxhighlight>
 
=={{header|Tcl}}==
Line 2,111 ⟶ 2,294:
A careful reader will notice that '''$peeks''' is treated alternately as a dictionary ('''dict set''', '''dict get''') and as a list ('''lsort''', '''lassign'''), exploiting the fact that dictionaries are simply lists of even length. For large dictionaries this would not be recommended, as it causes [https://wiki.tcl.tk/3033 "shimmering"], but in this example the impact is too small to matter.
 
<langsyntaxhighlight Tcllang="tcl">#!/usr/bin/env tclsh
proc merge {args} {
set peeks {}
Line 2,131 ⟶ 2,314:
 
merge {*}[lmap f $::argv {open $f r}]
</syntaxhighlight>
</lang>
 
== {{header|UNIX Shell}} ==
 
sort --merge source1 source2 sourceN > sink
 
== {{header|zklWren}} ==
{{trans|Kotlin}}
{{libheader|Wren-ioutil}}
{{libheader|Wren-str}}
{{libheader|Wren-seq}}
No Heap class, so we use a List. Comparisons are text based even for numbers.
<syntaxhighlight lang="wren">import "io" for File
import "./ioutil" for FileUtil
import "./str" for Str
import "./seq" for Lst
 
var merge2 = Fn.new { |inputFile1, inputFile2, outputFile|
// Note that the FileUtil.readEachLine method checks the file exists and closes it
// when there are no more lines to read.
var reader1 = Fiber.new(FileUtil.readEachLine(inputFile1))
var reader2 = Fiber.new(FileUtil.readEachLine(inputFile2))
var writer = File.create(outputFile)
var line1 = reader1.call()
var line2 = reader2.call()
while (line1 && line2) {
if (Str.le(line1, line2)) {
writer.writeBytes(line1 + "\n")
line1 = reader1.call()
} else {
writer.writeBytes(line2 + "\n")
line2 = reader2.call()
}
}
while (line1) {
writer.writeBytes(line1 + "\n")
line1 = reader1.call()
}
while (line2) {
writer.writeBytes(line2 + "\n")
line2 = reader2.call()
}
writer.close()
}
 
var mergeN = Fn.new { |inputFiles, outputFile|
var readers = inputFiles.map { |f| Fiber.new(FileUtil.readEachLine(f)) }.toList
var writer = File.create(outputFile)
var lines = readers.map { |reader| reader.call() }.toList
while (lines.any { |line| line }) {
var line = lines.where { |line| line }.reduce() { |acc, s| Str.lt(s, acc) ? s : acc }
var index = Lst.indexOf(lines, line)
writer.writeBytes(line + "\n")
lines[index] = readers[index].call()
}
writer.close()
}
 
var files = ["merge1.txt", "merge2.txt", "merge3.txt", "merge4.txt"]
merge2.call(files[0], files[1], "merged2.txt")
mergeN.call(files, "mergedN.txt")
// check it worked
System.print(File.read("merged2.txt"))
System.print(File.read("mergedN.txt"))</syntaxhighlight>
 
{{out}}
<pre>
1
17
19
33
500
8
 
1
17
19
1999
2999
2e3
3000
33
3999
500
8
</pre>
 
=={{header|zkl}}==
This solution uses iterators, doesn't care where the streams orginate and only keeps the head of the stream on hand.
<langsyntaxhighlight lang="zkl">fcn mergeStreams(s1,s2,etc){ //-->Walker
streams:=vm.arglist.pump(List(),fcn(s){ // prime and prune
if( (w:=s.walker())._next() ) return(w);
Line 2,152 ⟶ 2,416:
v
}.fp(streams));
}</langsyntaxhighlight>
Using infinite streams:
<langsyntaxhighlight lang="zkl">w:=mergeStreams([0..],[2..*,2],[3..*,3],T(5));
w.walk(20).println();</langsyntaxhighlight>
{{out}}
<pre>
Line 2,161 ⟶ 2,425:
</pre>
Using files:
<langsyntaxhighlight lang="zkl">w:=mergeStreams(File("unixdict.txt"),File("2hkprimes.txt"),File("/dev/null"));
do(10){ w.read().print() }</langsyntaxhighlight>
{{out}}
<pre>
Line 2,177 ⟶ 2,441:
</pre>
Using the above example to squirt the merged stream to a file:
<langsyntaxhighlight lang="zkl">mergeStreams(File("unixdict.txt"),File("2hkprimes.txt"),File("/dev/null"))
.pump(File("foo.txt","w"));</langsyntaxhighlight>
{{out}}
<pre>
2,122

edits