Class UnorderedCollection<E>
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- net.automatalib.common.smartcollection.AbstractSmartCollection<E>
-
- net.automatalib.common.smartcollection.UnorderedCollection<E>
-
- Type Parameters:
E
- element class.
- All Implemented Interfaces:
Iterable<E>
,Collection<E>
,CapacityManagement
,SmartCollection<E>
public final class UnorderedCollection<E> extends AbstractSmartCollection<E> implements CapacityManagement
This class implements a collection for storing objects in no particular order.It supports (amortized) constant time insertion and removal. Removal does not invalidate the references of other objects, and can be performed during iteration (using the respective
Iterator.remove()
method).
-
-
Constructor Summary
Constructors Constructor Description UnorderedCollection()
Default constructor.UnorderedCollection(int initialCapacity)
Constructor.UnorderedCollection(Collection<? extends E> coll)
Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
addAll(Collection<? extends E> coll)
<T extends E>
voidaddAll(T[] array)
Adds all elements from the specified array.E
choose()
Retrieves an arbitrary element from the collection.ElementReference
chooseRef()
Retrieves the reference to an arbitrary element from the collection.void
clear()
void
deepClear()
Thoroughly clears the collection, fixing all issues that may have been caused by a call of the aboveSmartCollection.quickClear()
.boolean
ensureAdditionalCapacity(int additionalSpace)
Ensures that the internal storage has room for at least the provided number of additional elements.boolean
ensureCapacity(int minCapacity)
Ensures that the internal storage has room for at least the provided number of elements.E
get(ElementReference ref)
Retrieves an element by its reference.void
hintNextCapacity(int nextCapacityHint)
Gives a hint regarding the capacity that should be reserved when resizing the internal storage for the next time.boolean
isEmpty()
Iterator<E>
iterator()
void
quickClear()
Quickly clears this collection.ElementReference
referencedAdd(E elem)
Adds an element to the collection, returning a reference to the newly added element.Iterator<ElementReference>
referenceIterator()
Retrieves an iterator for iterating over the references of elements in this collection.Iterable<ElementReference>
references()
This is a method provided for convenience, which allows iterating over the element references using a foreach-stylefor
-loop.void
remove(ElementReference ref)
Removes an element (by its reference) from the collection.void
replace(ElementReference ref, E newElement)
Replaces the element referenced by the given reference with the specified element.int
size()
void
swap(UnorderedCollection<E> other)
Swaps the contents of thisUnorderedCollection
with another one storing the same elements.-
Methods inherited from class net.automatalib.common.smartcollection.AbstractSmartCollection
add, addAll, find, remove
-
Methods inherited from class java.util.AbstractCollection
contains, containsAll, removeAll, retainAll, toArray, toArray, toString
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Collection
contains, containsAll, equals, hashCode, parallelStream, removeAll, removeIf, retainAll, spliterator, stream, toArray, toArray
-
-
-
-
Constructor Detail
-
UnorderedCollection
public UnorderedCollection()
Default constructor. Reserves capacity for 10 elements.
-
UnorderedCollection
public UnorderedCollection(int initialCapacity)
Constructor. Reserves the specified initial capacity.- Parameters:
initialCapacity
- the number of elements to reserve capacity for.
-
UnorderedCollection
public UnorderedCollection(Collection<? extends E> coll)
Constructor. Initializes the collection with the contents of the specified collection.- Parameters:
coll
- the collection.
-
-
Method Detail
-
ensureCapacity
public boolean ensureCapacity(@UnknownInitialization(UnorderedCollection.class) UnorderedCollection<E> this, int minCapacity)
Description copied from interface:CapacityManagement
Ensures that the internal storage has room for at least the provided number of elements.- Specified by:
ensureCapacity
in interfaceCapacityManagement
- Parameters:
minCapacity
- the minimal number of elements the storage should have room for.- Returns:
true
iff the internal storage had to be resized,false
otherwise.
-
ensureAdditionalCapacity
public boolean ensureAdditionalCapacity(int additionalSpace)
Description copied from interface:CapacityManagement
Ensures that the internal storage has room for at least the provided number of additional elements.Calling this method is equivalent to calling the above
CapacityManagement.ensureCapacity(int)
with an argument ofsize() + additionalCapacity
.- Specified by:
ensureAdditionalCapacity
in interfaceCapacityManagement
- Parameters:
additionalSpace
- the number of additional elements the storage should have room for.- Returns:
true
iff the internal storage had to be resized,false
otherwise.
-
hintNextCapacity
public void hintNextCapacity(int nextCapacityHint)
Description copied from interface:CapacityManagement
Gives a hint regarding the capacity that should be reserved when resizing the internal storage for the next time. This method acts like a "lazy"CapacityManagement.ensureCapacity(int)
, i.e. it reserves the specified capacity at the time the next resizing of the internal storage is performed.This method is useful when a not too imprecise upper bound on the elements that will in consequence be added is known. Since the actual number of elements added may be lower than the specified upper bound, a resizing that would have been performed by
CapacityManagement.ensureCapacity(int)
might not be necessary.- Specified by:
hintNextCapacity
in interfaceCapacityManagement
- Parameters:
nextCapacityHint
- the next capacity hint.
-
get
public E get(ElementReference ref)
Description copied from interface:SmartCollection
Retrieves an element by its reference.If the reference belongs to another collection, the behavior is undefined.
- Specified by:
get
in interfaceSmartCollection<E>
- Parameters:
ref
- the element's reference.- Returns:
- the element.
-
referencedAdd
public ElementReference referencedAdd(E elem)
Description copied from interface:SmartCollection
Adds an element to the collection, returning a reference to the newly added element. If the collection does not support containing the same element multiple times, a reference to the previously existing element is returned.- Specified by:
referencedAdd
in interfaceSmartCollection<E>
- Parameters:
elem
- the element to be added.- Returns:
- a reference to this element in the collection.
-
remove
public void remove(ElementReference ref)
Description copied from interface:SmartCollection
Removes an element (by its reference) from the collection.If the reference does not belong to this collection, the behavior is undefined.
- Specified by:
remove
in interfaceSmartCollection<E>
- Parameters:
ref
- the reference to the element to be removed.
-
referenceIterator
public Iterator<ElementReference> referenceIterator()
Description copied from interface:SmartCollection
Retrieves an iterator for iterating over the references of elements in this collection.- Specified by:
referenceIterator
in interfaceSmartCollection<E>
- Returns:
- the reference iterator.
-
replace
public void replace(ElementReference ref, E newElement)
Description copied from interface:SmartCollection
Replaces the element referenced by the given reference with the specified element.- Specified by:
replace
in interfaceSmartCollection<E>
- Parameters:
ref
- the reference of the element to be replaced.newElement
- the replacement.
-
choose
public E choose()
Description copied from interface:SmartCollection
Retrieves an arbitrary element from the collection.- Specified by:
choose
in interfaceSmartCollection<E>
- Overrides:
choose
in classAbstractSmartCollection<E>
- Returns:
- an arbitrary element from the collection
-
chooseRef
public ElementReference chooseRef()
Description copied from interface:SmartCollection
Retrieves the reference to an arbitrary element from the collection. If the collection is empty, aNoSuchElementException
is thrown.- Specified by:
chooseRef
in interfaceSmartCollection<E>
- Overrides:
chooseRef
in classAbstractSmartCollection<E>
- Returns:
- the reference to an arbitrary element in the collection
-
references
public Iterable<ElementReference> references()
Description copied from interface:SmartCollection
This is a method provided for convenience, which allows iterating over the element references using a foreach-stylefor
-loop.- Specified by:
references
in interfaceSmartCollection<E>
- Overrides:
references
in classAbstractSmartCollection<E>
- Returns:
- an
Iterable
with the aboveSmartCollection.referenceIterator()
as its iterator.
-
addAll
public <T extends E> void addAll(T[] array)
Description copied from interface:SmartCollection
Adds all elements from the specified array.- Specified by:
addAll
in interfaceSmartCollection<E>
- Overrides:
addAll
in classAbstractSmartCollection<E>
- Type Parameters:
T
- array element class, may be a subclass ofE
.- Parameters:
array
- the array of elements to be added.
-
addAll
public boolean addAll(@UnknownInitialization(UnorderedCollection.class) UnorderedCollection<E> this, Collection<? extends E> coll)
- Specified by:
addAll
in interfaceCollection<E>
- Overrides:
addAll
in classAbstractCollection<E>
-
quickClear
public void quickClear()
Description copied from interface:SmartCollection
Quickly clears this collection. This method is supposed to perform the minimum amount of effort such that this collection is emptied, disregarding all other side effects such as referencing or garbage collection issues.Depending on the implementation, this may be just the same as
Collection.clear()
. However, this could also have side effects like hampering the garbage collection or such.After calling this method, even a call of the normal
Collection.clear()
is not guaranteed to fix all these issues. This can only be achieved by the methodSmartCollection.deepClear()
below.- Specified by:
quickClear
in interfaceSmartCollection<E>
- Overrides:
quickClear
in classAbstractSmartCollection<E>
-
deepClear
public void deepClear()
Description copied from interface:SmartCollection
Thoroughly clears the collection, fixing all issues that may have been caused by a call of the aboveSmartCollection.quickClear()
.- Specified by:
deepClear
in interfaceSmartCollection<E>
- Overrides:
deepClear
in classAbstractSmartCollection<E>
-
iterator
public Iterator<E> iterator()
- Specified by:
iterator
in interfaceCollection<E>
- Specified by:
iterator
in interfaceIterable<E>
- Overrides:
iterator
in classAbstractSmartCollection<E>
-
size
public int size()
- Specified by:
size
in interfaceCollection<E>
- Specified by:
size
in classAbstractCollection<E>
-
isEmpty
public boolean isEmpty()
- Specified by:
isEmpty
in interfaceCollection<E>
- Overrides:
isEmpty
in classAbstractCollection<E>
-
clear
public void clear()
- Specified by:
clear
in interfaceCollection<E>
- Overrides:
clear
in classAbstractCollection<E>
-
swap
public void swap(UnorderedCollection<E> other)
Swaps the contents of thisUnorderedCollection
with another one storing the same elements. This operation runs in constant time, by only swapping storage references.- Parameters:
other
- theUnorderedCollection
to swap contents with.
-
-