com.gs.collections.impl.set.mutable.primitive.LongHashSet Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gs-collections Show documentation
Show all versions of gs-collections Show documentation
GS Collections is a collections framework for Java. It has JDK-compatible List, Set and Map
implementations with a rich API and set of utility classes that work with any JDK compatible Collections,
Arrays, Maps or Strings. The iteration protocol was inspired by the Smalltalk collection framework.
/*
* Copyright 2014 Goldman Sachs.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.gs.collections.impl.set.mutable.primitive;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.Serializable;
import java.util.Arrays;
import java.util.NoSuchElementException;
import com.gs.collections.api.LongIterable;
import com.gs.collections.api.block.function.primitive.LongToObjectFunction;
import com.gs.collections.api.block.function.primitive.ObjectLongToObjectFunction;
import com.gs.collections.api.block.predicate.primitive.LongPredicate;
import com.gs.collections.api.block.procedure.primitive.LongProcedure;
import com.gs.collections.api.iterator.LongIterator;
import com.gs.collections.api.iterator.MutableLongIterator;
import com.gs.collections.api.set.ImmutableSet;
import com.gs.collections.api.set.MutableSet;
import com.gs.collections.api.set.primitive.ImmutableLongSet;
import com.gs.collections.api.set.primitive.MutableLongSet;
import com.gs.collections.api.set.primitive.LongSet;
import com.gs.collections.impl.factory.primitive.LongSets;
import com.gs.collections.impl.set.immutable.primitive.ImmutableLongSetSerializationProxy;
import com.gs.collections.impl.set.mutable.UnifiedSet;
import com.gs.collections.impl.SpreadFunctions;
import com.gs.collections.impl.set.immutable.primitive.AbstractImmutableLongSet;
import com.gs.collections.impl.set.primitive.AbstractLongSet;
/**
* This file was automatically generated from template file primitiveHashSet.stg.
*
* @since 3.0.
*/
public final class LongHashSet extends AbstractLongSet implements MutableLongSet, Externalizable
{
private static final long serialVersionUID = 1L;
private static final int OCCUPIED_DATA_RATIO = 2;
private static final int OCCUPIED_SENTINEL_RATIO = 4;
private static final int DEFAULT_INITIAL_CAPACITY = 16;
private static final long EMPTY = 0L;
private static final long REMOVED = 1L;
private static final int CACHE_LINE_SIZE = 64;
private static final int KEY_SIZE = 8;
private static final int INITIAL_LINEAR_PROBE = CACHE_LINE_SIZE / KEY_SIZE / 2; /* half a cache line */
private long[] table;
private int occupiedWithData;
private int occupiedWithSentinels;
// The 32 bits of this integer indicate whether the items 0L to 31L are present in the set.
private int zeroToThirtyOne;
private int zeroToThirtyOneOccupied;
private transient boolean copyOnWrite;
public LongHashSet()
{
this.allocateTable(DEFAULT_INITIAL_CAPACITY);
}
public LongHashSet(int initialCapacity)
{
if (initialCapacity < 0)
{
throw new IllegalArgumentException("initial capacity cannot be less than 0");
}
int capacity = this.smallestPowerOfTwoGreaterThan(this.fastCeil(initialCapacity * OCCUPIED_DATA_RATIO));
this.allocateTable(capacity);
}
public LongHashSet(long... elements)
{
this();
this.addAll(elements);
}
public LongHashSet(LongHashSet set)
{
this.occupiedWithData = set.occupiedWithData;
this.occupiedWithSentinels = set.occupiedWithSentinels;
this.zeroToThirtyOneOccupied = set.zeroToThirtyOneOccupied;
this.zeroToThirtyOne = set.zeroToThirtyOne;
this.allocateTable(set.table.length);
System.arraycopy(set.table, 0, this.table, 0, set.table.length);
}
private int smallestPowerOfTwoGreaterThan(int n)
{
return n > 1 ? Integer.highestOneBit(n - 1) << 1 : 1;
}
private int fastCeil(float v)
{
int possibleResult = (int) v;
if (v - possibleResult > 0.0F)
{
possibleResult++;
}
return possibleResult;
}
public static LongHashSet newSet(LongIterable source)
{
if (source instanceof LongHashSet)
{
return new LongHashSet((LongHashSet) source);
}
return LongHashSet.newSetWith(source.toArray());
}
public static LongHashSet newSetWith(long... source)
{
return new LongHashSet(source);
}
private static boolean isBetweenZeroAndThirtyOne(long value)
{
return value >= 0L && value <= 31L;
}
@Override
public int hashCode()
{
int result = 0;
int zeroToThirtyOne = this.zeroToThirtyOne;
while (zeroToThirtyOne != 0)
{
long value = (long) Integer.numberOfTrailingZeros(zeroToThirtyOne);
result += (int) (value ^ value >>> 32);
zeroToThirtyOne &= ~(1 << value);
}
if (this.table != null)
{
for (int i = 0; i < this.table.length; i++)
{
if (isNonSentinel(this.table[i]))
{
result += (int) (this.table[i] ^ this.table[i] >>> 32);
}
}
}
return result;
}
public int size()
{
return this.occupiedWithData + this.zeroToThirtyOneOccupied;
}
public void appendString(Appendable appendable, String start, String separator, String end)
{
try
{
appendable.append(start);
int count = 0;
int zeroToThirtyOne = this.zeroToThirtyOne;
while (zeroToThirtyOne != 0)
{
long value = (long) Integer.numberOfTrailingZeros(zeroToThirtyOne);
if (count > 0)
{
appendable.append(separator);
}
count++;
appendable.append(String.valueOf(value));
zeroToThirtyOne &= ~(1 << value);
}
for (long value : this.table)
{
if (isNonSentinel(value))
{
if (count > 0)
{
appendable.append(separator);
}
count++;
appendable.append(String.valueOf(value));
}
}
appendable.append(end);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
public boolean add(long element)
{
if (isBetweenZeroAndThirtyOne(element))
{
int initial = this.zeroToThirtyOne;
this.zeroToThirtyOne |= 1 << element;
if (this.zeroToThirtyOne != initial)
{
this.zeroToThirtyOneOccupied++;
return true;
}
return false;
}
int index = this.probe(element);
if (this.table[index] == element)
{
// element already present in set
return false;
}
if (this.copyOnWrite)
{
this.copyTable();
}
if (this.table[index] == REMOVED)
{
--this.occupiedWithSentinels;
}
this.table[index] = element;
++this.occupiedWithData;
if (this.occupiedWithData > this.maxOccupiedWithData())
{
this.rehashAndGrow();
}
return true;
}
public boolean addAll(long... source)
{
int oldSize = this.size();
for (long item : source)
{
this.add(item);
}
return this.size() != oldSize;
}
public boolean addAll(LongIterable source)
{
if (source.isEmpty())
{
return false;
}
int oldSize = this.size();
if (source instanceof LongHashSet)
{
LongHashSet hashSet = (LongHashSet) source;
this.zeroToThirtyOne |= hashSet.zeroToThirtyOne;
this.zeroToThirtyOneOccupied = Integer.bitCount(this.zeroToThirtyOne);
for (long item : hashSet.table)
{
if (isNonSentinel(item))
{
this.add(item);
}
}
}
else
{
LongIterator iterator = source.longIterator();
while (iterator.hasNext())
{
long item = iterator.next();
this.add(item);
}
}
return this.size() != oldSize;
}
public boolean remove(long value)
{
if (isBetweenZeroAndThirtyOne(value))
{
int initial = this.zeroToThirtyOne;
this.zeroToThirtyOne &= ~(1 << value);
if (this.zeroToThirtyOne == initial)
{
return false;
}
this.zeroToThirtyOneOccupied--;
return true;
}
int index = this.probe(value);
if (this.table[index] == value)
{
if (this.copyOnWrite)
{
this.copyTable();
}
this.table[index] = REMOVED;
this.occupiedWithData--;
this.occupiedWithSentinels++;
if (this.occupiedWithSentinels > this.maxOccupiedWithSentinels())
{
this.rehash();
}
return true;
}
return false;
}
public boolean removeAll(LongIterable source)
{
if (source.isEmpty())
{
return false;
}
int oldSize = this.size();
if (source instanceof LongHashSet)
{
LongHashSet hashSet = (LongHashSet) source;
this.zeroToThirtyOne &= ~hashSet.zeroToThirtyOne;
this.zeroToThirtyOneOccupied = Integer.bitCount(this.zeroToThirtyOne);
for (long item : hashSet.table)
{
if (isNonSentinel(item))
{
this.remove(item);
}
}
}
else
{
LongIterator iterator = source.longIterator();
while (iterator.hasNext())
{
long item = iterator.next();
this.remove(item);
}
}
return this.size() != oldSize;
}
public boolean removeAll(long... source)
{
if (source.length == 0)
{
return false;
}
int oldSize = this.size();
for (long item : source)
{
this.remove(item);
}
return this.size() != oldSize;
}
public boolean retainAll(LongIterable source)
{
int oldSize = this.size();
final LongSet sourceSet = source instanceof LongSet ? (LongSet) source : source.toSet();
LongHashSet retained = this.select(new LongPredicate()
{
public boolean accept(long value)
{
return sourceSet.contains(value);
}
});
if (retained.size() != oldSize)
{
this.zeroToThirtyOne = retained.zeroToThirtyOne;
this.zeroToThirtyOneOccupied = retained.zeroToThirtyOneOccupied;
this.occupiedWithData = retained.occupiedWithData;
this.occupiedWithSentinels = retained.occupiedWithSentinels;
this.table = retained.table;
return true;
}
return false;
}
public boolean retainAll(long... source)
{
return this.retainAll(LongHashSet.newSetWith(source));
}
public void clear()
{
this.zeroToThirtyOneOccupied = 0;
this.occupiedWithData = 0;
this.occupiedWithSentinels = 0;
this.zeroToThirtyOne = 0;
if (this.copyOnWrite)
{
this.table = new long[this.table.length];
this.copyOnWrite = false;
}
else
{
Arrays.fill(this.table, EMPTY);
}
}
public LongHashSet with(long element)
{
this.add(element);
return this;
}
public LongHashSet without(long element)
{
this.remove(element);
return this;
}
public LongHashSet withAll(LongIterable elements)
{
this.addAll(elements.toArray());
return this;
}
public LongHashSet withoutAll(LongIterable elements)
{
this.removeAll(elements);
return this;
}
public MutableLongSet asUnmodifiable()
{
return new UnmodifiableLongSet(this);
}
public MutableLongSet asSynchronized()
{
return new SynchronizedLongSet(this);
}
public ImmutableLongSet toImmutable()
{
if (this.size() == 0)
{
return LongSets.immutable.with();
}
if (this.size() == 1)
{
return LongSets.immutable.with(this.longIterator().next());
}
LongHashSet mutableSet = LongHashSet.newSetWith(this.toArray());
return new ImmutableLongHashSet(mutableSet.table, mutableSet.occupiedWithData, mutableSet.zeroToThirtyOne, mutableSet.zeroToThirtyOneOccupied);
}
public MutableLongIterator longIterator()
{
return new InternalLongIterator();
}
public long[] toArray()
{
long[] array = new long[this.size()];
int j = 0;
int zeroToThirtyOne = this.zeroToThirtyOne;
while (zeroToThirtyOne != 0)
{
long value = (long) Integer.numberOfTrailingZeros(zeroToThirtyOne);
array[j] = value;
j++;
zeroToThirtyOne &= ~(1 << value);
}
for (int i = 0; i < this.table.length && j < this.size(); i++)
{
if (isNonSentinel(this.table[i]))
{
array[j] = this.table[i];
j++;
}
}
return array;
}
public boolean contains(long value)
{
if (isBetweenZeroAndThirtyOne(value))
{
int temp = this.zeroToThirtyOne;
return ((temp >>> value) & 1) != 0;
}
return this.table[this.probe(value)] == value;
}
public void forEach(LongProcedure procedure)
{
this.each(procedure);
}
/**
* @since 7.0.
*/
public void each(LongProcedure procedure)
{
int zeroToThirtyOne = this.zeroToThirtyOne;
while (zeroToThirtyOne != 0)
{
long value = (long) Integer.numberOfTrailingZeros(zeroToThirtyOne);
procedure.value(value);
zeroToThirtyOne &= ~(1 << value);
}
for (long value : this.table)
{
if (isNonSentinel(value))
{
procedure.value(value);
}
}
}
public LongHashSet select(LongPredicate predicate)
{
LongHashSet result = new LongHashSet();
int zeroToThirtyOne = this.zeroToThirtyOne;
while (zeroToThirtyOne != 0)
{
long value = (long) Integer.numberOfTrailingZeros(zeroToThirtyOne);
if (predicate.accept(value))
{
result.add(value);
}
zeroToThirtyOne &= ~(1 << value);
}
for (long value : this.table)
{
if (isNonSentinel(value))
{
if (predicate.accept(value))
{
result.add(value);
}
}
}
return result;
}
public MutableLongSet reject(LongPredicate predicate)
{
LongHashSet result = new LongHashSet();
int zeroToThirtyOne = this.zeroToThirtyOne;
while (zeroToThirtyOne != 0)
{
long value = (long) Integer.numberOfTrailingZeros(zeroToThirtyOne);
if (!predicate.accept(value))
{
result.add(value);
}
zeroToThirtyOne &= ~(1 << value);
}
for (long value : this.table)
{
if (isNonSentinel(value))
{
if (!predicate.accept(value))
{
result.add(value);
}
}
}
return result;
}
public MutableSet collect(LongToObjectFunction extends V> function)
{
MutableSet target = UnifiedSet.newSet(this.size());
int zeroToThirtyOne = this.zeroToThirtyOne;
while (zeroToThirtyOne != 0)
{
long value = (long) Integer.numberOfTrailingZeros(zeroToThirtyOne);
target.add(function.valueOf(value));
zeroToThirtyOne &= ~(1 << value);
}
for (long value : this.table)
{
if (isNonSentinel(value))
{
target.add(function.valueOf(value));
}
}
return target;
}
public long detectIfNone(LongPredicate predicate, long ifNone)
{
int zeroToThirtyOne = this.zeroToThirtyOne;
while (zeroToThirtyOne != 0)
{
long value = (long) Integer.numberOfTrailingZeros(zeroToThirtyOne);
if (predicate.accept(value))
{
return value;
}
zeroToThirtyOne &= ~(1 << value);
}
for (long value : this.table)
{
if (isNonSentinel(value))
{
if (predicate.accept(value))
{
return value;
}
}
}
return ifNone;
}
public int count(LongPredicate predicate)
{
int count = 0;
int zeroToThirtyOne = this.zeroToThirtyOne;
while (zeroToThirtyOne != 0)
{
long value = (long) Integer.numberOfTrailingZeros(zeroToThirtyOne);
if (predicate.accept(value))
{
count++;
}
zeroToThirtyOne &= ~(1 << value);
}
for (long value : this.table)
{
if (isNonSentinel(value))
{
if (predicate.accept(value))
{
count++;
}
}
}
return count;
}
public boolean anySatisfy(LongPredicate predicate)
{
int zeroToThirtyOne = this.zeroToThirtyOne;
while (zeroToThirtyOne != 0)
{
long value = (long) Integer.numberOfTrailingZeros(zeroToThirtyOne);
if (predicate.accept(value))
{
return true;
}
zeroToThirtyOne &= ~(1 << value);
}
for (long value : this.table)
{
if (isNonSentinel(value))
{
if (predicate.accept(value))
{
return true;
}
}
}
return false;
}
public boolean allSatisfy(LongPredicate predicate)
{
int zeroToThirtyOne = this.zeroToThirtyOne;
while (zeroToThirtyOne != 0)
{
long value = (long) Integer.numberOfTrailingZeros(zeroToThirtyOne);
if (!predicate.accept(value))
{
return false;
}
zeroToThirtyOne &= ~(1 << value);
}
for (long value : this.table)
{
if (isNonSentinel(value))
{
if (!predicate.accept(value))
{
return false;
}
}
}
return true;
}
public boolean noneSatisfy(LongPredicate predicate)
{
int zeroToThirtyOne = this.zeroToThirtyOne;
while (zeroToThirtyOne != 0)
{
long value = (long) Integer.numberOfTrailingZeros(zeroToThirtyOne);
if (predicate.accept(value))
{
return false;
}
zeroToThirtyOne &= ~(1 << value);
}
for (long value : this.table)
{
if (isNonSentinel(value))
{
if (predicate.accept(value))
{
return false;
}
}
}
return true;
}
public long sum()
{
long result = 0L;
int zeroToThirtyOne = this.zeroToThirtyOne;
while (zeroToThirtyOne != 0)
{
long value = (long) Integer.numberOfTrailingZeros(zeroToThirtyOne);
result += value;
zeroToThirtyOne &= ~(1 << value);
}
for (long value : this.table)
{
if (isNonSentinel(value))
{
result += value;
}
}
return result;
}
public long max()
{
if (this.isEmpty())
{
throw new NoSuchElementException();
}
long max = 31 - Integer.numberOfLeadingZeros(this.zeroToThirtyOne);
boolean isMaxSet = this.zeroToThirtyOneOccupied != 0;
for (long value : this.table)
{
if (isNonSentinel(value) && (!isMaxSet || max < value))
{
max = value;
isMaxSet = true;
}
}
return max;
}
public long min()
{
if (this.isEmpty())
{
throw new NoSuchElementException();
}
long min = (long) Integer.numberOfTrailingZeros(this.zeroToThirtyOne);
boolean isMinSet = this.zeroToThirtyOneOccupied != 0;
for (long value : this.table)
{
if (isNonSentinel(value) && (!isMinSet || value < min))
{
min = value;
isMinSet = true;
}
}
return min;
}
public LongSet freeze()
{
if (this.size() == 0)
{
return LongSets.immutable.with();
}
if (this.size() == 1)
{
return LongSets.immutable.with(this.longIterator().next());
}
this.copyOnWrite = true;
return new ImmutableLongHashSet(this.table, this.occupiedWithData, this.zeroToThirtyOne, this.zeroToThirtyOneOccupied);
}
public void writeExternal(ObjectOutput out) throws IOException
{
out.writeInt(this.size());
int zeroToThirtyOne = this.zeroToThirtyOne;
while (zeroToThirtyOne != 0)
{
long value = (long) Integer.numberOfTrailingZeros(zeroToThirtyOne);
out.writeLong(value);
zeroToThirtyOne &= ~(1 << value);
}
for (long value : this.table)
{
if (isNonSentinel(value))
{
out.writeLong(value);
}
}
}
public void readExternal(ObjectInput in) throws IOException
{
int size = in.readInt();
for (int i = 0; i < size; i++)
{
this.add(in.readLong());
}
}
public T injectInto(T injectedValue, ObjectLongToObjectFunction super T, ? extends T> function)
{
T result = injectedValue;
int zeroToThirtyOne = this.zeroToThirtyOne;
while (zeroToThirtyOne != 0)
{
long value = (long) Integer.numberOfTrailingZeros(zeroToThirtyOne);
result = function.valueOf(result, value);
zeroToThirtyOne &= ~(1 << value);
}
for (long value : this.table)
{
if (isNonSentinel(value))
{
result = function.valueOf(result, value);
}
}
return result;
}
/**
* Rehashes every element in the set into a new backing table of the smallest possible size and eliminating removed sentinels.
*/
public void compact()
{
this.rehash(this.smallestPowerOfTwoGreaterThan(this.size()));
}
private void rehash()
{
this.rehash(this.table.length);
}
private void rehashAndGrow()
{
this.rehash(this.table.length << 1);
}
private void rehash(int newCapacity)
{
int oldLength = this.table.length;
long[] old = this.table;
this.allocateTable(newCapacity);
this.occupiedWithData = 0;
this.occupiedWithSentinels = 0;
for (int i = 0; i < oldLength; i++)
{
if (isNonSentinel(old[i]))
{
this.add(old[i]);
}
}
}
private void allocateTable(int sizeToAllocate)
{
this.table = new long[sizeToAllocate];
}
// exposed for testing
int probe(long element)
{
int index = this.spreadAndMask(element);
long valueAtIndex = this.table[index];
if (valueAtIndex == element || valueAtIndex == EMPTY)
{
return index;
}
int removedIndex = valueAtIndex == REMOVED ? index : -1;
for (int i = 1; i < INITIAL_LINEAR_PROBE; i++)
{
int nextIndex = (index + i) & (this.table.length - 1);
valueAtIndex = this.table[nextIndex];
if (valueAtIndex == element)
{
return nextIndex;
}
if (valueAtIndex == EMPTY)
{
return removedIndex == -1 ? nextIndex : removedIndex;
}
if (valueAtIndex == REMOVED && removedIndex == -1)
{
removedIndex = nextIndex;
}
}
return this.probeTwo(element, removedIndex);
}
int probeTwo(long element, int removedIndex)
{
int index = this.spreadTwoAndMask(element);
for (int i = 0; i < INITIAL_LINEAR_PROBE; i++)
{
int nextIndex = (index + i) & (this.table.length - 1);
long valueAtIndex = this.table[nextIndex];
if (valueAtIndex == element)
{
return nextIndex;
}
if (valueAtIndex == EMPTY)
{
return removedIndex == -1 ? nextIndex : removedIndex;
}
if (valueAtIndex == REMOVED && removedIndex == -1)
{
removedIndex = nextIndex;
}
}
return this.probeThree(element, removedIndex);
}
int probeThree(long element, int removedIndex)
{
int nextIndex = (int) Long.reverse(SpreadFunctions.longSpreadOne(element));
int spreadTwo = (int) Long.reverse(SpreadFunctions.longSpreadTwo(element)) | 1;
while(true)
{
nextIndex = this.mask(nextIndex + spreadTwo);
long valueAtIndex = this.table[nextIndex];
if (valueAtIndex == element)
{
return nextIndex;
}
if (valueAtIndex == EMPTY)
{
return removedIndex == -1 ? nextIndex : removedIndex;
}
if (valueAtIndex == REMOVED && removedIndex == -1)
{
removedIndex = nextIndex;
}
}
}
// exposed for testing
int spreadAndMask(long element)
{
long code = SpreadFunctions.longSpreadOne(element);
return this.mask((int) code);
}
int spreadTwoAndMask(long element)
{
long code = SpreadFunctions.longSpreadTwo(element);
return this.mask((int) code);
}
private int mask(int spread)
{
return spread & (this.table.length - 1);
}
private void copyTable()
{
this.copyOnWrite = false;
long[] copy = new long[this.table.length];
System.arraycopy(this.table, 0, copy, 0, this.table.length);
this.table = copy;
}
private int maxOccupiedWithData()
{
int capacity = this.table.length;
// need at least one free slot for open addressing
return Math.min(capacity - 1, capacity / OCCUPIED_DATA_RATIO);
}
private int maxOccupiedWithSentinels()
{
return this.table.length / OCCUPIED_SENTINEL_RATIO;
}
private static boolean isNonSentinel(long value)
{
return value != EMPTY && value != REMOVED;
}
private static final class ImmutableLongHashSet extends AbstractImmutableLongSet implements Serializable
{
private static final long serialVersionUID = 1L;
private final long[] table;
private final int occupied;
// The 32 bits of this integer indicate whether the items 0L to 31L are present in the set.
private final int zeroToThirtyOne;
private final int zeroToThirtyOneOccupied;
private ImmutableLongHashSet(long[] table, int occupied, int zeroToThirtyOne, int zeroToThirtyOneOccupied)
{
this.checkOptimizedSize(occupied + zeroToThirtyOneOccupied);
this.occupied = occupied;
this.zeroToThirtyOneOccupied = zeroToThirtyOneOccupied;
this.zeroToThirtyOne = zeroToThirtyOne;
this.table = table;
}
private void checkOptimizedSize(int length)
{
if (length <= 1)
{
throw new IllegalArgumentException("Use LongSets.immutable.with() to instantiate an optimized collection");
}
}
public static ImmutableLongSet newSetWith(long... elements)
{
return LongHashSet.newSetWith(elements).toImmutable();
}
@Override
public int hashCode()
{
int result = 0;
int zeroToThirtyOne = this.zeroToThirtyOne;
while (zeroToThirtyOne != 0)
{
long value = (long) Integer.numberOfTrailingZeros(zeroToThirtyOne);
result += (int) (value ^ value >>> 32);
zeroToThirtyOne &= ~(1 << value);
}
if (this.table != null)
{
for (int i = 0; i < this.table.length; i++)
{
if (isNonSentinel(this.table[i]))
{
result += (int) (this.table[i] ^ this.table[i] >>> 32);
}
}
}
return result;
}
public int size()
{
return this.occupied + this.zeroToThirtyOneOccupied;
}
public void appendString(Appendable appendable, String start, String separator, String end)
{
try
{
appendable.append(start);
int count = 0;
int zeroToThirtyOne = this.zeroToThirtyOne;
while (zeroToThirtyOne != 0)
{
long value = (long) Integer.numberOfTrailingZeros(zeroToThirtyOne);
if (count > 0)
{
appendable.append(separator);
}
count++;
appendable.append(String.valueOf(value));
zeroToThirtyOne &= ~(1 << value);
}
for (long value : this.table)
{
if (isNonSentinel(value))
{
if (count > 0)
{
appendable.append(separator);
}
count++;
appendable.append(String.valueOf(value));
}
}
appendable.append(end);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
public LongIterator longIterator()
{
return new InternalLongIterator();
}
public long[] toArray()
{
long[] array = new long[this.size()];
int j = 0;
int zeroToThirtyOne = this.zeroToThirtyOne;
while (zeroToThirtyOne != 0)
{
long value = (long) Integer.numberOfTrailingZeros(zeroToThirtyOne);
array[j] = value;
j++;
zeroToThirtyOne &= ~(1 << value);
}
for (int i = 0; i < this.table.length && j < this.size(); i++)
{
if (isNonSentinel(this.table[i]))
{
array[j] = this.table[i];
j++;
}
}
return array;
}
public boolean contains(long value)
{
if (isBetweenZeroAndThirtyOne(value))
{
int temp = this.zeroToThirtyOne;
return ((temp >>> value) & 1) != 0;
}
return this.table[this.probe(value)] == value;
}
public void forEach(LongProcedure procedure)
{
this.each(procedure);
}
public void each(LongProcedure procedure)
{
int zeroToThirtyOne = this.zeroToThirtyOne;
while (zeroToThirtyOne != 0)
{
long value = (long) Integer.numberOfTrailingZeros(zeroToThirtyOne);
procedure.value(value);
zeroToThirtyOne &= ~(1 << value);
}
for (long value : this.table)
{
if (isNonSentinel(value))
{
procedure.value(value);
}
}
}
public ImmutableLongSet select(LongPredicate predicate)
{
LongHashSet result = new LongHashSet();
int zeroToThirtyOne = this.zeroToThirtyOne;
while (zeroToThirtyOne != 0)
{
long value = (long) Integer.numberOfTrailingZeros(zeroToThirtyOne);
if (predicate.accept(value))
{
result.add(value);
}
zeroToThirtyOne &= ~(1 << value);
}
for (long value : this.table)
{
if (isNonSentinel(value))
{
if (predicate.accept(value))
{
result.add(value);
}
}
}
return result.toImmutable();
}
public ImmutableLongSet reject(LongPredicate predicate)
{
LongHashSet result = new LongHashSet();
int zeroToThirtyOne = this.zeroToThirtyOne;
while (zeroToThirtyOne != 0)
{
long value = (long) Integer.numberOfTrailingZeros(zeroToThirtyOne);
if (!predicate.accept(value))
{
result.add(value);
}
zeroToThirtyOne &= ~(1 << value);
}
for (long value : this.table)
{
if (isNonSentinel(value))
{
if (!predicate.accept(value))
{
result.add(value);
}
}
}
return result.toImmutable();
}
public ImmutableSet collect(LongToObjectFunction extends V> function)
{
MutableSet target = UnifiedSet.newSet(this.size());
int zeroToThirtyOne = this.zeroToThirtyOne;
while (zeroToThirtyOne != 0)
{
long value = (long) Integer.numberOfTrailingZeros(zeroToThirtyOne);
target.add(function.valueOf(value));
zeroToThirtyOne &= ~(1 << value);
}
for (long value : this.table)
{
if (isNonSentinel(value))
{
target.add(function.valueOf(value));
}
}
return target.toImmutable();
}
public long detectIfNone(LongPredicate predicate, long ifNone)
{
int zeroToThirtyOne = this.zeroToThirtyOne;
while (zeroToThirtyOne != 0)
{
long value = (long) Integer.numberOfTrailingZeros(zeroToThirtyOne);
if (predicate.accept(value))
{
return value;
}
zeroToThirtyOne &= ~(1 << value);
}
for (long value : this.table)
{
if (isNonSentinel(value))
{
if (predicate.accept(value))
{
return value;
}
}
}
return ifNone;
}
public int count(LongPredicate predicate)
{
int count = 0;
int zeroToThirtyOne = this.zeroToThirtyOne;
while (zeroToThirtyOne != 0)
{
long value = (long) Integer.numberOfTrailingZeros(zeroToThirtyOne);
if (predicate.accept(value))
{
count++;
}
zeroToThirtyOne &= ~(1 << value);
}
for (long value : this.table)
{
if (isNonSentinel(value))
{
if (predicate.accept(value))
{
count++;
}
}
}
return count;
}
public boolean anySatisfy(LongPredicate predicate)
{
int zeroToThirtyOne = this.zeroToThirtyOne;
while (zeroToThirtyOne != 0)
{
long value = (long) Integer.numberOfTrailingZeros(zeroToThirtyOne);
if (predicate.accept(value))
{
return true;
}
zeroToThirtyOne &= ~(1 << value);
}
for (long value : this.table)
{
if (isNonSentinel(value))
{
if (predicate.accept(value))
{
return true;
}
}
}
return false;
}
public boolean allSatisfy(LongPredicate predicate)
{
int zeroToThirtyOne = this.zeroToThirtyOne;
while (zeroToThirtyOne != 0)
{
long value = (long) Integer.numberOfTrailingZeros(zeroToThirtyOne);
if (!predicate.accept(value))
{
return false;
}
zeroToThirtyOne &= ~(1 << value);
}
for (long value : this.table)
{
if (isNonSentinel(value))
{
if (!predicate.accept(value))
{
return false;
}
}
}
return true;
}
public boolean noneSatisfy(LongPredicate predicate)
{
int zeroToThirtyOne = this.zeroToThirtyOne;
while (zeroToThirtyOne != 0)
{
long value = (long) Integer.numberOfTrailingZeros(zeroToThirtyOne);
if (predicate.accept(value))
{
return false;
}
zeroToThirtyOne &= ~(1 << value);
}
for (long value : this.table)
{
if (isNonSentinel(value))
{
if (predicate.accept(value))
{
return false;
}
}
}
return true;
}
public long sum()
{
long result = 0L;
int zeroToThirtyOne = this.zeroToThirtyOne;
while (zeroToThirtyOne != 0)
{
long value = (long) Integer.numberOfTrailingZeros(zeroToThirtyOne);
result += value;
zeroToThirtyOne &= ~(1 << value);
}
for (long value : this.table)
{
if (isNonSentinel(value))
{
result += value;
}
}
return result;
}
public long max()
{
if (this.isEmpty())
{
throw new NoSuchElementException();
}
long max = 31 - Integer.numberOfLeadingZeros(this.zeroToThirtyOne);
boolean isMaxSet = this.zeroToThirtyOneOccupied != 0;
for (long value : this.table)
{
if (isNonSentinel(value) && (!isMaxSet || max < value))
{
max = value;
isMaxSet = true;
}
}
return max;
}
public long min()
{
if (this.isEmpty())
{
throw new NoSuchElementException();
}
long min = (long) Integer.numberOfTrailingZeros(this.zeroToThirtyOne);
boolean isMinSet = this.zeroToThirtyOneOccupied != 0;
for (long value : this.table)
{
if (isNonSentinel(value) && (!isMinSet || value < min))
{
min = value;
isMinSet = true;
}
}
return min;
}
public T injectInto(T injectedValue, ObjectLongToObjectFunction super T, ? extends T> function)
{
T result = injectedValue;
int zeroToThirtyOne = this.zeroToThirtyOne;
while (zeroToThirtyOne != 0)
{
long value = (long) Integer.numberOfTrailingZeros(zeroToThirtyOne);
result = function.valueOf(result, value);
zeroToThirtyOne &= ~(1 << value);
}
for (long value : this.table)
{
if (isNonSentinel(value))
{
result = function.valueOf(result, value);
}
}
return result;
}
private Object writeReplace()
{
return new ImmutableLongSetSerializationProxy(this);
}
// exposed for testing
int probe(long element)
{
int index = this.spreadAndMask(element);
long valueAtIndex = this.table[index];
if (valueAtIndex == element || valueAtIndex == EMPTY)
{
return index;
}
int removedIndex = valueAtIndex == REMOVED ? index : -1;
for (int i = 1; i < INITIAL_LINEAR_PROBE; i++)
{
int nextIndex = (index + i) & (this.table.length - 1);
valueAtIndex = this.table[nextIndex];
if (valueAtIndex == element)
{
return nextIndex;
}
if (valueAtIndex == EMPTY)
{
return removedIndex == -1 ? nextIndex : removedIndex;
}
if (valueAtIndex == REMOVED && removedIndex == -1)
{
removedIndex = nextIndex;
}
}
return this.probeTwo(element, removedIndex);
}
int probeTwo(long element, int removedIndex)
{
int index = this.spreadTwoAndMask(element);
for (int i = 0; i < INITIAL_LINEAR_PROBE; i++)
{
int nextIndex = (index + i) & (this.table.length - 1);
long valueAtIndex = this.table[nextIndex];
if (valueAtIndex == element)
{
return nextIndex;
}
if (valueAtIndex == EMPTY)
{
return removedIndex == -1 ? nextIndex : removedIndex;
}
if (valueAtIndex == REMOVED && removedIndex == -1)
{
removedIndex = nextIndex;
}
}
return this.probeThree(element, removedIndex);
}
int probeThree(long element, int removedIndex)
{
int nextIndex = (int) Long.reverse(SpreadFunctions.longSpreadOne(element));
int spreadTwo = (int) Long.reverse(SpreadFunctions.longSpreadTwo(element)) | 1;
while(true)
{
nextIndex = this.mask(nextIndex + spreadTwo);
long valueAtIndex = this.table[nextIndex];
if (valueAtIndex == element)
{
return nextIndex;
}
if (valueAtIndex == EMPTY)
{
return removedIndex == -1 ? nextIndex : removedIndex;
}
if (valueAtIndex == REMOVED && removedIndex == -1)
{
removedIndex = nextIndex;
}
}
}
// exposed for testing
int spreadAndMask(long element)
{
long code = SpreadFunctions.longSpreadOne(element);
return this.mask((int) code);
}
int spreadTwoAndMask(long element)
{
long code = SpreadFunctions.longSpreadTwo(element);
return this.mask((int) code);
}
private int mask(int spread)
{
return spread & (this.table.length - 1);
}
private class InternalLongIterator implements LongIterator
{
private int count;
private int position;
private long zeroToThirtyOne;
public boolean hasNext()
{
return this.count < ImmutableLongHashSet.this.size();
}
public long next()
{
if (!this.hasNext())
{
throw new NoSuchElementException("next() called, but the iterator is exhausted");
}
this.count++;
while (this.zeroToThirtyOne < 32)
{
if (ImmutableLongHashSet.this.contains(this.zeroToThirtyOne))
{
long result = this.zeroToThirtyOne;
this.zeroToThirtyOne++;
return result;
}
this.zeroToThirtyOne++;
}
long[] table = ImmutableLongHashSet.this.table;
while (!isNonSentinel(table[this.position]))
{
this.position++;
}
long result = table[this.position];
this.position++;
return result;
}
}
}
private class InternalLongIterator implements MutableLongIterator
{
private int count;
private int position;
private long zeroToThirtyOne;
public boolean hasNext()
{
return this.count < LongHashSet.this.size();
}
public long next()
{
if (!this.hasNext())
{
throw new NoSuchElementException("next() called, but the iterator is exhausted");
}
this.count++;
while (this.zeroToThirtyOne < 32)
{
if (LongHashSet.this.contains(this.zeroToThirtyOne))
{
long result = this.zeroToThirtyOne;
this.zeroToThirtyOne++;
return result;
}
this.zeroToThirtyOne++;
}
long[] table = LongHashSet.this.table;
while (!isNonSentinel(table[this.position]))
{
this.position++;
}
long result = table[this.position];
this.position++;
return result;
}
public void remove()
{
if (this.count == 0)
{
throw new IllegalStateException();
}
long removeValue;
if (this.zeroToThirtyOne <= 32L && this.position == 0)
{
if (LongHashSet.this.zeroToThirtyOne != (LongHashSet.this.zeroToThirtyOne | 1 << (this.zeroToThirtyOne - 1)))
{
throw new IllegalStateException();
}
removeValue = this.zeroToThirtyOne - 1;
}
else if (LongHashSet.this.table[this.position - 1] == REMOVED)
{
throw new IllegalStateException();
}
else
{
removeValue = LongHashSet.this.table[this.position - 1];
}
LongHashSet.this.remove(removeValue);
this.count--;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy