/** * Construct a hash set with a proposed initial capacity, load factor, and indicated iterator caching support. * * @param proposedCapacity for the initial capacity of the set. * @param loadFactor to be used for resizing. * @param shouldAvoidAllocation should the iterator be cached to avoid further allocation. */ @SuppressWarnings("unchecked") public ObjectHashSet(final int proposedCapacity, final float loadFactor, final boolean shouldAvoidAllocation) { validateLoadFactor(loadFactor); this.shouldAvoidAllocation = shouldAvoidAllocation; this.loadFactor = loadFactor; size = 0; final int capacity = findNextPositivePowerOfTwo(Math.max(DEFAULT_INITIAL_CAPACITY, proposedCapacity)); resizeThreshold = (int)(capacity * loadFactor); values = (T[])new Object[capacity]; }
public Int2ObjectCache( @DoNotSub final int numSets, @DoNotSub final int setSize, final Consumer<V> evictionConsumer) { validatePositivePowerOfTwo(numSets); validatePositivePowerOfTwo(setSize); requireNonNull(evictionConsumer, "null values are not permitted"); if (((long)numSets) * setSize > (Integer.MAX_VALUE - 8)) { throw new IllegalArgumentException( "total capacity must be <= max array size: numSets=" + numSets + " setSize=" + setSize); } this.setSize = setSize; this.setSizeShift = Integer.numberOfTrailingZeros(setSize); capacity = numSets << setSizeShift; mask = numSets - 1; keys = new int[capacity]; values = new Object[capacity]; this.evictionConsumer = evictionConsumer; }
private void assertRemoveIfRemoves(final int requiredValue, final Integer... expectedValues) { final List<Integer> values = new ArrayList<>(asList(1, 2, 3)); assertEquals(1, removeIf(values, value -> value == requiredValue)); assertEquals(values, asList(expectedValues)); }
@Test public void getOrDefaultUsesSupplier() { final Map<Integer, Integer> ints = new HashMap<>(); final Integer result = CollectionUtil.getOrDefault(ints, 0, (x) -> x + 1); assertThat(result, is(1)); }
public int doWork() { return subscription.controlledPoll(this, LIMIT) + CollectionUtil.sum(indices, Index::doWork); }
@Test public void getOrDefaultDoesNotCreateNewValueWhenOneExists() { final Map<Integer, Integer> ints = new HashMap<>(); ints.put(0, 0); final Integer result = CollectionUtil.getOrDefault( ints, 0, (x) -> { Assert.fail("Shouldn't be called"); return x + 1; }); assertThat(result, is(0)); } }
/** * Construct a map that sets it initial capacity and load factor. * * @param initialCapacity for the underlying hash map * @param loadFactor for the underlying hash map */ public BiInt2ObjectMap(final int initialCapacity, final float loadFactor) { validateLoadFactor(loadFactor); this.loadFactor = loadFactor; final int capacity = findNextPositivePowerOfTwo(Math.max(MIN_CAPACITY, initialCapacity)); resizeThreshold = (int)(capacity * loadFactor); keys = new long[capacity]; values = new Object[capacity]; }
public Int2ObjectCache( @DoNotSub final int numSets, @DoNotSub final int setSize, final Consumer<V> evictionConsumer) { validatePositivePowerOfTwo(numSets); validatePositivePowerOfTwo(setSize); requireNonNull(evictionConsumer, "null values are not permitted"); if (((long)numSets) * setSize > (Integer.MAX_VALUE - 8)) { throw new IllegalArgumentException( "total capacity must be <= max array size: numSets=" + numSets + " setSize=" + setSize); } this.setSize = setSize; this.setSizeShift = Integer.numberOfTrailingZeros(setSize); capacity = numSets << setSizeShift; mask = numSets - 1; keys = new int[capacity]; values = new Object[capacity]; this.evictionConsumer = evictionConsumer; }
int attemptSteps() { return removeIf(polledUnitOfWorks, step -> step.attemptToAction() == CONTINUE); } }
/** * @param initialCapacity for the map to override {@link #MIN_CAPACITY} * @param loadFactor for the map to override {@link Hashing#DEFAULT_LOAD_FACTOR}. * @param shouldAvoidAllocation should allocation be avoided by caching iterators and map entries. */ public Object2ObjectHashMap(final int initialCapacity, final float loadFactor, final boolean shouldAvoidAllocation) { validateLoadFactor(loadFactor); this.loadFactor = loadFactor; this.shouldAvoidAllocation = shouldAvoidAllocation; capacity(findNextPositivePowerOfTwo(Math.max(MIN_CAPACITY, initialCapacity))); }
public Long2ObjectCache( @DoNotSub final int numSets, @DoNotSub final int setSize, final Consumer<V> evictionConsumer) { validatePositivePowerOfTwo(numSets); validatePositivePowerOfTwo(setSize); requireNonNull(evictionConsumer, "null values are not permitted"); if (((long)numSets) * setSize > (Long.MAX_VALUE - 8)) { throw new IllegalArgumentException( "total capacity must be <= max array size: numSets=" + numSets + " setSize=" + setSize); } this.setSize = setSize; this.setSizeShift = Long.numberOfTrailingZeros(setSize); capacity = numSets << setSizeShift; mask = numSets - 1; keys = new long[capacity]; values = new Object[capacity]; this.evictionConsumer = evictionConsumer; }
private int checkDutyCycle() { return removeIf(replies, ResetSequenceNumberCommand::poll) + resendSaveNotifications(this.resendSlowStatus, SlowStatus.SLOW) + resendSaveNotifications(this.resendNotSlowStatus, SlowStatus.NOT_SLOW); }
/** * Construct a new map allowing a configuration for initial capacity and load factor. * @param initialCapacity for the backing array * @param loadFactor limit for resizing on puts * @param missingValue value to be used as a null marker in the map * @param shouldAvoidAllocation should allocation be avoided by caching iterators and map entries. */ @SuppressWarnings("unchecked") public Object2IntHashMap( @DoNotSub final int initialCapacity, final float loadFactor, final int missingValue, final boolean shouldAvoidAllocation) { validateLoadFactor(loadFactor); this.loadFactor = loadFactor; /* @DoNotSub */ final int capacity = findNextPositivePowerOfTwo(Math.max(MIN_CAPACITY, initialCapacity)); /* @DoNotSub */ resizeThreshold = (int)(capacity * loadFactor); this.missingValue = missingValue; this.shouldAvoidAllocation = shouldAvoidAllocation; keys = (K[])new Object[capacity]; values = new int[capacity]; Arrays.fill(values, missingValue); }
protected AbstractIterator() { reset(); }
private int pollLibraries(final long timeInMs) { int total = 0; final Iterator<LiveLibraryInfo> iterator = idToLibrary.values().iterator(); while (iterator.hasNext()) { final LiveLibraryInfo library = iterator.next(); total += library.poll(timeInMs); if (!library.isConnected()) { DebugLogger.log(LIBRARY_MANAGEMENT, "Timing out connection to library %s%n", library.libraryId()); iterator.remove(); library.releaseSlowPeeker(); tryAcquireLibrarySessions(library); saveLibraryTimeout(library); } } total += removeIf(librariesBeingAcquired, retryAcquireLibrarySessionsFunc); return total; }
/** * Construct a new map allowing a configuration for initial capacity and load factor. * @param initialCapacity for the backing array * @param loadFactor limit for resizing on puts * @param shouldAvoidAllocation should allocation be avoided by caching iterators and map entries. */ public Int2ObjectHashMap( @DoNotSub final int initialCapacity, final float loadFactor, final boolean shouldAvoidAllocation) { validateLoadFactor(loadFactor); this.loadFactor = loadFactor; this.shouldAvoidAllocation = shouldAvoidAllocation; /* @DoNotSub */ final int capacity = findNextPositivePowerOfTwo(Math.max(MIN_CAPACITY, initialCapacity)); /* @DoNotSub */ resizeThreshold = (int)(capacity * loadFactor); keys = new int[capacity]; values = new Object[capacity]; }
protected AbstractIterator() { reset(); }
/** * Construct a hash set with a proposed initial capacity, load factor, and indicated iterator caching support. * * @param proposedCapacity for the initial capacity of the set. * @param loadFactor to be used for resizing. * @param shouldAvoidAllocation should the iterator be cached to avoid further allocation. */ public IntHashSet( @DoNotSub final int proposedCapacity, final float loadFactor, final boolean shouldAvoidAllocation) { validateLoadFactor(loadFactor); this.shouldAvoidAllocation = shouldAvoidAllocation; this.loadFactor = loadFactor; sizeOfArrayValues = 0; @DoNotSub final int capacity = findNextPositivePowerOfTwo(Math.max(DEFAULT_INITIAL_CAPACITY, proposedCapacity)); resizeThreshold = (int)(capacity * loadFactor); // @DoNotSub values = new int[capacity]; Arrays.fill(values, MISSING_VALUE); }
public Int2IntCounterMap( @DoNotSub final int initialCapacity, @DoNotSub final float loadFactor, final int initialValue) { validateLoadFactor(loadFactor); this.loadFactor = loadFactor; this.initialValue = initialValue; capacity(findNextPositivePowerOfTwo(Math.max(MIN_CAPACITY, initialCapacity))); }
/** * @param initialCapacity for the map to override {@link #MIN_CAPACITY} * @param loadFactor for the map to override {@link Hashing#DEFAULT_LOAD_FACTOR}. * @param missingValue for the map that represents null. * @param shouldAvoidAllocation should allocation be avoided by caching iterators and map entries. */ public Int2IntHashMap( @DoNotSub final int initialCapacity, @DoNotSub final float loadFactor, final int missingValue, final boolean shouldAvoidAllocation) { validateLoadFactor(loadFactor); this.loadFactor = loadFactor; this.missingValue = missingValue; this.shouldAvoidAllocation = shouldAvoidAllocation; capacity(findNextPositivePowerOfTwo(Math.max(MIN_CAPACITY, initialCapacity))); }