Example usage for com.google.common.primitives UnsignedLongs compare

List of usage examples for com.google.common.primitives UnsignedLongs compare

Introduction

In this page you can find the example usage for com.google.common.primitives UnsignedLongs compare.

Prototype

@CheckReturnValue
public static int compare(long a, long b) 

Source Link

Document

Compares the two specified long values, treating them as unsigned values between 0 and 2^64 - 1 inclusive.

Usage

From source file:org.apache.tajo.tuple.offheap.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;// w w  w  . jav a  2  s.  co  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);
        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));
        }

        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 = UNSAFE.getByte(ptr1++) - UNSAFE.getByte(ptr2++);
        if (result != 0) {
            return result;
        }
    }
    return lstrLen - rstrLen;
}

From source file:org.eclipse.milo.opcua.sdk.server.events.operators.LessThan.java

@Nullable
@Override//from  ww w .  j a  va 2s.  com
protected Boolean apply(OperatorContext context, BaseEventNode eventNode, BuiltinDataType dataType,
        @Nullable Object operand0, @Nullable Object operand1) {

    if (operand0 instanceof Number && operand1 instanceof Number) {
        Number n0 = (Number) operand0;
        Number n1 = (Number) operand1;

        switch (dataType) {
        case SByte:
        case Int16:
        case Int32:
        case Int64:
        case Byte:
        case UInt16:
        case UInt32:
            return n0.longValue() < n1.longValue();

        case UInt64:
            return UnsignedLongs.compare(n0.longValue(), n1.longValue()) < 0;

        case Float:
            return n0.floatValue() < n1.floatValue();

        case Double:
            return n0.doubleValue() < n1.doubleValue();

        default:
            return false;
        }
    } else {
        return false;
    }
}

From source file:org.eclipse.milo.opcua.sdk.server.events.operators.GreaterThan.java

@Nullable
@Override/*from w  ww .j av a2 s .c  o m*/
protected Boolean apply(OperatorContext context, BaseEventNode eventNode, BuiltinDataType dataType,
        @Nullable Object operand0, @Nullable Object operand1) {

    if (operand0 instanceof Number && operand1 instanceof Number) {
        Number n0 = (Number) operand0;
        Number n1 = (Number) operand1;

        switch (dataType) {
        case SByte:
        case Int16:
        case Int32:
        case Int64:
        case Byte:
        case UInt16:
        case UInt32:
            return n0.longValue() > n1.longValue();

        case UInt64:
            return UnsignedLongs.compare(n0.longValue(), n1.longValue()) > 0;

        case Float:
            return n0.floatValue() > n1.floatValue();

        case Double:
            return n0.doubleValue() > n1.doubleValue();

        default:
            return false;
        }
    } else {
        return false;
    }
}

From source file:org.eclipse.milo.opcua.sdk.server.events.operators.LessThanOrEqual.java

@Nullable
@Override/*from  w w  w  . ja  va  2  s.  c  om*/
protected Boolean apply(OperatorContext context, BaseEventNode eventNode, BuiltinDataType dataType,
        @Nullable Object operand0, @Nullable Object operand1) {

    if (operand0 instanceof Number && operand1 instanceof Number) {
        Number n0 = (Number) operand0;
        Number n1 = (Number) operand1;

        switch (dataType) {
        case SByte:
        case Int16:
        case Int32:
        case Int64:
        case Byte:
        case UInt16:
        case UInt32:
            return n0.longValue() <= n1.longValue();

        case UInt64:
            return UnsignedLongs.compare(n0.longValue(), n1.longValue()) <= 0;

        case Float:
            return n0.floatValue() <= n1.floatValue();

        case Double:
            return n0.doubleValue() <= n1.doubleValue();

        default:
            return false;
        }
    } else {
        return false;
    }
}

From source file:org.eclipse.milo.opcua.sdk.server.events.operators.GreaterThanOrEqual.java

@Nullable
@Override//from   w  w w.j  a  va 2  s . com
protected Boolean apply(OperatorContext context, BaseEventNode eventNode, BuiltinDataType dataType,
        @Nullable Object operand0, @Nullable Object operand1) {

    if (operand0 instanceof Number && operand1 instanceof Number) {
        Number n0 = (Number) operand0;
        Number n1 = (Number) operand1;

        switch (dataType) {
        case SByte:
        case Int16:
        case Int32:
        case Int64:
        case Byte:
        case UInt16:
        case UInt32:
            return n0.longValue() >= n1.longValue();

        case UInt64:
            return UnsignedLongs.compare(n0.longValue(), n1.longValue()) >= 0;

        case Float:
            return n0.floatValue() >= n1.floatValue();

        case Double:
            return n0.doubleValue() >= n1.doubleValue();

        default:
            return false;
        }
    } else {
        return false;
    }
}

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;//from w  w  w  . j av  a2 s.  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:com.cinchapi.concourse.server.model.PrimaryKey.java

/**
 * Compares keys such that they are sorted in ascending order.
 *//* ww w.j  av a  2s  .co  m*/
@Override
public int compareTo(PrimaryKey other) {
    return UnsignedLongs.compare(data, other.data);
}

From source file:org.apache.cassandra.db.marshal.UUIDType.java

public int compare(ByteBuffer b1, ByteBuffer b2) {
    // Compare for length
    int s1 = b1.position(), s2 = b2.position();
    int l1 = b1.limit(), l2 = b2.limit();

    // should we assert exactly 16 bytes (or 0)? seems prudent
    boolean p1 = l1 - s1 == 16, p2 = l2 - s2 == 16;
    if (!(p1 & p2)) {
        assert p1 | (l1 == s1);
        assert p2 | (l2 == s2);
        return p1 ? 1 : p2 ? -1 : 0;
    }//  w w w . j  a  v a 2 s . c o m

    // Compare versions
    long msb1 = b1.getLong(s1);
    long msb2 = b2.getLong(s2);

    int version1 = (int) ((msb1 >>> 12) & 0xf);
    int version2 = (int) ((msb2 >>> 12) & 0xf);
    if (version1 != version2)
        return version1 - version2;

    // bytes: version is top 4 bits of byte 6
    // then: [6.5-8), [4-6), [0-4)
    if (version1 == 1) {
        long reorder1 = TimeUUIDType.reorderTimestampBytes(msb1);
        long reorder2 = TimeUUIDType.reorderTimestampBytes(msb2);
        // we know this is >= 0, since the top 3 bits will be 0
        int c = Long.compare(reorder1, reorder2);
        if (c != 0)
            return c;
    } else {
        int c = UnsignedLongs.compare(msb1, msb2);
        if (c != 0)
            return c;
    }

    return UnsignedLongs.compare(b1.getLong(s1 + 8), b2.getLong(s2 + 8));
}

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  w  w  .j  av a 2  s.  com*/
    }

    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:org.projectfloodlight.openflow.types.U64.java

@Override
public int compareTo(U64 o) {
    return UnsignedLongs.compare(raw, o.raw);
}