List of usage examples for com.google.common.primitives UnsignedBytes compare
@CheckReturnValue public static int compare(byte a, byte b)
From source file:c5db.C5Compare.java
private static int compareTo(byte[] left, byte[] right) { if (isEmpty(left)) { if (isEmpty(right)) { return 0; }/* w w w. j a v a 2 s.c o m*/ return 1; } else if (isEmpty(right)) { return -1; } int minLength = Math.min(left.length, right.length); for (int i = 0; i < minLength; i++) { int result = UnsignedBytes.compare(left[i], right[i]); if (result != 0) { return result; } } return left.length - right.length; }
From source file:com.nearinfinity.honeycomb.hbase.ByteArrayComparator.java
@Override public int compare(byte[] b1, byte[] b2) { if (b1.length != b2.length) { return b1.length - b2.length; }// w w w . ja v a 2 s .c o m for (int i = 0; i < b1.length; i++) { if (b1[i] == b2[i]) { continue; } return UnsignedBytes.compare(b1[i], b2[i]); } return 0; }
From source file:org.apache.tajo.tuple.memory.UnSafeTupleBytesComparator.java
public static int compare(long ptr1, long ptr2) { int lstrLen = UNSAFE.getInt(ptr1); int rstrLen = UNSAFE.getInt(ptr2); ptr1 += SizeOf.SIZE_OF_INT;/*ww w. j av a 2s .c o m*/ ptr2 += SizeOf.SIZE_OF_INT; int minLength = Math.min(lstrLen, rstrLen); int minWords = minLength / Longs.BYTES; /* * Compare 8 bytes at a time. Benchmarking shows comparing 8 bytes at a * time is no slower than comparing 4 bytes at a time even on 32-bit. * On the other hand, it is substantially faster on 64-bit. */ for (int i = 0; i < minWords * Longs.BYTES; i += Longs.BYTES) { long lw = UNSAFE.getLong(ptr1); long rw = UNSAFE.getLong(ptr2); if (lw != rw) { if (!littleEndian) { return UnsignedLongs.compare(lw, rw); } /* * We want to compare only the first index where left[index] != right[index]. * This corresponds to the least significant nonzero byte in lw ^ rw, since lw * and rw are little-endian. Long.numberOfTrailingZeros(diff) tells us the least * significant nonzero bit, and zeroing out the first three bits of L.nTZ gives us the * shift to get that least significant nonzero byte. */ int n = Long.numberOfTrailingZeros(lw ^ rw) & ~0x7; return (int) (((lw >>> n) & UNSIGNED_MASK) - ((rw >>> n) & UNSIGNED_MASK)); } ptr1 += SizeOf.SIZE_OF_LONG; ptr2 += SizeOf.SIZE_OF_LONG; } // The epilogue to cover the last (minLength % 8) elements. for (int i = minWords * Longs.BYTES; i < minLength; i++) { int result = UnsignedBytes.compare(UNSAFE.getByte(ptr1++), UNSAFE.getByte(ptr2++)); if (result != 0) { return result; } } return lstrLen - rstrLen; }
From source file:org.apache.ignite.hadoop.io.TextPartiallyRawComparator.java
/** * Internal comparison routine./*from ww w . j av a 2s .c o m*/ * * @param buf1 Bytes 1. * @param len1 Length 1. * @param ptr2 Pointer 2. * @param len2 Length 2. * @return Result. */ @SuppressWarnings("SuspiciousNameCombination") private static int compareBytes(byte[] buf1, int len1, long ptr2, int len2) { int minLength = Math.min(len1, len2); int minWords = minLength / Longs.BYTES; for (int i = 0; i < minWords * Longs.BYTES; i += Longs.BYTES) { long lw = GridUnsafe.getLong(buf1, GridUnsafe.BYTE_ARR_OFF + i); long rw = GridUnsafe.getLong(ptr2 + i); long diff = lw ^ rw; if (diff != 0) { if (GridUnsafe.BIG_ENDIAN) return (lw + Long.MIN_VALUE) < (rw + Long.MIN_VALUE) ? -1 : 1; // Use binary search int n = 0; int y; int x = (int) diff; if (x == 0) { x = (int) (diff >>> 32); n = 32; } y = x << 16; if (y == 0) n += 16; else x = y; y = x << 8; if (y == 0) n += 8; return (int) (((lw >>> n) & 0xFFL) - ((rw >>> n) & 0xFFL)); } } // The epilogue to cover the last (minLength % 8) elements. for (int i = minWords * Longs.BYTES; i < minLength; i++) { int res = UnsignedBytes.compare(buf1[i], GridUnsafe.getByte(ptr2 + i)); if (res != 0) return res; } return len1 - len2; }
From source file:com.yandex.yoctodb.util.UnsignedByteArrays.java
private static int compare(@NotNull final Buffer left, long leftFrom, final long leftLength, @NotNull final Buffer right, long rightFrom, final long rightLength) { assert leftLength > 0; assert rightLength > 0; // Adapted from Guava UnsignedBytes long length = Math.min(leftLength, rightLength); for (; length >= Longs.BYTES; leftFrom += Longs.BYTES, rightFrom += Longs.BYTES, length -= Longs.BYTES) { final long lw = left.getLong(leftFrom); final long rw = right.getLong(rightFrom); if (lw != rw) { return UnsignedLongs.compare(lw, rw); }//from w ww . ja va2 s. c o m } if (length >= Ints.BYTES) { final int lw = left.getInt(leftFrom); final int rw = right.getInt(rightFrom); if (lw != rw) { return UnsignedInts.compare(lw, rw); } leftFrom += Ints.BYTES; rightFrom += Ints.BYTES; length -= Ints.BYTES; } for (; length > 0; leftFrom++, rightFrom++, length--) { final int result = UnsignedBytes.compare(left.get(leftFrom), right.get(rightFrom)); if (result != 0) { return result; } } return (leftLength < rightLength) ? -1 : ((leftLength == rightLength) ? 0 : 1); }
From source file:com.eightkdata.mongowp.bson.netty.ParsingTools.java
protected static BinarySubtype getBinarySubtype(byte readByte) { switch (readByte) { case 0x00:// w ww. j a v a2 s. c om return BinarySubtype.GENERIC; case 0x01: return BinarySubtype.FUNCTION; case 0x02: return BinarySubtype.OLD_BINARY; case 0x03: return BinarySubtype.OLD_UUID; case 0x04: return BinarySubtype.UUID; case 0x05: return BinarySubtype.MD5; default: { if (UnsignedBytes.compare(readByte, FIRST_USER_DEFINED) >= 0) { return BinarySubtype.USER_DEFINED; } else { throw new AssertionError("Unrecognized binary type 0x" + UnsignedBytes.toString(readByte, 16)); } } } }
From source file:org.apache.tajo.util.UnsafeComparer.java
@Override public int compare(byte[] left, byte[] right) { int minLength = Math.min(left.length, right.length); int minWords = minLength / Longs.BYTES; /*// ww w .j a v a 2 s . c o m * Compare 8 bytes at a time. Benchmarking shows comparing 8 bytes at a * time is no slower than comparing 4 bytes at a time even on 32-bit. * On the other hand, it is substantially faster on 64-bit. */ for (int i = 0; i < minWords * Longs.BYTES; i += Longs.BYTES) { long lw = theUnsafe.getLong(left, BYTE_ARRAY_BASE_OFFSET + (long) i); long rw = theUnsafe.getLong(right, BYTE_ARRAY_BASE_OFFSET + (long) i); long diff = lw ^ rw; if (diff != 0) { if (!littleEndian) { return UnsignedLongs.compare(lw, rw); } // Use binary search int n = 0; int y; int x = (int) diff; if (x == 0) { x = (int) (diff >>> 32); n = 32; } y = x << 16; if (y == 0) { n += 16; } else { x = y; } y = x << 8; if (y == 0) { n += 8; } return (int) (((lw >>> n) & 0xFFL) - ((rw >>> n) & 0xFFL)); } } // The epilogue to cover the last (minLength % 8) elements. for (int i = minWords * Longs.BYTES; i < minLength; i++) { int result = UnsignedBytes.compare(left[i], right[i]); if (result != 0) { return result; } } return left.length - right.length; }
From source file:org.commoncrawl.util.ByteArrayUtils.java
/** * Lexicographically compare two arrays. * * @param buffer1 left operand/*from ww w . j a va 2 s. co m*/ * @param buffer2 right operand * @param offset1 Where to start comparing in the left buffer * @param offset2 Where to start comparing in the right buffer * @param length1 How much to compare from the left buffer * @param length2 How much to compare from the right buffer * @return 0 if equal, < 0 if left is less than right, etc. */ public static int compareBytes(byte[] buffer1, int offset1, int length1, byte[] buffer2, int offset2, int length2) { // Short circuit equal case if (buffer1 == buffer2 && offset1 == offset2 && length1 == length2) { return 0; } int minLength = Math.min(length1, length2); int minWords = minLength / Longs.BYTES; int offset1Adj = offset1 + BYTE_ARRAY_BASE_OFFSET; int offset2Adj = offset2 + BYTE_ARRAY_BASE_OFFSET; /* * Compare 8 bytes at a time. Benchmarking shows comparing 8 bytes at a * time is no slower than comparing 4 bytes at a time even on 32-bit. * On the other hand, it is substantially faster on 64-bit. */ for (int i = 0; i < minWords * Longs.BYTES; i += Longs.BYTES) { long lw = theUnsafe.getLong(buffer1, offset1Adj + (long) i); long rw = theUnsafe.getLong(buffer2, offset2Adj + (long) i); long diff = lw ^ rw; if (diff != 0) { if (!littleEndian) { return lessThanUnsigned(lw, rw) ? -1 : 1; } // Use binary search int n = 0; int y; int x = (int) diff; if (x == 0) { x = (int) (diff >>> 32); n = 32; } y = x << 16; if (y == 0) { n += 16; } else { x = y; } y = x << 8; if (y == 0) { n += 8; } return (int) (((lw >>> n) & 0xFFL) - ((rw >>> n) & 0xFFL)); } } // The epilogue to cover the last (minLength % 8) elements. for (int i = minWords * Longs.BYTES; i < minLength; i++) { int result = UnsignedBytes.compare(buffer1[offset1 + i], buffer2[offset2 + i]); if (result != 0) { return result; } } return length1 - length2; }
From source file:org.onlab.packet.IpAddress.java
@Override public int compareTo(IpAddress o) { // Compare first the version if (this.version != o.version) { return this.version.compareTo(o.version); }// w w w.j a va 2 s . com // Compare the bytes, one-by-one for (int i = 0; i < this.octets.length; i++) { if (this.octets[i] != o.octets[i]) { return UnsignedBytes.compare(this.octets[i], o.octets[i]); } } return 0; // Equal }
From source file:org.opendaylight.atrium.util.AtriumIpAddress.java
@Override public int compareTo(AtriumIpAddress o) { // Compare first the version if (this.version != o.version) { return this.version.compareTo(o.version); }/*ww w . j a va 2 s. co m*/ // Compare the bytes, one-by-one for (int i = 0; i < this.octets.length; i++) { if (this.octets[i] != o.octets[i]) { return UnsignedBytes.compare(this.octets[i], o.octets[i]); } } return 0; // Equal }