Class ReferenceArraySet<K>
- java.lang.Object
-
- java.util.AbstractCollection<K>
-
- it.unimi.dsi.fastutil.objects.AbstractReferenceCollection<K>
-
- it.unimi.dsi.fastutil.objects.AbstractReferenceSet<K>
-
- it.unimi.dsi.fastutil.objects.ReferenceArraySet<K>
-
- All Implemented Interfaces:
ObjectIterable<K>
,ReferenceCollection<K>
,ReferenceSet<K>
,java.io.Serializable
,java.lang.Cloneable
,java.lang.Iterable<K>
,java.util.Collection<K>
,java.util.Set<K>
public class ReferenceArraySet<K> extends AbstractReferenceSet<K> implements java.io.Serializable, java.lang.Cloneable
A simple, brute-force implementation of a set based on a backing array.The main purpose of this implementation is that of wrapping cleanly the brute-force approach to the storage of a very small number of items: just put them into an array and scan linearly to find an item.
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description ReferenceArraySet()
Creates a new empty array set.ReferenceArraySet(int capacity)
Creates a new empty array set of given initial capacity.ReferenceArraySet(ReferenceCollection<K> c)
Creates a new array set copying the contents of a given collection.ReferenceArraySet(ReferenceSet<K> c)
Creates a new array set copying the contents of a given collection.ReferenceArraySet(java.lang.Object[] a)
Creates a new array set using the given backing array.ReferenceArraySet(java.lang.Object[] a, int size)
Creates a new array set using the given backing array and the given number of elements of the array.ReferenceArraySet(java.util.Collection<? extends K> c)
Creates a new array set copying the contents of a given set.ReferenceArraySet(java.util.Set<? extends K> c)
Creates a new array set copying the contents of a given set.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
add(K k)
void
clear()
ReferenceArraySet<K>
clone()
Returns a deep copy of this set.boolean
contains(java.lang.Object k)
boolean
isEmpty()
ObjectIterator<K>
iterator()
Returns a type-specific iterator on the elements of this collection.static <K> ReferenceArraySet<K>
of()
Creates a new empty array set.static <K> ReferenceArraySet<K>
of(K e)
Creates a new array set using the element given.static <K> ReferenceArraySet<K>
of(K... a)
Creates a new array set using an array of elements.static <K> ReferenceArraySet<K>
ofUnchecked()
Creates a new empty array set.static <K> ReferenceArraySet<K>
ofUnchecked(K... a)
Creates a new array set using an array of elements.boolean
remove(java.lang.Object k)
int
size()
ObjectSpliterator<K>
spliterator()
Returns a type-specific spliterator on the elements of this set.java.lang.Object[]
toArray()
<K> K[]
toArray(K[] a)
-
Methods inherited from class it.unimi.dsi.fastutil.objects.AbstractReferenceSet
equals, hashCode
-
Methods inherited from class it.unimi.dsi.fastutil.objects.AbstractReferenceCollection
toString
-
-
-
-
Constructor Detail
-
ReferenceArraySet
public ReferenceArraySet(java.lang.Object[] a)
Creates a new array set using the given backing array. The resulting set will have as many elements as the array.It is the responsibility of the caller to ensure that the elements of
a
are distinct.- Parameters:
a
- the backing array.
-
ReferenceArraySet
public ReferenceArraySet()
Creates a new empty array set.
-
ReferenceArraySet
public ReferenceArraySet(int capacity)
Creates a new empty array set of given initial capacity.- Parameters:
capacity
- the initial capacity.
-
ReferenceArraySet
public ReferenceArraySet(ReferenceCollection<K> c)
Creates a new array set copying the contents of a given collection.- Parameters:
c
- a collection.
-
ReferenceArraySet
public ReferenceArraySet(java.util.Collection<? extends K> c)
Creates a new array set copying the contents of a given set.- Parameters:
c
- a collection.
-
ReferenceArraySet
public ReferenceArraySet(ReferenceSet<K> c)
Creates a new array set copying the contents of a given collection.- Parameters:
c
- a collection.
-
ReferenceArraySet
public ReferenceArraySet(java.util.Set<? extends K> c)
Creates a new array set copying the contents of a given set.- Parameters:
c
- a collection.
-
ReferenceArraySet
public ReferenceArraySet(java.lang.Object[] a, int size)
Creates a new array set using the given backing array and the given number of elements of the array.It is the responsibility of the caller to ensure that the first
size
elements ofa
are distinct.- Parameters:
a
- the backing array.size
- the number of valid elements ina
.
-
-
Method Detail
-
of
public static <K> ReferenceArraySet<K> of()
Creates a new empty array set.- Returns:
- a new empty array set.
-
of
public static <K> ReferenceArraySet<K> of(K e)
Creates a new array set using the element given.- Parameters:
e
- the element that the returned set will contain.- Returns:
- a new array set containing
e
.
-
of
@SafeVarargs public static <K> ReferenceArraySet<K> of(K... a)
Creates a new array set using an array of elements.Unlike the array accepting constructors, this method will throw if duplicate elements are encountered. This adds a non-trivial validation burden. Use
ofUnchecked()
if you know your input has no duplicates, which will skip this validation.- Parameters:
a
- the backing array of the returned array set.- Returns:
- a new array set containing the elements in
a
. - Throws:
java.lang.IllegalArgumentException
- if there were duplicate entries.
-
ofUnchecked
public static <K> ReferenceArraySet<K> ofUnchecked()
Creates a new empty array set.- Returns:
- a new empty array set.
-
ofUnchecked
@SafeVarargs public static <K> ReferenceArraySet<K> ofUnchecked(K... a)
Creates a new array set using an array of elements.It is the responsibility of the caller to ensure that the elements of
a
are distinct.- Parameters:
a
- the backing array of the returned array set.- Returns:
- a new array set containing the elements in
a
.
-
iterator
public ObjectIterator<K> iterator()
Description copied from interface:ReferenceCollection
Returns a type-specific iterator on the elements of this collection.- Specified by:
iterator
in interfacejava.util.Collection<K>
- Specified by:
iterator
in interfacejava.lang.Iterable<K>
- Specified by:
iterator
in interfaceObjectIterable<K>
- Specified by:
iterator
in interfaceReferenceCollection<K>
- Specified by:
iterator
in interfaceReferenceSet<K>
- Specified by:
iterator
in interfacejava.util.Set<K>
- Specified by:
iterator
in classAbstractReferenceSet<K>
- Returns:
- a type-specific iterator on the elements of this collection.
- See Also:
Iterable.iterator()
-
spliterator
public ObjectSpliterator<K> spliterator()
Returns a type-specific spliterator on the elements of this set.Set spliterators must report at least
Spliterator.DISTINCT
.See
Set.spliterator()
for more documentation on the requirements of the returned spliterator.In addition to the usual trait of
DISTINCT
for sets, the returned spliterator will also report the traitORDERED
.The returned spliterator is late-binding; it will track structural changes after the current item, up until the first
trySplit()
, at which point the maximum index will be fixed.
Structural changes before the current item or after the firsttrySplit()
will result in unspecified behavior.- Specified by:
spliterator
in interfacejava.util.Collection<K>
- Specified by:
spliterator
in interfacejava.lang.Iterable<K>
- Specified by:
spliterator
in interfaceObjectIterable<K>
- Specified by:
spliterator
in interfaceReferenceCollection<K>
- Specified by:
spliterator
in interfaceReferenceSet<K>
- Specified by:
spliterator
in interfacejava.util.Set<K>
- Returns:
- a type-specific spliterator on the elements of this collection.
-
contains
public boolean contains(java.lang.Object k)
-
size
public int size()
-
remove
public boolean remove(java.lang.Object k)
-
add
public boolean add(K k)
-
clear
public void clear()
-
isEmpty
public boolean isEmpty()
-
toArray
public java.lang.Object[] toArray()
-
toArray
public <K> K[] toArray(K[] a)
-
clone
public ReferenceArraySet<K> clone()
Returns a deep copy of this set.This method performs a deep copy of this array set; the data stored in the set, however, is not cloned. Note that this makes a difference only for object keys.
- Returns:
- a deep copy of this set.
-
-