Class SimpleArrayList<V>

  • Type Parameters:
    V - the type which is being stored by this class.
    All Implemented Interfaces:
    java.lang.Iterable<V>, java.util.Collection<V>, java.util.List<V>

    public class SimpleArrayList<V>
    extends java.util.AbstractList<V>
    Different implementation of an ArrayList data structures. This version is tailored for JaCoP. Use with care, check when it uses == instead of equals().
    Version:
    4.8
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private V[] elementData
      The array buffer into which the elements of the ArrayList are stored.
      private int size
      The size of the ArrayList (the number of elements it contains).
      • Fields inherited from class java.util.AbstractList

        modCount
    • Constructor Summary

      Constructors 
      Constructor Description
      SimpleArrayList()
      Constructs an empty list with an initial capacity of ten.
      SimpleArrayList​(int initialCapacity)
      Constructs an empty list with the specified initial capacity.
      SimpleArrayList​(java.util.Collection<? extends V> c)
      Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(int index, V element)
      Inserts the specified element at the specified position in this list.
      boolean add​(V o)
      Appends the specified element to the end of this list.
      boolean addAll​(java.util.Collection<? extends V> c)
      Appends all of the elements in the specified Collection to the end of this list, in the order that they are returned by the specified Collection's Iterator.
      void clear()
      Removes all of the elements from this list.
      void clearNoGC()
      same as clear(), but references to objects are kept internally.
      boolean contains​(java.lang.Object elem)
      Returns true if this list contains the specified element.
      void ensureCapacity​(int minCapacity)
      Increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.
      boolean equals​(java.lang.Object o)
      AbstractList defines equals so that it depends on the objects included.
      private void fastRemove​(int index)
      Private remove method that skips bounds checking and does not return the value removed.
      V get​(int index)
      Returns the element at the specified position in this list.
      int hashCode()
      AbstractList defines hashCode so that it depends on the objects included.
      int indexOf​(java.lang.Object elem)
      Searches for the first occurrence of the given argument, testing for equality using the equals method.
      int indexOf​(java.lang.Object elem, int lastPosition)
      Searches for the first occurrence of the given argument, testing for equality using the == method.
      boolean isEmpty()
      Tests if this list has no elements.
      int lastIndexOf​(java.lang.Object elem)
      Returns the index of the last occurrence of the specified object in this list.
      V pop()
      It removes and returns the last element in the list.
      void push​(V element)
      It inserts the element at the end of the list
      V remove​(int index)
      Removes the element at the specified position in this list.
      boolean remove​(java.lang.Object o)
      Removes a single instance of the specified element from this list, if it is present (optional operation).
      V set​(int index, V element)
      Replaces the element at the specified position in this list with the specified element.
      void setElementAt​(V element, int index)
      Replaces the element at the specified position in this list with the specified element.
      int size()
      Returns the number of elements in this list.
      java.lang.Object[] toArray()
      Returns an array containing all of the elements in this list in the correct order.
      <T> T[] toArray​(T[] a)
      Returns an array containing all of the elements in this list in the correct order; the runtime type of the returned array is that of the specified array.
      java.lang.String toString()
      Check if the given index is in range.
      void trimToSize()
      Trims the capacity of this ArrayList instance to be the list's current size.
      • Methods inherited from class java.util.AbstractList

        addAll, iterator, listIterator, listIterator, removeRange, subList
      • Methods inherited from class java.util.AbstractCollection

        containsAll, removeAll, retainAll
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Collection

        parallelStream, removeIf, stream, toArray
      • Methods inherited from interface java.lang.Iterable

        forEach
      • Methods inherited from interface java.util.List

        containsAll, removeAll, replaceAll, retainAll, sort, spliterator
    • Field Detail

      • elementData

        private V[] elementData
        The array buffer into which the elements of the ArrayList are stored. The capacity of the ArrayList is the length of this array buffer.
      • size

        private int size
        The size of the ArrayList (the number of elements it contains).
    • Constructor Detail

      • SimpleArrayList

        public SimpleArrayList()
        Constructs an empty list with an initial capacity of ten.
      • SimpleArrayList

        public SimpleArrayList​(java.util.Collection<? extends V> c)
        Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator. The ArrayList instance has an initial capacity of 110% the size of the specified collection.
        Parameters:
        c - the collection whose elements are to be placed into this list.
        Throws:
        java.lang.NullPointerException - if the specified collection is null.
      • SimpleArrayList

        public SimpleArrayList​(int initialCapacity)
        Constructs an empty list with the specified initial capacity.
        Parameters:
        initialCapacity - the initial capacity of the list.
    • Method Detail

      • hashCode

        public int hashCode()
        AbstractList defines hashCode so that it depends on the objects included. This makes it costly (linear in the number of elements).

        Taking the hash of elementData will make it faster TODO make sure this is what we want

        Specified by:
        hashCode in interface java.util.Collection<V>
        Specified by:
        hashCode in interface java.util.List<V>
        Overrides:
        hashCode in class java.util.AbstractList<V>
      • equals

        public boolean equals​(java.lang.Object o)
        AbstractList defines equals so that it depends on the objects included. This makes it costly (linear in the number of elements).

        Equality of references makes it faster TODO make sure this is what we want

        Specified by:
        equals in interface java.util.Collection<V>
        Specified by:
        equals in interface java.util.List<V>
        Overrides:
        equals in class java.util.AbstractList<V>
      • add

        public boolean add​(V o)
        Appends the specified element to the end of this list.
        Specified by:
        add in interface java.util.Collection<V>
        Specified by:
        add in interface java.util.List<V>
        Overrides:
        add in class java.util.AbstractList<V>
        Parameters:
        o - element to be appended to this list.
      • add

        public void add​(int index,
                        V element)
        Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).
        Specified by:
        add in interface java.util.List<V>
        Overrides:
        add in class java.util.AbstractList<V>
        Parameters:
        index - index at which the specified element is to be inserted.
        element - element to be inserted.
      • addAll

        public boolean addAll​(java.util.Collection<? extends V> c)
        Appends all of the elements in the specified Collection to the end of this list, in the order that they are returned by the specified Collection's Iterator. The behavior of this operation is undefined if the specified Collection is modified while the operation is in progress. (This implies that the behavior of this call is undefined if the specified Collection is this list, and this list is nonempty.)
        Specified by:
        addAll in interface java.util.Collection<V>
        Specified by:
        addAll in interface java.util.List<V>
        Overrides:
        addAll in class java.util.AbstractCollection<V>
        Parameters:
        c - the elements to be inserted into this list.
        Returns:
        true if some elements has been added.
      • clear

        public void clear()
        Removes all of the elements from this list. The list will be empty after this call returns.
        Specified by:
        clear in interface java.util.Collection<V>
        Specified by:
        clear in interface java.util.List<V>
        Overrides:
        clear in class java.util.AbstractList<V>
      • clearNoGC

        public void clearNoGC()
        same as clear(), but references to objects are kept internally. This allows the operation to be constant time, but implies that objects will not be garbage collected until their references are overwritten to store other objects.
      • contains

        public boolean contains​(java.lang.Object elem)
        Returns true if this list contains the specified element.
        Specified by:
        contains in interface java.util.Collection<V>
        Specified by:
        contains in interface java.util.List<V>
        Overrides:
        contains in class java.util.AbstractCollection<V>
        Parameters:
        elem - element whose presence in this list is to be tested.
        Returns:
        true if the specified element is present; false otherwise.
      • ensureCapacity

        public void ensureCapacity​(int minCapacity)
        Increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.
        Parameters:
        minCapacity - the desired minimum capacity.
      • fastRemove

        private void fastRemove​(int index)
        Private remove method that skips bounds checking and does not return the value removed.
      • get

        public V get​(int index)
        Returns the element at the specified position in this list.
        Specified by:
        get in interface java.util.List<V>
        Specified by:
        get in class java.util.AbstractList<V>
        Parameters:
        index - index of element to return.
        Returns:
        the element at the specified position in this list.
      • pop

        public V pop()
        It removes and returns the last element in the list.
        Returns:
        the last element in the list.
      • push

        public void push​(V element)
        It inserts the element at the end of the list
        Parameters:
        element - the added element.
      • indexOf

        public int indexOf​(java.lang.Object elem)
        Searches for the first occurrence of the given argument, testing for equality using the equals method.
        Specified by:
        indexOf in interface java.util.List<V>
        Overrides:
        indexOf in class java.util.AbstractList<V>
        Parameters:
        elem - an object.
        Returns:
        the index of the first occurrence of the argument in this list; returns value -1 if the object is not found.
      • indexOf

        public int indexOf​(java.lang.Object elem,
                           int lastPosition)
        Searches for the first occurrence of the given argument, testing for equality using the == method.
        Parameters:
        elem - an object.
        lastPosition - last index to which it should check.
        Returns:
        the index of the first occurrence of the argument in this list; returns value -1 if the object is not found.
      • isEmpty

        public boolean isEmpty()
        Tests if this list has no elements.
        Specified by:
        isEmpty in interface java.util.Collection<V>
        Specified by:
        isEmpty in interface java.util.List<V>
        Overrides:
        isEmpty in class java.util.AbstractCollection<V>
        Returns:
        true if this list has no elements; false otherwise.
      • lastIndexOf

        public int lastIndexOf​(java.lang.Object elem)
        Returns the index of the last occurrence of the specified object in this list.
        Specified by:
        lastIndexOf in interface java.util.List<V>
        Overrides:
        lastIndexOf in class java.util.AbstractList<V>
        Parameters:
        elem - the desired element.
        Returns:
        the index of the last occurrence of the specified object in this list; returns -1 if the object is not found.
      • remove

        public V remove​(int index)
        Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).
        Specified by:
        remove in interface java.util.List<V>
        Overrides:
        remove in class java.util.AbstractList<V>
        Parameters:
        index - the index of the element to removed.
        Returns:
        the element that was removed from the list.
      • remove

        public boolean remove​(java.lang.Object o)
        Removes a single instance of the specified element from this list, if it is present (optional operation). More formally, removes an element e such that (o==null ? e==null : o.equals(e)), if the list contains one or more such elements. Returns true if the list contained the specified element (or equivalently, if the list changed as a result of the call).

        Specified by:
        remove in interface java.util.Collection<V>
        Specified by:
        remove in interface java.util.List<V>
        Overrides:
        remove in class java.util.AbstractCollection<V>
        Parameters:
        o - element to be removed from this list, if present.
        Returns:
        true if the list contained the specified element.
      • set

        public V set​(int index,
                     V element)
        Replaces the element at the specified position in this list with the specified element.
        Specified by:
        set in interface java.util.List<V>
        Overrides:
        set in class java.util.AbstractList<V>
        Parameters:
        index - index of element to replace.
        element - element to be stored at the specified position.
        Returns:
        the element previously at the specified position.
      • setElementAt

        public void setElementAt​(V element,
                                 int index)
        Replaces the element at the specified position in this list with the specified element.
        Parameters:
        index - index of element to replace.
        element - element to be stored at the specified position.
      • size

        public int size()
        Returns the number of elements in this list.
        Specified by:
        size in interface java.util.Collection<V>
        Specified by:
        size in interface java.util.List<V>
        Specified by:
        size in class java.util.AbstractCollection<V>
        Returns:
        the number of elements in this list.
      • toArray

        public java.lang.Object[] toArray()
        Returns an array containing all of the elements in this list in the correct order.
        Specified by:
        toArray in interface java.util.Collection<V>
        Specified by:
        toArray in interface java.util.List<V>
        Overrides:
        toArray in class java.util.AbstractCollection<V>
        Returns:
        an array containing all of the elements in this list in the correct order.
      • toArray

        public <T> T[] toArray​(T[] a)
        Returns an array containing all of the elements in this list in the correct order; the runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list.

        If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the collection is set to null. This is useful in determining the length of the list only if the caller knows that the list does not contain any null elements.

        Specified by:
        toArray in interface java.util.Collection<V>
        Specified by:
        toArray in interface java.util.List<V>
        Overrides:
        toArray in class java.util.AbstractCollection<V>
        Type Parameters:
        T - the type which is being stored by a SimpleArrayList.
        Parameters:
        a - the array into which the elements of the list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
        Returns:
        an array containing the elements of the list.
        Throws:
        java.lang.ArrayStoreException - if the runtime type of a is not a supertype of the runtime type of every element in this list.
      • toString

        public java.lang.String toString()
        Check if the given index is in range. If not, throw an appropriate runtime exception. This method does *not* check if the index is negative: It is always used immediately prior to an array access, which throws an ArrayIndexOutOfBoundsException if index is negative.
        Overrides:
        toString in class java.util.AbstractCollection<V>
      • trimToSize

        public void trimToSize()
        Trims the capacity of this ArrayList instance to be the list's current size. An application can use this operation to minimize the storage of an ArrayList instance.