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 Details

  • Method Details

    • size

      public int size()
      Number of elements in the array.
      Specified by:
      size in interface Collection<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 interface Collection<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

      public E get​(int i)
      I-th element.
      Parameters:
      i - The element index.
      Returns:
      The element at index i.
    • unsafeGet

      public E unsafeGet​(int i)
      I-th element. Like the get(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

      public boolean contains​(Object o)
      Specified by:
      contains in interface Collection<E>
    • containsAll

      public boolean containsAll​(Collection<?> c)
      Specified by:
      containsAll in interface Collection<E>
    • equals

      public boolean equals​(Object o)
      Specified by:
      equals in interface Collection<E>
      Overrides:
      equals in class Object
    • iterator

      public Iterator<E> iterator()
      Specified by:
      iterator in interface Collection<E>
      Specified by:
      iterator in interface Iterable<E>
    • getLastIndex

      public int getLastIndex()
      Last index used by the add(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

      public Object[] toArray()
      Specified by:
      toArray in interface Collection<E>
    • toArray

      public <T> T[] toArray​(T[] a)
      Specified by:
      toArray in interface Collection<E>
    • add

      public boolean add​(E element) throws NullPointerException
      Add one element in the array. The index used for inserting the element is then available using getLastIndex(). This method complexity is O(1).
      Specified by:
      add in interface Collection<E>
      Parameters:
      element - The element to add.
      Returns:
      Always true.
      Throws:
      NullPointerException - If a null value is inserted.
      See Also:
      getLastIndex()
    • addAll

      public boolean addAll​(Collection<? extends E> c) throws UnsupportedOperationException
      Specified by:
      addAll in interface Collection<E>
      Throws:
      UnsupportedOperationException
    • addAt

      public boolean addAt​(int i, E element)
      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 by getNextAddIndex() 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

      public E remove​(int i)
      Remove the element at index i. This method complexity is O(1).
      Parameters:
      i - Index of the element to remove.
      Returns:
      The removed element.
    • remove

      public boolean remove​(Object e)
      Remove the element e. At worse the complexity is O(n).
      Specified by:
      remove in interface Collection<E>
      Parameters:
      e - The element to remove.
      Returns:
      True if removed.
    • removeAll

      public boolean removeAll​(Collection<?> c)
      Specified by:
      removeAll in interface Collection<E>
    • retainAll

      public boolean retainAll​(Collection<?> c)
      Specified by:
      retainAll in interface Collection<E>
    • clear

      public void clear()
      Specified by:
      clear in interface Collection<E>