Example usage for com.google.common.primitives Ints ensureCapacity

List of usage examples for com.google.common.primitives Ints ensureCapacity

Introduction

In this page you can find the example usage for com.google.common.primitives Ints ensureCapacity.

Prototype

public static int[] ensureCapacity(int[] array, int minLength, int padding) 

Source Link

Document

Returns an array containing the same values as array , but guaranteed to be of a specified minimum length.

Usage

From source file:com.spotify.helios.cli.Table.java

public void row(final Object... row) {
    columns = Ints.ensureCapacity(columns, row.length, row.length);
    for (int i = 0; i < row.length; i++) {
        row[i] = row[i].toString();//from www.java 2s  . c  o m
        columns[i] = max(columns[i], row[i].toString().length());
    }
    rows.add(row);
}

From source file:org.openscience.cdk.graph.invariant.MorganNumbersTools.java

/**
 * Makes an array containing the morgan numbers of the atoms of
 * atomContainer. These number are the extended connectivity values and not
 * the lexicographic smallest labelling on the graph.
 *
 * @param molecule the molecule to analyse.
 * @return The morgan numbers value.//from   w ww .j  a va  2  s  . c o  m
 */
public static long[] getMorganNumbers(IAtomContainer molecule) {

    int order = molecule.getAtomCount();

    long[] currentInvariants = new long[order];
    long[] previousInvariants = new long[order];

    int[][] graph = new int[order][INITIAL_DEGREE];
    int[] degree = new int[order];

    // which atoms are the non-hydrogens.
    int[] nonHydrogens = new int[order];

    for (int v = 0; v < order; v++)
        nonHydrogens[v] = "H".equals(molecule.getAtom(v).getSymbol()) ? 0 : 1;

    // build the graph and initialise the current connectivity
    // value to the number of connected non-hydrogens
    for (IBond bond : molecule.bonds()) {
        int u = molecule.getAtomNumber(bond.getAtom(0));
        int v = molecule.getAtomNumber(bond.getAtom(1));
        graph[u] = Ints.ensureCapacity(graph[u], degree[u] + 1, INITIAL_DEGREE);
        graph[v] = Ints.ensureCapacity(graph[v], degree[v] + 1, INITIAL_DEGREE);
        graph[u][degree[u]++] = v;
        graph[v][degree[v]++] = u;
        currentInvariants[u] += nonHydrogens[v];
        currentInvariants[v] += nonHydrogens[u];
    }

    // iteratively sum the connectivity values for each vertex
    for (int i = 0; i < order; i++) {
        System.arraycopy(currentInvariants, 0, previousInvariants, 0, order);
        for (int u = 0; u < order; u++) {
            currentInvariants[u] = 0;

            // for each of the vertices adjacent to 'u' sum their
            // previous connectivity value
            int[] neighbors = graph[u];
            for (int j = 0; j < degree[u]; j++) {
                int v = neighbors[j];
                currentInvariants[u] += previousInvariants[v] * nonHydrogens[v];
            }
        }
    }
    return currentInvariants;
}

From source file:io.airlift.stats.cardinality.DenseHll.java

public void insert(int bucket, int value) {
    int delta = value - baseline;
    final int oldDelta = getDelta(bucket);

    if (delta <= oldDelta || (oldDelta == MAX_DELTA && (delta <= oldDelta + getOverflow(bucket)))) {
        // the old bucket value is (baseline + oldDelta) + possibly an overflow, so it's guaranteed to be >= the new value
        return;// w  w  w .  j  a  v a2  s .  c  o m
    }

    if (delta > MAX_DELTA) {
        int overflow = delta - MAX_DELTA;

        if (!setOverflow(bucket, overflow)) {
            // grow overflows arrays if necessary
            overflowBuckets = Ints.ensureCapacity(overflowBuckets, overflows + 1, OVERFLOW_GROW_INCREMENT);
            overflowValues = Bytes.ensureCapacity(overflowValues, overflows + 1, OVERFLOW_GROW_INCREMENT);

            overflowBuckets[overflows] = bucket;
            overflowValues[overflows] = (byte) overflow;
            overflows++;
        }

        delta = MAX_DELTA;
    }

    setDelta(bucket, delta);

    if (oldDelta == 0) {
        --baselineCount;
        adjustBaselineIfNeeded();
    }
}

From source file:io.airlift.stats.cardinality.DenseHll.java

/**
 * Returns "this" for chaining/*  www . j av a 2 s.co  m*/
 */
public DenseHll mergeWith(DenseHll other) {
    if (indexBitLength != other.indexBitLength) {
        throw new IllegalArgumentException(
                String.format("Cannot merge HLLs with different number of buckets: %s vs %s",
                        numberOfBuckets(indexBitLength), numberOfBuckets(other.indexBitLength)));
    }

    int baseline = Math.max(this.baseline, other.baseline);
    int baselineCount = 0;

    int overflows = 0;
    int[] overflowBuckets = new int[OVERFLOW_GROW_INCREMENT];
    byte[] overflowValues = new byte[OVERFLOW_GROW_INCREMENT];

    int numberOfBuckets = numberOfBuckets(indexBitLength);
    for (int i = 0; i < numberOfBuckets; i++) {
        int value = Math.max(getValue(i), other.getValue(i));

        int delta = value - baseline;
        if (delta == 0) {
            baselineCount++;
        } else if (delta > MAX_DELTA) {
            // grow overflows arrays if necessary
            overflowBuckets = Ints.ensureCapacity(overflowBuckets, overflows + 1, OVERFLOW_GROW_INCREMENT);
            overflowValues = Bytes.ensureCapacity(overflowValues, overflows + 1, OVERFLOW_GROW_INCREMENT);

            overflowBuckets[overflows] = i;
            overflowValues[overflows] = (byte) (delta - MAX_DELTA);

            overflows++;

            delta = MAX_DELTA;
        }

        setDelta(i, delta);
    }

    this.baseline = (byte) baseline;
    this.baselineCount = baselineCount;
    this.overflows = overflows;
    this.overflowBuckets = overflowBuckets;
    this.overflowValues = overflowValues;

    // all baseline values in one of the HLLs lost to the values
    // in the other HLL, so we need to adjust the final baseline
    adjustBaselineIfNeeded();

    return this;
}