Package org.jcsp.plugNplay.ints
Class MergeInt
java.lang.Object
org.jcsp.plugNplay.ints.MergeInt
- All Implemented Interfaces:
CSProcess
Merges an array of strictly increasing int input streams into one
strictly increasing output stream.
Process Diagram
Description
MergeInt is a process whose output stream is the ordered merging of the integers on its input streams. It assumes that each input stream is strictly increasing (i.e. with no repeats) sequence of integers. It generates a strictly increasing output stream containing all -- and only -- the numbers from its input streams (eliminating any duplicates).Warning: this process assumes that its input channel array has at least two elements.
Channel Protocols
Input Channels | ||
---|---|---|
in[] | int | Assume: in.length >= 2. |
Output Channels | ||
out | int | All channels in this package carry integers. |
Example
The following example shows how to use MergeInt in a small program. The program also uses some of the other plugNplay processes. The program prints, in ascending order (up to Integer.MAX_VALUE), all integers whose prime factors consist only of 2, 3, 5 and 7. Curious readers may like to reason why the infinitely buffered channels are needed.import org.jcsp.lang.*; import org.jcsp.util.ints.*; import org.jcsp.plugNplay.ints.*; public class MergeIntExample { public static void main (String[] argv) { final One2OneChannelInt[] a = Channel.one2oneIntArray (5); final One2OneChannelInt[] b = Channel.one2oneIntArray (4, new InfiniteBufferInt ()); final One2OneChannelInt c = Channel.one2oneInt (); final One2OneChannelInt d = Channel.one2oneInt (); new Parallel ( new CSProcess[] { new MultInt (2, a[0].in (), b[0].out ()), new MultInt (3, a[1].in (), b[1].out ()), new MultInt (5, a[2].in (), b[2].out ()), new MultInt (7, a[3].in (), b[3].out ()), new MergeInt (Channel.getInputArray (b), c.out ()), new PrefixInt (1, c.in (), d.out ()), new DeltaInt (d.in (), Channel.getOutputArray (a)), new PrinterInt (a[4].in (), "--> ", "\n") } ).run (); } }
Implementation Note
The implementation sets up a balanced binary tree of parallel Merge2Int processes to fan-in the merge from its external input channels to its external output. It's a nice example of recursion and parallelism -- here's the run method:public void run () { final int n = in.length; // deduce: n >= 2 switch (n) { case 2: new Merge2Int (in[0], in[1], out).run (); break; case 3: final One2OneChannelInt c = Channel.one2oneInt (); new Parallel ( new CSProcess[] { new Merge2Int (in[0], in[1], c.out ()), new Merge2Int (c.in (), in[2], out) } ).run (); break; default: // deduce: n >= 4 final int n2 = n/2; ChannelInputInt[] bottom = new ChannelInputInt[n2]; ChannelInputInt[] top = new ChannelInputInt[n - n2]; for (int i = 0; i < n2; i++) { bottom[i] = in[i]; } for (int i = n2; i < n; i++) { top[i - n2] = in[i]; } final One2OneChannelInt[] d = Channel.one2oneIntArray (2); new Parallel ( new CSProcess[] { new MergeInt (bottom, d[0].out ()), new MergeInt (top, d[1].out ()), new Merge2Int (d[0].in (), d[1].in (), out) } ).run (); break; } }
- Author:
- P.H. Welch
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionMergeInt
(ChannelInputInt[] in, ChannelOutputInt out) Construct a new Merge2Int process with the input channels inand the output channel out. -
Method Summary
-
Constructor Details
-
MergeInt
Construct a new Merge2Int process with the input channels inand the output channel out. The ordering of the input channels makes no difference to the behaviour of this process.- Parameters:
in
- the input channels (there must be at least 2)out
- the output channel
-
-
Method Details