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

mikera.vectorz.impl.WrappedSubVector Maven / Gradle / Ivy

Go to download

Fast double-precision vector and matrix maths library for Java, supporting N-dimensional numeric arrays.

There is a newer version: 0.67.0
Show newest version
package mikera.vectorz.impl;

import java.util.Iterator;

import mikera.vectorz.AVector;
import mikera.vectorz.Op;

/**
 * View class referencing a contiguous subvector of another vector. 
 * @author Mike
 *
 */
public final class WrappedSubVector extends ASizedVector {
	private static final long serialVersionUID = 2323553136938665228L;

	private final AVector wrapped;
	private final int offset;
	
	private WrappedSubVector(AVector source, int offset, int length) {
		super(length);
		if (source instanceof WrappedSubVector) {
			// avoid stacking WrappedSubVectors by using underlying vector
			WrappedSubVector v=(WrappedSubVector)source;
			this.wrapped=v.wrapped;
			this.offset=offset+v.offset;
		} else {
			wrapped=source;
			this.offset=offset;
		}
	}
	
	public static WrappedSubVector wrap(AVector source, int offset, int length) {
		return new WrappedSubVector(source,offset,length);
	}
	
	@Override 
	public Iterator iterator() {
		return new VectorIterator(wrapped,offset,length);
	}
	
	@Override
	public boolean isFullyMutable() {
		return wrapped.isFullyMutable();
	}
	
	@Override
	public boolean isElementConstrained() {
		return wrapped.isElementConstrained();
	}
	
	@Override
	public boolean isView() {
		return true;
	}
	
	@Override
	public boolean isZero() {
		return wrapped.isRangeZero(this.offset, this.length);
	}
	
	@Override
	public boolean isRangeZero(int start, int length) {
		return wrapped.isRangeZero(this.offset + start, length);
	}

	@Override
	public double get(int i) {
		checkIndex(i);
		return wrapped.unsafeGet(i+offset);
	}

	@Override
	public void set(int i, double value) {
		checkIndex(i);
		wrapped.unsafeSet(i+offset,value);
	}
	
	@Override
	public double unsafeGet(int i) {
		return wrapped.unsafeGet(i+offset);
	}

	@Override
	public void unsafeSet(int i, double value) {
		wrapped.unsafeSet(i+offset,value);
	}
	
	@Override 
	public void applyOp(Op op) {
		for (int i=0; i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy