/*
* Redberry: symbolic tensor computations.
*
* Copyright (c) 2010-2012:
* Stanislav Poslavsky <stvlpos@mail.ru>
* Bolotin Dmitriy <bolotin.dmitriy@gmail.com>
*
* This file is part of Redberry.
*
* Redberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Redberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Redberry. If not, see <http://www.gnu.org/licenses/>.
*/
package cc.redberry.core.tensor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import cc.redberry.core.combinatorics.PermutationsProvider;
import cc.redberry.core.combinatorics.PermutationsProviderImpl;
import cc.redberry.core.combinatorics.SimplePermutationProvider;
/**
*
* @author Dmitry Bolotin
* @author Stanislav Poslavsky
*/
public class TensorSortedContentImpl implements TensorSortedContent {
protected final boolean isDerivativeContent;
private PermutationsProvider provider = null;
protected final Tensor[] data;
public TensorSortedContentImpl(final boolean isDerivativeContent, final Tensor... data) {
this.data = data;
this.isDerivativeContent = isDerivativeContent;
Arrays.sort(data, isDerivativeContent ? 1 : 0, data.length);
}
@Override
public Tensor get(final int position) {
return data[position];
}
@Override
public int size() {
return data.length;
}
@Override
public Tensor[] getRange(int from, int to) {
return Arrays.copyOfRange(data, from, to);
}
@Override
public Tensor[] getDataCopy() {
return data.clone();
}
@Override
public boolean isDerivativeContent() {
return isDerivativeContent;
}
@Override
public PermutationsProvider permutationsProvider() {
if (provider == null)
generateProvider();
return provider;
}
private void generateProvider() {
int begin = isDerivativeContent ? 1 : 0;
int i;
List<PermutationsProvider> disjointProviders = new ArrayList<>();
for (i = (isDerivativeContent ? 1 : 0) + 1; i <= size(); ++i)
if (i == size() || get(i).hash() != get(i - 1).hash()) {
if (i - 1 != begin)
disjointProviders.add(new SimplePermutationProvider(begin, i));
begin = i;
}
provider = new PermutationsProviderImpl(disjointProviders);
}
}