All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.citec.tcs.alignment.SparseMatrix Maven / Gradle / Ivy

/* 
 * TCS Alignment Toolbox
 * 
 * Copyright (C) 2013-2015
 * Benjamin Paaßen, Georg Zentgraf
 * AG Theoretical Computer Science
 * Centre of Excellence Cognitive Interaction Technology (CITEC)
 * University of Bielefeld
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program 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 Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see .
 */
package de.citec.tcs.alignment;

import java.util.HashMap;
import java.util.Map.Entry;

/**
 * This models a sparse matrix that does not have to be fully calculated.
 * Internally the matrix is modelled using nested HashMaps which are not very
 * fast. So it is recommended to use full matrices if you have dimensions
 * roughly smaller than 100 x 100.
 *
 * @author Benjamin Paassen - [email protected]
 * @param  The value class.
 */
public class SparseMatrix {

    final HashMap> actualMatrix = new HashMap<>();

    public SparseMatrix() {
    }

    /**
     * Returns the value stored at the given position or null if no value was
     * stored there until now.
     *
     * @param i the row index.
     * @param j the column index.
     * @return the value stored at the given position or null if no value was
     * stored there until now.
     */
    public X get(int i, int j) {
        final HashMap row = actualMatrix.get(i);
        if (row == null) {
            return null;
        }
        return row.get(j);
    }

    /**
     * Sets the value at the given position in the matrix.
     *
     * @param i the row index.
     * @param j the column index.
     * @param value a value.
     */
    public void set(int i, int j, X value) {
        HashMap row = actualMatrix.get(i);
        if (row == null) {
            row = new HashMap();
            actualMatrix.put(i, row);
        }
        row.put(j, value);
    }

    /**
     * {@inheritDoc }
     */
    @Override
    public String toString() {
        final StringBuilder builder = new StringBuilder();
        for (final Entry> rowEntry : actualMatrix.entrySet()) {
            for (final Entry entry : rowEntry.getValue().entrySet()) {
                builder.append("(");
                builder.append(rowEntry.getKey().toString());
                builder.append(",");
                builder.append(entry.getKey());
                builder.append(") = ");
                builder.append(entry.getValue().toString());
                builder.append("\n");
            }
        }
        builder.delete(builder.length() - 1, builder.length());
        return builder.toString();
    }

    /**
     * {@inheritDoc }
     */
    @Override
    public int hashCode() {
        int hash = 5;
        hash = 47 * hash + (this.actualMatrix != null ? this.actualMatrix.hashCode() : 0);
        return hash;
    }

    /**
     * {@inheritDoc }
     */
    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final SparseMatrix other = (SparseMatrix) obj;
        if (this.actualMatrix != other.actualMatrix && (this.actualMatrix == null || !this.actualMatrix.equals(other.actualMatrix))) {
            return false;
        }
        return true;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy