From 6ee9a6dcfdf53a8b2ed57798d8cd626039f2508c Mon Sep 17 00:00:00 2001 From: Andrea Aime Date: Thu, 23 May 2024 10:08:03 +0200 Subject: [PATCH] Optimize shuffle method --- .../main/java/ucar/nc2/filter/Shuffle.java | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/cdm/core/src/main/java/ucar/nc2/filter/Shuffle.java b/cdm/core/src/main/java/ucar/nc2/filter/Shuffle.java index 2e24fcf29f..27379f9c6a 100644 --- a/cdm/core/src/main/java/ucar/nc2/filter/Shuffle.java +++ b/cdm/core/src/main/java/ucar/nc2/filter/Shuffle.java @@ -61,24 +61,24 @@ public byte[] encode(byte[] dataIn) { @Override public byte[] decode(byte[] dataIn) { - if (dataIn.length % elemSize != 0 || elemSize <= 1) { - return dataIn; - } - - int nElems = dataIn.length / elemSize; - int[] start = new int[elemSize]; - for (int k = 0; k < elemSize; k++) { - start[k] = k * nElems; - } - - byte[] result = new byte[dataIn.length]; - for (int i = 0; i < nElems; i++) { - for (int j = 0; j < elemSize; j++) { - result[(i * elemSize) + j] = dataIn[i + start[j]]; + if (dataIn.length % this.elemSize == 0 && this.elemSize > 1) { + int nElems = dataIn.length / this.elemSize; + byte[] result = new byte[dataIn.length]; + + for (int j = 0; j < this.elemSize; ++j) { + int sourceIndex = j * nElems; + int destIndex = j; + for (int i = 0; i < nElems; ++i) { + result[destIndex] = dataIn[sourceIndex]; + sourceIndex++; + destIndex += this.elemSize; + } } - } - return result; + return result; + } else { + return dataIn; + } } public static class Provider implements FilterProvider {