1
//////////////////////////////////////////////////////////////////////
3
// JCSP ("CSP for Java") Libraries //
4
// Copyright (C) 1996-2008 Peter Welch and Paul Austin. //
5
// 2001-2004 Quickstone Technologies Limited. //
7
// This library is free software; you can redistribute it and/or //
8
// modify it under the terms of the GNU Lesser General Public //
9
// License as published by the Free Software Foundation; either //
10
// version 2.1 of the License, or (at your option) any later //
13
// This library is distributed in the hope that it will be //
14
// useful, but WITHOUT ANY WARRANTY; without even the implied //
15
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
16
// PURPOSE. See the GNU Lesser General Public License for more //
19
// You should have received a copy of the GNU Lesser General //
20
// Public License along with this library; if not, write to the //
21
// Free Software Foundation, Inc., 59 Temple Place, Suite 330, //
22
// Boston, MA 02111-1307, USA. //
24
// Author contact: P.H.Welch@kent.ac.uk //
27
//////////////////////////////////////////////////////////////////////
30
import org.jcsp.lang.*;
31
import org.jcsp.util.ints.*;
32
import org.jcsp.plugNplay.ints.*;
37
public final class PrimeMultiples2 implements CSProcess {
39
private final ChannelOutputInt trap;
41
private final int[] primes;
42
private final int minPrimes = 2;
44
private final int howMany;
46
public PrimeMultiples2 (final int[] primes, final int howMany,
47
final ChannelOutputInt trap) {
48
// assume these are distinct primes (and at least minPrimes of them)
50
if (howMany < minPrimes) {
51
this.howMany = minPrimes;
52
} else if (howMany > primes.length) {
53
this.howMany = primes.length;
55
this.howMany = howMany;
62
final One2OneChannelInt[] a = Channel.one2oneIntArray (howMany + 1);
63
final One2OneChannelInt[] b = Channel.one2oneIntArray (howMany, new InfiniteBufferInt ());
64
final One2OneChannelInt c = Channel.one2oneInt ();
65
final One2OneChannelInt d = Channel.one2oneInt ();
66
final One2OneChannelInt e = Channel.one2oneInt ();
68
final CSProcess[] Multipliers = new CSProcess[howMany];
69
for (int i = 0; i < howMany; i++) {
70
Multipliers[i] = new MultInt (primes[i], a[i].in (), b[i].out ());
75
new Parallel (Multipliers),
76
new MergeInt (Channel.getInputArray (b), c.out ()),
77
new PrefixInt (1, c.in (), d.out ()),
78
new DeltaInt (d.in (), Channel.getOutputArray (a)),
79
new TrapNegative (a[howMany].in (), e.out (), trap),
80
new PrinterInt (e.in (), "", " ")