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.shortmatrix.impl;
025    
026    import org.ujmp.core.Matrix;
027    import org.ujmp.core.exceptions.MatrixException;
028    import org.ujmp.core.shortmatrix.stub.AbstractDenseShortMatrix2D;
029    
030    public class SimpleDenseShortMatrix2D extends AbstractDenseShortMatrix2D {
031            private static final long serialVersionUID = 4034565357457805099L;
032    
033            private short[][] values = null;
034    
035            public SimpleDenseShortMatrix2D(Matrix m) throws MatrixException {
036                    if (m instanceof SimpleDenseShortMatrix2D) {
037                            short[][] v = ((SimpleDenseShortMatrix2D) m).values;
038                            this.values = new short[v.length][v[0].length];
039                            for (int r = v.length; --r >= 0;) {
040                                    for (int c = v[0].length; --c >= 0;) {
041                                            values[r][c] = v[r][c];
042                                    }
043                            }
044                    } else {
045                            values = new short[(int) m.getRowCount()][(int) m.getColumnCount()];
046                            for (long[] c : m.allCoordinates()) {
047                                    setAsShort(m.getAsShort(c), c);
048                            }
049                    }
050            }
051    
052            public SimpleDenseShortMatrix2D(short[]... v) {
053                    this.values = v;
054            }
055    
056            public SimpleDenseShortMatrix2D(long... size) {
057                    values = new short[(int) size[ROW]][(int) size[COLUMN]];
058            }
059    
060            public long[] getSize() {
061                    return new long[] { values.length, values.length == 0 ? 0 : values[0].length };
062            }
063    
064            
065            public long getRowCount() {
066                    return values.length;
067            }
068    
069            
070            public long getColumnCount() {
071                    return values.length == 0 ? 0 : values[0].length;
072            }
073    
074            public short getShort(long row, long column) {
075                    return values[(int) row][(int) column];
076            }
077    
078            public void setShort(short value, long row, long column) {
079                    values[(int) row][(int) column] = value;
080            }
081    
082            
083            public final Matrix transpose() {
084                    short[][] result = new short[values[0].length][values.length];
085                    for (int r = result.length; --r >= 0;) {
086                            for (int c = result[0].length; --c >= 0;) {
087                                    result[r][c] = values[c][r];
088                            }
089                    }
090                    return new SimpleDenseShortMatrix2D(result);
091            }
092    
093    }