Skip to content

Commit

Permalink
* Add Indexer.Index nested class to allow overriding how the index…
Browse files Browse the repository at this point in the history
… is calculated (issue bytedeco#391)
  • Loading branch information
saudet committed Apr 18, 2020
1 parent e424ffa commit 01b234a
Show file tree
Hide file tree
Showing 59 changed files with 949 additions and 797 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

* Add `Indexer.Index` nested class to allow overriding how the index is calculated ([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))
* Fix `Parser` producing invalid wrappers for basic containers like `std::set<std::pair<...> >`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,25 @@ public Bfloat16ArrayIndexer(short[] array, long[] sizes, long[] strides) {
}

@Override public float get(long i) {
return toFloat(array[(int)i]);
return toFloat(array[(int)index(i)]);
}
@Override public Bfloat16Indexer get(long i, float[] h, int offset, int length) {
for (int n = 0; n < length; n++) {
h[offset + n] = toFloat(array[(int)i * (int)strides[0] + n]);
h[offset + n] = toFloat(array[(int)index(i) + n]);
}
return this;
}
@Override public float get(long i, long j) {
return toFloat(array[(int)i * (int)strides[0] + (int)j]);
return toFloat(array[(int)index(i, j)]);
}
@Override public Bfloat16Indexer get(long i, long j, float[] h, int offset, int length) {
for (int n = 0; n < length; n++) {
h[offset + n] = toFloat(array[(int)i * (int)strides[0] + (int)j * (int)strides[1] + n]);
h[offset + n] = toFloat(array[(int)index(i, j) + n]);
}
return this;
}
@Override public float get(long i, long j, long k) {
return toFloat(array[(int)i * (int)strides[0] + (int)j * (int)strides[1] + (int)k]);
return toFloat(array[(int)index(i, j, k)]);
}
@Override public float get(long... indices) {
return toFloat(array[(int)index(indices)]);
Expand All @@ -83,27 +83,27 @@ public Bfloat16ArrayIndexer(short[] array, long[] sizes, long[] strides) {
}

@Override public Bfloat16Indexer put(long i, float h) {
array[(int)i] = (short)fromFloat(h);
array[(int)index(i)] = (short)fromFloat(h);
return this;
}
@Override public Bfloat16Indexer put(long i, float[] h, int offset, int length) {
for (int n = 0; n < length; n++) {
array[(int)i * (int)strides[0] + n] = (short)fromFloat(h[offset + n]);
array[(int)index(i) + n] = (short)fromFloat(h[offset + n]);
}
return this;
}
@Override public Bfloat16Indexer put(long i, long j, float h) {
array[(int)i * (int)strides[0] + (int)j] = (short)fromFloat(h);
array[(int)index(i, j)] = (short)fromFloat(h);
return this;
}
@Override public Bfloat16Indexer put(long i, long j, float[] h, int offset, int length) {
for (int n = 0; n < length; n++) {
array[(int)i * (int)strides[0] + (int)j * (int)strides[1] + n] = (short)fromFloat(h[offset + n]);
array[(int)index(i, j) + n] = (short)fromFloat(h[offset + n]);
}
return this;
}
@Override public Bfloat16Indexer put(long i, long j, long k, float h) {
array[(int)i * (int)strides[0] + (int)j * (int)strides[1] + (int)k] = (short)fromFloat(h);
array[(int)index(i, j, k)] = (short)fromFloat(h);
return this;
}
@Override public Bfloat16Indexer put(long[] indices, float h) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,25 @@ public Bfloat16BufferIndexer(ShortBuffer buffer, long[] sizes, long[] strides) {
}

@Override public float get(long i) {
return toFloat(buffer.get((int)i));
return toFloat(buffer.get((int)index(i)));
}
@Override public Bfloat16Indexer get(long i, float[] h, int offset, int length) {
for (int n = 0; n < length; n++) {
h[offset + n] = toFloat(buffer.get((int)i * (int)strides[0] + n));
h[offset + n] = toFloat(buffer.get((int)index(i) + n));
}
return this;
}
@Override public float get(long i, long j) {
return toFloat(buffer.get((int)i * (int)strides[0] + (int)j));
return toFloat(buffer.get((int)index(i, j)));
}
@Override public Bfloat16Indexer get(long i, long j, float[] h, int offset, int length) {
for (int n = 0; n < length; n++) {
h[offset + n] = toFloat(buffer.get((int)i * (int)strides[0] + (int)j * (int)strides[1] + n));
h[offset + n] = toFloat(buffer.get((int)index(i, j) + n));
}
return this;
}
@Override public float get(long i, long j, long k) {
return toFloat(buffer.get((int)i * (int)strides[0] + (int)j * (int)strides[1] + (int)k));
return toFloat(buffer.get((int)index(i, j, k)));
}
@Override public float get(long... indices) {
return toFloat(buffer.get((int)index(indices)));
Expand All @@ -86,27 +86,27 @@ public Bfloat16BufferIndexer(ShortBuffer buffer, long[] sizes, long[] strides) {
}

@Override public Bfloat16Indexer put(long i, float h) {
buffer.put((int)i, (short)fromFloat(h));
buffer.put((int)index(i), (short)fromFloat(h));
return this;
}
@Override public Bfloat16Indexer put(long i, float[] h, int offset, int length) {
for (int n = 0; n < length; n++) {
buffer.put((int)i * (int)strides[0] + n, (short)fromFloat(h[offset + n]));
buffer.put((int)index(i) + n, (short)fromFloat(h[offset + n]));
}
return this;
}
@Override public Bfloat16Indexer put(long i, long j, float h) {
buffer.put((int)i * (int)strides[0] + (int)j, (short)fromFloat(h));
buffer.put((int)index(i, j), (short)fromFloat(h));
return this;
}
@Override public Bfloat16Indexer put(long i, long j, float[] h, int offset, int length) {
for (int n = 0; n < length; n++) {
buffer.put((int)i * (int)strides[0] + (int)j * (int)strides[1] + n, (short)fromFloat(h[offset + n]));
buffer.put((int)index(i, j) + n, (short)fromFloat(h[offset + n]));
}
return this;
}
@Override public Bfloat16Indexer put(long i, long j, long k, float h) {
buffer.put((int)i * (int)strides[0] + (int)j * (int)strides[1] + (int)k, (short)fromFloat(h));
buffer.put((int)index(i, j, k), (short)fromFloat(h));
return this;
}
@Override public Bfloat16Indexer put(long[] indices, float h) {
Expand Down
40 changes: 20 additions & 20 deletions src/main/java/org/bytedeco/javacpp/indexer/Bfloat16Indexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,48 +112,48 @@ public static int fromFloat(float h) {
return Float.floatToIntBits(h) >>> 16;
}

/** Returns {@code array/buffer[i]} */
/** Returns {@code array/buffer[index(i)]} */
public abstract float get(long i);
/** Returns {@code this} where {@code s = array/buffer[i]} */
/** Returns {@code this} where {@code h = array/buffer[index(i)]} */
public Bfloat16Indexer get(long i, float[] h) { return get(i, h, 0, h.length); }
/** Returns {@code this} where {@code s[offset:offset + length] = array/buffer[i]} */
/** Returns {@code this} where {@code h[offset:offset + length] = array/buffer[index(i)]} */
public abstract Bfloat16Indexer get(long i, float[] h, int offset, int length);
/** Returns {@code array/buffer[i * strides[0] + j]} */
/** Returns {@code array/buffer[index(i, j)]} */
public abstract float get(long i, long j);
/** Returns {@code this} where {@code s = array/buffer[i * strides[0] + j]} */
/** Returns {@code this} where {@code h = array/buffer[index(i, j)]} */
public Bfloat16Indexer get(long i, long j, float[] h) { return get(i, j, h, 0, h.length); }
/** Returns {@code this} where {@code s[offset:offset + length] = array/buffer[i * strides[0] + j]} */
/** Returns {@code this} where {@code h[offset:offset + length] = array/buffer[index(i, j)]} */
public abstract Bfloat16Indexer get(long i, long j, float[] h, int offset, int length);
/** Returns {@code array/buffer[i * strides[0] + j * strides[1] + k]} */
/** Returns {@code array/buffer[index(i, j, k)]} */
public abstract float get(long i, long j, long k);
/** Returns {@code array/buffer[index(indices)]} */
public abstract float get(long... indices);
/** Returns {@code this} where {@code s = array/buffer[index(indices)]} */
/** Returns {@code this} where {@code h = array/buffer[index(indices)]} */
public Bfloat16Indexer get(long[] indices, float[] h) { return get(indices, h, 0, h.length); }
/** Returns {@code this} where {@code s[offset:offset + length] = array/buffer[index(indices)]} */
/** Returns {@code this} where {@code h[offset:offset + length] = array/buffer[index(indices)]} */
public abstract Bfloat16Indexer get(long[] indices, float[] h, int offset, int length);

/** Returns {@code this} where {@code array/buffer[i] = s} */
/** Returns {@code this} where {@code array/buffer[index(i)] = h} */
public abstract Bfloat16Indexer put(long i, float h);
/** Returns {@code this} where {@code array/buffer[i] = s} */
/** Returns {@code this} where {@code array/buffer[index(i)] = h} */
public Bfloat16Indexer put(long i, float... h) { return put(i, h, 0, h.length); }
/** Returns {@code this} where {@code array/buffer[i] = s[offset:offset + length]} */
/** Returns {@code this} where {@code array/buffer[index(i)] = h[offset:offset + length]} */
public abstract Bfloat16Indexer put(long i, float[] h, int offset, int length);
/** Returns {@code this} where {@code array/buffer[i * strides[0] + j] = s} */
/** Returns {@code this} where {@code array/buffer[index(i, j)] = h} */
public abstract Bfloat16Indexer put(long i, long j, float h);
/** Returns {@code this} where {@code array/buffer[i * strides[0] + j] = s} */
/** Returns {@code this} where {@code array/buffer[index(i, j)] = h} */
public Bfloat16Indexer put(long i, long j, float... h) { return put(i, j, h, 0, h.length); }
/** Returns {@code this} where {@code array/buffer[i * strides[0] + j] = s[offset:offset + length]} */
/** Returns {@code this} where {@code array/buffer[index(i, j)] = h[offset:offset + length]} */
public abstract Bfloat16Indexer put(long i, long j, float[] h, int offset, int length);
/** Returns {@code this} where {@code array/buffer[i * strides[0] + j * strides[1] + k] = s} */
/** Returns {@code this} where {@code array/buffer[index(i, j, k)] = h} */
public abstract Bfloat16Indexer put(long i, long j, long k, float h);
/** Returns {@code this} where {@code array/buffer[index(indices)] = s} */
/** Returns {@code this} where {@code array/buffer[index(indices)] = h} */
public abstract Bfloat16Indexer put(long[] indices, float h);
/** Returns {@code this} where {@code array/buffer[index(indices)] = s} */
/** Returns {@code this} where {@code array/buffer[index(indices)] = h} */
public Bfloat16Indexer put(long[] indices, float... h) { return put(indices, h, 0, h.length); }
/** Returns {@code this} where {@code array/buffer[index(indices)] = s[offset:offset + length]} */
/** Returns {@code this} where {@code array/buffer[index(indices)] = h[offset:offset + length]} */
public abstract Bfloat16Indexer put(long[] indices, float[] h, int offset, int length);

@Override public double getDouble(long... indices) { return get(indices); }
@Override public Bfloat16Indexer putDouble(long[] indices, double s) { return put(indices, (float)s); }
@Override public Bfloat16Indexer putDouble(long[] indices, double h) { return put(indices, (float)h); }
}
34 changes: 20 additions & 14 deletions src/main/java/org/bytedeco/javacpp/indexer/Bfloat16RawIndexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,68 +60,74 @@ public Bfloat16RawIndexer(ShortPointer pointer, long[] sizes, long[] strides) {
return pointer;
}

@Override public float get(long i) {
public float getRaw(long i) {
return toFloat(RAW.getShort(base + checkIndex(i, size) * VALUE_BYTES));
}
@Override public float get(long i) {
return getRaw(index(i));
}
@Override public Bfloat16Indexer get(long i, float[] h, int offset, int length) {
for (int n = 0; n < length; n++) {
h[offset + n] = get(i * strides[0] + n);
h[offset + n] = getRaw(index(i) + n);
}
return this;
}
@Override public float get(long i, long j) {
return get(i * strides[0] + j);
return getRaw(index(i, j));
}
@Override public Bfloat16Indexer get(long i, long j, float[] h, int offset, int length) {
for (int n = 0; n < length; n++) {
h[offset + n] = get(i * strides[0] + j * strides[1] + n);
h[offset + n] = getRaw(index(i, j) + n);
}
return this;
}
@Override public float get(long i, long j, long k) {
return get(i * strides[0] + j * strides[1] + k);
return getRaw(index(i, j, k));
}
@Override public float get(long... indices) {
return get(index(indices));
return getRaw(index(indices));
}
@Override public Bfloat16Indexer get(long[] indices, float[] h, int offset, int length) {
for (int n = 0; n < length; n++) {
h[offset + n] = get(index(indices) + n);
h[offset + n] = getRaw(index(indices) + n);
}
return this;
}

@Override public Bfloat16Indexer put(long i, float h) {
public Bfloat16Indexer putRaw(long i, float h) {
RAW.putShort(base + checkIndex(i, size) * VALUE_BYTES, (short)fromFloat(h));
return this;
}
@Override public Bfloat16Indexer put(long i, float h) {
return putRaw(index(i), h);
}
@Override public Bfloat16Indexer put(long i, float[] h, int offset, int length) {
for (int n = 0; n < length; n++) {
put(i * strides[0] + n, h[offset + n]);
putRaw(index(i) + n, h[offset + n]);
}
return this;
}
@Override public Bfloat16Indexer put(long i, long j, float h) {
put(i * strides[0] + j, h);
putRaw(index(i, j), h);
return this;
}
@Override public Bfloat16Indexer put(long i, long j, float[] h, int offset, int length) {
for (int n = 0; n < length; n++) {
put(i * strides[0] + j * strides[1] + n, h[offset + n]);
putRaw(index(i, j) + n, h[offset + n]);
}
return this;
}
@Override public Bfloat16Indexer put(long i, long j, long k, float h) {
put(i * strides[0] + j * strides[1] + k, h);
putRaw(index(i, j, k), h);
return this;
}
@Override public Bfloat16Indexer put(long[] indices, float h) {
put(index(indices), h);
putRaw(index(indices), h);
return this;
}
@Override public Bfloat16Indexer put(long[] indices, float[] h, int offset, int length) {
for (int n = 0; n < length; n++) {
put(index(indices) + n, h[offset + n]);
putRaw(index(indices) + n, h[offset + n]);
}
return this;
}
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/org/bytedeco/javacpp/indexer/BooleanArrayIndexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,25 @@ public BooleanArrayIndexer(boolean[] array, long[] sizes, long[] strides) {
}

@Override public boolean get(long i) {
return array[(int)i];
return array[(int)index(i)];
}
@Override public BooleanIndexer get(long i, boolean[] b, int offset, int length) {
for (int n = 0; n < length; n++) {
b[offset + n] = array[(int)i * (int)strides[0] + n];
b[offset + n] = array[(int)index(i) + n];
}
return this;
}
@Override public boolean get(long i, long j) {
return array[(int)i * (int)strides[0] + (int)j];
return array[(int)index(i, j)];
}
@Override public BooleanIndexer get(long i, long j, boolean[] b, int offset, int length) {
for (int n = 0; n < length; n++) {
b[offset + n] = array[(int)i * (int)strides[0] + (int)j * (int)strides[1] + n];
b[offset + n] = array[(int)index(i, j) + n];
}
return this;
}
@Override public boolean get(long i, long j, long k) {
return array[(int)i * (int)strides[0] + (int)j * (int)strides[1] + (int)k];
return array[(int)index(i, j, k)];
}
@Override public boolean get(long... indices) {
return array[(int)index(indices)];
Expand All @@ -83,27 +83,27 @@ public BooleanArrayIndexer(boolean[] array, long[] sizes, long[] strides) {
}

@Override public BooleanIndexer put(long i, boolean b) {
array[(int)i] = b;
array[(int)index(i)] = b;
return this;
}
@Override public BooleanIndexer put(long i, boolean[] b, int offset, int length) {
for (int n = 0; n < length; n++) {
array[(int)i * (int)strides[0] + n] = b[offset + n];
array[(int)index(i) + n] = b[offset + n];
}
return this;
}
@Override public BooleanIndexer put(long i, long j, boolean b) {
array[(int)i * (int)strides[0] + (int)j] = b;
array[(int)index(i, j)] = b;
return this;
}
@Override public BooleanIndexer put(long i, long j, boolean[] b, int offset, int length) {
for (int n = 0; n < length; n++) {
array[(int)i * (int)strides[0] + (int)j * (int)strides[1] + n] = b[offset + n];
array[(int)index(i, j) + n] = b[offset + n];
}
return this;
}
@Override public BooleanIndexer put(long i, long j, long k, boolean b) {
array[(int)i * (int)strides[0] + (int)j * (int)strides[1] + (int)k] = b;
array[(int)index(i, j, k)] = b;
return this;
}
@Override public BooleanIndexer put(long[] indices, boolean b) {
Expand Down
Loading

0 comments on commit 01b234a

Please sign in to comment.