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

package ilog.language.design.base;

/**
 * @version     Last modified on Mon Apr 15 17:56:38 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.Runtime;
import ilog.language.design.backend.Indexable;
import ilog.language.design.backend.IntSet;
import ilog.language.design.backend.RuntimeSet;
import ilog.language.design.backend.IntRange;
import ilog.language.design.backend.ArrayAllocationErrorException;

public class ReconcileIndexables extends Instruction
{
  public ReconcileIndexables ()
    {
      setName("RECONCILE_INDEXABLES");
    }

  public final void execute (Runtime r) throws ArrayAllocationErrorException
    {
      Indexable indexable = (Indexable)r.popObject();
      Indexable indexset = (Indexable)r.objectResult();

      int[] permutation = null;

      if (indexset instanceof RuntimeSet || indexable instanceof IntSet)
        {
          RuntimeSet set = indexset instanceof RuntimeSet
                         ? (RuntimeSet)indexset
                         : ((IntRange)indexset).toIntSet();

          permutation = new int[indexable.size()];
          if (!set.equals(indexable,permutation))
            throw new ArrayAllocationErrorException
              ("can't initialize a map from one with a different index set");
        }
      else
        if (!indexset.equals(indexable))
          throw new ArrayAllocationErrorException
            ("can't initialize a map from one with a different index set");

      r.pushObject(permutation);
      r.incIP();
    }
}
