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

package ilog.language.design.base;
/**
 * @version     Last modified on Mon Apr 15 13:45:17 2002 by hak
 * @author      <a href="mailto:hak@ilog.fr">Hassan A&iuml;t-Kaci</a>
 * @copyright   &copy; 2000-2002 <a href="http://www.ilog.fr/">ILOG, S.A.</a>
 */


import ilog.language.design.backend.RealMap;
import ilog.language.design.backend.Indexable;
import ilog.language.design.backend.Runtime;
import ilog.language.design.backend.SizeMatchException;

public class ShuffleRealMap extends Instruction
{
  public ShuffleRealMap ()
    {
      setName("SHUFFLE_MAP_R");
    }

  public final void execute (Runtime r) throws SizeMatchException
    {
      int[] permutation = (int[])r.popObject();      
      Indexable indexable = (Indexable)r.popObject();
      int size = r.popInt();

      if (indexable.size() != size)
        throw new SizeMatchException("indexable size ("+indexable.size()+
                                     ") does not match array size ("+size+")");

      double[] array = new double[size];

      if (permutation == null)
        for (int i=0; i<size; i++)
          array[i] = r.popReal();
      else
        for (int i=0; i<size; i++)
          array[permutation[i]] = r.popReal();

      r.pushObject(new RealMap(array,indexable));
      r.incIP();
    }
}
