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.stringmatrix.impl; 025 026 import org.ujmp.core.Matrix; 027 import org.ujmp.core.exceptions.MatrixException; 028 import org.ujmp.core.interfaces.HasStringArray; 029 import org.ujmp.core.stringmatrix.stub.AbstractDenseStringMatrix2D; 030 031 public class DefaultDenseStringMatrix2D extends AbstractDenseStringMatrix2D implements 032 HasStringArray { 033 private static final long serialVersionUID = 1643931435178952984L; 034 035 private String[] values = null; 036 037 private long[] size = null; 038 039 private int rows = 0; 040 041 private int cols = 0; 042 043 public DefaultDenseStringMatrix2D(Matrix m) throws MatrixException { 044 this.rows = (int) m.getRowCount(); 045 this.cols = (int) m.getColumnCount(); 046 this.size = new long[] { rows, cols }; 047 if (m instanceof DefaultDenseStringMatrix2D) { 048 String[] v = ((DefaultDenseStringMatrix2D) m).values; 049 this.values = new String[v.length]; 050 System.arraycopy(v, 0, this.values, 0, v.length); 051 } else { 052 this.values = new String[rows * cols]; 053 for (long[] c : m.allCoordinates()) { 054 setString(m.getAsString(c), c); 055 } 056 } 057 } 058 059 public DefaultDenseStringMatrix2D(long... size) { 060 this.rows = (int) size[ROW]; 061 this.cols = (int) size[COLUMN]; 062 this.size = new long[] { rows, cols }; 063 this.values = new String[rows * cols]; 064 } 065 066 public DefaultDenseStringMatrix2D(String[] v, int rows, int cols) { 067 this.rows = rows; 068 this.cols = cols; 069 this.size = new long[] { rows, cols }; 070 this.values = v; 071 } 072 073 public long[] getSize() { 074 return size; 075 } 076 077 078 public long getRowCount() { 079 return rows; 080 } 081 082 083 public long getColumnCount() { 084 return cols; 085 } 086 087 public String getString(long row, long column) { 088 return values[(int) (column * rows + row)]; 089 } 090 091 public void setString(String value, long row, long column) { 092 values[(int) (column * rows + row)] = value; 093 } 094 095 public String getString(int row, int column) { 096 return values[column * rows + row]; 097 } 098 099 public void setString(String value, int row, int column) { 100 values[column * rows + row] = value; 101 } 102 103 104 public final Matrix copy() throws MatrixException { 105 String[] result = new String[values.length]; 106 System.arraycopy(values, 0, result, 0, values.length); 107 Matrix m = new DefaultDenseStringMatrix2D(result, rows, cols); 108 if (getAnnotation() != null) { 109 m.setAnnotation(getAnnotation().clone()); 110 } 111 return m; 112 } 113 114 115 public final Matrix transpose() { 116 final String[] result = new String[cols * rows]; 117 for (int c = rows; --c != -1;) { 118 for (int r = cols; --r != -1;) { 119 result[c * cols + r] = values[r * rows + c]; 120 } 121 } 122 return new DefaultDenseStringMatrix2D(result, cols, rows); 123 } 124 125 126 public String[] getStringArray() { 127 return values; 128 } 129 130 }