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

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

Introduction

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

Prototype

@CheckReturnValue
public static long remainder(long dividend, long divisor) 

Source Link

Document

Returns dividend % divisor, where the dividend and divisor are treated as unsigned 64-bit quantities.

Usage

From source file:org.apache.kudu.client.KeyEncoder.java

/**
 * Returns the bucket of the row for the given hash bucket schema. All columns
 * in the hash bucket schema must be set in the row.
 *
 * @param row the row containing hash schema columns
 * @param hashSchema the hash schema//from   w ww .j  ava 2  s.co  m
 * @return the hash bucket of the row
 */
public static int getHashBucket(PartialRow row, HashBucketSchema hashSchema) {
    ByteVec buf = ByteVec.create();
    encodeColumns(row, hashSchema.getColumnIds(), buf);
    long hash = Murmur2.hash64(buf.data(), buf.len(), hashSchema.getSeed());
    return (int) UnsignedLongs.remainder(hash, hashSchema.getNumBuckets());
}

From source file:org.kududb.client.KeyEncoder.java

/**
 * Encodes the provided row into a partition key according to the partition schema.
 *
 * @param row the row to encode/*from   w  w  w.jav  a  2  s  .  c  om*/
 * @param partitionSchema the partition schema describing the table's partitioning
 * @return an encoded partition key
 */
public byte[] encodePartitionKey(PartialRow row, PartitionSchema partitionSchema) {
    buf.reset();
    if (!partitionSchema.getHashBucketSchemas().isEmpty()) {
        ByteBuffer bucketBuf = ByteBuffer.allocate(4 * partitionSchema.getHashBucketSchemas().size());
        bucketBuf.order(ByteOrder.BIG_ENDIAN);

        for (final HashBucketSchema hashBucketSchema : partitionSchema.getHashBucketSchemas()) {
            encodeColumns(row, hashBucketSchema.getColumnIds());
            byte[] encodedColumns = extractByteArray();
            long hash = Murmur2.hash64(encodedColumns, encodedColumns.length, hashBucketSchema.getSeed());
            int bucket = (int) UnsignedLongs.remainder(hash, hashBucketSchema.getNumBuckets());
            bucketBuf.putInt(bucket);
        }

        assert bucketBuf.arrayOffset() == 0;
        buf.write(bucketBuf.array(), 0, bucketBuf.position());
    }

    encodeColumns(row, partitionSchema.getRangeSchema().getColumns());
    return extractByteArray();
}

From source file:org.sosy_lab.cpachecker.cpa.value.AbstractExpressionValueVisitor.java

/**
 * Calculate an arithmetic operation on two integer types.
 *
 * @param l left hand side value//  w  w w.jav a  2s.co m
 * @param r right hand side value
 * @param op the binary operator
 * @param calculationType The type the result of the calculation should have
 * @param machineModel the machine model
 * @param logger logging
 * @return the resulting value
 */
private static long arithmeticOperation(final long l, final long r, final BinaryOperator op,
        final CType calculationType, final MachineModel machineModel, final LogManager logger) {

    // special handling for UNSIGNED_LONGLONG (32 and 64bit), UNSIGNED_LONG (64bit)
    // because Java only has SIGNED_LONGLONG
    CSimpleType st = getArithmeticType(calculationType);
    if (st != null) {
        if (machineModel.getSizeofInBits(st) >= SIZE_OF_JAVA_LONG && st.isUnsigned()) {
            switch (op) {
            case DIVIDE:
                if (r == 0) {
                    logger.logf(Level.SEVERE, "Division by Zero (%d / %d)", l, r);
                    return 0;
                }
                return UnsignedLongs.divide(l, r);
            case MODULO:
                return UnsignedLongs.remainder(l, r);
            case SHIFT_RIGHT:
                /*
                 * from http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html
                 *
                 * The unsigned right shift operator ">>>" shifts a zero
                 * into the leftmost position, while the leftmost position
                 * after ">>" depends on sign extension.
                 */
                return l >>> r;
            default:
                // fall-through, calculation is done correct as SINGED_LONG_LONG
            }
        }
    }

    switch (op) {
    case PLUS:
        return l + r;
    case MINUS:
        return l - r;
    case DIVIDE:
        if (r == 0) {
            logger.logf(Level.SEVERE, "Division by Zero (%d / %d)", l, r);
            return 0;
        }
        return l / r;
    case MODULO:
        return l % r;
    case MULTIPLY:
        return l * r;
    case SHIFT_LEFT:
        /* There is a difference in the SHIFT-operation in Java and C.
         * In C a SHIFT is a normal SHIFT, in Java the rVal is used as (r%64).
         *
         * http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.19
         *
         * If the promoted type of the left-hand operand is long, then only the
         * six lowest-order bits of the right-hand operand are used as the
         * shift distance. It is as if the right-hand operand were subjected to
         * a bitwise logical AND operator & (15.22.1) with the mask value 0x3f.
         * The shift distance actually used is therefore always in the range 0 to 63.
         */
        return (r >= SIZE_OF_JAVA_LONG) ? 0 : l << r;
    case SHIFT_RIGHT:
        return l >> r;
    case BINARY_AND:
        return l & r;
    case BINARY_OR:
        return l | r;
    case BINARY_XOR:
        return l ^ r;

    default:
        throw new AssertionError("unknown binary operation: " + op);
    }

}

From source file:org.apache.hadoop.hdfs.hoss.meta.IndexHash.java

private static long getWantedSlot(long hash, long capacity) {
    return UnsignedLongs.remainder(hash, capacity);
}