Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring to add hyperslab selection to define an indexer #392

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

* Take into account `platform.library.path` when extracting executables on `Loader.load()` ([issue bytedeco/javacv#1410](https://github.com/bytedeco/javacv/issues/1410))
* Move init code for `Loader.getPlatform()` to `Detector` to avoid warning messages ([issue #393](https://github.com/bytedeco/javacpp/issues/393))
* Add `Indexer.Index` nested class to allow overriding how the index is calculated ([issue #391](https://github.com/bytedeco/javacpp/issues/391))
* Add `HyperslabIndex` class with `offsets`, `strides`, `counts`, and `blocks` parameters ([pull #392](https://github.com/bytedeco/javacpp/pull/392))
* Add `Index` class to allow overriding how the index is calculated in `Indexer` ([issue #391](https://github.com/bytedeco/javacpp/issues/391))

### April 14, 2020 version 1.5.3
* Deprecate but also fix `Indexer.rows()`, `cols()`, `width()`, `height()`, and `channels()` ([pull #390](https://github.com/bytedeco/javacpp/pull/390))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,35 @@ public class Bfloat16ArrayIndexer extends Bfloat16Indexer {
/** The backing array. */
protected short[] array;

/** Calls {@code Bfloat16ArrayIndexer(array, { array.length }, { 1 })}. */
/** Calls {@code Bfloat16ArrayIndexer(array, Index.create(array.length))}. */
public Bfloat16ArrayIndexer(short[] array) {
this(array, new long[] { array.length }, ONE_STRIDE);
this(array, Index.create(array.length));
}

/** Calls {@code Bfloat16ArrayIndexer(array, sizes, strides(sizes))}. */
/** Calls {@code Bfloat16ArrayIndexer(array, Index.create(sizes))}. */
public Bfloat16ArrayIndexer(short[] array, long... sizes) {
this(array, sizes, strides(sizes));
this(array, Index.create(sizes));
}

/** Constructor to set the {@link #array}, {@link #sizes} and {@link #strides}. */
/** Calls {@code Bfloat16ArrayIndexer(array, Index.create(sizes, strides))}. */
public Bfloat16ArrayIndexer(short[] array, long[] sizes, long[] strides) {
super(sizes, strides);
this(array, Index.create(sizes, strides));
}

/** Constructor to set the {@link #array} and {@link #index}. */
public Bfloat16ArrayIndexer(short[] array, Index index) {
super(index);
this.array = array;
}

@Override public short[] array() {
return array;
}

@Override public Bfloat16Indexer reindex(Index index) {
return new Bfloat16ArrayIndexer(array, index);
}

@Override public float get(long i) {
return toFloat(array[(int)index(i)]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,35 @@ public class Bfloat16BufferIndexer extends Bfloat16Indexer {
/** The backing buffer. */
protected ShortBuffer buffer;

/** Calls {@code Bfloat16BufferIndexer(buffer, { buffer.limit() }, { 1 })}. */
/** Calls {@code Bfloat16BufferIndexer(buffer, Index.create(buffer.limit()))}. */
public Bfloat16BufferIndexer(ShortBuffer buffer) {
this(buffer, new long[] { buffer.limit() }, ONE_STRIDE);
this(buffer, Index.create(buffer.limit()));
}

/** Calls {@code Bfloat16BufferIndexer(buffer, sizes, strides(sizes))}. */
/** Calls {@code Bfloat16BufferIndexer(buffer, Index.create(sizes))}. */
public Bfloat16BufferIndexer(ShortBuffer buffer, long... sizes) {
this(buffer, sizes, strides(sizes));
this(buffer, Index.create(sizes));
}

/** Constructor to set the {@link #buffer}, {@link #sizes} and {@link #strides}. */
/** Calls {@code Bfloat16BufferIndexer(buffer, Index.create(sizes, strides))}. */
public Bfloat16BufferIndexer(ShortBuffer buffer, long[] sizes, long[] strides) {
super(sizes, strides);
this(buffer, Index.create(sizes, strides));
}

/** Constructor to set the {@link #buffer} and {@link #index}. */
public Bfloat16BufferIndexer(ShortBuffer buffer, Index index) {
super(index);
this.buffer = buffer;
}

@Override public Buffer buffer() {
return buffer;
}

@Override public Bfloat16Indexer reindex(Index index) {
return new Bfloat16BufferIndexer(buffer, index);
}

@Override public float get(long i) {
return toFloat(buffer.get((int)index(i)));
}
Expand Down
42 changes: 32 additions & 10 deletions src/main/java/org/bytedeco/javacpp/indexer/Bfloat16Indexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public abstract class Bfloat16Indexer extends Indexer {
/** The number of bytes used to represent a short. */
public static final int VALUE_BYTES = 2;

protected Bfloat16Indexer(Index index) {
super(index);
}

protected Bfloat16Indexer(long[] sizes, long[] strides) {
super(sizes, strides);
}
Expand All @@ -48,9 +52,22 @@ public static Bfloat16Indexer create(short[] array) {
public static Bfloat16Indexer create(ShortBuffer buffer) {
return new Bfloat16BufferIndexer(buffer);
}
/** Returns {@code create(pointer, { pointer.limit() - pointer.position() }, { 1 }, true)} */
/** Returns {@code new Bfloat16RawIndexer(pointer)} */
public static Bfloat16Indexer create(ShortPointer pointer) {
return create(pointer, new long[] { pointer.limit() - pointer.position() }, ONE_STRIDE);
return new Bfloat16RawIndexer(pointer);
}

/** Returns {@code new Bfloat16ArrayIndexer(array, index)} */
public static Bfloat16Indexer create(short[] array, Index index) {
return new Bfloat16ArrayIndexer(array, index);
}
/** Returns {@code new Bfloat16BufferIndexer(buffer, index)} */
public static Bfloat16Indexer create(ShortBuffer buffer, Index index) {
return new Bfloat16BufferIndexer(buffer, index);
}
/** Returns {@code new Bfloat16RawIndexer(pointer, index)} */
public static Bfloat16Indexer create(ShortPointer pointer, Index index) {
return new Bfloat16RawIndexer(pointer, index);
}

/** Returns {@code new Bfloat16ArrayIndexer(array, sizes)} */
Expand All @@ -61,9 +78,9 @@ public static Bfloat16Indexer create(short[] array, long... sizes) {
public static Bfloat16Indexer create(ShortBuffer buffer, long... sizes) {
return new Bfloat16BufferIndexer(buffer, sizes);
}
/** Returns {@code create(pointer, sizes, strides(sizes))} */
/** Returns {@code new Bfloat16RawIndexer(pointer, sizes)} */
public static Bfloat16Indexer create(ShortPointer pointer, long... sizes) {
return create(pointer, sizes, strides(sizes));
return new Bfloat16RawIndexer(pointer, sizes);
}

/** Returns {@code new Bfloat16ArrayIndexer(array, sizes, strides)} */
Expand All @@ -74,26 +91,31 @@ public static Bfloat16Indexer create(short[] array, long[] sizes, long[] strides
public static Bfloat16Indexer create(ShortBuffer buffer, long[] sizes, long[] strides) {
return new Bfloat16BufferIndexer(buffer, sizes, strides);
}
/** Returns {@code create(pointer, sizes, strides, true)} */
/** Returns {@code new Bfloat16RawIndexer(pointer, sizes, strides)} */
public static Bfloat16Indexer create(ShortPointer pointer, long[] sizes, long[] strides) {
return create(pointer, sizes, strides, true);
return new Bfloat16RawIndexer(pointer, sizes, strides);
}
/** Returns {@code create(pointer, Index.create(sizes, strides), direct)} */
public static Bfloat16Indexer create(final ShortPointer pointer, long[] sizes, long[] strides, boolean direct) {
return create(pointer, Index.create(sizes, strides), direct);
}
/**
* Creates a bfloat16 indexer to access efficiently the data of a pointer.
*
* @param pointer data to access via a buffer or to copy to an array
* @param index to use
* @param direct {@code true} to use a direct buffer, see {@link Indexer} for details
* @return the new bfloat16 indexer backed by the raw memory interface, a buffer, or an array
*/
public static Bfloat16Indexer create(final ShortPointer pointer, long[] sizes, long[] strides, boolean direct) {
public static Bfloat16Indexer create(final ShortPointer pointer, Index index, boolean direct) {
if (direct) {
return Raw.getInstance() != null ? new Bfloat16RawIndexer(pointer, sizes, strides)
: new Bfloat16BufferIndexer(pointer.asBuffer(), sizes, strides);
return Raw.getInstance() != null ? new Bfloat16RawIndexer(pointer, index)
: new Bfloat16BufferIndexer(pointer.asBuffer(), index);
} else {
final long position = pointer.position();
short[] array = new short[(int)Math.min(pointer.limit() - position, Integer.MAX_VALUE)];
pointer.get(array);
return new Bfloat16ArrayIndexer(array, sizes, strides) {
return new Bfloat16ArrayIndexer(array, index) {
@Override public void release() {
pointer.position(position).put(array);
super.release();
Expand Down
25 changes: 17 additions & 8 deletions src/main/java/org/bytedeco/javacpp/indexer/Bfloat16RawIndexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,37 @@ public class Bfloat16RawIndexer extends Bfloat16Indexer {
/** Base address and number of elements accessible. */
final long base, size;

/** Calls {@code Bfloat16RawIndexer(pointer, { pointer.limit() - pointer.position() }, { 1 })}. */
/** Calls {@code Bfloat16RawIndexer(pointer, Index.create(pointer.limit() - pointer.position()))}. */
public Bfloat16RawIndexer(ShortPointer pointer) {
this(pointer, new long[] { pointer.limit() - pointer.position() }, ONE_STRIDE);
this(pointer, Index.create(pointer.limit() - pointer.position()));
}

/** Calls {@code Bfloat16RawIndexer(pointer, sizes, strides(sizes))}. */
/** Calls {@code Bfloat16RawIndexer(pointer, Index.create(sizes))}. */
public Bfloat16RawIndexer(ShortPointer pointer, long... sizes) {
this(pointer, sizes, strides(sizes));
this(pointer, Index.create(sizes));
}

/** Constructor to set the {@link #pointer}, {@link #sizes} and {@link #strides}. */
/** Calls {@code Bfloat16RawIndexer(pointer, Index.create(sizes, strides))}. */
public Bfloat16RawIndexer(ShortPointer pointer, long[] sizes, long[] strides) {
super(sizes, strides);
this(pointer, Index.create(sizes, strides));
}

/** Constructor to set the {@link #pointer} and {@link #index}. */
public Bfloat16RawIndexer(ShortPointer pointer, Index index) {
super(index);
this.pointer = pointer;
base = pointer.address() + pointer.position() * VALUE_BYTES;
size = pointer.limit() - pointer.position();
this.base = pointer.address() + pointer.position() * VALUE_BYTES;
this.size = pointer.limit() - pointer.position();
}

@Override public Pointer pointer() {
return pointer;
}

@Override public Bfloat16Indexer reindex(Index index) {
return new Bfloat16RawIndexer(pointer, index);
}

public float getRaw(long i) {
return toFloat(RAW.getShort(base + checkIndex(i, size) * VALUE_BYTES));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,35 @@ public class BooleanArrayIndexer extends BooleanIndexer {
/** The backing array. */
protected boolean[] array;

/** Calls {@code BooleanArrayIndexer(array, { array.length }, { 1 })}. */
/** Calls {@code BooleanArrayIndexer(array, Index.create(array.length))}. */
public BooleanArrayIndexer(boolean[] array) {
this(array, new long[] { array.length }, ONE_STRIDE);
this(array, Index.create(array.length));
}

/** Calls {@code BooleanArrayIndexer(array, sizes, strides(sizes))}. */
/** Calls {@code BooleanArrayIndexer(array, Index.create(sizes))}. */
public BooleanArrayIndexer(boolean[] array, long... sizes) {
this(array, sizes, strides(sizes));
this(array, Index.create(sizes));
}

/** Constructor to set the {@link #array}, {@link #sizes} and {@link #strides}. */
/** Calls {@code BooleanArrayIndexer(array, Index.create(sizes, strides))}. */
public BooleanArrayIndexer(boolean[] array, long[] sizes, long[] strides) {
super(sizes, strides);
this(array, Index.create(sizes, strides));
}

/** Constructor to set the {@link #array} and {@link #index}. */
public BooleanArrayIndexer(boolean[] array, Index index) {
super(index);
this.array = array;
}

@Override public boolean[] array() {
return array;
}

@Override public BooleanIndexer reindex(Index index) {
return new BooleanArrayIndexer(array, index);
}

@Override public boolean get(long i) {
return array[(int)index(i)];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,35 @@ public class BooleanBufferIndexer extends BooleanIndexer {
/** The backing buffer. */
protected ByteBuffer buffer;

/** Calls {@code BooleanBufferIndexer(buffer, { buffer.limit() }, { 1 })}. */
/** Calls {@code BooleanBufferIndexer(buffer, Index.create(buffer.limit()))}. */
public BooleanBufferIndexer(ByteBuffer buffer) {
this(buffer, new long[] { buffer.limit() }, ONE_STRIDE);
this(buffer, Index.create(buffer.limit()));
}

/** Calls {@code BooleanBufferIndexer(buffer, sizes, strides(sizes))}. */
/** Calls {@code BooleanBufferIndexer(buffer, Index.create(sizes))}. */
public BooleanBufferIndexer(ByteBuffer buffer, long... sizes) {
this(buffer, sizes, strides(sizes));
this(buffer, Index.create(sizes));
}

/** Constructor to set the {@link #buffer}, {@link #sizes} and {@link #strides}. */
/** Calls {@code BooleanBufferIndexer(buffer, Index.create(sizes, strides))}. */
public BooleanBufferIndexer(ByteBuffer buffer, long[] sizes, long[] strides) {
super(sizes, strides);
this(buffer, Index.create(sizes, strides));
}

/** Constructor to set the {@link #buffer} and {@link #index}. */
public BooleanBufferIndexer(ByteBuffer buffer, Index index) {
super(index);
this.buffer = buffer;
}

@Override public Buffer buffer() {
return buffer;
}

@Override public BooleanIndexer reindex(Index index) {
return new BooleanBufferIndexer(buffer, index);
}

@Override public boolean get(long i) {
return buffer.get((int)index(i)) != 0;
}
Expand Down
42 changes: 32 additions & 10 deletions src/main/java/org/bytedeco/javacpp/indexer/BooleanIndexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public abstract class BooleanIndexer extends Indexer {
/** The number of bytes used to represent a boolean. */
public static final int VALUE_BYTES = 1;

protected BooleanIndexer(Index index) {
super(index);
}

protected BooleanIndexer(long[] sizes, long[] strides) {
super(sizes, strides);
}
Expand All @@ -46,9 +50,22 @@ public static BooleanIndexer create(boolean[] array) {
public static BooleanIndexer create(ByteBuffer buffer) {
return new BooleanBufferIndexer(buffer);
}
/** Returns {@code create(pointer, { pointer.limit() - pointer.position() }, { 1 }, true)} */
/** Returns {@code new BooleanRawIndexer(pointer)} */
public static BooleanIndexer create(BooleanPointer pointer) {
return create(pointer, new long[] { pointer.limit() - pointer.position() }, ONE_STRIDE);
return new BooleanRawIndexer(pointer);
}

/** Returns {@code new BooleanArrayIndexer(array, index)} */
public static BooleanIndexer create(boolean[] array, Index index) {
return new BooleanArrayIndexer(array, index);
}
/** Returns {@code new BooleanBufferIndexer(buffer, index)} */
public static BooleanIndexer create(ByteBuffer buffer, Index index) {
return new BooleanBufferIndexer(buffer, index);
}
/** Returns {@code new BooleanRawIndexer(pointer, index)} */
public static BooleanIndexer create(BooleanPointer pointer, Index index) {
return new BooleanRawIndexer(pointer, index);
}

/** Returns {@code new BooleanArrayIndexer(array, sizes)} */
Expand All @@ -59,9 +76,9 @@ public static BooleanIndexer create(boolean[] array, long... sizes) {
public static BooleanIndexer create(ByteBuffer buffer, long... sizes) {
return new BooleanBufferIndexer(buffer, sizes);
}
/** Returns {@code create(pointer, sizes, strides(sizes))} */
/** Returns {@code new BooleanRawIndexer(pointer, index)} */
public static BooleanIndexer create(BooleanPointer pointer, long... sizes) {
return create(pointer, sizes, strides(sizes));
return new BooleanRawIndexer(pointer, sizes);
}

/** Returns {@code new BooleanArrayIndexer(array, sizes, strides)} */
Expand All @@ -72,26 +89,31 @@ public static BooleanIndexer create(boolean[] array, long[] sizes, long[] stride
public static BooleanIndexer create(ByteBuffer buffer, long[] sizes, long[] strides) {
return new BooleanBufferIndexer(buffer, sizes, strides);
}
/** Returns {@code create(pointer, sizes, strides, true)} */
/** Returns {@code new BooleanRawIndexer(pointer, sizes, strides)} */
public static BooleanIndexer create(BooleanPointer pointer, long[] sizes, long[] strides) {
return create(pointer, sizes, strides, true);
return new BooleanRawIndexer(pointer, sizes, strides);
}
/** Returns {@code create(pointer, Index.create(sizes, strides), direct)} */
public static BooleanIndexer create(final BooleanPointer pointer, long[] sizes, long[] strides, boolean direct) {
return create(pointer, Index.create(sizes, strides), direct);
}
/**
* Creates a boolean indexer to access efficiently the data of a pointer.
*
* @param pointer data to access via a buffer or to copy to an array
* @param index to use
* @param direct {@code true} to use a direct buffer, see {@link Indexer} for details
* @return the new boolean indexer backed by the raw memory interface, a buffer, or an array
*/
public static BooleanIndexer create(final BooleanPointer pointer, long[] sizes, long[] strides, boolean direct) {
public static BooleanIndexer create(final BooleanPointer pointer, Index index, boolean direct) {
if (direct) {
return Raw.getInstance() != null ? new BooleanRawIndexer(pointer, sizes, strides)
: new BooleanBufferIndexer(pointer.asByteBuffer(), sizes, strides);
return Raw.getInstance() != null ? new BooleanRawIndexer(pointer, index)
: new BooleanBufferIndexer(pointer.asByteBuffer(), index);
} else {
final long position = pointer.position();
boolean[] array = new boolean[(int)Math.min(pointer.limit() - position, Integer.MAX_VALUE)];
pointer.get(array);
return new BooleanArrayIndexer(array, sizes, strides) {
return new BooleanArrayIndexer(array, index) {
@Override public void release() {
pointer.position(position).put(array);
super.release();
Expand Down
Loading