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

package ilog.language.design.base;

/**
 * @version     Last modified on Sun Nov 03 20:42:03 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 ApplyInPlaceIntCollectionFilterHomomorphism extends Instruction
{
  public  ApplyInPlaceIntCollectionFilterHomomorphism ()
    {
      setName("APPLY_IP_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();

      r.saveState();

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

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

      r.restoreState();
    }
}
