public class Lexicographic
extends java.lang.Object
This class has static methods for lexicographically comparing bytes. Lexicographic comparison compares the unsigned byte representations starting from the left-most bit. It is like alphabetical comparison but for bytes. For example, consider the following:
The lexicographic ordering for this example is c < a < b
.
Modifier and Type | Field and Description |
---|---|
static java.util.Comparator<byte[]> |
BYTE_ARRAY_COMPARATOR
A Comparator for performing lexicographical comparison on
byte[] arrays. |
static java.util.Comparator<java.nio.ByteBuffer> |
BYTE_BUFFER_COMPARATOR
A Comparator for performing lexicographical comparison on
ByteBuffer instances. |
Modifier and Type | Method and Description |
---|---|
static int |
compare(byte[] a,
byte[] b)
Performs lexicographical comparison on two byte arrays, starting with index 0.
|
static int |
compare(java.nio.ByteBuffer a,
java.nio.ByteBuffer b)
Performs lexicographical comparison on two
ByteBuffer s, starting with index 0. |
static int |
compare(byte a,
byte b)
Perform lexicographical (ie, unsigned) comparison on two bytes
|
static java.util.Iterator<java.nio.ByteBuffer> |
merge(java.util.Iterator<java.nio.ByteBuffer> a,
java.util.Iterator<java.nio.ByteBuffer> b,
java.util.Iterator<java.nio.ByteBuffer>... moreIterators)
Takes two or more iterators as input and creates a
MergeIterator that produces values in lexicographic
order as would be produced by merge(List, List) |
static java.util.List<java.nio.ByteBuffer> |
merge(java.util.List<java.nio.ByteBuffer> a,
java.util.List<java.nio.ByteBuffer> b)
Lexicographically merges the two provided lists of ByteBuffers.
|
static java.nio.ByteBuffer[] |
sort(java.nio.ByteBuffer[] bufs)
Performs an in-place sort of
bufs lexicographically |
static java.util.List<java.nio.ByteBuffer> |
sort(java.util.List<java.nio.ByteBuffer> bufs)
Performs an in-place sort of
bufs lexicographically |
public static final java.util.Comparator<byte[]> BYTE_ARRAY_COMPARATOR
byte[]
arrays.public static final java.util.Comparator<java.nio.ByteBuffer> BYTE_BUFFER_COMPARATOR
ByteBuffer
instances.public static int compare(byte a, byte b)
a
- a byte. this function uses the unsigned value of a, so if a < 0
, the function compares a + 256.b
- a byte. this function uses the unsigned value of b, so if b < 0
, the function compares b + 256.a
and b
, returning a negative integer if
a < b
, 0 if a == b
, or a positive integer if a > b
.public static int compare(byte[] a, byte[] b)
a
- an array of bytesb
- an array of bytesa < b
, 0 if a == b
, or a
positive integer if a > b
.public static int compare(java.nio.ByteBuffer a, java.nio.ByteBuffer b)
ByteBuffer
s, starting with index 0. This method uses absolute
indexes to compare a and b, and does not change their position or limit.a
- an array of bytesb
- an array of bytesa < b
, 0 if a == b
, or a
positive integer if a > b
.public static java.nio.ByteBuffer[] sort(java.nio.ByteBuffer[] bufs)
bufs
lexicographicallybufs
- an array of bytebuffersbufs
, sorted lexicographicallypublic static java.util.List<java.nio.ByteBuffer> sort(java.util.List<java.nio.ByteBuffer> bufs)
bufs
lexicographicallybufs
- a list of bytebuffersbufs
, sorted lexicographicallypublic static java.util.List<java.nio.ByteBuffer> merge(java.util.List<java.nio.ByteBuffer> a, java.util.List<java.nio.ByteBuffer> b)
Lexicographically merges the two provided lists of ByteBuffers. This method does not sort a, or b, or the returned list.
If the merge encounters identical values (e.g., the next value of a == the next value of b), then both a and b are advanced, and the value is only include once. For example:
a = [ 00000100, 00001000, 00000001 ]; // [4, 8, 1] b = [ 00000010, 00001000, 00000011 ]; // [2, 8, 3] merge(a, b) = [ 00000010, 00000100, 00001000, 00000001, 00000011 ]; // [2, 4, 8, 1, 3]
a
- a list of bytebuffers, possibly nullb
- a list of bytebuffers, possibly null@SafeVarargs public static java.util.Iterator<java.nio.ByteBuffer> merge(java.util.Iterator<java.nio.ByteBuffer> a, java.util.Iterator<java.nio.ByteBuffer> b, java.util.Iterator<java.nio.ByteBuffer>... moreIterators)
Takes two or more iterators as input and creates a MergeIterator
that produces values in lexicographic
order as would be produced by merge(List, List)
If the merge encounters identical values (e.g., the next value of a == the next value of b), then both a and b are advanced, and the value is only include once. For example:
a = [ 00000100, 00001000, 00000001 ]; // [4, 8, 1] b = [ 00000010, 00001000, 00000011 ]; // [2, 8, 3] merge(a, b) = [ 00000010, 00000100, 00001000, 00000001, 00000011 ]; // [2, 4, 8, 1, 3]
a
- an iterator of bytebuffers, possibly null, possibly exhaustedb
- an iterator of bytebuffers, possibly null, possibly exhaustedmoreIterators
- zero or more iterators of bytebuffers, possibly null, possibly exhausted