The Filter Process Next: The Sift ProcessUp: Eratosthenes SievePrevious: Eratosthenes Sieve

The Filter Process

Filter  filters all multiples of the first value it receives.

public class Filter extends ReactiveProcess
{
  private Channel in, out;
  private int prime;


  public Filter(Channel in, Channel out,int prime)
  {
    this.in    = in;
    this.out   = out;
    this.prime = prime;
  }

  public Filter(NrpMachine machine,String in,
                             String out,int prime)
  {
    this.in    = machine.getChannel(in);
    this.out   = machine.getChannel(out);
    this.prime = prime;
    machine.add(this);
  }

  final public String toString(){ 
    return "filter(" + prime + "," + out + "," + in + ")";
  }

  protected byte activation(Machine machine)
  {
    byte b = fix(in,machine);
    if (b == TERM){
      int v = ((Integer)in.get()).intValue();
      if (0 != v % prime){ put(out,new Integer(v),machine); }
      return STOP;
    }
    return b;
  }
}