cern.colt.matrix.tint.impl.SelectedSparseIntMatrix3D Maven / Gradle / Ivy
Show all versions of parallelcolt Show documentation
/*
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.tint.impl;
import cern.colt.map.tlong.AbstractLongIntMap;
import cern.colt.matrix.AbstractMatrix3D;
import cern.colt.matrix.tint.IntMatrix1D;
import cern.colt.matrix.tint.IntMatrix2D;
import cern.colt.matrix.tint.IntMatrix3D;
/**
* Selection view on sparse 3-d matrices holding int elements. First
* see the package summary and javadoc tree view to get the broad picture.
*
* Implementation:
*
* Objects of this class are typically constructed via viewIndexes
* methods on some source matrix. The interface introduced in abstract super
* classes defines everything a user can do. From a user point of view there is
* nothing special about this class; it presents the same functionality with the
* same signatures and semantics as its abstract superclass(es) while
* introducing no additional functionality. Thus, this class need not be visible
* to users. By the way, the same principle applies to concrete DenseXXX and
* SparseXXX classes: they presents the same functionality with the same
* signatures and semantics as abstract superclass(es) while introducing no
* additional functionality. Thus, they need not be visible to users, either.
* Factory methods could hide all these concrete types.
*
* This class uses no delegation. Its instances point directly to the data. Cell
* addressing overhead is is 1 additional int addition and 3 additional array
* index accesses per get/set.
*
* Note that this implementation is not synchronized.
*
* Memory requirements:
*
* memory [bytes] = 4*(sliceIndexes.length+rowIndexes.length+columnIndexes.length)
* . Thus, an index view with 100 x 100 x 100 indexes additionally uses 8 KB.
*
* Time complexity:
*
* Depends on the parent view holding cells.
*
*
* @author [email protected]
* @version 1.0, 09/24/99
*/
class SelectedSparseIntMatrix3D extends IntMatrix3D {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* The elements of this matrix.
*/
protected AbstractLongIntMap elements;
/**
* The offsets of the visible cells of this matrix.
*/
protected int[] sliceOffsets;
protected int[] rowOffsets;
protected int[] columnOffsets;
/**
* The offset.
*/
protected int offset;
/**
* Constructs a matrix view with the given parameters.
*
* @param elements
* the cells.
* @param sliceOffsets
* The slice offsets of the cells that shall be visible.
* @param rowOffsets
* The row offsets of the cells that shall be visible.
* @param columnOffsets
* The column offsets of the cells that shall be visible.
*/
protected SelectedSparseIntMatrix3D(AbstractLongIntMap elements, int[] sliceOffsets, int[] rowOffsets,
int[] columnOffsets, int offset) {
// be sure parameters are valid, we do not check...
int slices = sliceOffsets.length;
int rows = rowOffsets.length;
int columns = columnOffsets.length;
setUp(slices, rows, columns);
this.elements = elements;
this.sliceOffsets = sliceOffsets;
this.rowOffsets = rowOffsets;
this.columnOffsets = columnOffsets;
this.offset = offset;
this.isNoView = false;
}
public AbstractLongIntMap elements() {
return elements;
}
/**
* Returns the matrix cell value at coordinate [slice,row,column].
*
*
* Provided with invalid parameters this method may return invalid objects
* without throwing any exception. You should only use this method when
* you are absolutely sure that the coordinate is within bounds.
* Precondition (unchecked):
* slice<0 || slice>=slices() || row<0 || row>=rows() || column<0 || column>=column().
*
* @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 int 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.get(index(slice,row,column));
// manually inlined:
return elements.get((long) offset + (long) sliceOffsets[sliceZero + slice * sliceStride]
+ (long) rowOffsets[rowZero + row * rowStride]
+ (long) columnOffsets[columnZero + column * columnStride]);
}
/**
* 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.
*/
public long index(int slice, int row, int column) {
// return this.offset + super.index(slice,row,column);
// manually inlined:
return (long) this.offset + (long) sliceOffsets[sliceZero + slice * sliceStride]
+ (long) rowOffsets[rowZero + row * rowStride]
+ (long) columnOffsets[columnZero + column * columnStride];
}
/**
* Construct and returns a new empty matrix of the same dynamic type
* as the receiver, having the specified number of slices, rows and columns.
* For example, if the receiver is an instance of type
* DenseIntMatrix3D the new matrix must also be of type
* DenseIntMatrix3D, if the receiver is an instance of type
* SparseIntMatrix3D the new matrix must also be of type
* SparseIntMatrix3D, 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 IntMatrix3D like(int slices, int rows, int columns) {
return new SparseIntMatrix3D(slices, rows, columns);
}
public IntMatrix2D like2D(int rows, int columns) {
return new SparseIntMatrix2D(rows, columns);
}
/**
* Sets the matrix cell at coordinate [slice,row,column] to the
* specified value.
*
*
* Provided with invalid parameters this method may access illegal indexes
* without throwing any exception. You should only use this method when
* you are absolutely sure that the coordinate is within bounds.
* Precondition (unchecked):
* slice<0 || slice>=slices() || row<0 || row>=rows() || column<0 || column>=column().
*
* @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, int 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);
// int index = index(slice,row,column);
// manually inlined:
long index = (long) offset + (long) sliceOffsets[sliceZero + slice * sliceStride]
+ (long) rowOffsets[rowZero + row * rowStride]
+ (long) columnOffsets[columnZero + column * columnStride];
if (value == 0)
this.elements.removeKey(index);
else
this.elements.put(index, value);
}
/**
* Returns a vector obtained by stacking the columns of each slice of the
* matrix on top of one another.
*
* @return
*/
public IntMatrix1D vectorize() {
throw new IllegalArgumentException("This method is not supported.");
}
/**
* Constructs and returns a new 2-dimensional slice view representing
* the slices and rows of the given column. The returned view is backed by
* this matrix, so changes in the returned view are reflected in this
* matrix, and vice-versa.
*
* To obtain a slice view on subranges, construct a sub-ranging view (
* view().part(...)), then apply this method to the sub-range view.
* To obtain 1-dimensional views, apply this method, then apply another
* slice view (methods viewColumn, viewRow) on the
* intermediate 2-dimensional view. To obtain 1-dimensional views on
* subranges, apply both steps.
*
* @param column
* the index of the column to fix.
* @return a new 2-dimensional slice view.
* @throws IndexOutOfBoundsException
* if column < 0 || column >= columns().
* @see #viewSlice(int)
* @see #viewRow(int)
*/
public IntMatrix2D viewColumn(int column) {
checkColumn(column);
int viewRows = this.slices;
int viewColumns = this.rows;
int viewRowZero = sliceZero;
int viewColumnZero = rowZero;
int viewOffset = this.offset + _columnOffset(_columnRank(column));
int viewRowStride = this.sliceStride;
int viewColumnStride = this.rowStride;
int[] viewRowOffsets = this.sliceOffsets;
int[] viewColumnOffsets = this.rowOffsets;
return new SelectedSparseIntMatrix2D(viewRows, viewColumns, this.elements, viewRowZero, viewColumnZero,
viewRowStride, viewColumnStride, viewRowOffsets, viewColumnOffsets, viewOffset);
}
/**
* Constructs and returns a new 2-dimensional slice view representing
* the slices and columns of the given row. The returned view is backed by
* this matrix, so changes in the returned view are reflected in this
* matrix, and vice-versa.
*
* To obtain a slice view on subranges, construct a sub-ranging view (
* view().part(...)), then apply this method to the sub-range view.
* To obtain 1-dimensional views, apply this method, then apply another
* slice view (methods viewColumn, viewRow) on the
* intermediate 2-dimensional view. To obtain 1-dimensional views on
* subranges, apply both steps.
*
* @param row
* the index of the row to fix.
* @return a new 2-dimensional slice view.
* @throws IndexOutOfBoundsException
* if row < 0 || row >= row().
* @see #viewSlice(int)
* @see #viewColumn(int)
*/
public IntMatrix2D viewRow(int row) {
checkRow(row);
int viewRows = this.slices;
int viewColumns = this.columns;
int viewRowZero = sliceZero;
int viewColumnZero = columnZero;
int viewOffset = this.offset + _rowOffset(_rowRank(row));
int viewRowStride = this.sliceStride;
int viewColumnStride = this.columnStride;
int[] viewRowOffsets = this.sliceOffsets;
int[] viewColumnOffsets = this.columnOffsets;
return new SelectedSparseIntMatrix2D(viewRows, viewColumns, this.elements, viewRowZero, viewColumnZero,
viewRowStride, viewColumnStride, viewRowOffsets, viewColumnOffsets, viewOffset);
}
/**
* Constructs and returns a new 2-dimensional slice view representing
* the rows and columns of the given slice. The returned view is backed by
* this matrix, so changes in the returned view are reflected in this
* matrix, and vice-versa.
*
* To obtain a slice view on subranges, construct a sub-ranging view (
* view().part(...)), then apply this method to the sub-range view.
* To obtain 1-dimensional views, apply this method, then apply another
* slice view (methods viewColumn, viewRow) on the
* intermediate 2-dimensional view. To obtain 1-dimensional views on
* subranges, apply both steps.
*
* @param slice
* the index of the slice to fix.
* @return a new 2-dimensional slice view.
* @throws IndexOutOfBoundsException
* if slice < 0 || slice >= slices().
* @see #viewRow(int)
* @see #viewColumn(int)
*/
public IntMatrix2D viewSlice(int slice) {
checkSlice(slice);
int viewRows = this.rows;
int viewColumns = this.columns;
int viewRowZero = rowZero;
int viewColumnZero = columnZero;
int viewOffset = this.offset + _sliceOffset(_sliceRank(slice));
int viewRowStride = this.rowStride;
int viewColumnStride = this.columnStride;
int[] viewRowOffsets = this.rowOffsets;
int[] viewColumnOffsets = this.columnOffsets;
return new SelectedSparseIntMatrix2D(viewRows, viewColumns, this.elements, viewRowZero, viewColumnZero,
viewRowStride, viewColumnStride, viewRowOffsets, viewColumnOffsets, viewOffset);
}
/**
* Returns the position of the given absolute rank within the (virtual or
* non-virtual) internal 1-dimensional array. Default implementation.
* Override, if necessary.
*
* @param rank
* the absolute rank of the element.
* @return the position.
*/
protected int _columnOffset(int absRank) {
return columnOffsets[absRank];
}
/**
* Returns the position of the given absolute rank within the (virtual or
* non-virtual) internal 1-dimensional array. Default implementation.
* Override, if necessary.
*
* @param rank
* the absolute rank of the element.
* @return the position.
*/
protected int _rowOffset(int absRank) {
return rowOffsets[absRank];
}
/**
* Returns the position of the given absolute rank within the (virtual or
* non-virtual) internal 1-dimensional array. Default implementation.
* Override, if necessary.
*
* @param rank
* the absolute rank of the element.
* @return the position.
*/
protected int _sliceOffset(int absRank) {
return sliceOffsets[absRank];
}
/**
* Returns true if both matrices share common cells. More formally,
* returns true if other != null and at least one of the
* following conditions is met
*
* - the receiver is a view of the other matrix
*
- the other matrix is a view of the receiver
*
- this == other
*
*/
protected boolean haveSharedCellsRaw(IntMatrix3D other) {
if (other instanceof SelectedSparseIntMatrix3D) {
SelectedSparseIntMatrix3D otherMatrix = (SelectedSparseIntMatrix3D) other;
return this.elements == otherMatrix.elements;
} else if (other instanceof SparseIntMatrix3D) {
SparseIntMatrix3D otherMatrix = (SparseIntMatrix3D) other;
return this.elements == otherMatrix.elements;
}
return false;
}
/**
* Construct and returns a new 2-d matrix of the corresponding dynamic
* type, sharing the same cells. For example, if the receiver is an
* instance of type DenseIntMatrix3D the new matrix must also be of
* type DenseIntMatrix2D, if the receiver is an instance of type
* SparseIntMatrix3D the new matrix must also be of type
* SparseIntMatrix2D, 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.
* index(i+1,j)-index(i,j).
* @param columnStride
* the number of elements between two columns, i.e.
* index(i,j+1)-index(i,j).
* @return a new matrix of the corresponding dynamic type.
*/
protected IntMatrix2D like2D(int rows, int columns, int rowZero, int columnZero, int rowStride, int columnStride) {
throw new InternalError(); // this method is never called since
// viewRow() and viewColumn are overridden
// properly.
}
/**
* Sets up a matrix with a given number of slices and rows.
*
* @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 (int)rows*slices > Integer.MAX_VALUE.
*/
protected void setUp(int slices, int rows, int columns) {
super.setUp(slices, rows, columns);
this.sliceStride = 1;
this.rowStride = 1;
this.columnStride = 1;
this.offset = 0;
}
/**
* Self modifying version of viewDice().
*
* @throws IllegalArgumentException
* if some of the parameters are equal or not in range 0..2.
*/
protected AbstractMatrix3D vDice(int axis0, int axis1, int axis2) {
super.vDice(axis0, axis1, axis2);
// swap offsets
int[][] offsets = new int[3][];
offsets[0] = this.sliceOffsets;
offsets[1] = this.rowOffsets;
offsets[2] = this.columnOffsets;
this.sliceOffsets = offsets[axis0];
this.rowOffsets = offsets[axis1];
this.columnOffsets = offsets[axis2];
return this;
}
/**
* 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 IntMatrix3D viewSelectionLike(int[] sliceOffsets, int[] rowOffsets, int[] columnOffsets) {
return new SelectedSparseIntMatrix3D(this.elements, sliceOffsets, rowOffsets, columnOffsets, this.offset);
}
}