io.deephaven.engine.table.impl.join.stamp.CharReverseStampKernel Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of deephaven-engine-table Show documentation
Show all versions of deephaven-engine-table Show documentation
Engine Table: Implementation and closely-coupled utilities
/**
* Copyright (c) 2016-2022 Deephaven Data Labs and Patent Pending
*/
package io.deephaven.engine.table.impl.join.stamp;
import io.deephaven.chunk.*;
import io.deephaven.engine.rowset.chunkattributes.RowKeys;
import io.deephaven.chunk.attributes.Values;
import io.deephaven.engine.rowset.RowSequence;
public class CharReverseStampKernel implements StampKernel {
static final CharReverseStampKernel INSTANCE = new CharReverseStampKernel();
private CharReverseStampKernel() {} // static use only
@Override
public void computeRedirections(Chunk leftStamps, Chunk rightStamps, LongChunk rightKeyIndices, WritableLongChunk leftRedirections) {
computeRedirections(leftStamps.asCharChunk(), rightStamps.asCharChunk(), rightKeyIndices, leftRedirections);
}
static private void computeRedirections(CharChunk leftStamps, CharChunk rightStamps, LongChunk rightKeyIndices, WritableLongChunk leftRedirections) {
final int leftSize = leftStamps.size();
final int rightSize = rightStamps.size();
if (rightSize == 0) {
leftRedirections.fillWithValue(0, leftSize, RowSequence.NULL_ROW_KEY);
leftRedirections.setSize(leftSize);
return;
}
int rightLowIdx = 0;
char rightLowValue = rightStamps.get(0);
final int maxRightIdx = rightSize - 1;
for (int li = 0; li < leftSize; ) {
final char leftValue = leftStamps.get(li);
if (lt(leftValue, rightLowValue)) {
leftRedirections.set(li++, RowSequence.NULL_ROW_KEY);
continue;
}
else if (eq(leftValue, rightLowValue)) {
leftRedirections.set(li++, rightKeyIndices.get(rightLowIdx));
continue;
}
int rightHighIdx = rightSize;
while (rightLowIdx < rightHighIdx) {
final int rightMidIdx = ((rightHighIdx - rightLowIdx) / 2) + rightLowIdx;
final char rightMidValue = rightStamps.get(rightMidIdx);
if (leq(rightMidValue, leftValue)) {
rightLowIdx = rightMidIdx;
rightLowValue = rightMidValue;
if (rightLowIdx == rightHighIdx - 1 || eq(rightLowValue, leftValue)) {
break;
}
} else {
rightHighIdx = rightMidIdx;
}
}
final long redirectionKey = rightKeyIndices.get(rightLowIdx);
if (rightLowIdx == maxRightIdx) {
leftRedirections.fillWithValue(li, leftSize - li, redirectionKey);
return;
} else {
leftRedirections.set(li++, redirectionKey);
final char nextRightValue = rightStamps.get(rightLowIdx + 1);
while (li < leftSize && lt(leftStamps.get(li), nextRightValue)) {
leftRedirections.set(li++, redirectionKey);
}
}
}
}
// region comparison functions
// note that this is a descending kernel, thus the comparisons here are backwards (e.g., the lt function is in terms of the sort direction, so is implemented by gt)
private static int doComparison(char lhs, char rhs) {
return -1 * Character.compare(lhs, rhs);
}
// endregion comparison functions
private static boolean lt(char lhs, char rhs) {
return doComparison(lhs, rhs) < 0;
}
private static boolean leq(char lhs, char rhs) {
return doComparison(lhs, rhs) <= 0;
}
private static boolean eq(char lhs, char rhs) {
// region equality function
return lhs == rhs;
// endregion equality function
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy