com.io7m.jtensors.bytebuffered.VectorByteBufferedM4I Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of io7m-jtensors-bytebuffered Show documentation
Show all versions of io7m-jtensors-bytebuffered Show documentation
Vector/matrix math package (Buffered vectors/matrices)
/*
* Copyright © 2015 http://io7m.com
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
package com.io7m.jtensors.bytebuffered;
import com.io7m.jintegers.CheckedMath;
import com.io7m.jnull.NullCheck;
import com.io7m.jnull.Nullable;
import com.io7m.jtensors.VectorReadable2IType;
import com.io7m.jtensors.VectorReadable3IType;
import com.io7m.jtensors.VectorReadable4IType;
import java.nio.ByteBuffer;
import java.util.concurrent.atomic.AtomicLong;
/**
* A four-element vector type with {@code int} elements, packed into a {@link
* ByteBuffer}.
*
* Values of this type cannot be accessed safely from multiple threads
* without explicit synchronization.
*/
public final class VectorByteBufferedM4I extends ByteBuffered
implements VectorByteBuffered4IType
{
private final ByteBuffer buffer;
private VectorByteBufferedM4I(
final ByteBuffer in_buffer,
final AtomicLong in_base,
final int in_offset)
{
super(in_base, in_offset);
this.buffer = NullCheck.notNull(in_buffer);
}
/**
* Return a new vector that is backed by whatever data is at byte offset
* {@code byte_offset} in the byte buffer {@code}.
*
* No initialization of the data is performed.
*
* @param b The byte buffer
* @param byte_offset A byte offset
*
* @return A new buffered vector
*/
public static VectorByteBuffered4IType newVectorFromByteBuffer(
final ByteBuffer b,
final long byte_offset)
{
return new VectorByteBufferedM4I(b, new AtomicLong(byte_offset), 0);
}
/**
* Return a new vector that is backed by the given byte buffer {@code b}
*
*
* The data for the instance will be taken from the data at the current
* value of {@code base.get() + offset}, each time a field is requested or
* set.
*
* No initialization of the data is performed.
*
* @param b The byte buffer
* @param base The base address
* @param offset A constant offset
*
* @return A new buffered vector
*/
public static VectorByteBuffered4IType newVectorFromByteBufferAndBase(
final ByteBuffer b,
final AtomicLong base,
final int offset)
{
return new VectorByteBufferedM4I(b, base, offset);
}
private static int getByteOffsetForIndex(
final long base,
final int index)
{
final long b = CheckedMath.add(base, (long) (index * 4));
return (int) ByteBufferRanges.checkByteOffset(b);
}
@Override public int getWI()
{
return this.getAtOffsetAndIndex(super.getIndex(), 3);
}
@Override public void setWI(final int w)
{
this.setAtOffsetAndIndex(super.getIndex(), 3, w);
}
@Override public int getZI()
{
return this.getAtOffsetAndIndex(super.getIndex(), 2);
}
@Override public void setZI(final int z)
{
this.setAtOffsetAndIndex(super.getIndex(), 2, z);
}
@Override public int getXI()
{
return this.getAtOffsetAndIndex(super.getIndex(), 0);
}
@Override public void setXI(final int x)
{
this.setAtOffsetAndIndex(super.getIndex(), 0, x);
}
private void setAtOffsetAndIndex(
final long o,
final int i,
final int x)
{
this.buffer.putInt(VectorByteBufferedM4I.getByteOffsetForIndex(o, i), x);
}
private int getAtOffsetAndIndex(
final long o,
final int i)
{
return this.buffer.getInt(
VectorByteBufferedM4I.getByteOffsetForIndex(o, i));
}
@Override public int getYI()
{
return this.getAtOffsetAndIndex(super.getIndex(), 1);
}
@Override public void setYI(final int y)
{
this.setAtOffsetAndIndex(super.getIndex(), 1, y);
}
@Override public void copyFrom4I(final VectorReadable4IType in_v)
{
final long o = super.getIndex();
this.setAtOffsetAndIndex(o, 0, in_v.getXI());
this.setAtOffsetAndIndex(o, 1, in_v.getYI());
this.setAtOffsetAndIndex(o, 2, in_v.getZI());
this.setAtOffsetAndIndex(o, 3, in_v.getWI());
}
@Override public void set4I(
final int x,
final int y,
final int z,
final int w)
{
final long o = super.getIndex();
this.setAtOffsetAndIndex(o, 0, x);
this.setAtOffsetAndIndex(o, 1, y);
this.setAtOffsetAndIndex(o, 2, z);
this.setAtOffsetAndIndex(o, 3, w);
}
@Override public void copyFrom3I(final VectorReadable3IType in_v)
{
final long o = super.getIndex();
this.setAtOffsetAndIndex(o, 0, in_v.getXI());
this.setAtOffsetAndIndex(o, 1, in_v.getYI());
this.setAtOffsetAndIndex(o, 2, in_v.getZI());
}
@Override public void set3I(
final int x,
final int y,
final int z)
{
final long o = super.getIndex();
this.setAtOffsetAndIndex(o, 0, x);
this.setAtOffsetAndIndex(o, 1, y);
this.setAtOffsetAndIndex(o, 2, z);
}
@Override public void copyFrom2I(final VectorReadable2IType in_v)
{
final long o = super.getIndex();
this.setAtOffsetAndIndex(o, 0, in_v.getXI());
this.setAtOffsetAndIndex(o, 1, in_v.getYI());
}
@Override public void set2I(
final int x,
final int y)
{
final long o = super.getIndex();
this.setAtOffsetAndIndex(o, 0, x);
this.setAtOffsetAndIndex(o, 1, y);
}
@Override public int hashCode()
{
final int prime = 31;
int result = 1;
result = (prime * result) + this.getWI();
result = (prime * result) + this.getXI();
result = (prime * result) + this.getYI();
result = (prime * result) + this.getZI();
return result;
}
@Override public String toString()
{
final StringBuilder builder = new StringBuilder(128);
builder.append("[VectorByteBufferedM4I ");
builder.append(this.getXI());
builder.append(" ");
builder.append(this.getYI());
builder.append(" ");
builder.append(this.getZI());
builder.append(" ");
builder.append(this.getWI());
builder.append("]");
final String r = builder.toString();
return NullCheck.notNull(r);
}
@Override public boolean equals(
final @Nullable Object obj)
{
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (this.getClass() != obj.getClass()) {
return false;
}
final VectorByteBufferedM4I other = (VectorByteBufferedM4I) obj;
if (this.getWI() != other.getWI()) {
return false;
}
if (this.getXI() != other.getXI()) {
return false;
}
if (this.getYI() != other.getYI()) {
return false;
}
return this.getZI() == other.getZI();
}
}