Skip to content

Replacing byte[] and ByteBuffer compareTo methods with JDK builtins #199

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

Closed
Closed
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
30 changes: 1 addition & 29 deletions src/main/java/org/lmdbjava/ByteArrayProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,42 +45,14 @@ public final class ByteArrayProxy extends BufferProxy<byte[]> {
private ByteArrayProxy() {
}

/**
* Lexicographically compare two byte arrays.
*
* @param o1 left operand (required)
* @param o2 right operand (required)
* @return as specified by {@link Comparable} interface
*/
@SuppressWarnings("PMD.CompareObjectsWithEquals")
public static int compareArrays(final byte[] o1, final byte[] o2) {
requireNonNull(o1);
requireNonNull(o2);
if (o1 == o2) {
return 0;
}
final int minLength = Math.min(o1.length, o2.length);

for (int i = 0; i < minLength; i++) {
final int lw = Byte.toUnsignedInt(o1[i]);
final int rw = Byte.toUnsignedInt(o2[i]);
final int result = Integer.compareUnsigned(lw, rw);
if (result != 0) {
return result;
}
}

return o1.length - o2.length;
}

@Override
protected byte[] allocate() {
return new byte[0];
}

@Override
protected int compare(final byte[] o1, final byte[] o2) {
return compareArrays(o1, o2);
return Arrays.compare(o1, o2);
}

@Override
Expand Down
42 changes: 1 addition & 41 deletions src/main/java/org/lmdbjava/ByteBufferProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,46 +115,6 @@ abstract static class AbstractByteBufferProxy extends BufferProxy<ByteBuffer> {
private static final ThreadLocal<ArrayDeque<ByteBuffer>> BUFFERS
= withInitial(() -> new ArrayDeque<>(16));

/**
* Lexicographically compare two buffers.
*
* @param o1 left operand (required)
* @param o2 right operand (required)
* @return as specified by {@link Comparable} interface
*/
@SuppressWarnings("PMD.CyclomaticComplexity")
public static int compareBuff(final ByteBuffer o1, final ByteBuffer o2) {
requireNonNull(o1);
requireNonNull(o2);
if (o1.equals(o2)) {
return 0;
}
final int minLength = Math.min(o1.limit(), o2.limit());
final int minWords = minLength / Long.BYTES;

final boolean reverse1 = o1.order() == LITTLE_ENDIAN;
final boolean reverse2 = o2.order() == LITTLE_ENDIAN;
for (int i = 0; i < minWords * Long.BYTES; i += Long.BYTES) {
final long lw = reverse1 ? reverseBytes(o1.getLong(i)) : o1.getLong(i);
final long rw = reverse2 ? reverseBytes(o2.getLong(i)) : o2.getLong(i);
final int diff = Long.compareUnsigned(lw, rw);
if (diff != 0) {
return diff;
}
}

for (int i = minWords * Long.BYTES; i < minLength; i++) {
final int lw = Byte.toUnsignedInt(o1.get(i));
final int rw = Byte.toUnsignedInt(o2.get(i));
final int result = Integer.compareUnsigned(lw, rw);
if (result != 0) {
return result;
}
}

return o1.remaining() - o2.remaining();
}

static Field findField(final Class<?> c, final String name) {
Class<?> clazz = c;
do {
Expand Down Expand Up @@ -190,7 +150,7 @@ protected final ByteBuffer allocate() {

@Override
protected final int compare(final ByteBuffer o1, final ByteBuffer o2) {
return compareBuff(o1, o2);
return o1.compareTo(o2);
}

@Override
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy