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.objectmatrix.impl; 025 026 import org.ujmp.core.Matrix; 027 import org.ujmp.core.exceptions.MatrixException; 028 import org.ujmp.core.interfaces.HasObjectArray; 029 import org.ujmp.core.objectmatrix.stub.AbstractDenseObjectMatrix2D; 030 031 public class DefaultDenseObjectMatrix2D extends AbstractDenseObjectMatrix2D implements 032 HasObjectArray { 033 private static final long serialVersionUID = 8929799323811301397L; 034 035 private Object[] values = null; 036 037 private long[] size = null; 038 039 private int rows = 0; 040 041 private int cols = 0; 042 043 public DefaultDenseObjectMatrix2D(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 DefaultDenseObjectMatrix2D) { 048 Object[] v = ((DefaultDenseObjectMatrix2D) m).values; 049 this.values = new Object[v.length]; 050 System.arraycopy(v, 0, this.values, 0, v.length); 051 } else { 052 this.values = new Object[rows * cols]; 053 for (long[] c : m.allCoordinates()) { 054 setObject(m.getAsObject(c), c); 055 } 056 } 057 } 058 059 public DefaultDenseObjectMatrix2D(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 Object[rows * cols]; 064 } 065 066 public DefaultDenseObjectMatrix2D(Object[] 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 public long getRowCount() { 078 return rows; 079 } 080 081 public long getColumnCount() { 082 return cols; 083 } 084 085 public Object getObject(long row, long column) { 086 return values[(int) (column * rows + row)]; 087 } 088 089 public void setObject(Object value, long row, long column) { 090 values[(int) (column * rows + row)] = value; 091 } 092 093 public Object getObject(int row, int column) { 094 return values[column * rows + row]; 095 } 096 097 public void setObject(Object value, int row, int column) { 098 values[column * rows + row] = value; 099 } 100 101 public final Matrix copy() throws MatrixException { 102 Object[] result = new Object[values.length]; 103 System.arraycopy(values, 0, result, 0, values.length); 104 Matrix m = new DefaultDenseObjectMatrix2D(result, rows, cols); 105 if (getAnnotation() != null) { 106 m.setAnnotation(getAnnotation().clone()); 107 } 108 return m; 109 } 110 111 public final Matrix transpose() { 112 final Object[] result = new Object[cols * rows]; 113 for (int c = rows; --c != -1;) { 114 for (int r = cols; --r != -1;) { 115 result[c * cols + r] = values[r * rows + c]; 116 } 117 } 118 return new DefaultDenseObjectMatrix2D(result, cols, rows); 119 } 120 121 public Object[] getObjectArray() { 122 return values; 123 } 124 125 }