Package cc.redberry.core.indexmapping

Source Code of cc.redberry.core.indexmapping.IndexMappingImpl

/*
* 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.indexmapping;

import java.util.*;
import cc.redberry.core.context.Context;
import cc.redberry.core.context.ToStringMode;
import cc.redberry.core.indices.Indices;
import cc.redberry.core.indices.IndicesUtils;
import cc.redberry.core.indexgenerator.IndexGenerator;
import cc.redberry.core.tensor.Tensor;
import cc.redberry.core.transformations.ApplyIndexMappingTransformation;

/**
*
* @author Dmitry Bolotin
* @author Stanislav Poslavsky
*/
public class IndexMappingImpl implements IndexMapping {
    private final Map<Integer, Integer> map;
    private final int[] usedIndices;
    private IndexGenerator indexGenerator = null;

    public IndexMappingImpl() {
        map = new HashMap<>();
        usedIndices = new int[0];
    }

    public IndexMappingImpl(int[] usedIndices) {
        map = new HashMap<>();
        this.usedIndices = usedIndices;
    }

    public IndexMappingImpl(int[] usedIndices, IndexMappingBuffer indexMappingBuffer) {
        this(usedIndices);
        for (Map.Entry<Integer, IndexMappingBufferRecord> entry : indexMappingBuffer.getMap().entrySet())
            map.put(entry.getKey(), entry.getValue().getIndexName());
    }

    public IndexMappingImpl(int[] usedIndices, int[] from, int[] to) {
        this(usedIndices);
        for (int i = 0; i < from.length; ++i)
            map.put(from[i], to[i]);
    }

    private IndexMappingImpl(int[] usedIndices, Map<Integer, Integer> map, IndexGenerator indexGenerator) {
        this.usedIndices = usedIndices;
        this.map = map;
        this.indexGenerator = indexGenerator;
    }

    public void preprocessIndices(int[] indices) {
        for (int i : indices)
            map(i);
    }

    public void preprocessIndices(Indices indices) {
        for (int i = 0; i < indices.size(); ++i)
            map(indices.get(i));
    }

    private void generateGenerator() {
        int[] ui = new int[usedIndices.length + map.size()];
        System.arraycopy(usedIndices, 0, ui, 0, usedIndices.length);
        int i = usedIndices.length;
        for (Integer index : map.values())
            ui[i++] = index.intValue();
        indexGenerator = new IndexGenerator(ui);
    }

    public void add(int from, int to) {
        indexGenerator = null;
        int _from = IndicesUtils.getNameWithType(from);
        if (map.containsKey(_from))
            throw new IllegalArgumentException("already contains");
        map.put(_from, IndicesUtils.getNameWithType(to));
    }

    public int map(int from) {
        if (indexGenerator == null)
            generateGenerator();
        Integer i;
        int _from = IndicesUtils.getNameWithType(from);
        if ((i = map.get(_from)) != null)
            return IndicesUtils.getRawStateInt(from) | i.intValue();
        if (indexGenerator.contains(_from)) {
            int newName = indexGenerator.generate(IndicesUtils.getType(_from));
            map.put(_from, newName);
            return IndicesUtils.getRawStateInt(from) | newName;
        }
        map.put(_from, _from);
        indexGenerator.add(_from);
        return from;
    }

    /**
     * Returns integer array representing mapped indices names. (names with types)<br/>
     * This is all "from" indices, including used indices for which new indices was generated.
     *
     * @return see description
     */
    public int[] getAllFromIndices() {
        Set<Integer> values = map.keySet();
        int[] result = new int[values.size()];
        int i = 0;
        for (Integer ind : values)
            result[i++] = ind.intValue();
        return result;
    }

    /**
     * Returns sorted int array representing original indices names. (names with types)<br/>
     * This is all "from" indices, including used indices for which new indices was generated.
     *
     * @return see description
     */
    public int[] getAllFromIndicesSorted() {
        int[] result = getAllFromIndices();
        Arrays.sort(result);
        return result;
    }

    /**
     * Returns int array representing indices names to which this mapping change originals. (names with types)<br/>
     * This is all "to" indices, including indices which was generated as the replacement for used indices.
     *
     * @return see description
     */
    public int[] getAllToIndices() {
        Collection<Integer> values = map.values();
        int[] result = new int[values.size()];
        int i = 0;
        for (Integer ind : values)
            result[i++] = ind.intValue();
        return result;
    }

    /**
     * Returns sorted int array representing indices names to which this mapping change originals. (names with types)<br/>
     * This is all "to" indices, including indices which was generated as the replacement for used indices.
     *
     * @return see description
     */
    public int[] getAllToIndicesSorted() {
        int[] result = getAllToIndices();
        Arrays.sort(result);
        return result;
    }

    @Override
    public boolean isEmpty() {
        return map.isEmpty();
    }

    @Override
    public IndexMappingImpl clone() {
        return new IndexMappingImpl(usedIndices, new HashMap<>(map), indexGenerator == null ? null : indexGenerator.clone());
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            sb.append(Context.get().getIndexConverterManager().getSymbol(entry.getKey().intValue(), ToStringMode.UTF8));
            sb.append("->");
            sb.append(Context.get().getIndexConverterManager().getSymbol(entry.getValue(), ToStringMode.UTF8));
            sb.append(",");
        }
        if (map.isEmpty())
            sb.append(":empty buffer");
        else
            sb.deleteCharAt(sb.length() - 1);
        return sb.toString();
    }

    public void apply(Tensor t) {
        ApplyIndexMappingTransformation.INSTANCE.perform(t, this);
    }
}
TOP

Related Classes of cc.redberry.core.indexmapping.IndexMappingImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.