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

The following examples show how to use com.google.common.primitives.UnsignedLongs#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: OpTime.java    From mongowp with Apache License 2.0 5 votes vote down vote up
@Override
public int compareTo(OpTime obj) {
  int diff = timestamp.compareTo(obj.getTimestamp());
  if (diff != 0) {
    return diff;
  }
  return UnsignedLongs.compare(term, obj.getTerm());
}
 
Example 2
Source File: FastByteOperations.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Lexicographically compare two arrays.
 *
 * @param buffer1 left operand: a byte[] or null
 * @param buffer2 right operand: a byte[] or null
 * @param memoryOffset1 Where to start comparing in the left buffer (pure memory address if buffer1 is null, or relative otherwise)
 * @param memoryOffset2 Where to start comparing in the right buffer (pure memory address if buffer1 is null, or relative otherwise)
 * @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 compareTo(Object buffer1, long memoryOffset1, int length1,
                     Object buffer2, long memoryOffset2, int length2)
{
    int minLength = Math.min(length1, length2);

    /*
     * 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.
     */
    int wordComparisons = minLength & ~7;
    for (int i = 0; i < wordComparisons ; i += Longs.BYTES)
    {
        long lw = theUnsafe.getLong(buffer1, memoryOffset1 + (long) i);
        long rw = theUnsafe.getLong(buffer2, memoryOffset2 + (long) i);

        if (lw != rw)
        {
            if (BIG_ENDIAN) {
                return UnsignedLongs.compare(lw, rw);
            }

            return UnsignedLongs.compare(Long.reverseBytes(lw), Long.reverseBytes(rw));
        }
    }

    for (int i = wordComparisons ; i < minLength ; i++)
    {
        int b1 = theUnsafe.getByte(buffer1, memoryOffset1 + i) & 0xFF;
        int b2 = theUnsafe.getByte(buffer2, memoryOffset2 + i) & 0xFF;
        if (b1 != b2) {
            return b1 - b2;
        }
    }

    return length1 - length2;
}
 
Example 3
Source File: FastByteOperations.java    From dble with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Lexicographically compare two arrays.
 *
 * @param buffer1       left operand: a byte[] or null
 * @param buffer2       right operand: a byte[] or null
 * @param memoryOffset1 Where to start comparing in the left buffer (pure memory address if buffer1 is null, or relative otherwise)
 * @param memoryOffset2 Where to start comparing in the right buffer (pure memory address if buffer1 is null, or relative otherwise)
 * @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 compareTo(Object buffer1, long memoryOffset1, int length1,
                            Object buffer2, long memoryOffset2, int length2) {
    int minLength = Math.min(length1, length2);

    /*
     * 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.
     */
    int wordComparisons = minLength & ~7;
    for (int i = 0; i < wordComparisons; i += Longs.BYTES) {
        long lw = THE_UNSAFE.getLong(buffer1, memoryOffset1 + (long) i);
        long rw = THE_UNSAFE.getLong(buffer2, memoryOffset2 + (long) i);

        if (lw != rw) {
            if (BIG_ENDIAN) {
                return UnsignedLongs.compare(lw, rw);
            }

            return UnsignedLongs.compare(Long.reverseBytes(lw), Long.reverseBytes(rw));
        }
    }

    for (int i = wordComparisons; i < minLength; i++) {
        int b1 = THE_UNSAFE.getByte(buffer1, memoryOffset1 + i) & 0xFF;
        int b2 = THE_UNSAFE.getByte(buffer2, memoryOffset2 + i) & 0xFF;
        if (b1 != b2) {
            return b1 - b2;
        }
    }

    return length1 - length2;
}
 
Example 4
Source File: PrefixComparators.java    From indexr with Apache License 2.0 4 votes vote down vote up
@Override
public int compare(long aPrefix, long bPrefix) {
    return UnsignedLongs.compare(aPrefix, bPrefix);
}
 
Example 5
Source File: Utils.java    From bitherj with Apache License 2.0 4 votes vote down vote up
/**
 * Work around lack of unsigned types in Java.
 */
public static boolean isLessThanUnsigned(long n1, long n2) {
    return UnsignedLongs.compare(n1, n2) < 0;
}
 
Example 6
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 7
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 8
Source File: MNumeric.java    From sql-layer with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
protected int doCompare(TInstance typeA, ValueSource sourceA, TInstance typeB, ValueSource sourceB) {
    return UnsignedLongs.compare(sourceA.getInt64(), sourceB.getInt64());
}
 
Example 9
Source File: Utils.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Work around lack of unsigned types in Java.
 */
public static boolean isLessThanOrEqualToUnsigned(long n1, long n2) {
    return UnsignedLongs.compare(n1, n2) <= 0;
}
 
Example 10
Source File: Utils.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Work around lack of unsigned types in Java.
 */
public static boolean isLessThanUnsigned(long n1, long n2) {
    return UnsignedLongs.compare(n1, n2) < 0;
}
 
Example 11
Source File: PrefixComparators.java    From indexr with Apache License 2.0 4 votes vote down vote up
@Override
public int compare(long bPrefix, long aPrefix) {
    return UnsignedLongs.compare(aPrefix, bPrefix);
}
 
Example 12
Source File: Utils.java    From green_android with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Work around lack of unsigned types in Java.
 */
public static boolean isLessThanUnsigned(long n1, long n2) {
    return UnsignedLongs.compare(n1, n2) < 0;
}
 
Example 13
Source File: PrefixComparators.java    From indexr with Apache License 2.0 4 votes vote down vote up
@Override
public int compare(long bPrefix, long aPrefix) {
    return UnsignedLongs.compare(aPrefix, bPrefix);
}
 
Example 14
Source File: PrefixComparators.java    From indexr with Apache License 2.0 4 votes vote down vote up
@Override
public int compare(long aPrefix, long bPrefix) {
    return UnsignedLongs.compare(aPrefix, bPrefix);
}
 
Example 15
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 16
Source File: PrefixComparators.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
@Override
public int compare(long bPrefix, long aPrefix) {
  return UnsignedLongs.compare(aPrefix, bPrefix);
}
 
Example 17
Source File: PrefixComparators.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
@Override
public int compare(long aPrefix, long bPrefix) {
  return UnsignedLongs.compare(aPrefix, bPrefix);
}
 
Example 18
Source File: Utils.java    From green_android with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Work around lack of unsigned types in Java.
 */
public static boolean isLessThanOrEqualToUnsigned(long n1, long n2) {
    return UnsignedLongs.compare(n1, n2) <= 0;
}