From 32132d34c026131700f6dcab460c8b31589afd1f Mon Sep 17 00:00:00 2001 From: Matteo Di Giovinazzo Date: Mon, 27 Apr 2020 16:32:58 -0600 Subject: [PATCH] #391 In case of single dimension, do not calculate strides, so we save some multiplications --- .../org/bytedeco/javacpp/indexer/Index.java | 4 +- .../bytedeco/javacpp/indexer/OneIndex.java | 40 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/bytedeco/javacpp/indexer/OneIndex.java diff --git a/src/main/java/org/bytedeco/javacpp/indexer/Index.java b/src/main/java/org/bytedeco/javacpp/indexer/Index.java index 53ed65464..1ae1d99a9 100644 --- a/src/main/java/org/bytedeco/javacpp/indexer/Index.java +++ b/src/main/java/org/bytedeco/javacpp/indexer/Index.java @@ -28,7 +28,9 @@ */ public abstract class Index { - public static Index ONE = create(1); + public static Index create(long size) { + return new OneIndex(size); + } public static Index create(long... sizes) { return new StrideIndex(sizes, defaultStrides(sizes)); diff --git a/src/main/java/org/bytedeco/javacpp/indexer/OneIndex.java b/src/main/java/org/bytedeco/javacpp/indexer/OneIndex.java new file mode 100644 index 000000000..411a13b2d --- /dev/null +++ b/src/main/java/org/bytedeco/javacpp/indexer/OneIndex.java @@ -0,0 +1,40 @@ +package org.bytedeco.javacpp.indexer; + +public class OneIndex extends Index { + + private final long[] sizes; + + /** + * TODO + * + * @param size + */ + protected OneIndex(long size) { + this.sizes = new long[]{size}; + } + + @Override + public long index(long i) { + return i; + } + + @Override + public long index(long i, long j) { + return i; + } + + @Override + public long index(long i, long j, long k) { + return i; + } + + @Override + public long index(long... indices) { + return indices[0]; + } + + @Override + public long[] sizes() { + return sizes; + } +}