/** * Calculates the minimum number of bits required to encode a value. This can * then in turn be used to calculate the number of septets or octets (as * appropriate) to use to encode a length parameter. * * @param value The value to calculate the minimum number of bits required to encode * @return The minimum number of bits required to encode the supplied value */ private static int bitsToEncode(int value) { int highestOneBit = Integer.highestOneBit(value); int bitLength = 0; while ((highestOneBit >>= 1) != 0) { bitLength++; } return bitLength; }
static int closedTableSize(int expectedEntries, double loadFactor) { // Get the recommended table size. // Round down to the nearest power of 2. expectedEntries = Math.max(expectedEntries, 2); int tableSize = Integer.highestOneBit(expectedEntries); // Check to make sure that we will not exceed the maximum load factor. if (expectedEntries > (int) (loadFactor * tableSize)) { tableSize <<= 1; return (tableSize > 0) ? tableSize : MAX_TABLE_SIZE; } return tableSize; }
/** * Decrements the given number down to the closest power of two. If the argument is a * power of two, it remains unchanged. * * @param value The value to round down. * @return The closest value that is a power of two and less or equal than the given value. */ public static int roundDownToPowerOf2(int value) { return Integer.highestOneBit(value); }
private static int log2Ceiling(int value) { return Integer.highestOneBit(value - 1) << 1; }
private static int expandedCapacity(int oldCapacity, int minCapacity) { if (minCapacity < 0) { throw new AssertionError("cannot store more than MAX_VALUE elements"); } // careful of overflow! int newCapacity = oldCapacity + (oldCapacity >> 1) + 1; if (newCapacity < minCapacity) { newCapacity = Integer.highestOneBit(minCapacity - 1) << 1; } if (newCapacity < 0) { newCapacity = Integer.MAX_VALUE; // guaranteed to be >= newCapacity } return newCapacity; }
private static int expandedCapacity(int oldCapacity, int minCapacity) { if (minCapacity < 0) { throw new AssertionError("cannot store more than MAX_VALUE elements"); } // careful of overflow! int newCapacity = oldCapacity + (oldCapacity >> 1) + 1; if (newCapacity < minCapacity) { newCapacity = Integer.highestOneBit(minCapacity - 1) << 1; } if (newCapacity < 0) { newCapacity = Integer.MAX_VALUE; // guaranteed to be >= newCapacity } return newCapacity; }
private static int expandedCapacity(int oldCapacity, int minCapacity) { if (minCapacity < 0) { throw new AssertionError("cannot store more than MAX_VALUE elements"); } // careful of overflow! int newCapacity = oldCapacity + (oldCapacity >> 1) + 1; if (newCapacity < minCapacity) { newCapacity = Integer.highestOneBit(minCapacity - 1) << 1; } if (newCapacity < 0) { newCapacity = Integer.MAX_VALUE; // guaranteed to be >= newCapacity } return newCapacity; }
static int expandedCapacity(int oldCapacity, int minCapacity) { if (minCapacity < 0) { throw new AssertionError("cannot store more than MAX_VALUE elements"); } // careful of overflow! int newCapacity = oldCapacity + (oldCapacity >> 1) + 1; if (newCapacity < minCapacity) { newCapacity = Integer.highestOneBit(minCapacity - 1) << 1; } if (newCapacity < 0) { newCapacity = Integer.MAX_VALUE; // guaranteed to be >= newCapacity } return newCapacity; }
@Override public float getScaleFactor(int sourceWidth, int sourceHeight, int requestedWidth, int requestedHeight) { int minIntegerFactor = Math.min(sourceHeight / requestedHeight, sourceWidth / requestedWidth); return minIntegerFactor == 0 ? 1f : 1f / Integer.highestOneBit(minIntegerFactor); }
/** * Returns an array size suitable for the backing array of a hash table that uses open addressing * with linear probing in its implementation. The returned size is the smallest power of two that * can hold setSize elements with the desired load factor. */ @VisibleForTesting static int chooseTableSize(int setSize) { if (setSize == 1) { return 2; } // Correct the size for open addressing to match desired load factor. // Round up to the next highest power of 2. int tableSize = Integer.highestOneBit(setSize - 1) << 1; while (tableSize * DESIRED_LOAD_FACTOR < setSize) { tableSize <<= 1; } return tableSize; }
static int closedTableSize(int expectedEntries, double loadFactor) { // Get the recommended table size. // Round down to the nearest power of 2. expectedEntries = Math.max(expectedEntries, 2); int tableSize = Integer.highestOneBit(expectedEntries); // Check to make sure that we will not exceed the maximum load factor. if (expectedEntries > (int) (loadFactor * tableSize)) { tableSize <<= 1; return (tableSize > 0) ? tableSize : MAX_TABLE_SIZE; } return tableSize; }
@Override public float getScaleFactor(int sourceWidth, int sourceHeight, int requestedWidth, int requestedHeight) { int maxIntegerFactor = (int) Math.ceil(Math.max(sourceHeight / (float) requestedHeight, sourceWidth / (float) requestedWidth)); int lesserOrEqualSampleSize = Math.max(1, Integer.highestOneBit(maxIntegerFactor)); int greaterOrEqualSampleSize = lesserOrEqualSampleSize << (lesserOrEqualSampleSize < maxIntegerFactor ? 1 : 0); return 1f / greaterOrEqualSampleSize; }
/** * Returns the largest power of two less than or equal to {@code x}. This is equivalent to {@code * checkedPow(2, log2(x, FLOOR))}. * * @throws IllegalArgumentException if {@code x <= 0} * @since 20.0 */ @Beta public static int floorPowerOfTwo(int x) { checkPositive("x", x); return Integer.highestOneBit(x); }
/** * Returns an array size suitable for the backing array of a hash table that uses open addressing * with linear probing in its implementation. The returned size is the smallest power of two that * can hold setSize elements with the desired load factor. Always returns at least setSize + 2. */ @VisibleForTesting static int chooseTableSize(int setSize) { setSize = Math.max(setSize, 2); // Correct the size for open addressing to match desired load factor. if (setSize < CUTOFF) { // Round up to the next highest power of 2. int tableSize = Integer.highestOneBit(setSize - 1) << 1; while (tableSize * DESIRED_LOAD_FACTOR < setSize) { tableSize <<= 1; } return tableSize; } // The table can't be completely full or we'll get infinite reprobes checkArgument(setSize < MAX_TABLE_SIZE, "collection too large"); return MAX_TABLE_SIZE; }
public FSTIdentity2IdMap(int initialSize) { if (initialSize < 2) { initialSize = 2; } initialSize = adjustSize(initialSize * GROFAC); mKeys = new Object[initialSize]; mValues = new int[initialSize]; mNumberOfElements = 0; mask = (Integer.highestOneBit(initialSize) << 1) - 1; klen = initialSize - 4; }
public static int maxIntsInBufferForBytes(int numBytes) { int maxSizePer = (CompressedPools.BUFFER_SIZE - bufferPadding(numBytes)) / numBytes; // round down to the nearest power of 2 return Integer.highestOneBit(maxSizePer); }
/** * Ensures that this {@code CompactHashMap} has the smallest representation in memory, given its * current size. */ public void trimToSize() { int size = this.size; if (size < entries.length) { resizeEntries(size); } // size / loadFactor gives the table size of the appropriate load factor, // but that may not be a power of two. We floor it to a power of two by // keeping its highest bit. But the smaller table may have a load factor // larger than what we want; then we want to go to the next power of 2 if we can int minimumTableSize = Math.max(1, Integer.highestOneBit((int) (size / loadFactor))); if (minimumTableSize < MAXIMUM_CAPACITY) { double load = (double) size / minimumTableSize; if (load > loadFactor) { minimumTableSize <<= 1; // increase to next power if possible } } if (minimumTableSize < table.length) { resizeTable(minimumTableSize); } }
/** * Ensures that this {@code CompactHashSet} has the smallest representation in memory, given its * current size. */ public void trimToSize() { int size = this.size; if (size < entries.length) { resizeEntries(size); } // size / loadFactor gives the table size of the appropriate load factor, // but that may not be a power of two. We floor it to a power of two by // keeping its highest bit. But the smaller table may have a load factor // larger than what we want; then we want to go to the next power of 2 if we can int minimumTableSize = Math.max(1, Integer.highestOneBit((int) (size / loadFactor))); if (minimumTableSize < MAXIMUM_CAPACITY) { double load = (double) size / minimumTableSize; if (load > loadFactor) { minimumTableSize <<= 1; // increase to next power if possible } } if (minimumTableSize < table.length) { resizeTable(minimumTableSize); } }
/** * Returns the largest power of two less than or equal to {@code x}. This is equivalent to {@code * checkedPow(2, log2(x, FLOOR))}. * * @throws IllegalArgumentException if {@code x <= 0} * @since 20.0 */ @Beta public static int floorPowerOfTwo(int x) { checkPositive("x", x); return Integer.highestOneBit(x); }
private static int getSampleSize(GifHeader gifHeader, int targetWidth, int targetHeight) { int exactSampleSize = Math.min(gifHeader.getHeight() / targetHeight, gifHeader.getWidth() / targetWidth); int powerOfTwoSampleSize = exactSampleSize == 0 ? 0 : Integer.highestOneBit(exactSampleSize); // Although functionally equivalent to 0 for BitmapFactory, 1 is a safer default for our code // than 0. int sampleSize = Math.max(1, powerOfTwoSampleSize); if (Log.isLoggable(TAG, Log.VERBOSE) && sampleSize > 1) { Log.v(TAG, "Downsampling GIF" + ", sampleSize: " + sampleSize + ", target dimens: [" + targetWidth + "x" + targetHeight + "]" + ", actual dimens: [" + gifHeader.getWidth() + "x" + gifHeader.getHeight() + "]"); } return sampleSize; }