Java Code Examples for com.google.common.primitives.UnsignedBytes#compare()

The following examples show how to use com.google.common.primitives.UnsignedBytes#compare() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MongoBsonTranslator.java    From mongowp with Apache License 2.0 6 votes vote down vote up
protected static BinarySubtype getBinarySubtype(byte readByte) {
  switch (readByte) {
    case 0x00:
      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));
      }
    }
  }
}
 
Example 2
Source File: ParsingTools.java    From mongowp with Apache License 2.0 6 votes vote down vote up
protected static BinarySubtype getBinarySubtype(byte readByte) {
  switch (readByte) {
    case 0x00:
      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));
      }
    }
  }
}
 
Example 3
Source File: Ideas_2011_08_01.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
@ExpectWarning(value="RV_CHECK_COMPARETO_FOR_SPECIFIC_RETURN_VALUE", num = 9)
public static int testGuavaPrimitiveCompareCalls() {
    int count = 0;
    if (Booleans.compare(false, true) == -1)
        count++;
    if (Chars.compare('a', 'b') == -1)
        count++;
    if (Doubles.compare(1, 2) == -1)
        count++;
    if (Floats.compare(1, 2) == -1)
        count++;
    if (Ints.compare(1, 2) == -1)
        count++;
    if (Longs.compare(1, 2) == -1)
        count++;
    if (Shorts.compare((short) 1, (short) 2) == -1)
        count++;
    if (SignedBytes.compare((byte) 1, (byte) 2) == -1)
        count++;
    if (UnsignedBytes.compare((byte) 1, (byte) 2) == -1)
        count++;
    return count;

}
 
Example 4
Source File: AtriumIpAddress.java    From atrium-odl with Apache License 2.0 5 votes vote down vote up
@Override
public int compareTo(AtriumIpAddress o) {
    // Compare first the version
    if (this.version != o.version) {
        return this.version.compareTo(o.version);
    }

    // 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
}
 
Example 5
Source File: IpAddress.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public int compareTo(IpAddress o) {
    // Compare first the version
    if (this.version != o.version) {
        return this.version.compareTo(o.version);
    }

    // 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
}
 
Example 6
Source File: Comparables.java    From tikv-client-lib-java with Apache License 2.0 5 votes vote down vote up
@Override
public int compareTo(@Nonnull ComparableByteString other) {
  requireNonNull(other, "other is null");
  ByteString otherBytes = other.bytes;
  int n = Math.min(bytes.size(), otherBytes.size());
  for (int i = 0, j = 0; i < n; i++, j++) {
    int cmp = UnsignedBytes.compare(bytes.byteAt(i), otherBytes.byteAt(j));
    if (cmp != 0) return cmp;
  }
  // one is the prefix of other then the longer is larger
  return bytes.size() - otherBytes.size();
}
 
Example 7
Source File: UnsignedByteBuffer.java    From tracingplane-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public int compare(ByteBuffer left, ByteBuffer right) {
    if (!left.hasArray() || !right.hasArray()) {
        // TODO: might nonetheless be faster to copy bytes out of buffers in chunks of 8
        return UnsignedByteBuffer.lexicographicalComparatorJavaImpl().compare(left, right);
    }
    int initialLeftPosition = left.position();
    int initialRightPosition = right.position();

    try {
        int minLength = Math.min(left.remaining(), right.remaining());
        int minWords = minLength / Longs.BYTES;

        byte[] leftArray = left.array();
        byte[] rightArray = right.array();
        int leftOffset = left.arrayOffset() + initialLeftPosition;
        int rightOffset = right.arrayOffset() + initialRightPosition;

        /* 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(leftArray, BYTE_ARRAY_BASE_OFFSET + leftOffset + (long) i);
            long rw = theUnsafe.getLong(rightArray, BYTE_ARRAY_BASE_OFFSET + rightOffset + (long) i);
            if (lw != rw) {
                if (BIG_ENDIAN) {
                    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)) - ((int) ((rw >>> n) & UNSIGNED_MASK));
            }
        }

        // The epilogue to cover the last (minLength % 8) elements.
        for (int i = minWords * Longs.BYTES; i < minLength; i++) {
            int result = UnsignedBytes.compare(leftArray[leftOffset + i], rightArray[rightOffset + i]);
            if (result != 0) {
                return result;
            }
        }
        return left.remaining() - right.remaining();
    } finally {
        left.position(initialLeftPosition);
        right.position(initialRightPosition);
    }
}
 
Example 8
Source File: FastByteComparisons.java    From tez with Apache License 2.0 4 votes vote down vote up
/**
 * Lexicographically compare two arrays.
 *
 * @param buffer1 left operand
 * @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.
 */
@Override
public int compareTo(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;
}
 
Example 9
Source File: FastByteComparisons.java    From big-c with Apache License 2.0 4 votes vote down vote up
/**
 * Lexicographically compare two arrays.
 *
 * @param buffer1 left operand
 * @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.
 */
@Override
public int compareTo(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;
}
 
Example 10
Source File: FastByteComparisons.java    From hadoop with Apache License 2.0 4 votes vote down vote up
/**
 * Lexicographically compare two arrays.
 *
 * @param buffer1 left operand
 * @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.
 */
@Override
public int compareTo(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;
}
 
Example 11
Source File: InnerNodeUnitTest.java    From adaptive-radix-tree with MIT License 4 votes vote down vote up
@Override
public int compareTo(Pair o) {
	return UnsignedBytes.compare(partialKey, o.partialKey);
}
 
Example 12
Source File: HadoopUtils.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * Internal comparison routine.
 *
 * @param buf1 Bytes 1.
 * @param len1 Length 1.
 * @param ptr2 Pointer 2.
 * @param len2 Length 2.
 * @return Result.
 */
@SuppressWarnings("SuspiciousNameCombination")
public 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;
}
 
Example 13
Source File: FastByteComparison.java    From rya with Apache License 2.0 4 votes vote down vote up
/**
 * Lexicographically compare two arrays.
 *
 * @param buffer1 left operand
 * @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.
 */
@Override
public int compareTo(final byte[] buffer1, final int offset1, final int length1, final byte[] buffer2,
        final int offset2, final int length2) {
    // Short circuit equal case
    if (buffer1 == buffer2 && offset1 == offset2 && length1 == length2) {
        return 0;
    }
    final int minLength = Math.min(length1, length2);
    final int minWords = minLength / Longs.BYTES;
    final int offset1Adj = offset1 + BYTE_ARRAY_BASE_OFFSET;
    final 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) {
        final long lw = theUnsafe.getLong(buffer1, offset1Adj + (long) i);
        final long rw = theUnsafe.getLong(buffer2, offset2Adj + (long) i);
        final 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++) {
        final int result = UnsignedBytes.compare(buffer1[offset1 + i], buffer2[offset2 + i]);
        if (result != 0) {
            return result;
        }
    }
    return length1 - length2;
}
 
Example 14
Source File: UnSafeTupleBytesComparator.java    From tajo with Apache License 2.0 4 votes vote down vote up
public static int compare(long ptr1, long ptr2) {
  int lstrLen = UNSAFE.getInt(ptr1);
  int rstrLen = UNSAFE.getInt(ptr2);

  ptr1 += SizeOf.SIZE_OF_INT;
  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;
}
 
Example 15
Source File: UnsafeComparer.java    From tajo with Apache License 2.0 4 votes vote down vote up
@Override public int compare(byte[] left, byte[] right) {
  int minLength = Math.min(left.length, right.length);
  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 = 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;
}
 
Example 16
Source File: FastByteComparisons.java    From tikv-client-lib-java with Apache License 2.0 4 votes vote down vote up
/**
 * Lexicographically compare two arrays.
 *
 * @param buffer1 left operand
 * @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.
 */
@Override
public int compareTo(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;
}
 
Example 17
Source File: FastByteComparisons.java    From ethereumj with MIT License 4 votes vote down vote up
/**
 * Lexicographically compare two arrays.
 *
 * @param buffer1 left operand
 * @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.
 */
@Override
public int compareTo(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;
}
 
Example 18
Source File: DescVarLengthFastByteComparisons.java    From phoenix with Apache License 2.0 4 votes vote down vote up
/**
 * Lexicographically compare two arrays.
 *
 * @param buffer1
 *            left operand
 * @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.
 */
@Override
public int compareTo(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; }
    if (length1 == 0 && length2 != 0) { // nulls sort first, even for descending
        return -1; 
    }
    if (length2 == 0 && length1 != 0) { // nulls sort first, even for descending
        return 1; 
    }
    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 length2 - length1;
}
 
Example 19
Source File: FastByteComparisons.java    From client-java with Apache License 2.0 4 votes vote down vote up
/**
 * Lexicographically compare two arrays.
 *
 * @param buffer1 left operand
 * @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.
 */
@Override
public int compareTo(
    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;
}
 
Example 20
Source File: Lexicographic.java    From tracingplane-java with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Perform lexicographical (ie, unsigned) comparison on two bytes
 * 
 * @param a a byte. this function uses the unsigned value of a, so if {@code a < 0}, the function compares a + 256.
 * @param b a byte. this function uses the unsigned value of b, so if {@code b < 0}, the function compares b + 256.
 * @return compares the unsigned values of <code>a</code> and <code>b</code>, returning a negative integer if
 *         {@code a < b}, 0 if {@code a == b}, or a positive integer if {@code a > b}.
 */
public static int compare(byte a, byte b) {
    return UnsignedBytes.compare(a, b);
}