cern.colt.matrix.tint.impl.SelectedDenseColumnLongMatrix2D 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.matrix.AbstractMatrix2D;
import cern.colt.matrix.tint.IntMatrix1D;
import cern.colt.matrix.tint.IntMatrix2D;
/**
* Selection view on dense 2-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 1 additional int addition and 2 additional array index
* accesses per get/set.
*
* Note that this implementation is not synchronized.
*
* Memory requirements:
*
* memory [bytes] = 4*(rowIndexes.length+columnIndexes.length). Thus,
* an index view with 1000 x 1000 indexes additionally uses 8 KB.
*
* Time complexity:
*
* Depends on the parent view holding cells.
*
*
* @author [email protected]
* @version 1.0, 09/24/99
*
* @author Piotr Wendykier ([email protected])
* @version 1.1, 08/22/2007
*/
class SelectedDenseColumnIntMatrix2D extends IntMatrix2D {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* The elements of this matrix.
*/
protected int[] elements;
/**
* The offsets of the visible cells of this matrix.
*/
protected int[] rowOffsets;
protected int[] columnOffsets;
/**
* The offset.
*/
protected int offset;
/**
* Constructs a matrix view with the given parameters.
*
* @param elements
* the cells.
* @param rowOffsets
* The row offsets of the cells that shall be visible.
* @param columnOffsets
* The column offsets of the cells that shall be visible.
* @param offset
*/
protected SelectedDenseColumnIntMatrix2D(int[] elements, int[] rowOffsets, int[] columnOffsets, int offset) {
this(rowOffsets.length, columnOffsets.length, elements, 0, 0, 1, 1, rowOffsets, columnOffsets, offset);
}
/**
* Constructs a matrix view with the given parameters.
*
* @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 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).
* @param rowOffsets
* The row offsets of the cells that shall be visible.
* @param columnOffsets
* The column offsets of the cells that shall be visible.
* @param offset
*/
protected SelectedDenseColumnIntMatrix2D(int rows, int columns, int[] elements, int rowZero, int columnZero,
int rowStride, int columnStride, int[] rowOffsets, int[] columnOffsets, int offset) {
// be sure parameters are valid, we do not check...
setUp(rows, columns, rowZero, columnZero, rowStride, columnStride);
this.elements = elements;
this.rowOffsets = rowOffsets;
this.columnOffsets = columnOffsets;
this.offset = offset;
this.isNoView = false;
}
public int[] elements() {
return elements;
}
/**
* Returns the matrix cell value at coordinate [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):
* 0 <= column < columns() && 0 <= row < rows().
*
* @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 row, int column) {
// if (debug) if (column<0 || column>=columns || row<0 || row>=rows)
// throw new IndexOutOfBoundsException("row:"+row+", column:"+column);
// return elements[index(row,column)];
// manually inlined:
return elements[offset + rowOffsets[rowZero + row * rowStride]
+ columnOffsets[columnZero + column * columnStride]];
}
/**
* Returns the position of the given coordinate within the (virtual or
* non-virtual) internal 1-dimensional array.
*
* @param row
* the index of the row-coordinate.
* @param column
* the index of the column-coordinate.
*/
public long index(int row, int column) {
// return this.offset + super.index(row,column);
// manually inlined:
return this.offset + rowOffsets[rowZero + row * rowStride] + columnOffsets[columnZero + column * columnStride];
}
/**
* Construct and returns a new empty matrix of the same dynamic type
* as the receiver, having the specified number of rows and columns. For
* example, if the receiver is an instance of type
* DenseColIntMatrix2D the new matrix must also be of type
* DenseColIntMatrix2D, if the receiver is an instance of type
* SparseColIntMatrix2D the new matrix must also be of type
* SparseColIntMatrix2D, etc. In general, the new matrix should
* have internal parametrization as similar as possible.
*
* @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 IntMatrix2D like(int rows, int columns) {
return new DenseColumnIntMatrix2D(rows, columns);
}
/**
* Construct and returns a new 1-d matrix of the corresponding dynamic
* type, entirelly independent of the receiver. For example, if the
* receiver is an instance of type DenseColIntMatrix2D the new
* matrix must be of type DenseIntMatrix1D, if the receiver is an
* instance of type SparseColIntMatrix2D the new matrix must be of
* type SparseIntMatrix1D, etc.
*
* @param size
* the number of cells the matrix shall have.
* @return a new matrix of the corresponding dynamic type.
*/
public IntMatrix1D like1D(int size) {
return new DenseIntMatrix1D(size);
}
/**
* Sets the matrix cell at coordinate [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):
* 0 <= column < columns() && 0 <= row < rows().
*
* @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 row, int column, int value) {
// if (debug) if (column<0 || column>=columns || row<0 || row>=rows)
// throw new IndexOutOfBoundsException("row:"+row+", column:"+column);
// elements[index(row,column)] = value;
// manually inlined:
elements[offset + rowOffsets[rowZero + row * rowStride] + columnOffsets[columnZero + column * columnStride]] = value;
}
/**
* Returns a vector obtained by stacking the columns of the matrix on top of
* one another.
*
* @return
*/
public IntMatrix1D vectorize() {
DenseIntMatrix1D v = new DenseIntMatrix1D((int) size());
int idx = 0;
for (int c = 0; c < columns; c++) {
for (int r = 0; r < rows; r++) {
v.setQuick(idx++, getQuick(c, r));
}
}
return v;
}
/**
* Constructs and returns a new slice view representing the 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 (
* viewPart(...)), then apply this method to the sub-range view.
*
* Example:
*
*
* 2 x 3 matrix:
* 1, 2, 3
* 4, 5, 6
* viewColumn(0) ==>
* Matrix1D of size 2:
* 1, 4
*
*
*
* @param the
* column to fix.
* @return a new slice view.
* @throws IllegalArgumentException
* if column < 0 || column >= columns().
* @see #viewRow(int)
*/
public IntMatrix1D viewColumn(int column) {
checkColumn(column);
int viewSize = this.rows;
int viewZero = this.rowZero;
int viewStride = this.rowStride;
int[] viewOffsets = this.rowOffsets;
int viewOffset = this.offset + _columnOffset(_columnRank(column));
return new SelectedDenseIntMatrix1D(viewSize, this.elements, viewZero, viewStride, viewOffsets, viewOffset);
}
/**
* Constructs and returns a new slice view representing the 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 (
* viewPart(...)), then apply this method to the sub-range view.
*
* Example:
*
*
* 2 x 3 matrix:
* 1, 2, 3
* 4, 5, 6
* viewRow(0) ==>
* Matrix1D of size 3:
* 1, 2, 3
*
*
*
* @param the
* row to fix.
* @return a new slice view.
* @throws IndexOutOfBoundsException
* if row < 0 || row >= rows().
* @see #viewColumn(int)
*/
public IntMatrix1D viewRow(int row) {
checkRow(row);
int viewSize = this.columns;
int viewZero = columnZero;
int viewStride = this.columnStride;
int[] viewOffsets = this.columnOffsets;
int viewOffset = this.offset + _rowOffset(_rowRank(row));
return new SelectedDenseIntMatrix1D(viewSize, this.elements, viewZero, viewStride, viewOffsets, 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 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(IntMatrix2D other) {
if (other instanceof SelectedDenseColumnIntMatrix2D) {
SelectedDenseColumnIntMatrix2D otherMatrix = (SelectedDenseColumnIntMatrix2D) other;
return this.elements == otherMatrix.elements;
} else if (other instanceof DenseColumnIntMatrix2D) {
DenseColumnIntMatrix2D otherMatrix = (DenseColumnIntMatrix2D) other;
return this.elements == otherMatrix.elements;
}
return false;
}
/**
* Construct and returns a new 1-d matrix of the corresponding dynamic
* type, sharing the same cells. For example, if the receiver is an
* instance of type DenseColIntMatrix2D the new matrix must be of
* type DenseIntMatrix1D, if the receiver is an instance of type
* SparseColIntMatrix2D the new matrix must be of type
* SparseIntMatrix1D, etc.
*
* @param size
* the number of cells the matrix shall have.
* @param zero
* the index of the first element.
* @param stride
* the number of indexes between any two elements, i.e.
* index(i+1)-index(i).
* @return a new matrix of the corresponding dynamic type.
*/
protected IntMatrix1D like1D(int size, int zero, int stride) {
throw new InternalError(); // this method is never called since
// viewRow() and viewColumn are overridden
// properly.
}
/**
* Sets up a matrix with a given number of rows and columns.
*
* @param rows
* the number of rows the matrix shall have.
* @param columns
* the number of columns the matrix shall have.
* @throws IllegalArgumentException
* if (double)columns*rows > Integer.MAX_VALUE.
*/
protected void setUp(int rows, int columns) {
super.setUp(rows, columns);
this.rowStride = 1;
this.columnStride = 1;
this.offset = 0;
}
/**
* Self modifying version of viewDice().
*/
protected AbstractMatrix2D vDice() {
super.vDice();
// swap
int[] tmp = rowOffsets;
rowOffsets = columnOffsets;
columnOffsets = tmp;
// flips stay unaffected
this.isNoView = false;
return this;
}
/**
* Construct and returns a new selection view.
*
* @param rowOffsets
* the offsets of the visible elements.
* @param columnOffsets
* the offsets of the visible elements.
* @return a new view.
*/
protected IntMatrix2D viewSelectionLike(int[] rowOffsets, int[] columnOffsets) {
return new SelectedDenseColumnIntMatrix2D(this.elements, rowOffsets, columnOffsets, this.offset);
}
}