com.google.common.primitives.UnsignedBytes#compare ( )源码实例Demo

下面列出了com.google.common.primitives.UnsignedBytes#compare ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: spotbugs   文件: Ideas_2011_08_01.java
@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;

}
 
源代码2 项目: mongowp   文件: ParsingTools.java
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));
      }
    }
  }
}
 
源代码3 项目: mongowp   文件: MongoBsonTranslator.java
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));
      }
    }
  }
}
 
源代码4 项目: tikv-client-lib-java   文件: Comparables.java
@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();
}
 
源代码5 项目: atrium-odl   文件: AtriumIpAddress.java
@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
}
 
源代码6 项目: onos   文件: IpAddress.java
@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
}
 
源代码7 项目: adaptive-radix-tree   文件: InnerNodeUnitTest.java
@Override
public int compareTo(Pair o) {
	return UnsignedBytes.compare(partialKey, o.partialKey);
}
 
源代码8 项目: client-java   文件: FastByteComparisons.java
/**
 * 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;
}
 
/**
 * 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;
}
 
源代码10 项目: hadoop   文件: FastByteComparisons.java
/**
 * 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;
}
 
源代码11 项目: tracingplane-java   文件: UnsignedByteBuffer.java
@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);
    }
}
 
源代码12 项目: tez   文件: FastByteComparisons.java
/**
 * 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;
}
 
源代码13 项目: big-c   文件: FastByteComparisons.java
/**
 * 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;
}
 
源代码14 项目: ignite   文件: HadoopUtils.java
/**
 * 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;
}
 
源代码15 项目: rya   文件: FastByteComparison.java
/**
 * 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;
}
 
源代码16 项目: tajo   文件: 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;
  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;
}
 
源代码17 项目: tajo   文件: UnsafeComparer.java
@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;
}
 
源代码18 项目: ethereumj   文件: FastByteComparisons.java
/**
 * 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;
}
 
源代码19 项目: phoenix   文件: DescVarLengthFastByteComparisons.java
/**
 * 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;
}
 
源代码20 项目: tracingplane-java   文件: Lexicographic.java
/**
 * 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);
}