cern.colt.matrix.tint.impl.WrapperIntMatrix1D 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.tint.IntMatrix1D;
import cern.colt.matrix.tint.IntMatrix2D;
import cern.colt.matrix.tint.IntMatrix3D;
/**
* 1-d matrix holding int elements; either a view wrapping another
* matrix or a matrix whose views are wrappers.
*
* @author [email protected]
* @version 1.0, 09/24/99
*
* @author Piotr Wendykier ([email protected])
* @version 1.1, 08/22/2007
*/
public class WrapperIntMatrix1D extends IntMatrix1D {
/**
*
*/
private static final long serialVersionUID = 1L;
/*
* The elements of the matrix.
*/
protected IntMatrix1D content;
public WrapperIntMatrix1D(IntMatrix1D newContent) {
if (newContent != null)
setUp((int) newContent.size());
this.content = newContent;
}
/**
* Returns the content of this matrix if it is a wrapper; or this
* otherwise. Override this method in wrappers.
*/
protected IntMatrix1D getContent() {
return this.content;
}
/**
* Returns the matrix cell value at coordinate index.
*
*
* 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): index<0 || index>=size().
*
* @param index
* the index of the cell.
* @return the value of the specified cell.
*/
public synchronized int getQuick(int index) {
return content.getQuick(index);
}
public Object elements() {
return content.elements();
}
/**
* Construct and returns a new empty matrix of the same dynamic type
* as the receiver, having the specified size. For example, if the receiver
* is an instance of type DenseIntMatrix1D the new matrix must also
* be of type DenseIntMatrix1D, if the receiver is an instance of
* type SparseIntMatrix1D the new matrix must also be of type
* SparseIntMatrix1D, etc. In general, the new matrix should have
* internal parametrization as similar as possible.
*
* @param size
* the number of cell the matrix shall have.
* @return a new empty matrix of the same dynamic type.
*/
public IntMatrix1D like(int size) {
return content.like(size);
}
/**
* Construct and returns a new 2-d matrix of the corresponding dynamic
* type, entirelly independent of the receiver. For example, if the
* receiver is an instance of type DenseIntMatrix1D the new matrix
* must be of type DenseIntMatrix2D, if the receiver is an instance
* of type SparseIntMatrix1D the new matrix must 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.
* @return a new matrix of the corresponding dynamic type.
*/
public IntMatrix2D like2D(int rows, int columns) {
return content.like2D(rows, columns);
}
public IntMatrix2D reshape(int rows, int columns) {
throw new IllegalArgumentException("This method is not supported.");
}
public IntMatrix3D reshape(int slices, int rows, int columns) {
throw new IllegalArgumentException("This method is not supported.");
}
/**
* Sets the matrix cell at coordinate index 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): index<0 || index>=size().
*
* @param index
* the index of the cell.
* @param value
* the value to be filled into the specified cell.
*/
public synchronized void setQuick(int index, int value) {
content.setQuick(index, value);
}
/**
* Constructs and returns a new flip view. What used to be index
* 0 is now index size()-1, ..., what used to be index
* size()-1 is now index 0. The returned view is backed by
* this matrix, so changes in the returned view are reflected in this
* matrix, and vice-versa.
*
* @return a new flip view.
*/
public IntMatrix1D viewFlip() {
IntMatrix1D view = new WrapperIntMatrix1D(this) {
/**
*
*/
private static final long serialVersionUID = 1L;
public synchronized int getQuick(int index) {
return content.getQuick(size - 1 - index);
}
public synchronized void setQuick(int index, int value) {
content.setQuick(size - 1 - index, value);
}
public synchronized int get(int index) {
return content.get(size - 1 - index);
}
public synchronized void set(int index, int value) {
content.set(size - 1 - index, value);
}
};
return view;
}
/**
* Constructs and returns a new sub-range view that is a
* width sub matrix starting at index.
*
* Operations on the returned view can only be applied to the restricted
* range. Any attempt to access coordinates not contained in the view will
* throw an IndexOutOfBoundsException.
*
* Note that the view is really just a range restriction: The
* returned matrix is backed by this matrix, so changes in the returned
* matrix are reflected in this matrix, and vice-versa.
*
* The view contains the cells from index..index+width-1. and has
* view.size() == width. A view's legal coordinates are again zero
* based, as usual. In other words, legal coordinates of the view are
* 0 .. view.size()-1==width-1. As usual, any attempt to access a
* cell at other coordinates will throw an
* IndexOutOfBoundsException.
*
* @param index
* The index of the first cell.
* @param width
* The width of the range.
* @throws IndexOutOfBoundsException
* if index<0 || width<0 || index+width>size().
* @return the new view.
*
*/
public IntMatrix1D viewPart(final int index, int width) {
checkRange(index, width);
IntMatrix1D view = new WrapperIntMatrix1D(this) {
/**
*
*/
private static final long serialVersionUID = 1L;
public synchronized int getQuick(int i) {
return content.getQuick(index + i);
}
public synchronized void setQuick(int i, int value) {
content.setQuick(index + i, value);
}
public synchronized int get(int i) {
return content.get(index + i);
}
public synchronized void set(int i, int value) {
content.set(index + i, value);
}
};
view.setSize(width);
return view;
}
/**
* Constructs and returns a new selection view that is a matrix
* holding the indicated cells. There holds
* view.size() == indexes.length and
* view.get(i) == this.get(indexes[i]). Indexes can occur multiple
* times and can be in arbitrary order.
*
* Example:
*
*
* this = (0,0,8,0,7)
* indexes = (0,2,4,2)
* -->
* view = (0,8,7,8)
*
*
*
* Note that modifying indexes after this call has returned has no
* effect on the view. The returned view is backed by this matrix, so
* changes in the returned view are reflected in this matrix, and
* vice-versa.
*
* @param indexes
* The indexes of the cells that shall be visible in the new
* view. To indicate that all cells shall be visible,
* simply set this parameter to null.
* @return the new view.
* @throws IndexOutOfBoundsException
* if !(0 <= indexes[i] < size()) for any
* i=0..indexes.length()-1.
*/
public IntMatrix1D viewSelection(int[] indexes) {
// check for "all"
if (indexes == null) {
indexes = new int[size];
for (int i = size; --i >= 0;)
indexes[i] = i;
}
checkIndexes(indexes);
final int[] idx = indexes;
IntMatrix1D view = new WrapperIntMatrix1D(this) {
/**
*
*/
private static final long serialVersionUID = 1L;
public synchronized int getQuick(int i) {
return content.getQuick(idx[i]);
}
public synchronized void setQuick(int i, int value) {
content.setQuick(idx[i], value);
}
public synchronized int get(int i) {
return content.get(idx[i]);
}
public synchronized void set(int i, int value) {
content.set(idx[i], value);
}
};
view.setSize(indexes.length);
return view;
}
/**
* Construct and returns a new selection view.
*
* @param offsets
* the offsets of the visible elements.
* @return a new view.
*/
protected IntMatrix1D viewSelectionLike(int[] offsets) {
throw new InternalError(); // should never get called
}
/**
* Constructs and returns a new stride view which is a sub matrix
* consisting of every i-th cell. More specifically, the view has size
* this.size()/stride holding cells this.get(i*stride) for
* all i = 0..size()/stride - 1.
*
* @param _stride
* the step factor.
* @throws IndexOutOfBoundsException
* if stride <= 0.
* @return the new view.
*
*/
public IntMatrix1D viewStrides(final int _stride) {
if (stride <= 0)
throw new IndexOutOfBoundsException("illegal stride: " + stride);
IntMatrix1D view = new WrapperIntMatrix1D(this) {
/**
*
*/
private static final long serialVersionUID = 1L;
public synchronized int getQuick(int index) {
return content.getQuick(index * _stride);
}
public synchronized void setQuick(int index, int value) {
content.setQuick(index * _stride, value);
}
public synchronized int get(int index) {
return content.get(index * _stride);
}
public synchronized void set(int index, int value) {
content.set(index * _stride, value);
}
};
view.setSize(size);
if (size != 0)
view.setSize((size - 1) / _stride + 1);
return view;
}
}