public void testCompare() { for (long a : UNSIGNED_INTS) { for (long b : UNSIGNED_INTS) { int cmpAsLongs = Longs.compare(a, b); int cmpAsUInt = UnsignedInts.compare((int) a, (int) b); assertEquals(Integer.signum(cmpAsLongs), Integer.signum(cmpAsUInt)); } } }
@Override public int compare(int[] left, int[] right) { int minLength = Math.min(left.length, right.length); for (int i = 0; i < minLength; i++) { if (left[i] != right[i]) { return UnsignedInts.compare(left[i], right[i]); } } return left.length - right.length; }
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); } } 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); }
protected final int compare(int index1, int index2) { final boolean n1 = nulls.get(index1); final boolean n2 = nulls.get(index2); if (n1 && n2) { return 0; } if (n1 ^ n2) { return nullsFirst ? (n1 ? -1 : 1) : (n1 ? 1 : -1); } int compare; switch (type) { case 0: compare = Booleans.compare(booleans[index1], booleans[index2]); break; case 1: compare = bits[index1] - bits[index2]; break; case 2: compare = Shorts.compare(shorts[index1], shorts[index2]); break; case 3: compare = Ints.compare(ints[index1], ints[index2]); break; case 4: compare = Longs.compare(longs[index1], longs[index2]); break; case 5: compare = Floats.compare(floats[index1], floats[index2]); break; case 6: compare = Doubles.compare(doubles[index1], doubles[index2]); break; case 7: compare = TextDatum.COMPARATOR.compare(bytes[index1], bytes[index2]); break; case 8: compare = UnsignedInts.compare(ints[index1], ints[index2]); break; default: throw new IllegalArgumentException(); } return ascending ? compare : -compare; }