Class CompactLinkedHashSet<E>
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractSet<E>
-
- com.google.common.collect.CompactHashSet<E>
-
- com.google.common.collect.CompactLinkedHashSet<E>
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Iterable<E>,java.util.Collection<E>,java.util.Set<E>
class CompactLinkedHashSet<E> extends CompactHashSet<E>
CompactLinkedHashSet is an implementation of a Set, which a predictable iteration order that matches the insertion order. All optional operations (adding and removing) are supported. All elements, includingnull, are permitted.contains(x),add(x)andremove(x), are all (expected and amortized) constant time operations. Expected in the hashtable sense (depends on the hash function doing a good job of distributing the elements to the buckets to a distribution not far from uniform), and amortized since some operations can trigger a hash table resize.This implementation consumes significantly less memory than
java.util.LinkedHashSetor evenjava.util.HashSet, and places considerably less load on the garbage collector. Likejava.util.LinkedHashSet, it offers insertion-order iteration, with identical behavior.This class should not be assumed to be universally superior to
java.util.LinkedHashSet. Generally speaking, this class reduces object allocation and memory consumption at the price of moderately increased constant factors of CPU. Only use this class when there is a specific reason to prioritize memory over CPU.
-
-
Field Summary
Fields Modifier and Type Field Description private static intENDPOINTprivate intfirstEntryPointer to the first node in the linked list, orENDPOINTif there are no entries.private intlastEntryPointer to the last node in the linked list, orENDPOINTif there are no entries.private int[]predecessorPointer to the predecessor of an entry in insertion order.private int[]successorPointer to the successor of an entry in insertion order.-
Fields inherited from class com.google.common.collect.CompactHashSet
elements, HASH_FLOODING_FPP
-
-
Constructor Summary
Constructors Constructor Description CompactLinkedHashSet()CompactLinkedHashSet(int expectedSize)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) intadjustAfterRemove(int indexBeforeRemove, int indexRemoved)Updates the index an iterator is pointing to after a call to remove: returns the index of the entry that should be looked at after a removal on indexRemoved, with indexBeforeRemove as the index that *was* the next entry that would be looked at.(package private) intallocArrays()Handle lazy allocation of arrays.voidclear()(package private) java.util.Set<E>convertToHashFloodingResistantImplementation()static <E> CompactLinkedHashSet<E>create()Creates an emptyCompactLinkedHashSetinstance.static <E> CompactLinkedHashSet<E>create(E... elements)Creates aCompactLinkedHashSetinstance containing the given elements in unspecified order.static <E> CompactLinkedHashSet<E>create(java.util.Collection<? extends E> collection)Creates a mutableCompactLinkedHashSetinstance containing the elements of the given collection in the order returned by the collection's iterator.static <E> CompactLinkedHashSet<E>createWithExpectedSize(int expectedSize)Creates aCompactLinkedHashSetinstance, with a high enough "initial capacity" that it should holdexpectedSizeelements without rebuilding internal data structures.(package private) intfirstEntryIndex()private intgetPredecessor(int entry)(package private) intgetSuccessor(int entry)(package private) voidinit(int expectedSize)Pseudoconstructor for serialization support.(package private) voidinsertEntry(int entryIndex, E object, int hash, int mask)Creates a fresh entry with the specified object at the specified position in the entry arrays.(package private) voidmoveLastEntry(int dstIndex, int mask)Moves the last entry in the entry array intodstIndex, and nulls out its old position.(package private) voidresizeEntries(int newCapacity)Resizes the internal entries array to the specified capacity, which may be greater or less than the current capacity.private voidsetPredecessor(int entry, int pred)private voidsetSucceeds(int pred, int succ)private voidsetSuccessor(int entry, int succ)java.util.Spliterator<E>spliterator()java.lang.Object[]toArray()<T> T[]toArray(T[] a)-
Methods inherited from class com.google.common.collect.CompactHashSet
add, contains, delegateOrNull, forEach, incrementModCount, isEmpty, isUsingHashFloodingResistance, iterator, needsAllocArrays, remove, size, trimToSize
-
-
-
-
Field Detail
-
ENDPOINT
private static final int ENDPOINT
- See Also:
- Constant Field Values
-
predecessor
private transient int[] predecessor
Pointer to the predecessor of an entry in insertion order. ENDPOINT indicates a node is the first node in insertion order; all values at indices ≥CompactHashSet.size()are UNSET.
-
successor
private transient int[] successor
Pointer to the successor of an entry in insertion order. ENDPOINT indicates a node is the last node in insertion order; all values at indices ≥CompactHashSet.size()are UNSET.
-
firstEntry
private transient int firstEntry
Pointer to the first node in the linked list, orENDPOINTif there are no entries.
-
lastEntry
private transient int lastEntry
Pointer to the last node in the linked list, orENDPOINTif there are no entries.
-
-
Method Detail
-
create
public static <E> CompactLinkedHashSet<E> create()
Creates an emptyCompactLinkedHashSetinstance.
-
create
public static <E> CompactLinkedHashSet<E> create(java.util.Collection<? extends E> collection)
Creates a mutableCompactLinkedHashSetinstance containing the elements of the given collection in the order returned by the collection's iterator.- Parameters:
collection- the elements that the set should contain- Returns:
- a new
CompactLinkedHashSetcontaining those elements (minus duplicates)
-
create
@SafeVarargs public static <E> CompactLinkedHashSet<E> create(E... elements)
Creates aCompactLinkedHashSetinstance containing the given elements in unspecified order.- Parameters:
elements- the elements that the set should contain- Returns:
- a new
CompactLinkedHashSetcontaining those elements (minus duplicates)
-
createWithExpectedSize
public static <E> CompactLinkedHashSet<E> createWithExpectedSize(int expectedSize)
Creates aCompactLinkedHashSetinstance, with a high enough "initial capacity" that it should holdexpectedSizeelements without rebuilding internal data structures.- Parameters:
expectedSize- the number of elements you expect to add to the returned set- Returns:
- a new, empty
CompactLinkedHashSetwith enough capacity to holdexpectedSizeelements without resizing - Throws:
java.lang.IllegalArgumentException- ifexpectedSizeis negative
-
init
void init(int expectedSize)
Description copied from class:CompactHashSetPseudoconstructor for serialization support.- Overrides:
initin classCompactHashSet<E>
-
allocArrays
int allocArrays()
Description copied from class:CompactHashSetHandle lazy allocation of arrays.- Overrides:
allocArraysin classCompactHashSet<E>
-
convertToHashFloodingResistantImplementation
java.util.Set<E> convertToHashFloodingResistantImplementation()
- Overrides:
convertToHashFloodingResistantImplementationin classCompactHashSet<E>
-
getPredecessor
private int getPredecessor(int entry)
-
getSuccessor
int getSuccessor(int entry)
- Overrides:
getSuccessorin classCompactHashSet<E>
-
setSuccessor
private void setSuccessor(int entry, int succ)
-
setPredecessor
private void setPredecessor(int entry, int pred)
-
setSucceeds
private void setSucceeds(int pred, int succ)
-
insertEntry
void insertEntry(int entryIndex, E object, int hash, int mask)Description copied from class:CompactHashSetCreates a fresh entry with the specified object at the specified position in the entry arrays.- Overrides:
insertEntryin classCompactHashSet<E>
-
moveLastEntry
void moveLastEntry(int dstIndex, int mask)Description copied from class:CompactHashSetMoves the last entry in the entry array intodstIndex, and nulls out its old position.- Overrides:
moveLastEntryin classCompactHashSet<E>
-
resizeEntries
void resizeEntries(int newCapacity)
Description copied from class:CompactHashSetResizes the internal entries array to the specified capacity, which may be greater or less than the current capacity.- Overrides:
resizeEntriesin classCompactHashSet<E>
-
firstEntryIndex
int firstEntryIndex()
- Overrides:
firstEntryIndexin classCompactHashSet<E>
-
adjustAfterRemove
int adjustAfterRemove(int indexBeforeRemove, int indexRemoved)Description copied from class:CompactHashSetUpdates the index an iterator is pointing to after a call to remove: returns the index of the entry that should be looked at after a removal on indexRemoved, with indexBeforeRemove as the index that *was* the next entry that would be looked at.- Overrides:
adjustAfterRemovein classCompactHashSet<E>
-
toArray
public java.lang.Object[] toArray()
- Specified by:
toArrayin interfacejava.util.Collection<E>- Specified by:
toArrayin interfacejava.util.Set<E>- Overrides:
toArrayin classCompactHashSet<E>
-
toArray
public <T> T[] toArray(T[] a)
- Specified by:
toArrayin interfacejava.util.Collection<E>- Specified by:
toArrayin interfacejava.util.Set<E>- Overrides:
toArrayin classCompactHashSet<E>
-
spliterator
public java.util.Spliterator<E> spliterator()
- Specified by:
spliteratorin interfacejava.util.Collection<E>- Specified by:
spliteratorin interfacejava.lang.Iterable<E>- Specified by:
spliteratorin interfacejava.util.Set<E>- Overrides:
spliteratorin classCompactHashSet<E>
-
clear
public void clear()
- Specified by:
clearin interfacejava.util.Collection<E>- Specified by:
clearin interfacejava.util.Set<E>- Overrides:
clearin classCompactHashSet<E>
-
-