Package org.graphstream.algorithm
Class FixedArrayList<E>
java.lang.Object
org.graphstream.algorithm.FixedArrayList<E>
- All Implemented Interfaces:
Iterable<E>
,Collection<E>
,RandomAccess
public class FixedArrayList<E> extends Object implements Collection<E>, RandomAccess
Array list with immutable element indices.
A fixed array list is like an array list, but it ensures the property that
each element will always stay at the same index, even if elements are
removed in between. The counterpart of this property is that the array
handles by itself the insertion of new elements (since when an element is
removed in the middle, this position can be reused), and therefore indices
cannot be chosen (i.e. only the add(Object)
and
addAll(Collection)
methods are usable to insert new elements in the
array).
This is the reason why this does not implement the List interface, because the add(int,E) method cannot be implemented.
Furthermore, this array cannot contain null values, because it marks unused positions within the array using the null value.
- Since:
- 20040912
- Author:
- Antoine Dutot
-
Constructor Summary
Constructors Constructor Description FixedArrayList()
FixedArrayList(int capacity)
-
Method Summary
Modifier and Type Method Description boolean
add(E element)
Add oneelement
in the array.boolean
addAll(Collection<? extends E> c)
boolean
addAt(int i, E element)
This operation set the i-th cell with the given value.void
clear()
boolean
contains(Object o)
boolean
containsAll(Collection<?> c)
boolean
equals(Object o)
E
get(int i)
I-th element.int
getLastIndex()
Last index used by theadd(Object)
method.int
getNextAddIndex()
The index that will be used in case of a next insertion in this array.boolean
hasIndex(int i)
True if the given index i references a value.boolean
isEmpty()
Iterator<E>
iterator()
int
realSize()
Real size of the array, counting elements that have been erased.E
remove(int i)
Remove the element at indexi
.boolean
remove(Object e)
Remove the elemente
.boolean
removeAll(Collection<?> c)
boolean
retainAll(Collection<?> c)
int
size()
Number of elements in the array.Object[]
toArray()
<T> T[]
toArray(T[] a)
E
unsafeGet(int i)
I-th element.Methods inherited from class java.lang.Object
getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.util.Collection
hashCode, parallelStream, removeIf, spliterator, stream, toArray
-
Constructor Details
-
FixedArrayList
public FixedArrayList() -
FixedArrayList
public FixedArrayList(int capacity)
-
-
Method Details
-
size
public int size()Number of elements in the array.- Specified by:
size
in interfaceCollection<E>
- Returns:
- The number of elements in the array.
-
realSize
public int realSize()Real size of the array, counting elements that have been erased.- See Also:
unsafeGet(int)
-
isEmpty
public boolean isEmpty()- Specified by:
isEmpty
in interfaceCollection<E>
-
hasIndex
public boolean hasIndex(int i)True if the given index i references a value.- Parameters:
i
- The index to test.- Returns:
- True if a value exists at the given index.
-
get
I-th element.- Parameters:
i
- The element index.- Returns:
- The element at index
i
.
-
unsafeGet
I-th element. Like theget(int)
method but it does not check the element does not exists at the given index.- Parameters:
i
- The element index.- Returns:
- The element at index
i
.
-
contains
- Specified by:
contains
in interfaceCollection<E>
-
containsAll
- Specified by:
containsAll
in interfaceCollection<E>
-
equals
- Specified by:
equals
in interfaceCollection<E>
- Overrides:
equals
in classObject
-
iterator
-
getLastIndex
public int getLastIndex()Last index used by theadd(Object)
method.- Returns:
- The last insertion index.
-
getNextAddIndex
public int getNextAddIndex()The index that will be used in case of a next insertion in this array.- Returns:
- next add index
-
toArray
- Specified by:
toArray
in interfaceCollection<E>
-
toArray
public <T> T[] toArray(T[] a)- Specified by:
toArray
in interfaceCollection<E>
-
add
Add oneelement
in the array. The index used for inserting the element is then available usinggetLastIndex()
. This method complexity is O(1).- Specified by:
add
in interfaceCollection<E>
- Parameters:
element
- The element to add.- Returns:
- Always true.
- Throws:
NullPointerException
- If a null value is inserted.- See Also:
getLastIndex()
-
addAll
- Specified by:
addAll
in interfaceCollection<E>
- Throws:
UnsupportedOperationException
-
addAt
This operation set the i-th cell with the given value. This works only if the cell is empty, or if i is larger or equal to the size of the array (if larger, empty cells are added to fill the gap, and free indices will be used by the add() method). If the cell is not empty, the return value is false. This method is a convenience method, and its complexity is not O(1) like the add() and remove() methods. At worse the complexity is O(n). It is optimized so that when adding the element whose id is the one given bygetNextAddIndex()
its complexity is O(1).- Parameters:
i
- The index of the cell to change.element
- The value to set.- Returns:
- false If the insertion was not successful (there was already something in the set).
-
remove
Remove the element at indexi
. This method complexity is O(1).- Parameters:
i
- Index of the element to remove.- Returns:
- The removed element.
-
remove
Remove the elemente
. At worse the complexity is O(n).- Specified by:
remove
in interfaceCollection<E>
- Parameters:
e
- The element to remove.- Returns:
- True if removed.
-
removeAll
- Specified by:
removeAll
in interfaceCollection<E>
-
retainAll
- Specified by:
retainAll
in interfaceCollection<E>
-
clear
public void clear()- Specified by:
clear
in interfaceCollection<E>
-