Example usage for java.lang Integer SIZE

List of usage examples for java.lang Integer SIZE

Introduction

In this page you can find the example usage for java.lang Integer SIZE.

Prototype

int SIZE

To view the source code for java.lang Integer SIZE.

Click Source Link

Document

The number of bits used to represent an int value in two's complement binary form.

Usage

From source file:com.moz.fiji.schema.FormattedEntityId.java

/**
 * Create an hbase row key, which is a byte array from the given formatted fijiRowKey.
 * This method requires that the fijiRowKey argument is the correct length for the specified
 * format./* www . j  av  a  2s.  co m*/
 * The following encoding will be used to ensure correct ordering:
 * Strings are UTF-8 encoded and terminated by a null byte. Strings cannot contain "\u0000".
 * Integers are exactly 4 bytes long.
 * Longs are exactly 8 bytes long.
 * Both integers and longs have the sign bit flipped so that their values are wrapped around to
 * create the correct lexicographic ordering. (i.e. after converting to byte array,
 * MIN_INT < 0 < MAX_INT).
 * Hashed components are exactly hash_size bytes long and are the first component of
 * the hbase key.
 * Except for the first, all components of a fijiRowKey can be null. However, to maintain
 * ordering, all components to the right of a null component must also be null. Nullable index
 * in the row key format specifies which component (and hence following components) are nullable.
 * By default, the hash only uses the first component, but this can be changed using the Range
 * Scan index.
 *
 * @param format The formatted row key format for this table.
 * @param fijiRowKey An ordered list of Objects of the key components.
 * @return A byte array representing the encoded Hbase row key.
 */
private static byte[] makeHbaseRowKey(RowKeyFormat2 format, List<Object> fijiRowKey) {

    ArrayList<byte[]> hbaseKey = new ArrayList<byte[]>();
    final byte zeroDelim = 0;

    int pos;
    for (pos = 0; pos < fijiRowKey.size(); pos++) {
        // we have already done the validation check for null cascades.
        if (null == fijiRowKey.get(pos)) {
            continue;
        }
        byte[] tempBytes;
        switch (getType(fijiRowKey.get(pos))) {
        case STRING:
            if (((String) fijiRowKey.get(pos)).contains("\u0000")) {
                throw new EntityIdException("String component cannot contain \u0000");
            }
            try {
                hbaseKey.add(((String) fijiRowKey.get(pos)).getBytes("UTF-8"));
            } catch (UnsupportedEncodingException e) {
                LOG.error(e.toString());
                throw new EntityIdException(String.format("UnsupportedEncoding for component %d", pos));
            }
            break;
        case INTEGER:
            int temp = (Integer) fijiRowKey.get(pos);
            tempBytes = ByteBuffer.allocate(Integer.SIZE / Byte.SIZE).putInt(temp).array();
            tempBytes[0] = (byte) ((int) tempBytes[0] ^ (int) Byte.MIN_VALUE);
            hbaseKey.add(tempBytes);
            break;
        case LONG:
            long templong = (Long) fijiRowKey.get(pos);
            tempBytes = ByteBuffer.allocate(Long.SIZE / Byte.SIZE).putLong(templong).array();
            tempBytes[0] = (byte) ((int) tempBytes[0] ^ (int) Byte.MIN_VALUE);
            hbaseKey.add(tempBytes);
            break;
        default:
            throw new RuntimeException("Invalid code path");
        }
    }

    // hash stuff
    int hashUpto = format.getRangeScanStartIndex() - 1;
    ByteArrayOutputStream tohash = new ByteArrayOutputStream();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    for (pos = 0; pos <= hashUpto && pos < hbaseKey.size(); pos++) {
        tohash.write(hbaseKey.get(pos), 0, hbaseKey.get(pos).length);
    }
    byte[] hashed = Arrays.copyOfRange(Hasher.hash(tohash.toByteArray()), 0, format.getSalt().getHashSize());
    baos.write(hashed, 0, hashed.length);

    // to materialize or not to materialize that is the question
    if (format.getSalt().getSuppressKeyMaterialization()) {
        return baos.toByteArray();
    } else {
        for (pos = 0; pos < hbaseKey.size(); pos++) {
            baos.write(hbaseKey.get(pos), 0, hbaseKey.get(pos).length);
            if (format.getComponents().get(pos).getType() == ComponentType.STRING
                    || format.getComponents().get(pos) == null) {
                // empty strings will be encoded as null, hence we need to delimit them too
                baos.write(zeroDelim);
            }
        }
        return baos.toByteArray();
    }
}

From source file:org.apache.beam.sdk.io.synthetic.SyntheticOptions.java

public void validate() {
    checkArgument(keySizeBytes > 0, "keySizeBytes should be a positive number, but found %s", keySizeBytes);
    checkArgument(valueSizeBytes >= 0, "valueSizeBytes should be a non-negative number, but found %s",
            valueSizeBytes);/*from w  w  w. j  a va2 s .co  m*/
    checkArgument(numHotKeys >= 0, "numHotKeys should be a non-negative number, but found %s", numHotKeys);
    checkArgument(hotKeyFraction >= 0, "hotKeyFraction should be a non-negative number, but found %s",
            hotKeyFraction);
    if (hotKeyFraction > 0) {
        int intBytes = Integer.SIZE / 8;
        checkArgument(keySizeBytes >= intBytes, "Allowing hot keys (hotKeyFraction=%s) requires keySizeBytes "
                + "to be at least %s, but found %s", hotKeyFraction, intBytes, keySizeBytes);
    }
}

From source file:com.yyl.inputmethod.accessibility.AccessibilityEntityProvider.java

/**
 * Generates a virtual view identifier for the given key. Returned
 * identifiers are valid until the next global layout state change.
 *
 * @param key The key to identify.//  ww  w  .j  a  v a  2s .com
 * @return A virtual view identifier.
 */
private static int generateVirtualViewIdForKey(final Key key) {
    // The key x- and y-coordinates are stable between layout changes.
    // Generate an identifier by bit-shifting the x-coordinate to the
    // left-half of the integer and OR'ing with the y-coordinate.
    return ((0xFFFF & key.mX) << (Integer.SIZE / 2)) | (0xFFFF & key.mY);
}

From source file:com.android.hareime.accessibility.AccessibilityEntityProvider.java

/**
 * Generates a virtual view identifier for the given key. Returned
 * identifiers are valid until the next global layout state change.
 *
 * @param key The key to identify./*  w  w  w.  ja  va 2  s.c  om*/
 * @return A virtual view identifier.
 */
private static int generateVirtualViewIdForKey(Key key) {
    // The key x- and y-coordinates are stable between layout changes.
    // Generate an identifier by bit-shifting the x-coordinate to the
    // left-half of the integer and OR'ing with the y-coordinate.
    return ((0xFFFF & key.mX) << (Integer.SIZE / 2)) | (0xFFFF & key.mY);
}

From source file:com.onyx.latinime.accessibility.AccessibilityEntityProvider.java

/**
 * Generates a virtual view identifier for the given key. Returned
 * identifiers are valid until the next global layout state change.
 *
 * @param key The key to identify./*from  w  w  w  .  j  a  v a 2s .co  m*/
 * @return A virtual view identifier.
 */
private static int generateVirtualViewIdForKey(final Key key) {
    // The key x- and y-coordinates are stable between layout changes.
    // Generate an identifier by bit-shifting the x-coordinate to the
    // left-half of the integer and OR'ing with the y-coordinate.
    return ((0xFFFF & key.getX()) << (Integer.SIZE / 2)) | (0xFFFF & key.getY());
}

From source file:hivemall.fm.FactorizationMachineUDTF.java

protected void recordTrain(@Nonnull final Feature[] x, final double y) throws HiveException {
    if (_iterations <= 1) {
        return;/* ww w.  j ava 2  s  . c o  m*/
    }

    ByteBuffer inputBuf = _inputBuf;
    NioStatefullSegment dst = _fileIO;
    if (inputBuf == null) {
        final File file;
        try {
            file = File.createTempFile("hivemall_fm", ".sgmt");
            file.deleteOnExit();
            if (!file.canWrite()) {
                throw new UDFArgumentException("Cannot write a temporary file: " + file.getAbsolutePath());
            }
            LOG.info("Record training examples to a file: " + file.getAbsolutePath());
        } catch (IOException ioe) {
            throw new UDFArgumentException(ioe);
        } catch (Throwable e) {
            throw new UDFArgumentException(e);
        }

        this._inputBuf = inputBuf = ByteBuffer.allocateDirect(1024 * 1024); // 1 MiB
        this._fileIO = dst = new NioStatefullSegment(file, false);
    }

    int xBytes = Feature.requiredBytes(x);
    int recordBytes = (Integer.SIZE + Double.SIZE) / 8 + xBytes;
    int requiredBytes = (Integer.SIZE / 8) + recordBytes;
    int remain = inputBuf.remaining();
    if (remain < requiredBytes) {
        writeBuffer(inputBuf, dst);
    }

    inputBuf.putInt(recordBytes);
    inputBuf.putInt(x.length);
    for (Feature f : x) {
        f.writeTo(inputBuf);
    }
    inputBuf.putDouble(y);
}

From source file:it.unimi.dsi.sux4j.mph.TwoStepsGOV3Function.java

/** Creates a new two-step function for the given keys and values.
 * /*from  w  w w . j  a  va  2  s  .  com*/
 * @param keys the keys in the domain of the function.
 * @param transform a transformation strategy for the keys.
 * @param values values to be assigned to each key, in the same order of the iterator returned by <code>keys</code>; if {@code null}, the
 * assigned value will the the ordinal number of each key.
 * @param tempDir a temporary directory for the store files, or {@code null} for the standard temporary directory.
 * @param chunkedHashStore a chunked hash store containing the keys associated with their rank, or {@code null}; the store
 * can be unchecked, but in this case <code>keys</code> and <code>transform</code> must be non-{@code null}. 
 */
protected TwoStepsGOV3Function(final Iterable<? extends T> keys,
        final TransformationStrategy<? super T> transform, final LongBigList values, final File tempDir,
        ChunkedHashStore<T> chunkedHashStore) throws IOException {
    this.transform = transform;
    final ProgressLogger pl = new ProgressLogger(LOGGER);
    pl.displayLocalSpeed = true;
    pl.displayFreeMemory = true;
    final RandomGenerator random = new XorShift1024StarRandomGenerator();
    pl.itemsName = "keys";

    final boolean givenChunkedHashStore = chunkedHashStore != null;
    if (!givenChunkedHashStore) {
        if (keys == null)
            throw new IllegalArgumentException(
                    "If you do not provide a chunked hash store, you must provide the keys");
        chunkedHashStore = new ChunkedHashStore<T>(transform, pl);
        chunkedHashStore.reset(random.nextLong());
        chunkedHashStore.addAll(keys.iterator());
    }
    n = chunkedHashStore.size();
    defRetValue = -1; // For the very few cases in which we can decide

    if (n == 0) {
        rankMean = escape = width = 0;
        firstFunction = secondFunction = null;
        remap = null;
        if (!givenChunkedHashStore)
            chunkedHashStore.close();
        return;
    }

    // Compute distribution of values and maximum number of bits.
    int w = 0, size;
    long v;
    final Long2LongOpenHashMap counts = new Long2LongOpenHashMap();
    counts.defaultReturnValue(-1);
    for (LongIterator i = values.iterator(); i.hasNext();) {
        v = i.nextLong();
        counts.put(v, counts.get(v) + 1);
        size = Fast.length(v);
        if (size > w)
            w = size;
    }

    this.width = w;
    final int m = counts.size();

    LOGGER.debug("Generating two-steps GOV3 function with " + w + " output bits...");

    // Sort keys by reverse frequency
    final long[] keysArray = counts.keySet().toLongArray(new long[m]);
    LongArrays.quickSort(keysArray, 0, keysArray.length, new AbstractLongComparator() {
        private static final long serialVersionUID = 1L;

        public int compare(final long a, final long b) {
            return Long.signum(counts.get(b) - counts.get(a));
        }
    });

    long mean = 0;
    for (int i = 0; i < keysArray.length; i++)
        mean += i * counts.get(keysArray[i]);
    rankMean = (double) mean / n;

    // Analyze data and choose a threshold
    long post = n, bestCost = Long.MAX_VALUE;
    int pos = 0, best = -1;

    // Examine every possible choice for r. Note that r = 0 implies one function, so we do not need to test the case r == w.
    for (int r = 0; r < w && pos < m; r++) {

        /* This cost function is dependent on the implementation of GOV3Function. 
         * Note that for r = 0 we are actually computing the cost of a single function (the first one). */
        final long cost = (long) Math.min(GOV3Function.C * n * 1.126 + n * r, GOV3Function.C * n * r)
                + (long) Math.min(GOV3Function.C * post * 1.126 + post * w, GOV3Function.C * post * w)
                + pos * Long.SIZE;

        if (cost < bestCost) {
            best = r;
            bestCost = cost;
        }

        /* We add to pre and subtract from post the counts of keys from position (1<<r)-1 to position (1<<r+1)-1. */
        for (int j = 0; j < (1 << r) && pos < m; j++) {
            final long c = counts.get(keysArray[pos++]);
            post -= c;
        }
    }

    if (ASSERTS)
        assert pos == m;

    counts.clear();
    counts.trim();

    // We must keep the remap array small.
    if (best >= Integer.SIZE)
        best = Integer.SIZE - 1;

    LOGGER.debug("Best threshold: " + best);
    escape = (1 << best) - 1;
    System.arraycopy(keysArray, 0, remap = new long[escape], 0, remap.length);
    final Long2LongOpenHashMap map = new Long2LongOpenHashMap();
    map.defaultReturnValue(-1);
    for (int i = 0; i < escape; i++)
        map.put(remap[i], i);

    if (best != 0) {
        firstFunction = new GOV3Function.Builder<T>().keys(keys).transform(transform).store(chunkedHashStore)
                .values(new AbstractLongBigList() {
                    public long getLong(long index) {
                        long value = map.get(values.getLong(index));
                        return value == -1 ? escape : value;
                    }

                    public long size64() {
                        return n;
                    }
                }, best).indirect().build();

        LOGGER.debug("Actual bit cost per key of first function: " + (double) firstFunction.numBits() / n);
    } else
        firstFunction = null;

    chunkedHashStore.filter(new Predicate() {
        public boolean evaluate(Object triple) {
            return firstFunction == null || firstFunction.getLongByTriple((long[]) triple) == escape;
        }
    });

    secondFunction = new GOV3Function.Builder<T>().store(chunkedHashStore).values(values, w).indirect().build();

    this.seed = chunkedHashStore.seed();
    if (!givenChunkedHashStore)
        chunkedHashStore.close();

    LOGGER.debug("Actual bit cost per key of second function: " + (double) secondFunction.numBits() / n);

    LOGGER.info("Actual bit cost per key: " + (double) numBits() / n);
    LOGGER.info("Completed.");

}

From source file:org.cloudata.core.common.io.CWritableUtils.java

public static int getIntByteSize() {
    return Integer.SIZE / 8;
}

From source file:it.unimi.dsi.sux4j.mph.TwoStepsMWHCFunction.java

/** Creates a new two-step function for the given keys and values.
 * //from   w  w w .  ja va  2s . c o  m
 * @param keys the keys in the domain of the function.
 * @param transform a transformation strategy for the keys.
 * @param values values to be assigned to each key, in the same order of the iterator returned by <code>keys</code>; if {@code null}, the
 * assigned value will the the ordinal number of each key.
 * @param tempDir a temporary directory for the store files, or {@code null} for the standard temporary directory.
 * @param chunkedHashStore a chunked hash store containing the keys associated with their rank, or {@code null}; the store
 * can be unchecked, but in this case <code>keys</code> and <code>transform</code> must be non-{@code null}. 
 */
protected TwoStepsMWHCFunction(final Iterable<? extends T> keys,
        final TransformationStrategy<? super T> transform, final LongBigList values, final File tempDir,
        ChunkedHashStore<T> chunkedHashStore) throws IOException {
    this.transform = transform;
    final ProgressLogger pl = new ProgressLogger(LOGGER);
    pl.displayLocalSpeed = true;
    pl.displayFreeMemory = true;
    final RandomGenerator random = new XorShift1024StarRandomGenerator();
    pl.itemsName = "keys";

    final boolean givenChunkedHashStore = chunkedHashStore != null;
    if (!givenChunkedHashStore) {
        if (keys == null)
            throw new IllegalArgumentException(
                    "If you do not provide a chunked hash store, you must provide the keys");
        chunkedHashStore = new ChunkedHashStore<T>(transform, pl);
        chunkedHashStore.reset(random.nextLong());
        chunkedHashStore.addAll(keys.iterator());
    }
    n = chunkedHashStore.size();
    defRetValue = -1; // For the very few cases in which we can decide

    if (n == 0) {
        rankMean = escape = width = 0;
        firstFunction = secondFunction = null;
        remap = null;
        if (!givenChunkedHashStore)
            chunkedHashStore.close();
        return;
    }

    // Compute distribution of values and maximum number of bits.
    int w = 0, size;
    long v;
    final Long2LongOpenHashMap counts = new Long2LongOpenHashMap();
    counts.defaultReturnValue(-1);
    for (LongIterator i = values.iterator(); i.hasNext();) {
        v = i.nextLong();
        counts.put(v, counts.get(v) + 1);
        size = Fast.length(v);
        if (size > w)
            w = size;
    }

    this.width = w;
    final int m = counts.size();

    LOGGER.debug("Generating two-steps MWHC function with " + w + " output bits...");

    // Sort keys by reverse frequency
    final long[] keysArray = counts.keySet().toLongArray(new long[m]);
    LongArrays.quickSort(keysArray, 0, keysArray.length, new AbstractLongComparator() {
        private static final long serialVersionUID = 1L;

        public int compare(final long a, final long b) {
            return Long.signum(counts.get(b) - counts.get(a));
        }
    });

    long mean = 0;
    for (int i = 0; i < keysArray.length; i++)
        mean += i * counts.get(keysArray[i]);
    rankMean = (double) mean / n;

    // Analyze data and choose a threshold
    long post = n, bestCost = Long.MAX_VALUE;
    int pos = 0, best = -1;

    // Examine every possible choice for r. Note that r = 0 implies one function, so we do not need to test the case r == w.
    for (int r = 0; r < w && pos < m; r++) {

        /* This cost function is dependent on the implementation of MWHCFunction. 
         * Note that for r = 0 we are actually computing the cost of a single function (the first one). */
        final long cost = (long) Math.min(HypergraphSorter.GAMMA * n * 1.126 + n * r,
                HypergraphSorter.GAMMA * n * r)
                + (long) Math.min(HypergraphSorter.GAMMA * post * 1.126 + post * w,
                        HypergraphSorter.GAMMA * post * w)
                + pos * Long.SIZE;

        if (cost < bestCost) {
            best = r;
            bestCost = cost;
        }

        /* We add to pre and subtract from post the counts of keys from position (1<<r)-1 to position (1<<r+1)-1. */
        for (int j = 0; j < (1 << r) && pos < m; j++) {
            final long c = counts.get(keysArray[pos++]);
            post -= c;
        }
    }

    if (ASSERTS)
        assert pos == m;

    counts.clear();
    counts.trim();

    // We must keep the remap array small.
    if (best >= Integer.SIZE)
        best = Integer.SIZE - 1;

    LOGGER.debug("Best threshold: " + best);
    escape = (1 << best) - 1;
    System.arraycopy(keysArray, 0, remap = new long[escape], 0, remap.length);
    final Long2LongOpenHashMap map = new Long2LongOpenHashMap();
    map.defaultReturnValue(-1);
    for (int i = 0; i < escape; i++)
        map.put(remap[i], i);

    if (best != 0) {
        firstFunction = new MWHCFunction.Builder<T>().keys(keys).transform(transform).store(chunkedHashStore)
                .values(new AbstractLongBigList() {
                    public long getLong(long index) {
                        long value = map.get(values.getLong(index));
                        return value == -1 ? escape : value;
                    }

                    public long size64() {
                        return n;
                    }
                }, best).indirect().build();

        LOGGER.debug("Actual bit cost per key of first function: " + (double) firstFunction.numBits() / n);
    } else
        firstFunction = null;

    chunkedHashStore.filter(new Predicate() {
        public boolean evaluate(Object triple) {
            return firstFunction == null || firstFunction.getLongByTriple((long[]) triple) == escape;
        }
    });

    secondFunction = new MWHCFunction.Builder<T>().store(chunkedHashStore).values(values, w).indirect().build();

    this.seed = chunkedHashStore.seed();
    if (!givenChunkedHashStore)
        chunkedHashStore.close();

    LOGGER.debug("Actual bit cost per key of second function: " + (double) secondFunction.numBits() / n);

    LOGGER.info("Actual bit cost per key: " + (double) numBits() / n);
    LOGGER.info("Completed.");

}

From source file:de.undercouch.bson4jackson.BsonParser.java

/**
 * Can be called when a new embedded document is found. Reads the
 * document's header and creates a new context on the stack.
 * @param array true if the document is an embedded array
 * @return the json token read/*from www. ja  va 2 s . c  o m*/
 * @throws IOException if an I/O error occurs
 */
protected JsonToken handleNewDocument(boolean array) throws IOException {
    if (_in == null) {
        //this means Feature.HONOR_DOCUMENT_LENGTH is enabled, and we
        //haven't yet started reading. Read the first int to find out the
        //length of the document.
        byte[] buf = new byte[Integer.SIZE / Byte.SIZE];
        int len = 0;
        while (len < buf.length) {
            int l = _rawInputStream.read(buf, len, buf.length - len);
            if (l == -1) {
                throw new IOException("Not enough bytes for length of document");
            }
            len += l;
        }

        //wrap the input stream by a bounded stream, subtract buf.length from the
        //length because the size itself is included in the length
        int documentLength = ByteBuffer.wrap(buf).order(ByteOrder.LITTLE_ENDIAN).getInt();
        InputStream in = new BoundedInputStream(_rawInputStream, documentLength - buf.length);

        //buffer if the raw input stream is not already buffered
        if (!(_rawInputStream instanceof BufferedInputStream)) {
            in = new StaticBufferedInputStream(in);
        }
        _counter = new CountingInputStream(in);
        _in = new LittleEndianInputStream(_counter);
    } else {
        //read document header (skip size, we're not interested)
        _in.readInt();
    }

    _currentContext = new Context(_currentContext, array);
    return (array ? JsonToken.START_ARRAY : JsonToken.START_OBJECT);
}