Fix sorting
This commit is contained in:
parent
ee1e71da84
commit
bb855a6e27
@ -0,0 +1,11 @@
|
||||
package it.cavallium.buffer;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
public interface ArraysComparator extends Comparator<byte[]> {
|
||||
|
||||
@Override
|
||||
int compare(byte[] a, byte[] b);
|
||||
|
||||
int compare(byte[] a, int aFrom, int aTo, byte[] b, int bFrom, int bTo);
|
||||
}
|
@ -38,6 +38,8 @@ import org.jetbrains.annotations.VisibleForTesting;
|
||||
class ByteListBuf extends ByteArrayList implements Buf {
|
||||
|
||||
private static final String IMMUTABLE_ERROR = "The buffer is immutable";
|
||||
private static final VariableLengthLexiconographicComparator VAR_LENGTH_LEX_COMP = new VariableLengthLexiconographicComparator();
|
||||
|
||||
private boolean immutable;
|
||||
|
||||
protected ByteListBuf(byte[] a, boolean wrapped) {
|
||||
@ -511,7 +513,7 @@ class ByteListBuf extends ByteArrayList implements Buf {
|
||||
|
||||
int contentsCompareTo(byte[] otherA, int otherAFrom, int otherATo) {
|
||||
if (a == otherA && from == otherAFrom && to == otherATo) return 0;
|
||||
return Arrays.compareUnsigned(a, from, to, otherA, otherAFrom, otherATo);
|
||||
return VAR_LENGTH_LEX_COMP.compare(a, from, to, otherA, otherAFrom, otherATo);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -886,7 +888,7 @@ class ByteListBuf extends ByteArrayList implements Buf {
|
||||
final int s1 = size(), s2 = l.size();
|
||||
final byte[] a1 = a, a2 = l.elements();
|
||||
if (a1 == a2 && s1 == s2) return 0;
|
||||
return Arrays.compareUnsigned(a, 0, s1, a2, 0, s2);
|
||||
return VAR_LENGTH_LEX_COMP.compare(a, 0, s1, a2, 0, s2);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -0,0 +1,17 @@
|
||||
package it.cavallium.buffer;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class VariableLengthLexiconographicComparator implements ArraysComparator {
|
||||
|
||||
@Override
|
||||
public int compare(byte[] a, byte[] b) {
|
||||
return a.length != b.length ? Integer.compare(a.length, b.length) : Arrays.compareUnsigned(a, b);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compare(byte[] a, int aFrom, int aTo, byte[] b, int bFrom, int bTo) {
|
||||
return (aTo - aFrom) != (bTo - bFrom) ? Integer.compare(aTo - aFrom, bTo - bFrom)
|
||||
: Arrays.compareUnsigned(a, aFrom, aTo, b, bFrom, bTo);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user