Package cc.redberry.core.indexmapping

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

/*
* Redberry: symbolic tensor computations.
*
* Copyright (c) 2010-2013:
*   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 cc.redberry.core.context.Context;
import cc.redberry.core.context.OutputFormat;
import cc.redberry.core.indices.InconsistentIndicesException;
import cc.redberry.core.indices.IndicesUtils;

/**
* Holds the intermediate information about mapping entry.
*
* @author Dmitry Bolotin
* @author Stanislav Poslavsky
* @since 1.0
*/
final class IndexMappingBufferRecord {

    /*
     * Mask 0b00000SUL
     *
     * S - initialized with different states U - "to" upper index mapped L -
     * "to" lower index mapped
     *
     */
    byte states;
    private final int toName;

    public IndexMappingBufferRecord(int from, int to) {
        this.toName = IndicesUtils.getNameWithType(to);
        states |= 1 << IndicesUtils.getStateInt(to);
        states |= (IndicesUtils.getStateInt(from) ^ IndicesUtils.getStateInt(to)) << 2;
    }

    public IndexMappingBufferRecord(byte usedStates, int indexName) {
        this.states = usedStates;
        this.toName = indexName;
    }

    public boolean tryMap(int from, int to) {
        if (IndicesUtils.getNameWithType(to) != toName)
            return false;
        if ((IndicesUtils.getStateInt(from) != IndicesUtils.getStateInt(to)) != ((states & 0x4) == 0x4))
            throw new InconsistentIndicesException(from);
        if ((states & (1 << IndicesUtils.getStateInt(to))) != 0)
            throw new InconsistentIndicesException(to);
        states |= 1 << IndicesUtils.getStateInt(to);
        return true;
    }

    /**
     * @return name with type
     */
    public int getIndexName() {
        return toName;
    }

    //    public int getFromRawState() {
    //        return ((states & 4) == 4 ? 0x80000000 : 0) ^ (((states & 1) ^ 1) << 31);
    //    }
    //
    //    public int getToRawState() {
    //        return ((states & 1) ^ 1) << 31;
    //    }
    public byte getStates() {
        return states;
    }

    public int getToRawState() {
        return (states & 1) == 0 ? 0x80000000 : 0;
    }

    public int getFromRawState() {
        if ((states & 4) == 0)
            return getToRawState();
        else
            return 0x80000000 ^ getToRawState();
    }

    public boolean getStatesBit(int bit) {
        return ((states >>> bit) & 1) == 1;
    }

    public boolean isContracted() {
        return (states & 3) == 3;
    }

    public boolean diffStatesInitialized() {
        return (states & 4) == 4;
    }

    public int getRawDiffStateBit() {
        return ((states & 4) << 29);
    }

    public void invertStates() {
        if (isContracted()) return;
        states ^= 3;
    }

    @Override
    public IndexMappingBufferRecord clone() {
        return new IndexMappingBufferRecord(states, toName);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        final IndexMappingBufferRecord other = (IndexMappingBufferRecord) obj;
        if (this.states != other.states)
            return false;
        return this.toName == other.toName;
    }

    @Override
    public int hashCode() {
        return 31 * (31 * 7 + this.states) + this.toName;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();

        sb.append(Context.get().getIndexConverterManager().getSymbol(getIndexName(), OutputFormat.UTF8));
        sb.append(":");
        for (int i = 2; i >= 0; --i)
            sb.append(getStatesBit(i) ? 1 : 0);
        return sb.toString();
    }
}
TOP

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

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.