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

org.leibnizcenter.util.MutableMatrix Maven / Gradle / Ivy

There is a newer version: 2.0.4
Show newest version
package org.leibnizcenter.util;

import java.util.Collection;
import java.util.Vector;

/**
 * Mutable matrix
 * Created by maarten on 21-4-16.
 */
public class MutableMatrix {
    private final Vector> list;
    private final int rowCount;
    private final int columnCount;

    /**
     * initializes n by m matrix, with null at every cell.
     *
     * @param width  Number of cells per row
     * @param height Number of rows in the matrix
     */
    public MutableMatrix(int width, int height) {
        list = new Vector<>(height);
        list.setSize(height);
        for (int i = 0; i < height; i++) {
            Vector row = new Vector<>(width);
            row.setSize(width);
            list.set(i,row);
        }

        rowCount = height;
        columnCount = width;
    }

    /**
     * Creates a mutable copy of given matrix
     *
     * @param m immutable matrix
     */
    public MutableMatrix(ImmutableMatrix m) {
        int rowCount = m.getRowCount();
        int columnCount = m.getColumnCount();
        Vector> rows = new Vector<>(columnCount);
        rows.setSize(rowCount);
        for (int i = 0; i < rowCount; i++) {
            Vector row = new Vector<>(columnCount);
            row.setSize(columnCount);
            for (int j = 0; j < columnCount; j++) row.set(j, m.get(i, j));
            rows.set(i,row);
        }
        this.columnCount = m.getColumnCount();
        this.rowCount = m.getRowCount();
        this.list = rows;
    }

    public T get(int row, int column) {
        return list.get(row).get(column);
    }

    public T set(int row, int column, T item) {
        return list.get(row).set(column, item);
    }

    /**
     * @return number of non-null items
     */
    public int size() {
        return (int) list.stream().flatMap(Collection::stream).filter(r -> r != null).count();
    }

    public Vector> getRows() {
        return list;
    }

    public int getRowCount() {
        return rowCount;
    }

    public int getColumnCount() {
        return columnCount;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy