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

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

Introduction

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

Prototype

public static byte[] ensureCapacity(byte[] 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: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;//from   w  w  w.ja  v a  2  s . c om
    }

    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//  w w w  . java  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;
}