//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
// PLEASE DO NOT EDIT WITHOUT THE EXPLICIT CONSENT OF THE AUTHOR! \\
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\

package ilog.language.design.base;

/**
 * @version     Last modified on Sun Nov 03 20:43:01 2002 by hak
 * @author      <a href="mailto:hak@ilog.fr">Hassan A&iuml;t-Kaci</a>
 * @copyright   &copy; 2002 <a href="http://www.ilog.fr/">ILOG, S.A.</a>
 */

import ilog.language.util.IntIterator;
import ilog.language.design.backend.Runtime;
import ilog.language.design.backend.Iteratable;
import ilog.language.design.backend.Block;

public class ApplyIntCollectionFilterHomomorphism extends ApplyCollectionHomomorphism
{
  public  ApplyIntCollectionFilterHomomorphism (Instruction tally)
    {
      super(tally);
      setName("APPLY_COLL_FHOM_I");
    }

  public final void execute (Runtime r) throws Exception
    {
      Iteratable collection = (Iteratable)r.popObject();
      Block filter    = (Block)r.popObject();
      Block function  = (Block)r.popObject();
      Block operation = (Block)r.popObject();

      r.saveState();

      PushValueInt pushElement = new PushValueInt();
      Instruction[] code = { pushElement // collection element
                           , new EnterBlock(filter)
                           , Instruction.STOP_ON_FALSE
                           , pushElement
                           , new EnterBlock(function)
                           , new PushValueObject(operation)
                           , _tally
                           , Instruction.STOP
                           };
      r.setCode(code);

      IntIterator i = collection.intIterator(true);
      while (i.hasNext())
        {
          pushElement.setValue(i.next());
          r.resetIP();
          r.run();
        }

      r.restoreState();
    }
}
