@Override
public IndexMappingBuffer take() {
if (currentBuffer == null)
return null;
SimpleIndices fromIndices = from.getIndices();
SimpleIndices toIndices = to.getIndices();
int size = fromIndices.size();
if (size == 0) {
IndexMappingBuffer r = currentBuffer;
currentBuffer = null;
return r;
}
if (searchForPermutations != null) {
Permutation permutation;
out:
while (searchForPermutations.hasNext()) {
permutation = searchForPermutations.next();
IndexMappingBuffer tempBuffer = currentBuffer.clone();
for (int i = 0; i < size; ++i)
if (!tempBuffer.tryMap(fromIndices.get(i), toIndices.get(permutation.newIndexOf(i))))
continue out;
tempBuffer.addSign(permutation.antisymmetry());
return tempBuffer;
}
searchForPermutations = null;
return currentBuffer = null;
}
if (fromIndices.size() == 1 || fromIndices.getSymmetries().isTrivial()) {
IndexMappingBuffer tempBuffer = currentBuffer;
for (int i = 0; i < size; ++i)
if (!tempBuffer.tryMap(fromIndices.get(i), toIndices.get(i)))
return currentBuffer = null;
currentBuffer = null;
return tempBuffer;
}
//try to find partial mapping
IntArrayList permMappingFrom = null, permMappingTo = null;
outer:
for (int mapFrom = 0; mapFrom < size; ++mapFrom) {
int fromIndex = fromIndices.get(mapFrom);
IndexMappingBufferRecord bRec = currentBuffer.getMap().get(getNameWithType(fromIndex));
//no such index in mapping yet
if (bRec == null)
continue;
//index contained in mapping have same state
if (getRawStateInt(fromIndex) == bRec.getFromRawState())
return currentBuffer = null;
//toIndex that we'll find in toIndices
int toIndex = inverseIndexState(setRawState(bRec.getToRawState(), bRec.getIndexName()));
for (int mapTo = 0; mapTo < size; ++mapTo) {
if (toIndices.get(mapTo) == toIndex) {
if (permMappingFrom == null) {
permMappingFrom = new IntArrayList();
permMappingTo = new IntArrayList();
}
permMappingFrom.add(mapFrom);