private ProviderIntegralFactory() {
}
@Override
public IndexMappingProvider create(IndexMappingProvider opu, Tensor from, Tensor to, boolean allowDiffStates) {
final Integral intFrom = (Integral) from, intTo = (Integral) to;
final SimpleTensor[] varsFrom = intFrom.vars(), varsTo = intTo.vars();
if (varsFrom.length != varsTo.length)
return IndexMappingProvider.Util.EMPTY_PROVIDER;
Arrays.sort(varsFrom); //TODO change to quick Sort
Arrays.sort(varsTo); //TODO change to quick Sort
int i;
int[] hashes = new int[varsFrom.length];
for (i = 0; i < varsFrom.length; ++i)
if ((hashes[i] = varsFrom[i].hashCode()) != varsTo[i].hashCode())
return IndexMappingProvider.Util.EMPTY_PROVIDER;
PrecalculatedStretches stretches = new PrecalculatedStretches(hashes);
for (Stretch stretch : stretches)
if (stretch.length == 1)
if (!IndexMappings.mappingExists(varsFrom[stretch.from], varsTo[stretch.from], allowDiffStates))
return IndexMappingProvider.Util.EMPTY_PROVIDER;
STRETCHES:
for (Stretch stretch : stretches)
if (stretch.length != 1) {
PERMUTATIONS:
for (int[] permutation : new IntPermutationsGenerator(stretch.length)) {
for (i = 0; i < stretch.length; ++i)
if (!IndexMappings.mappingExists(varsFrom[stretch.from + i], varsTo[stretch.from + permutation[i]], allowDiffStates))
continue PERMUTATIONS;
continue STRETCHES;
}
return IndexMappingProvider.Util.EMPTY_PROVIDER;
}
return IndexMappings.createPort(opu, intFrom.target(), intTo.target(), allowDiffStates);
}