001    /*
002     * Copyright (C) 2008-2010 by Holger Arndt
003     *
004     * This file is part of the Universal Java Matrix Package (UJMP).
005     * See the NOTICE file distributed with this work for additional
006     * information regarding copyright ownership and licensing.
007     *
008     * UJMP is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU Lesser General Public License as
010     * published by the Free Software Foundation; either version 2
011     * of the License, or (at your option) any later version.
012     *
013     * UJMP is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016     * GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public
019     * License along with UJMP; if not, write to the
020     * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021     * Boston, MA  02110-1301  USA
022     */
023    
024    package org.ujmp.core.doublematrix.impl;
025    
026    import java.util.ArrayList;
027    import java.util.HashMap;
028    import java.util.List;
029    import java.util.Map;
030    
031    import org.ujmp.core.Coordinates;
032    import org.ujmp.core.Matrix;
033    import org.ujmp.core.calculation.Calculation.Ret;
034    import org.ujmp.core.doublematrix.stub.AbstractSparseDoubleMatrix2D;
035    import org.ujmp.core.exceptions.MatrixException;
036    import org.ujmp.core.interfaces.Wrapper;
037    import org.ujmp.core.objectmatrix.impl.DefaultSparseObjectMatrix;
038    
039    public class DefaultSparseRowDoubleMatrix2D extends AbstractSparseDoubleMatrix2D implements
040                    Wrapper<Map<Long, Matrix>> {
041            private static final long serialVersionUID = -5291604525500706427L;
042    
043            private long[] size = new long[] { 1, 1 };
044    
045            private Map<Long, Matrix> rows = new HashMap<Long, Matrix>();
046    
047            public DefaultSparseRowDoubleMatrix2D(long... size) {
048                    setSize(size);
049            }
050    
051            public DefaultSparseRowDoubleMatrix2D(Matrix m) {
052                    setSize(m.getSize());
053                    for (long[] c : m.availableCoordinates()) {
054                            setDouble(m.getAsDouble(c), c);
055                    }
056            }
057    
058            public double getDouble(long row, long column) throws MatrixException {
059                    Matrix m = rows.get(row);
060                    return m == null ? null : m.getAsDouble(0, column);
061            }
062    
063            public double getDouble(int row, int column) throws MatrixException {
064                    Matrix m = rows.get(row);
065                    return m == null ? null : m.getAsDouble(0, column);
066            }
067    
068            // TODO: this is certainly not the optimal way to do it!
069    
070            public Iterable<long[]> availableCoordinates() {
071                    List<long[]> coordinates = new ArrayList<long[]>();
072                    for (Long r : rows.keySet()) {
073                            Matrix m = rows.get(r);
074                            for (long[] c : m.availableCoordinates()) {
075                                    coordinates.add(Coordinates.plus(c, new long[] { r, 0 }));
076                            }
077                    }
078                    return coordinates;
079            }
080    
081            public boolean contains(long... coordinates) {
082                    if (Coordinates.isSmallerThan(coordinates, size)) {
083                            return getObject(coordinates) != null;
084                    } else {
085                            return false;
086                    }
087            }
088    
089            public void setDouble(double o, long row, long column) throws MatrixException {
090                    Matrix m = rows.get(row);
091                    if (m == null) {
092                            // TODO: there should be a faster implementation than this:
093                            m = new DefaultSparseObjectMatrix((long) 1, getColumnCount());
094                            rows.put(row, m);
095                    }
096                    m.setAsDouble(o, 0, column);
097            }
098    
099            public void setDouble(double o, int row, int column) throws MatrixException {
100                    setDouble(o, (long) row, (long) column);
101            }
102    
103            public long[] getSize() {
104                    return size;
105            }
106    
107            public void setSize(long... size) {
108                    if (this.size[COLUMN] != size[COLUMN]) {
109                            for (Matrix m : rows.values()) {
110                                    m.setSize(1, size[COLUMN]);
111                            }
112                    }
113                    this.size = size;
114            }
115    
116            public Matrix getRow(long row) {
117                    return rows.get(row);
118            }
119    
120            public Matrix selectRows(Ret returnType, long... rows) throws MatrixException {
121                    if (returnType == Ret.LINK && rows.length == 1) {
122                            return getRow(rows[0]);
123                    }
124                    return super.selectRows(returnType, rows);
125            }
126    
127            public Map<Long, Matrix> getWrappedObject() {
128                    return rows;
129            }
130    
131            public void setWrappedObject(Map<Long, Matrix> object) {
132                    this.rows = object;
133            }
134    
135    }