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

package hlt.language.design.instructions;
/**
 * @version     Last modified on Wed Jun 20 14:29:51 2012 by hak
 * @author      <a href="mailto:hak@acm.org">Hassan A&iuml;t-Kaci</a>
 * @copyright   &copy; <a href="http://www.hassan-ait-kaci.net/">by the author</a>
 */


import hlt.language.design.backend.IntMap;
import hlt.language.design.backend.Indexable;
import hlt.language.design.backend.Runtime;
import hlt.language.design.backend.SizeMatchException;

public class ShuffleIntMap extends Instruction
{
  public ShuffleIntMap ()
    {
      setName("SHUFFLE_MAP_I");
    }

  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+")");

      int[] array = new int[size];

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

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