/*
 * #%L
 * gitools-core
 * %%
 * Copyright (C) 2013 Universitat Pompeu Fabra - Biomedical Genomics group
 * %%
 * This program 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.
 * 
 * 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-3.0.html>.
 * #L%
 */
package org.gitools.core.matrix.model.matrix;

import cern.colt.bitvector.BitMatrix;
import cern.colt.matrix.ObjectFactory1D;
import org.gitools.core.matrix.model.matrix.element.DoubleElementAdapter;
import org.jetbrains.annotations.Nullable;

public class DoubleBinaryMatrix extends BaseMatrix {

    private BitMatrix cells;
    private BitMatrix cellsNan;

    public DoubleBinaryMatrix() {
        this("", new String[0], new String[0], new BitMatrix(0, 0));
    }

    public DoubleBinaryMatrix(String title, String[] colNames, String[] rowNames, BitMatrix cells) {

        super(title, ObjectFactory1D.dense.make(rowNames), ObjectFactory1D.dense.make(colNames), new DoubleElementAdapter());

        this.cells = cells;
    }

    protected int internalColumnCount() {
        return cells.columns();
    }

    protected int internalRowCount() {
        return cells.rows();
    }

    public boolean isEmpty(int row, int column) {
        return getCell(row, column) == null;
    }

    public Object getCell(int row, int column) {
        if (cellsNan.getQuick(column, row)) {
            return Double.NaN;
        } else {
            return cells.getQuick(column, row) ? 1.0 : 0.0;
        }
    }

    @Override
    public Object getValue(int[] position, int layer) {
        return getCell(position[0], position[1]);
    }

    @Override
    public void setValue(int[] position, int layer, @Nullable Object value) {
        if (value != null) {
            cells.putQuick(position[1], position[0], ((Double) value) == 1.0);
            cellsNan.putQuick(position[1], position[0], Double.isNaN((Double) value));
        } else {
            cellsNan.putQuick(position[1], position[0], true);
        }
    }

    @Override
    public void makeCells(int rows, int columns) {
        cells = new BitMatrix(columns, rows);
        cells.clear();

        cellsNan = new BitMatrix(columns, rows);
        cellsNan.clear();

        if (this.rows == null || this.rows.cardinality() != rows) {
            setRows(ObjectFactory1D.dense.make(rows));
        }

        if (this.columns == null || this.columns.cardinality() != columns) {
            setColumns(ObjectFactory1D.dense.make(columns));
        }
    }
}
