Class UnorderedSet<K>
- All Implemented Interfaces:
Serializable,Cloneable,Iterable<K>,Collection<K>,Set<K>
public class UnorderedSet<K> extends Object implements Set<K>, Serializable, Cloneable
OrderedSet in this library, which is
based on the fastutil library's ObjectLinkedOpenHashSet class; the ordering and indexed access have been removed to
potentially reduce the time cost of insertion and removal at the expense of increasing time cost for access by index.
This does support optional hash strategies for array (and other) keys, which fastutil's collections can do in a
different way, and has better support than HashSet for construction with an array of items or construction
with a Collection of items (this also helps addAll(Object[])).
Instances of this class use a hash table to represent a set. The table is filled up to a specified load factor, and then doubled in size to accommodate new entries. If the table is emptied below one fourth of the load factor, it is halved in size. However, halving is not performed when deleting entries from an iterator, as it would interfere with the iteration process.
Note that clear() does not modify the hash table size. Rather, a
family of trimming methods lets you control the size of
the table; this is particularly useful if you reuse instances of this class.
This class implements the interface of a Set, not a SortedSet.
You can pass an CrossHash.IHasher instance such as CrossHash.generalHasher as an extra parameter to
most of this class' constructors, which allows the OrderedSet to use arrays (usually primitive arrays) as items. If
you expect only one type of array, you can use an instance like CrossHash.intHasher to hash int arrays, or
the aforementioned generalHasher to hash most kinds of arrays (it can't handle most multi-dimensional arrays well).
If you aren't using array items, you don't need to give an IHasher to the constructor and can ignore this feature.
Thank you, Sebastiano Vigna, for making FastUtil available to the public with such high quality.
See https://github.com/vigna/fastutil for the original library.
- Author:
- Sebastiano Vigna (responsible for all the hard parts), Tommy Ettinger (mostly responsible for squashing several layers of parent classes into one monster class)
- See Also:
- Serialized Form
-
Field Summary
Fields Modifier and Type Field Description protected booleancontainsNullWhether this set contains the key zero.static intDEFAULT_INITIAL_SIZEThe initial default size of a hash table.static floatDEFAULT_LOAD_FACTORThe default load factor of a hash table.floatfThe acceptable load factor.static floatFAST_LOAD_FACTORThe load factor for a (usually small) table that is meant to be particularly fast.protected CrossHash.IHasherhasherprotected K[]keyThe array of keys.protected intmaskThe mask for wrapping a position counter.protected intmaxFillThreshold after which we rehash.protected intnThe current table size.protected intsizeNumber of entries in the set (including the key zero, if present).static floatVERY_FAST_LOAD_FACTORThe load factor for a (usually very small) table that is meant to be extremely fast. -
Constructor Summary
Constructors Constructor Description UnorderedSet()Creates a new hash set with initial expectedDEFAULT_INITIAL_SIZEelements andDEFAULT_LOAD_FACTORas load factor.UnorderedSet(int expected)Creates a new hash set withDEFAULT_LOAD_FACTORas load factor.UnorderedSet(int expected, float f)Creates a new hash map.UnorderedSet(int expected, float f, CrossHash.IHasher hasher)Creates a new hash map.UnorderedSet(int expected, CrossHash.IHasher hasher)Creates a new hash set withDEFAULT_LOAD_FACTORas load factor.UnorderedSet(Collection<? extends K> c)Creates a new hash set withDEFAULT_LOAD_FACTORas load factor copying a given collection.UnorderedSet(Collection<? extends K> c, float f)Creates a new hash set copying a given collection.UnorderedSet(Collection<? extends K> c, float f, CrossHash.IHasher hasher)Creates a new hash set copying a given collection.UnorderedSet(Collection<? extends K> c, CrossHash.IHasher hasher)Creates a new hash set withDEFAULT_LOAD_FACTORas load factor copying a given collection.UnorderedSet(Iterator<? extends K> i)Creates a new hash set withDEFAULT_LOAD_FACTORas load factor using elements provided by a type-specific iterator.UnorderedSet(Iterator<? extends K> i, float f)Creates a new hash set using elements provided by a type-specific iterator.UnorderedSet(K[] a)Creates a new hash set withDEFAULT_LOAD_FACTORas load factor copying the elements of an array.UnorderedSet(K[] a, float f)Creates a new hash set copying the elements of an array.UnorderedSet(K[] a, float f, CrossHash.IHasher hasher)Creates a new hash set copying the elements of an array.UnorderedSet(K[] a, int offset, int length)Creates a new hash set withDEFAULT_LOAD_FACTORas load factor and fills it with the elements of a given array.UnorderedSet(K[] a, int offset, int length, float f)Creates a new hash set and fills it with the elements of a given array.UnorderedSet(K[] a, int offset, int length, float f, CrossHash.IHasher hasher)Creates a new hash set and fills it with the elements of a given array.UnorderedSet(K[] a, int offset, int length, CrossHash.IHasher hasher)Creates a new hash set withDEFAULT_LOAD_FACTORas load factor and fills it with the elements of a given array.UnorderedSet(K[] a, CrossHash.IHasher hasher)Creates a new hash set withDEFAULT_LOAD_FACTORas load factor copying the elements of an array.UnorderedSet(CrossHash.IHasher hasher)Creates a new hash set withDEFAULT_LOAD_FACTORas load factor. -
Method Summary
Modifier and Type Method Description booleanadd(K k)booleanaddAll(Collection<? extends K> c)booleanaddAll(K[] a)KaddOrGet(K k)Add a random element if not present, get the existing value if already present.static intarraySize(int expected, float f)Returns the least power of two smaller than or equal to 230 and larger than or equal toMath.ceil( expected / f ).voidclear()Objectclone()Returns a deep copy of this map.booleancontains(Object k)booleancontainsAll(Collection<?> c)Checks whether this collection contains all elements from the given collection.booleanequals(Object o)Kget(Object k)Returns the element of this set that is equal to the given key, ornull.longhash64()inthashCode()Returns a hash code for this set.booleanisEmpty()Iterator<K>iterator()static intmaxFill(int n, float f)Returns the maximum number of entries that can be filled before rehashing.static longmaxFill(long n, float f)Returns the maximum number of entries that can be filled before rehashing.protected intpositionOf(Object k)protected voidrehash(int newN)Rehashes the map.booleanremove(Object k)booleanremoveAll(Collection<?> c)Remove from this collection all elements in the given collection.booleanretainAll(Collection<?> c)Retains in this collection only elements from the given collection.protected voidshiftKeys(int pos)Shifts left entries with the specified hash code, starting at the specified position, and empties the resulting free entry.intsize()Object[]toArray()<T> T[]toArray(T[] a)StringtoString()booleantrim()Rehashes the map, making the table as small as possible.booleantrim(int n)Rehashes this map if the table is too large.
-
Field Details
-
key
The array of keys. -
mask
The mask for wrapping a position counter. -
containsNull
Whether this set contains the key zero. -
n
The current table size. -
maxFill
Threshold after which we rehash. It must be the table size timesf. -
size
Number of entries in the set (including the key zero, if present). -
f
The acceptable load factor. -
DEFAULT_INITIAL_SIZE
The initial default size of a hash table.- See Also:
- Constant Field Values
-
DEFAULT_LOAD_FACTOR
The default load factor of a hash table.- See Also:
- Constant Field Values
-
FAST_LOAD_FACTOR
The load factor for a (usually small) table that is meant to be particularly fast.- See Also:
- Constant Field Values
-
VERY_FAST_LOAD_FACTOR
The load factor for a (usually very small) table that is meant to be extremely fast.- See Also:
- Constant Field Values
-
hasher
-
-
Constructor Details
-
UnorderedSet
Creates a new hash map.The actual table size will be the least power of two greater than
expected/f.- Parameters:
expected- the expected number of elements in the hash set.f- the load factor.
-
UnorderedSet
Creates a new hash set withDEFAULT_LOAD_FACTORas load factor.- Parameters:
expected- the expected number of elements in the hash set.
-
UnorderedSet
public UnorderedSet()Creates a new hash set with initial expectedDEFAULT_INITIAL_SIZEelements andDEFAULT_LOAD_FACTORas load factor. -
UnorderedSet
Creates a new hash set copying a given collection.- Parameters:
c- aCollectionto be copied into the new hash set.f- the load factor.
-
UnorderedSet
Creates a new hash set withDEFAULT_LOAD_FACTORas load factor copying a given collection.- Parameters:
c- aCollectionto be copied into the new hash set.
-
UnorderedSet
Creates a new hash set using elements provided by a type-specific iterator.- Parameters:
i- a type-specific iterator whose elements will fill the set.f- the load factor.
-
UnorderedSet
Creates a new hash set withDEFAULT_LOAD_FACTORas load factor using elements provided by a type-specific iterator.- Parameters:
i- a type-specific iterator whose elements will fill the set.
-
UnorderedSet
Creates a new hash set and fills it with the elements of a given array.- Parameters:
a- an array whose elements will be used to fill the set.offset- the first element to use.length- the number of elements to use.f- the load factor.
-
UnorderedSet
Creates a new hash set withDEFAULT_LOAD_FACTORas load factor and fills it with the elements of a given array.- Parameters:
a- an array whose elements will be used to fill the set.offset- the first element to use.length- the number of elements to use.
-
UnorderedSet
Creates a new hash set copying the elements of an array.- Parameters:
a- an array to be copied into the new hash set.f- the load factor.
-
UnorderedSet
Creates a new hash set withDEFAULT_LOAD_FACTORas load factor copying the elements of an array.- Parameters:
a- an array to be copied into the new hash set.
-
UnorderedSet
Creates a new hash map.The actual table size will be the least power of two greater than
expected/f.- Parameters:
expected- the expected number of elements in the hash set.f- the load factor.hasher- used to hash items; typically only needed when K is an array, where CrossHash has implementations
-
UnorderedSet
Creates a new hash set withDEFAULT_LOAD_FACTORas load factor.- Parameters:
hasher- used to hash items; typically only needed when K is an array, where CrossHash has implementations
-
UnorderedSet
Creates a new hash set withDEFAULT_LOAD_FACTORas load factor.- Parameters:
hasher- used to hash items; typically only needed when K is an array, where CrossHash has implementations
-
UnorderedSet
Creates a new hash set copying a given collection.- Parameters:
c- aCollectionto be copied into the new hash set.f- the load factor.hasher- used to hash items; typically only needed when K is an array, where CrossHash has implementations
-
UnorderedSet
Creates a new hash set withDEFAULT_LOAD_FACTORas load factor copying a given collection.- Parameters:
c- aCollectionto be copied into the new hash set.hasher- used to hash items; typically only needed when K is an array, where CrossHash has implementations
-
UnorderedSet
Creates a new hash set and fills it with the elements of a given array.- Parameters:
a- an array whose elements will be used to fill the set.offset- the first element to use.length- the number of elements to use.f- the load factor.
-
UnorderedSet
Creates a new hash set withDEFAULT_LOAD_FACTORas load factor and fills it with the elements of a given array.- Parameters:
a- an array whose elements will be used to fill the set.offset- the first element to use.length- the number of elements to use.
-
UnorderedSet
Creates a new hash set copying the elements of an array.- Parameters:
a- an array to be copied into the new hash set.f- the load factor.
-
UnorderedSet
Creates a new hash set withDEFAULT_LOAD_FACTORas load factor copying the elements of an array.- Parameters:
a- an array to be copied into the new hash set.
-
-
Method Details
-
addAll
-
addAll
-
add
-
addOrGet
Add a random element if not present, get the existing value if already present.This is equivalent to (but faster than) doing a:
K exist = set.get(k); if (exist == null) { set.add(k); exist = k; } -
shiftKeys
Shifts left entries with the specified hash code, starting at the specified position, and empties the resulting free entry.- Parameters:
pos- a starting position.
-
remove
-
get
Returns the element of this set that is equal to the given key, ornull.- Returns:
- the element of this set that is equal to the given key, or
null.
-
contains
-
positionOf
-
clear
-
size
-
containsAll
Checks whether this collection contains all elements from the given collection.- Specified by:
containsAllin interfaceCollection<K>- Specified by:
containsAllin interfaceSet<K>- Parameters:
c- a collection.- Returns:
trueif this collection contains all elements of the argument.
-
retainAll
Retains in this collection only elements from the given collection. -
removeAll
Remove from this collection all elements in the given collection. If the collection is an instance of this class, it uses faster iterators. -
isEmpty
-
iterator
-
trim
Rehashes the map, making the table as small as possible.This method rehashes the table to the smallest size satisfying the load factor. It can be used when the set will not be changed anymore, so to optimize access speed and size.
If the table size is already the minimum possible, this method does nothing.
- Returns:
- true if there was enough memory to trim the map.
- See Also:
trim(int)
-
trim
Rehashes this map if the table is too large.Let N be the smallest table size that can hold
max(n,entries, still satisfying the load factor. If the current table size is smaller than or equal to N, this method does nothing. Otherwise, it rehashes this map in a table of size N.size())This method is useful when reusing maps. Clearing a map leaves the table size untouched. If you are reusing a map many times, you can call this method with a typical size to avoid keeping around a very large table just because of a few large transient maps.
- Parameters:
n- the threshold for the trimming.- Returns:
- true if there was enough memory to trim the map.
- See Also:
trim()
-
rehash
Rehashes the map.This method implements the basic rehashing strategy, and may be overriden by subclasses implementing different rehashing strategies (e.g., disk-based rehashing). However, you should not override this method unless you understand the internal workings of this class.
- Parameters:
newN- the new size
-
clone
Returns a deep copy of this map.This method performs a deep copy of this hash map; the data stored in the map, however, is not cloned. Note that this makes a difference only for object keys.
-
hashCode
Returns a hash code for this set.This method overrides the generic method provided by the superclass. Since
equals()is not overriden, it is important that the value returned by this method is the same value as the one returned by the overriden method. -
hash64
-
maxFill
Returns the maximum number of entries that can be filled before rehashing.- Parameters:
n- the size of the backing array.f- the load factor.- Returns:
- the maximum number of entries before rehashing.
-
maxFill
Returns the maximum number of entries that can be filled before rehashing.- Parameters:
n- the size of the backing array.f- the load factor.- Returns:
- the maximum number of entries before rehashing.
-
arraySize
Returns the least power of two smaller than or equal to 230 and larger than or equal toMath.ceil( expected / f ).- Parameters:
expected- the expected number of elements in a hash table.f- the load factor.- Returns:
- the minimum possible size for a backing array.
- Throws:
IllegalArgumentException- if the necessary size is larger than 230.
-
toArray
-
toArray
-
toString
-
equals
-