/** * Fast method of finding the next power of 2 greater than or equal to the supplied value. * <p>This method will do runtime bounds checking and call {@link #findNextPositivePowerOfTwo(int)} if within a * valid range. * @param value from which to search for next power of 2 * @return The next power of 2 or the value itself if it is a power of 2. * <p>Special cases for return values are as follows: * <ul> * <li>{@code <= 0} -> 1</li> * <li>{@code >= 2^30} -> 2^30</li> * </ul> */ public static int safeFindNextPositivePowerOfTwo(final int value) { return value <= 0 ? 1 : value >= 0x40000000 ? 0x40000000 : findNextPositivePowerOfTwo(value); }
/** * Fast method of finding the next power of 2 greater than or equal to the supplied value. * <p>This method will do runtime bounds checking and call {@link #findNextPositivePowerOfTwo(int)} if within a * valid range. * @param value from which to search for next power of 2 * @return The next power of 2 or the value itself if it is a power of 2. * <p>Special cases for return values are as follows: * <ul> * <li>{@code <= 0} -> 1</li> * <li>{@code >= 2^30} -> 2^30</li> * </ul> */ public static int safeFindNextPositivePowerOfTwo(final int value) { return value <= 0 ? 1 : value >= 0x40000000 ? 0x40000000 : findNextPositivePowerOfTwo(value); }
@SuppressWarnings("unchecked") public GrowableArrayBlockingQueue(int initialCapacity) { headIndex.value = 0; tailIndex.value = 0; int capacity = MathUtil.findNextPositivePowerOfTwo(initialCapacity); data = (T[]) new Object[capacity]; }
/** * Fast method of finding the next power of 2 greater than or equal to the supplied value. * <p>This method will do runtime bounds checking and call {@link #findNextPositivePowerOfTwo(int)} if within a * valid range. * @param value from which to search for next power of 2 * @return The next power of 2 or the value itself if it is a power of 2. * <p>Special cases for return values are as follows: * <ul> * <li>{@code <= 0} -> 1</li> * <li>{@code >= 2^30} -> 2^30</li> * </ul> */ public static int safeFindNextPositivePowerOfTwo(final int value) { return value <= 0 ? 1 : value >= 0x40000000 ? 0x40000000 : findNextPositivePowerOfTwo(value); }
public AbstractByteBufPool(final int capacity) { entries = (T[]) new Object[MathUtil.findNextPositivePowerOfTwo(capacity)]; mask = entries.length - 1; //log2 of entries.length shift = 31 - Integer.numberOfLeadingZeros(entries.length); }
public AbstractPool(final int capacity) { entries = (O[]) new Object[MathUtil.findNextPositivePowerOfTwo(capacity)]; mask = entries.length - 1; //log2 of entries.length shift = 31 - Integer.numberOfLeadingZeros(entries.length); }
public GrowablePriorityLongPairQueue(int initialCapacity) { checkArgument(initialCapacity > 0); this.capacity = MathUtil.findNextPositivePowerOfTwo(initialCapacity); data = new long[2 * capacity]; Arrays.fill(data, 0, data.length, EmptyItem); }
/** * Creates a new encoder. */ public HpackEncoder(boolean ignoreMaxHeaderListSize, int arraySizeHint) { this.ignoreMaxHeaderListSize = ignoreMaxHeaderListSize; maxHeaderTableSize = DEFAULT_HEADER_TABLE_SIZE; maxHeaderListSize = MAX_HEADER_LIST_SIZE; // Enforce a bound of [2, 128] because hashMask is a byte. The max possible value of hashMask is one less // than the length of this array, and we want the mask to be > 0. headerFields = new HeaderEntry[findNextPositivePowerOfTwo(max(2, min(arraySizeHint, 128)))]; hashMask = (byte) (headerFields.length - 1); head.before = head.after = head; }
EnumNameMap(EnumNameMap.Node<T>... nodes) { values = (EnumNameMap.Node<T>[]) new EnumNameMap.Node[findNextPositivePowerOfTwo(nodes.length)]; valuesMask = values.length - 1; for (EnumNameMap.Node<T> node : nodes) { int i = hashCode(node.key) & valuesMask; if (values[i] != null) { throw new IllegalArgumentException("index " + i + " collision between values: [" + values[i].key + ", " + node.key + ']'); } values[i] = node; } }
/** * Create a new instance. * @param nameHashingStrategy Used to hash and equality compare names. * @param valueConverter Used to convert values to/from native types. * @param nameValidator Used to validate name elements. * @param arraySizeHint A hint as to how large the hash data structure should be. * The next positive power of two will be used. An upper bound may be enforced. */ @SuppressWarnings("unchecked") public DefaultHeaders(HashingStrategy<K> nameHashingStrategy, ValueConverter<V> valueConverter, NameValidator<K> nameValidator, int arraySizeHint) { this.valueConverter = checkNotNull(valueConverter, "valueConverter"); this.nameValidator = checkNotNull(nameValidator, "nameValidator"); this.hashingStrategy = checkNotNull(nameHashingStrategy, "nameHashingStrategy"); // Enforce a bound of [2, 128] because hashMask is a byte. The max possible value of hashMask is one less // than the length of this array, and we want the mask to be > 0. entries = new DefaultHeaders.HeaderEntry[findNextPositivePowerOfTwo(max(2, min(arraySizeHint, 128)))]; hashMask = (byte) (entries.length - 1); head = new HeaderEntry<K, V>(); }
/** * Create a new instance. * @param nameHashingStrategy Used to hash and equality compare names. * @param valueConverter Used to convert values to/from native types. * @param nameValidator Used to validate name elements. * @param arraySizeHint A hint as to how large the hash data structure should be. * The next positive power of two will be used. An upper bound may be enforced. */ @SuppressWarnings("unchecked") public DefaultHeaders(HashingStrategy<K> nameHashingStrategy, ValueConverter<V> valueConverter, NameValidator<K> nameValidator, int arraySizeHint) { this.valueConverter = checkNotNull(valueConverter, "valueConverter"); this.nameValidator = checkNotNull(nameValidator, "nameValidator"); this.hashingStrategy = checkNotNull(nameHashingStrategy, "nameHashingStrategy"); // Enforce a bound of [2, 128] because hashMask is a byte. The max possible value of hashMask is one less // than the length of this array, and we want the mask to be > 0. entries = new DefaultHeaders.HeaderEntry[findNextPositivePowerOfTwo(max(2, min(arraySizeHint, 128)))]; hashMask = (byte) (entries.length - 1); head = new HeaderEntry<K, V>(); }
/** * Create a new instance. * @param nameHashingStrategy Used to hash and equality compare names. * @param valueConverter Used to convert values to/from native types. * @param nameValidator Used to validate name elements. * @param arraySizeHint A hint as to how large the hash data structure should be. * The next positive power of two will be used. An upper bound may be enforced. */ @SuppressWarnings("unchecked") public DefaultHeaders(HashingStrategy<K> nameHashingStrategy, ValueConverter<V> valueConverter, NameValidator<K> nameValidator, int arraySizeHint) { this.valueConverter = checkNotNull(valueConverter, "valueConverter"); this.nameValidator = checkNotNull(nameValidator, "nameValidator"); this.hashingStrategy = checkNotNull(nameHashingStrategy, "nameHashingStrategy"); // Enforce a bound of [2, 128] because hashMask is a byte. The max possible value of hashMask is one less // than the length of this array, and we want the mask to be > 0. entries = new DefaultHeaders.HeaderEntry[findNextPositivePowerOfTwo(max(2, min(arraySizeHint, 128)))]; hashMask = (byte) (entries.length - 1); head = new HeaderEntry<K, V>(); }
/** * Fast method of finding the next power of 2 greater than or equal to the supplied value. * <p>This method will do runtime bounds checking and call {@link #findNextPositivePowerOfTwo(int)} if within a * valid range. * @param value from which to search for next power of 2 * @return The next power of 2 or the value itself if it is a power of 2. * <p>Special cases for return values are as follows: * <ul> * <li>{@code <= 0} -> 1</li> * <li>{@code >= 2^30} -> 2^30</li> * </ul> */ public static int safeFindNextPositivePowerOfTwo(final int value) { return value <= 0 ? 1 : value >= 0x40000000 ? 0x40000000 : findNextPositivePowerOfTwo(value); }
@SuppressWarnings("unchecked") public GrowableArrayBlockingQueue(int initialCapacity) { headIndex.value = 0; tailIndex.value = 0; int capacity = MathUtil.findNextPositivePowerOfTwo(initialCapacity); data = (T[]) new Object[capacity]; }
public AbstractPool(final int capacity) { entries = (O[]) new Object[MathUtil.findNextPositivePowerOfTwo(capacity)]; mask = entries.length - 1; //log2 of entries.length shift = 31 - Integer.numberOfLeadingZeros(entries.length); }
public AbstractByteBufPool(final int capacity) { entries = (T[]) new Object[MathUtil.findNextPositivePowerOfTwo(capacity)]; mask = entries.length - 1; //log2 of entries.length shift = 31 - Integer.numberOfLeadingZeros(entries.length); }
public AbstractByteBufPool(final int capacity) { entries = (T[]) new Object[MathUtil.findNextPositivePowerOfTwo(capacity)]; mask = entries.length - 1; //log2 of entries.length shift = 31 - Integer.numberOfLeadingZeros(entries.length); }
public AbstractPool(final int capacity) { entries = (O[]) new Object[MathUtil.findNextPositivePowerOfTwo(capacity)]; mask = entries.length - 1; //log2 of entries.length shift = 31 - Integer.numberOfLeadingZeros(entries.length); }
EnumNameMap(EnumNameMap.Node<T>... nodes) { values = (EnumNameMap.Node<T>[]) new EnumNameMap.Node[findNextPositivePowerOfTwo(nodes.length)]; valuesMask = values.length - 1; for (EnumNameMap.Node<T> node : nodes) { int i = hashCode(node.key) & valuesMask; if (values[i] != null) { throw new IllegalArgumentException("index " + i + " collision between values: [" + values[i].key + ", " + node.key + ']'); } values[i] = node; } }
EnumNameMap(EnumNameMap.Node<T>... nodes) { values = (EnumNameMap.Node<T>[]) new EnumNameMap.Node[findNextPositivePowerOfTwo(nodes.length)]; valuesMask = values.length - 1; for (EnumNameMap.Node<T> node : nodes) { int i = hashCode(node.key) & valuesMask; if (values[i] != null) { throw new IllegalArgumentException("index " + i + " collision between values: [" + values[i].key + ", " + node.key + ']'); } values[i] = node; } }