All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cern.colt.matrix.tlong.impl.SelectedSparseLongMatrix1D Maven / Gradle / Ivy

Go to download

Parallel Colt is a multithreaded version of Colt - a library for high performance scientific computing in Java. It contains efficient algorithms for data analysis, linear algebra, multi-dimensional arrays, Fourier transforms, statistics and histogramming.

The newest version!
/*
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.tlong.impl;

import cern.colt.map.tlong.AbstractLongLongMap;
import cern.colt.matrix.tlong.LongMatrix1D;
import cern.colt.matrix.tlong.LongMatrix2D;
import cern.colt.matrix.tlong.LongMatrix3D;

/**
 * Selection view on sparse 1-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, * 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 array index access per get/set. *

* Note that this implementation is not synchronized. *

* Memory requirements: *

* memory [bytes] = 4*indexes.length. Thus, an index view with 1000 * indexes additionally uses 4 KB. *

* Time complexity: *

* Depends on the parent view holding cells. *

* * @author [email protected] * @version 1.0, 09/24/99 */ class SelectedSparseLongMatrix1D extends LongMatrix1D { /** * */ private static final long serialVersionUID = 1L; /* * The elements of the matrix. */ protected AbstractLongLongMap elements; /** * The offsets of visible indexes of this matrix. */ protected int[] offsets; /** * The offset. */ protected int offset; /** * Constructs a matrix view with the given parameters. * * @param elements * the cells. * @param indexes * The indexes of the cells that shall be visible. */ protected SelectedSparseLongMatrix1D(AbstractLongLongMap elements, int[] offsets) { this(offsets.length, elements, 0, 1, offsets, 0); } /** * Constructs a matrix view with the given parameters. * * @param size * the number of cells the matrix shall have. * @param elements * the cells. * @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). * @param offsets * the offsets of the cells that shall be visible. * @param offset */ protected SelectedSparseLongMatrix1D(int size, AbstractLongLongMap elements, int zero, int stride, int[] offsets, int offset) { setUp(size, zero, stride); this.elements = elements; this.offsets = offsets; this.offset = offset; this.isNoView = false; } public AbstractLongLongMap elements() { throw new IllegalArgumentException("This method is not supported."); } /** * 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 long getQuick(int index) { // if (debug) if (index<0 || index>=size) checkIndex(index); // return elements.get(index(index)); // manually inlined: return elements.get((long) offset + (long) offsets[zero + index * stride]); } /** * Returns the position of the element with the given relative rank within * the (virtual or non-virtual) internal 1-dimensional array. You may want * to override this method for performance. * * @param rank * the rank of the element. */ public long index(int rank) { // return this.offset + super.index(rank); // manually inlined: return (long) offset + (long) offsets[zero + rank * stride]; } /** * 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 DenseLongMatrix1D the new matrix must * also be of type DenseLongMatrix1D, if the receiver is an * instance of type SparseLongMatrix1D the new matrix must also be * of type SparseLongMatrix1D, 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 LongMatrix1D like(int size) { return new SparseLongMatrix1D(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 DenseLongMatrix1D the new matrix * must be of type DenseLongMatrix2D, if the receiver is an * instance of type SparseLongMatrix1D the new matrix must be of * type SparseLongMatrix2D, 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 LongMatrix2D like2D(int rows, int columns) { return new SparseLongMatrix2D(rows, columns); } public LongMatrix2D reshape(int rows, int columns) { throw new IllegalArgumentException("This method is not supported."); } public LongMatrix3D 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 void setQuick(int index, long value) { // if (debug) if (index<0 || index>=size) checkIndex(index); // int i = index(index); // manually inlined: long i = (long) offset + (long) offsets[zero + index * stride]; if (value == 0) this.elements.removeKey(i); else this.elements.put(i, value); } /** * 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 _offset(int absRank) { return offsets[absRank]; } /** * Returns true if both matrices share at least one identical cell. */ protected boolean haveSharedCellsRaw(LongMatrix1D other) { if (other instanceof SelectedSparseLongMatrix1D) { SelectedSparseLongMatrix1D otherMatrix = (SelectedSparseLongMatrix1D) other; return this.elements == otherMatrix.elements; } else if (other instanceof SparseLongMatrix1D) { SparseLongMatrix1D otherMatrix = (SparseLongMatrix1D) other; return this.elements == otherMatrix.elements; } return false; } /** * Sets up a matrix with a given number of cells. * * @param size * the number of cells the matrix shall have. */ protected void setUp(int size) { super.setUp(size); this.stride = 1; this.offset = 0; } /** * Construct and returns a new selection view. * * @param offsets * the offsets of the visible elements. * @return a new view. */ protected LongMatrix1D viewSelectionLike(int[] offsets) { return new SelectedSparseLongMatrix1D(this.elements, offsets); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy