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.charmatrix.impl;
025    
026    import org.ujmp.core.Matrix;
027    import org.ujmp.core.charmatrix.stub.AbstractDenseCharMatrix2D;
028    import org.ujmp.core.exceptions.MatrixException;
029    
030    public class ArrayDenseCharMatrix2D extends AbstractDenseCharMatrix2D {
031            private static final long serialVersionUID = -172129670809500830L;
032    
033            private char[][] values = null;
034    
035            public ArrayDenseCharMatrix2D(Matrix m) throws MatrixException {
036                    if (m instanceof ArrayDenseCharMatrix2D) {
037                            char[][] v = ((ArrayDenseCharMatrix2D) m).values;
038                            this.values = new char[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 char[(int) m.getRowCount()][(int) m.getColumnCount()];
046                            for (long[] c : m.allCoordinates()) {
047                                    setAsChar(m.getAsChar(c), c);
048                            }
049                    }
050            }
051    
052            public ArrayDenseCharMatrix2D(char[]... v) {
053                    this.values = v;
054            }
055    
056            public ArrayDenseCharMatrix2D(long... size) {
057                    values = new char[(int) size[ROW]][(int) size[COLUMN]];
058            }
059    
060            public ArrayDenseCharMatrix2D(char[] v) {
061                    this.values = new char[v.length][1];
062                    for (int r = v.length; --r >= 0;) {
063                            values[r][0] = v[r];
064                    }
065            }
066    
067            public long[] getSize() {
068                    return new long[] { values.length, values.length == 0 ? 0 : values[0].length };
069            }
070    
071            
072            public long getRowCount() {
073                    return values.length;
074            }
075    
076            
077            public long getColumnCount() {
078                    return values.length == 0 ? 0 : values[0].length;
079            }
080    
081            public char getChar(long row, long column) {
082                    return values[(int) row][(int) column];
083            }
084    
085            public void setChar(char value, long row, long column) {
086                    values[(int) row][(int) column] = value;
087            }
088    
089            public char getChar(int row, int column) {
090                    return values[row][column];
091            }
092    
093            public void setChar(char value, int row, int column) {
094                    values[row][column] = value;
095            }
096    
097            
098            public final Matrix transpose() {
099                    char[][] result = new char[values[0].length][values.length];
100                    for (int r = result.length; --r >= 0;) {
101                            for (int c = result[0].length; --c >= 0;) {
102                                    result[r][c] = values[c][r];
103                            }
104                    }
105                    return new ArrayDenseCharMatrix2D(result);
106            }
107    
108    }