Example usage for com.google.common.math IntMath pow

List of usage examples for com.google.common.math IntMath pow

Introduction

In this page you can find the example usage for com.google.common.math IntMath pow.

Prototype

@GwtIncompatible("failing tests")
public static int pow(int b, int k) 

Source Link

Document

Returns b to the k th power.

Usage

From source file:spimedb.util.bloom.ByteArrayFilter.java

/**
 * Constructs a ByteArrayFilter with an underlying array of the given size, rounded up to the next
 * power of two./*  w w  w. j a  v a2s . c  o  m*/
 *
 * This rounding occurs because the hashing is much faster on an array the size of a power of two.
 * If you really want a different sized array, used the AtomicReferenceArray constructor.
 *
 * @param size The size of the underlying array.
 */
public ByteArrayFilter(int size) {
    if (size <= 0) {
        throw new IllegalArgumentException("array size must be greater than zero, was " + size);
    }
    if (size > MAX_SIZE) {
        throw new IllegalArgumentException(
                "array size may not be larger than 2**31-1, but will be rounded to larger. was " + size);
    }
    // round to the next largest power of two
    int poweredSize = IntMath.pow(2, IntMath.log2(size, RoundingMode.CEILING));
    this.sizeMask = poweredSize - 1;
    this.array = new AtomicReferenceArray<>(poweredSize);
}

From source file:spimedb.util.bloom.UnBloomFilter.java

/**
 * Constructs a OoaBFilter with an underlying array of the given size, rounded up to the next
 * power of two./*from  w w  w  .  j  av  a 2  s . c  o m*/
 * <p>
 * This rounding occurs because the hashing is much faster on an array the size of a power of two.
 *
 * @param size    The size of the underlying array.
 * @param asBytes
 */
public UnBloomFilter(int size, Function<X, byte[]> asBytes) {
    final int MAX_SIZE = 1 << 30;
    if (size <= 0 || size > MAX_SIZE) {
        throw new IllegalArgumentException(
                "array size must be greater than 0 and array size may not be larger than 2**31-1, but will be rounded to larger. was "
                        + size);
    }

    this.asBytes = asBytes;
    int poweredSize = IntMath.pow(2, IntMath.log2(size, RoundingMode.CEILING)); // round to the next largest power of two
    this.array = new AtomicReferenceArray(poweredSize);
    this.sizeMask = poweredSize - 1;

}

From source file:ai.grakn.test.kbs.AbstractPathKB.java

protected void buildExtensionalDB(GraknTx tx, int n, int children) {
    long startTime = System.currentTimeMillis();

    EntityType vertex = tx.getEntityType("vertex");
    EntityType startVertex = tx.getEntityType("start-vertex");
    Role arcFrom = tx.getRole("arc-from");
    Role arcTo = tx.getRole("arc-to");

    RelationshipType arc = tx.getRelationshipType("arc");
    putEntity(tx, "a0", startVertex, key);

    for (int i = 1; i <= n; i++) {
        int m = IntMath.pow(children, i);
        for (int j = 0; j < m; j++) {
            putEntity(tx, "a" + i + "," + j, vertex, key);
            if (j != 0 && j % 100 == 0) {
                System.out.println(j + " entities out of " + m + " inserted");
            }//from w  w  w  .j a  v a 2 s  .co  m
        }
    }

    for (int j = 0; j < children; j++) {
        arc.addRelationship().addRolePlayer(arcFrom, getInstance(tx, "a0")).addRolePlayer(arcTo,
                getInstance(tx, "a1," + j));
    }

    for (int i = 1; i < n; i++) {
        int m = IntMath.pow(children, i);
        for (int j = 0; j < m; j++) {
            for (int c = 0; c < children; c++) {
                arc.addRelationship().addRolePlayer(arcFrom, getInstance(tx, "a" + i + "," + j))
                        .addRolePlayer(arcTo, getInstance(tx, "a" + (i + 1) + "," + (j * children + c)));

            }
            if (j != 0 && j % 100 == 0) {
                System.out.println(
                        "level " + i + "/" + (n - 1) + ": " + j + " entities out of " + m + " connected");
            }
        }
    }

    long loadTime = System.currentTimeMillis() - startTime;
    System.out.println("PathKB loading time: " + loadTime + " ms");
}

From source file:ai.grakn.test.graphs.AbstractPathGraph.java

protected void buildExtensionalDB(GraknGraph graph, int n, int children) {
    long startTime = System.currentTimeMillis();

    EntityType vertex = graph.getEntityType("vertex");
    EntityType startVertex = graph.getEntityType("start-vertex");
    Role arcFrom = graph.getRole("arc-from");
    Role arcTo = graph.getRole("arc-to");

    RelationType arc = graph.getRelationType("arc");
    putEntity(graph, "a0", startVertex, key);

    for (int i = 1; i <= n; i++) {
        int m = IntMath.pow(children, i);
        for (int j = 0; j < m; j++) {
            putEntity(graph, "a" + i + "," + j, vertex, key);
            if (j != 0 && j % 100 == 0) {
                System.out.println(j + " entities out of " + m + " inserted");
            }//from   www.j  a va  2s  . c o  m
        }
    }

    for (int j = 0; j < children; j++) {
        arc.addRelation().addRolePlayer(arcFrom, getInstance(graph, "a0")).addRolePlayer(arcTo,
                getInstance(graph, "a1," + j));
    }

    for (int i = 1; i < n; i++) {
        int m = IntMath.pow(children, i);
        for (int j = 0; j < m; j++) {
            for (int c = 0; c < children; c++) {
                arc.addRelation().addRolePlayer(arcFrom, getInstance(graph, "a" + i + "," + j))
                        .addRolePlayer(arcTo, getInstance(graph, "a" + (i + 1) + "," + (j * children + c)));

            }
            if (j != 0 && j % 100 == 0) {
                System.out.println(
                        "level " + i + "/" + (n - 1) + ": " + j + " entities out of " + m + " connected");
            }
        }
    }

    long loadTime = System.currentTimeMillis() - startTime;
    System.out.println("PathGraph loading time: " + loadTime + " ms");
}

From source file:spimedb.util.bloom.OoaBFilter.java

/**
 * Constructs a OoaBFilter with an underlying array of the given size, rounded up to the next
 * power of two./*www  .j  av  a2s . c  o  m*/
 *
 * This rounding occurs because the hashing is much faster on an array the size of a power of two.
 *
 * @param size The size of the underlying array.
 * @param bufSize The size of the buffers occupying each slot in the array.
 */
public OoaBFilter(int size, int bufSize) {
    if (size <= 0) {
        throw new IllegalArgumentException("array size must be greater than zero, was " + size);
    }
    if (size > MAX_SIZE) {
        throw new IllegalArgumentException(
                "array size may not be larger than 2**31-1, but will be rounded to larger. was " + size);
    }
    // round to the next largest power of two
    int poweredSize = IntMath.pow(2, IntMath.log2(size, RoundingMode.CEILING));
    this.sizeMask = poweredSize - 1;
    this.array = new ByteBuffer[poweredSize];

    // pre-allocate a ByteBuffer for each slot in the array
    int i = 0;
    while (i < poweredSize) {
        array[i] = ByteBuffer.allocate(bufSize);
        i++;
    }
}

From source file:graphene.util.ooab.ByteArrayFilter.java

/**
 * Constructs a ByteArrayFilter with an underlying array of the given size, rounded up to the next
 * power of two./*from w w  w.  ja  v a 2 s  .c om*/
 *
 * This rounding occurs because the hashing is much faster on an array the size of a power of two.
 * If you really want a different sized array, used the AtomicReferenceArray constructor.
 *
 * @param size The size of the underlying array.
 */
public ByteArrayFilter(int size) {
    if (size <= 0) {
        throw new IllegalArgumentException("array size must be greater than zero, was " + size);
    }
    if (size > MAX_SIZE) {
        throw new IllegalArgumentException(
                "array size may not be larger than 2**31-1, but will be rounded to larger. was " + size);
    }
    // round to the next largest power of two
    int poweredSize = IntMath.pow(2, IntMath.log2(size, RoundingMode.CEILING));
    this.sizeMask = poweredSize - 1;
    this.array = new AtomicReferenceArray<byte[]>(poweredSize);
}

From source file:grakn.core.graql.reasoner.graph.PathTreeGraph.java

void buildTree(String fromRoleValue, String toRoleValue, int n, int children, TransactionOLTP tx) {
    Role fromRole = tx.getRole(fromRoleValue);
    Role toRole = tx.getRole(toRoleValue);

    EntityType vertex = tx.getEntityType("vertex");
    EntityType startVertex = tx.getEntityType("start-vertex");

    RelationType arc = tx.getRelationType("arc");
    putEntityWithResource(tx, "a0", startVertex, key);

    int outputThreshold = 500;
    for (int i = 1; i <= n; i++) {
        int m = IntMath.pow(children, i);
        for (int j = 0; j < m; j++) {
            putEntityWithResource(tx, "a" + i + "," + j, vertex, key);
            if (j != 0 && j % outputThreshold == 0) {
                System.out.println(j + " entities out of " + m + " inserted");
            }/* w  w  w. j  a  v a  2  s  . c  o m*/
        }
    }

    for (int j = 0; j < children; j++) {
        arc.create().assign(fromRole, getInstance(tx, "a0")).assign(toRole, getInstance(tx, "a1," + j));
    }

    for (int i = 1; i < n; i++) {
        int m = IntMath.pow(children, i);
        for (int j = 0; j < m; j++) {
            for (int c = 0; c < children; c++) {
                arc.create().assign(fromRole, getInstance(tx, "a" + i + "," + j)).assign(toRole,
                        getInstance(tx, "a" + (i + 1) + "," + (j * children + c)));

            }
            if (j != 0 && j % outputThreshold == 0) {
                System.out.println(
                        "level " + i + "/" + (n - 1) + ": " + j + " entities out of " + m + " connected");
            }
        }
    }
}

From source file:net.bafeimao.umbrella.support.network.netty.SimpleSocketClient.java

public SimpleSocketClient connect() {
    group = new NioEventLoopGroup();

    do {//  w  ww. j a  v  a2 s .c  om
        try {
            Bootstrap b = new Bootstrap();
            b.group(group).channel(NioSocketChannel.class).handler(new ClientChannelInitializer());
            this.channel = b.connect(host, port).sync().channel();

            this.setConnected();
        } catch (Exception e) {
            LOGGER.error("{}", e.getMessage());
        }

        if (!connected) {
            try {
                TimeUnit.SECONDS.sleep(Math.min(60, IntMath.pow(2, retryTimes)));
            } catch (InterruptedException ignored) {
            }

            LOGGER.info("Retrying to connect to server ... [{}]", retryTimes);
        }

    } while (!connected && retryTimes++ <= maxRetryTimes);

    if (!connected) {
        LOGGER.info("Failed to connect.");
    }

    return this;
}

From source file:org.spf4j.perf.impl.QuantizedRecorder.java

public QuantizedRecorder(final Object measuredEntity, final String description, final String unitOfMeasurement,
        final int factor, final int lowerMagnitude, final int higherMagnitude, final int quantasPerMagnitude) {
    assert (quantasPerMagnitude <= factor);
    assert (lowerMagnitude < higherMagnitude);
    assert (quantasPerMagnitude > 0);
    this.factor = factor;
    this.lowerMagnitude = lowerMagnitude;
    this.higherMagnitude = higherMagnitude;
    minMeasurement = Long.MAX_VALUE;
    maxMeasurement = Long.MIN_VALUE;
    measurementCount = 0;/*ww  w . ja va 2 s.  c o  m*/
    measurementTotal = 0;
    this.quatizedMeasurements = new long[(higherMagnitude - lowerMagnitude) * quantasPerMagnitude + 2];
    this.quantasPerMagnitude = quantasPerMagnitude;
    magnitudes = new long[higherMagnitude - lowerMagnitude + 1];

    int idx = 0;
    if (lowerMagnitude < 0) {
        int toMagnitude = Math.min(-1, higherMagnitude);
        int toValue = -IntMath.pow(factor, -toMagnitude);
        idx = toMagnitude - lowerMagnitude;
        int j = idx;
        while (j >= 0) {
            magnitudes[j--] = toValue;
            toValue *= factor;
        }
        idx++;
    }
    if (lowerMagnitude <= 0 && higherMagnitude >= 0) {
        magnitudes[idx++] = 0;
    }

    int fromMagnitude = Math.max(1, lowerMagnitude);
    int fromValue = IntMath.pow(factor, fromMagnitude);
    int j = idx;
    while (j < magnitudes.length) {
        magnitudes[j++] = fromValue;
        fromValue *= factor;
    }

    final List<String> uom = new ArrayList();

    final List<String> result = new ArrayList();
    result.add("total");
    uom.add(unitOfMeasurement);
    result.add("count");
    uom.add("count");
    result.add("min");
    uom.add(unitOfMeasurement);
    result.add("max");
    uom.add(unitOfMeasurement);
    result.add("QNI_" + magnitudes[0]);
    uom.add("count");

    if (magnitudes.length > 0) {
        long prevVal = magnitudes[0];
        for (int i = 1; i < magnitudes.length; i++) {
            long magVal = magnitudes[i];
            long intSize = magVal - prevVal;
            for (j = 0; j < quantasPerMagnitude; j++) {
                result.add("Q" + (prevVal + intSize * j / quantasPerMagnitude) + "_"
                        + (prevVal + intSize * (j + 1) / quantasPerMagnitude));
                uom.add("count");
            }
            prevVal = magVal;
        }
        result.add("Q" + prevVal + "_PI");
        uom.add("count");
    }
    info = new EntityMeasurementsInfoImpl(measuredEntity, description,
            result.toArray(new String[result.size()]), uom.toArray(new String[uom.size()]));

}

From source file:org.apache.hadoop.hive.ql.udf.generic.GenericUDFGrouping.java

@Override
public Object evaluate(DeferredObject[] arguments) throws HiveException {
    // groupingId = PrimitiveObjectInspectorUtils.getInt(arguments[0].get(), groupingIdOI);
    // Check that the bit at the given index is '1' or '0'
    int result = 0;
    // grouping(c1, c2, c3)
    // is equivalent to
    // 4 * grouping(c1) + 2 * grouping(c2) + grouping(c3)
    for (int a = 1; a < arguments.length; a++) {
        result += IntMath.pow(2, indices.length - a)
                * ((PrimitiveObjectInspectorUtils.getInt(arguments[0].get(), groupingIdOI) >> indices[a - 1])
                        & 1);//from   w  w  w. j a v  a  2  s .  c  om
    }
    intWritable.set(result);
    return intWritable;
}