1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
/*
Copyright (c) 1999 CERN - European Organization for Nuclear Research.
Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose
is hereby granted without fee, provided that the above copyright notice appear in all copies and
that both that copyright notice and this permission notice appear in supporting documentation.
CERN makes no representations about the suitability of this software for any purpose.
It is provided "as is" without expressed or implied warranty.
*/
package cern.colt.matrix.impl;
import cern.colt.matrix.ObjectMatrix2D;
import cern.colt.matrix.ObjectMatrix3D;
/**
Dense 3-d matrix holding <tt>Object</tt> elements.
First see the <a href="package-summary.html">package summary</a> and javadoc <a href="package-tree.html">tree view</a> to get the broad picture.
<p>
<b>Implementation:</b>
<p>
Internally holds one single contigous one-dimensional array, addressed in (in decreasing order of significance): slice major, row major, column major.
Note that this implementation is not synchronized.
<p>
<b>Memory requirements:</b>
<p>
<tt>memory [bytes] = 8*slices()*rows()*columns()</tt>.
Thus, a 100*100*100 matrix uses 8 MB.
<p>
<b>Time complexity:</b>
<p>
<tt>O(1)</tt> (i.e. constant time) for the basic operations
<tt>get</tt>, <tt>getQuick</tt>, <tt>set</tt>, <tt>setQuick</tt> and <tt>size</tt>,
<p>
Applications demanding utmost speed can exploit knowledge about the internal addressing.
Setting/getting values in a loop slice-by-slice, row-by-row, column-by-column is quicker than, for example, column-by-column, row-by-row, slice-by-slice.
Thus
<pre>
for (int slice=0; slice < slices; slice++) {
for (int row=0; row < rows; row++) {
for (int column=0; column < columns; column++) {
matrix.setQuick(slice,row,column,someValue);
}
}
}
</pre>
is quicker than
<pre>
for (int column=0; column < columns; column++) {
for (int row=0; row < rows; row++) {
for (int slice=0; slice < slices; slice++) {
matrix.setQuick(slice,row,column,someValue);
}
}
}
</pre>
@author wolfgang.hoschek@cern.ch
@version 1.0, 09/24/99
*/
public class DenseObjectMatrix3D extends ObjectMatrix3D {
/**
* The elements of this matrix.
* elements are stored in slice major, then row major, then column major, in order of significance, i.e.
* index==slice*sliceStride+ row*rowStride + column*columnStride
* i.e. {slice0 row0..m}, {slice1 row0..m}, ..., {sliceN row0..m}
* with each row storead as
* {row0 column0..m}, {row1 column0..m}, ..., {rown column0..m}
*/
protected Object[] elements;
/**
* Constructs a matrix with a copy of the given values.
* <tt>values</tt> is required to have the form <tt>values[slice][row][column]</tt>
* and have exactly the same number of rows in in every slice and exactly the same number of columns in in every row.
* <p>
* The values are copied. So subsequent changes in <tt>values</tt> are not reflected in the matrix, and vice-versa.
*
* @param values The values to be filled into the new matrix.
* @throws IllegalArgumentException if <tt>for any 1 <= slice < values.length: values[slice].length != values[slice-1].length</tt>.
* @throws IllegalArgumentException if <tt>for any 1 <= row < values[0].length: values[slice][row].length != values[slice][row-1].length</tt>.
*/
public DenseObjectMatrix3D(Object[][][] values) {
this(values.length, (values.length==0 ? 0: values[0].length), (values.length==0 ? 0: values[0].length==0 ? 0 : values[0][0].length));
assign(values);
}
/**
* Constructs a matrix with a given number of slices, rows and columns.
* All entries are initially <tt>0</tt>.
* @param slices the number of slices the matrix shall have.
* @param rows the number of rows the matrix shall have.
* @param columns the number of columns the matrix shall have.
* @throws IllegalArgumentException if <tt>(Object)slices*columns*rows > Integer.MAX_VALUE</tt>.
* @throws IllegalArgumentException if <tt>slices<0 || rows<0 || columns<0</tt>.
*/
public DenseObjectMatrix3D(int slices, int rows, int columns) {
setUp(slices,rows, columns);
this.elements = new Object[slices*rows*columns];
}
/**
* Constructs a view with the given parameters.
* @param slices the number of slices the matrix shall have.
* @param rows the number of rows the matrix shall have.
* @param columns the number of columns the matrix shall have.
* @param elements the cells.
* @param sliceZero the position of the first element.
* @param rowZero the position of the first element.
* @param columnZero the position of the first element.
* @param sliceStride the number of elements between two slices, i.e. <tt>index(k+1,i,j)-index(k,i,j)</tt>.
* @param rowStride the number of elements between two rows, i.e. <tt>index(k,i+1,j)-index(k,i,j)</tt>.
* @param columnnStride the number of elements between two columns, i.e. <tt>index(k,i,j+1)-index(k,i,j)</tt>.
* @throws IllegalArgumentException if <tt>(Object)slices*columns*rows > Integer.MAX_VALUE</tt>.
* @throws IllegalArgumentException if <tt>slices<0 || rows<0 || columns<0</tt>.
*/
protected DenseObjectMatrix3D(int slices, int rows, int columns, Object[] elements, int sliceZero, int rowZero, int columnZero, int sliceStride, int rowStride, int columnStride) {
setUp(slices,rows,columns,sliceZero,rowZero,columnZero,sliceStride,rowStride,columnStride);
this.elements = elements;
this.isNoView = false;
}
/**
* Sets all cells to the state specified by <tt>values</tt>.
* <tt>values</tt> is required to have the form <tt>values[slice][row][column]</tt>
* and have exactly the same number of slices, rows and columns as the receiver.
* <p>
* The values are copied. So subsequent changes in <tt>values</tt> are not reflected in the matrix, and vice-versa.
*
* @param values the values to be filled into the cells.
* @return <tt>this</tt> (for convenience only).
* @throws IllegalArgumentException if <tt>values.length != slices() || for any 0 <= slice < slices(): values[slice].length != rows()</tt>.
* @throws IllegalArgumentException if <tt>for any 0 <= column < columns(): values[slice][row].length != columns()</tt>.
*/
public ObjectMatrix3D assign(Object[][][] values) {
if (this.isNoView) {
if (values.length != slices) throw new IllegalArgumentException("Must have same number of slices: slices="+values.length+"slices()="+slices());
int i=slices*rows*columns - columns;
for (int slice=slices; --slice >= 0;) {
Object[][] currentSlice = values[slice];
if (currentSlice.length != rows) throw new IllegalArgumentException("Must have same number of rows in every slice: rows="+currentSlice.length+"rows()="+rows());
for (int row=rows; --row >= 0;) {
Object[] currentRow = currentSlice[row];
if (currentRow.length != columns) throw new IllegalArgumentException("Must have same number of columns in every row: columns="+currentRow.length+"columns()="+columns());
System.arraycopy(currentRow, 0, this.elements, i, columns);
i -= columns;
}
}
}
else {
super.assign(values);
}
return this;
}
/**
* Replaces all cell values of the receiver with the values of another matrix.
* Both matrices must have the same number of slices, rows and columns.
* If both matrices share the same cells (as is the case if they are views derived from the same matrix) and intersect in an ambiguous way, then replaces <i>as if</i> using an intermediate auxiliary deep copy of <tt>other</tt>.
*
* @param source the source matrix to copy from (may be identical to the receiver).
* @return <tt>this</tt> (for convenience only).
* @throws IllegalArgumentException if <tt>slices() != source.slices() || rows() != source.rows() || columns() != source.columns()</tt>
*/
public ObjectMatrix3D assign(ObjectMatrix3D source) {
// overriden for performance only
if (! (source instanceof DenseObjectMatrix3D)) {
return super.assign(source);
}
DenseObjectMatrix3D other = (DenseObjectMatrix3D) source;
if (other==this) return this;
checkShape(other);
if (haveSharedCells(other)) {
ObjectMatrix3D c = other.copy();
if (! (c instanceof DenseObjectMatrix3D)) { // should not happen
return super.assign(source);
}
other = (DenseObjectMatrix3D) c;
}
if (this.isNoView && other.isNoView) { // quickest
System.arraycopy(other.elements, 0, this.elements, 0, this.elements.length);
return this;
}
return super.assign(other);
}
/**
* Returns the matrix cell value at coordinate <tt>[slice,row,column]</tt>.
*
* <p>Provided with invalid parameters this method may return invalid objects without throwing any exception.
* <b>You should only use this method when you are absolutely sure that the coordinate is within bounds.</b>
* Precondition (unchecked): <tt>slice<0 || slice>=slices() || row<0 || row>=rows() || column<0 || column>=column()</tt>.
*
* @param slice the index of the slice-coordinate.
* @param row the index of the row-coordinate.
* @param column the index of the column-coordinate.
* @return the value at the specified coordinate.
*/
public Object getQuick(int slice, int row, int column) {
//if (debug) if (slice<0 || slice>=slices || row<0 || row>=rows || column<0 || column>=columns) throw new IndexOutOfBoundsException("slice:"+slice+", row:"+row+", column:"+column);
//return elements[index(slice,row,column)];
//manually inlined:
return elements[sliceZero + slice*sliceStride + rowZero + row*rowStride + columnZero + column*columnStride];
}
/**
* Returns <tt>true</tt> if both matrices share common cells.
* More formally, returns <tt>true</tt> if <tt>other != null</tt> and at least one of the following conditions is met
* <ul>
* <li>the receiver is a view of the other matrix
* <li>the other matrix is a view of the receiver
* <li><tt>this == other</tt>
* </ul>
*/
protected boolean haveSharedCellsRaw(ObjectMatrix3D other) {
if (other instanceof SelectedDenseObjectMatrix3D) {
SelectedDenseObjectMatrix3D otherMatrix = (SelectedDenseObjectMatrix3D) other;
return this.elements==otherMatrix.elements;
}
else if (other instanceof DenseObjectMatrix3D) {
DenseObjectMatrix3D otherMatrix = (DenseObjectMatrix3D) other;
return this.elements==otherMatrix.elements;
}
return false;
}
/**
* Returns the position of the given coordinate within the (virtual or non-virtual) internal 1-dimensional array.
*
* @param slice the index of the slice-coordinate.
* @param row the index of the row-coordinate.
* @param column the index of the third-coordinate.
*/
protected int index(int slice, int row, int column) {
//return _sliceOffset(_sliceRank(slice)) + _rowOffset(_rowRank(row)) + _columnOffset(_columnRank(column));
//manually inlined:
return sliceZero + slice*sliceStride + rowZero + row*rowStride + columnZero + column*columnStride;
}
/**
* Construct and returns a new empty matrix <i>of the same dynamic type</i> as the receiver, having the specified number of slices, rows and columns.
* For example, if the receiver is an instance of type <tt>DenseObjectMatrix3D</tt> the new matrix must also be of type <tt>DenseObjectMatrix3D</tt>,
* if the receiver is an instance of type <tt>SparseObjectMatrix3D</tt> the new matrix must also be of type <tt>SparseObjectMatrix3D</tt>, etc.
* In general, the new matrix should have internal parametrization as similar as possible.
*
* @param slices the number of slices the matrix shall have.
* @param rows the number of rows the matrix shall have.
* @param columns the number of columns the matrix shall have.
* @return a new empty matrix of the same dynamic type.
*/
public ObjectMatrix3D like(int slices, int rows, int columns) {
return new DenseObjectMatrix3D(slices,rows,columns);
}
/**
* Construct and returns a new 2-d matrix <i>of the corresponding dynamic type</i>, sharing the same cells.
* For example, if the receiver is an instance of type <tt>DenseObjectMatrix3D</tt> the new matrix must also be of type <tt>DenseObjectMatrix2D</tt>,
* if the receiver is an instance of type <tt>SparseObjectMatrix3D</tt> the new matrix must also be of type <tt>SparseObjectMatrix2D</tt>, etc.
*
* @param rows the number of rows the matrix shall have.
* @param columns the number of columns the matrix shall have.
* @param rowZero the position of the first element.
* @param columnZero the position of the first element.
* @param rowStride the number of elements between two rows, i.e. <tt>index(i+1,j)-index(i,j)</tt>.
* @param columnStride the number of elements between two columns, i.e. <tt>index(i,j+1)-index(i,j)</tt>.
* @return a new matrix of the corresponding dynamic type.
*/
protected ObjectMatrix2D like2D(int rows, int columns, int rowZero, int columnZero, int rowStride, int columnStride) {
return new DenseObjectMatrix2D(rows,columns,this.elements,rowZero,columnZero,rowStride,columnStride);
}
/**
* Sets the matrix cell at coordinate <tt>[slice,row,column]</tt> to the specified value.
*
* <p>Provided with invalid parameters this method may access illegal indexes without throwing any exception.
* <b>You should only use this method when you are absolutely sure that the coordinate is within bounds.</b>
* Precondition (unchecked): <tt>slice<0 || slice>=slices() || row<0 || row>=rows() || column<0 || column>=column()</tt>.
*
* @param slice the index of the slice-coordinate.
* @param row the index of the row-coordinate.
* @param column the index of the column-coordinate.
* @param value the value to be filled into the specified cell.
*/
public void setQuick(int slice, int row, int column, Object value) {
//if (debug) if (slice<0 || slice>=slices || row<0 || row>=rows || column<0 || column>=columns) throw new IndexOutOfBoundsException("slice:"+slice+", row:"+row+", column:"+column);
//elements[index(slice,row,column)] = value;
//manually inlined:
elements[sliceZero + slice*sliceStride + rowZero + row*rowStride + columnZero + column*columnStride] = value;
}
/**
* Construct and returns a new selection view.
*
* @param sliceOffsets the offsets of the visible elements.
* @param rowOffsets the offsets of the visible elements.
* @param columnOffsets the offsets of the visible elements.
* @return a new view.
*/
protected ObjectMatrix3D viewSelectionLike(int[] sliceOffsets, int[] rowOffsets, int[] columnOffsets) {
return new SelectedDenseObjectMatrix3D(this.elements,sliceOffsets,rowOffsets,columnOffsets,0);
}
}
|