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.bytematrix.impl; 025 026 import org.ujmp.core.Matrix; 027 import org.ujmp.core.bytematrix.stub.AbstractDenseByteMatrix2D; 028 import org.ujmp.core.exceptions.MatrixException; 029 030 public class ArrayDenseByteMatrix2D extends AbstractDenseByteMatrix2D { 031 private static final long serialVersionUID = 1111734188254187991L; 032 033 private byte[][] values = null; 034 035 public ArrayDenseByteMatrix2D(Matrix m) throws MatrixException { 036 if (m instanceof ArrayDenseByteMatrix2D) { 037 byte[][] v = ((ArrayDenseByteMatrix2D) m).values; 038 this.values = new byte[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 byte[(int) m.getRowCount()][(int) m.getColumnCount()]; 046 for (long[] c : m.allCoordinates()) { 047 setAsByte(m.getAsByte(c), c); 048 } 049 } 050 } 051 052 public ArrayDenseByteMatrix2D(byte[]... v) { 053 this.values = v; 054 } 055 056 public ArrayDenseByteMatrix2D(long... size) { 057 values = new byte[(int) size[ROW]][(int) size[COLUMN]]; 058 } 059 060 public ArrayDenseByteMatrix2D(byte[] v) { 061 this.values = new byte[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 byte getByte(long row, long column) { 082 return values[(int) row][(int) column]; 083 } 084 085 public void setByte(byte value, long row, long column) { 086 values[(int) row][(int) column] = value; 087 } 088 089 public byte getByte(int row, int column) { 090 return values[row][column]; 091 } 092 093 public void setByte(byte value, int row, int column) { 094 values[row][column] = value; 095 } 096 097 098 public final Matrix transpose() { 099 byte[][] result = new byte[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 ArrayDenseByteMatrix2D(result); 106 } 107 108 }