Class AbstractVector<T>

java.lang.Object
net.mtrop.doom.struct.vector.AbstractArrayStorage<T>
net.mtrop.doom.struct.vector.AbstractVector<T>
Type Parameters:
T - the underlying object type.
All Implemented Interfaces:
Iterable<T>, Sizable
Direct Known Subclasses:
AbstractMappedVector

public abstract class AbstractVector<T> extends AbstractArrayStorage<T> implements Iterable<T>, Sizable
An abstract class for performing unsynchronized Vector (data structure) operatons and concepts. None of the methods are synchronized, so be careful when using this in a multithreaded setting or extend this class with synchronized methods.
Author:
Matthew Tropiano
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    class 
    Iterator class for this vector.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected int
    Capacity increment.
    protected int
    Amount of objects in the storageArray.

    Fields inherited from class net.mtrop.doom.struct.vector.AbstractArrayStorage

    DEFAULT_CAPACITY, storageArray
  • Constructor Summary

    Constructors
    Constructor
    Description
    Makes a new vector.
    AbstractVector(int capacity)
    Makes a new vector that doubles every resize.
    AbstractVector(int capacity, int capacityIncrement)
    Makes a new vector.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(int index, T object)
    Adds an object at an index.
    void
    add(T object)
    Adds an object to the end of the vector.
    void
    Clears the vector.
    boolean
    contains(T object)
    Checks if an object exists in this vector.
    get(int index)
    Gets an object at an index in the vector.
    int
    Gets the capacity of this vector (size before it resizes itself).
    int
     
    int
    getIndexOf(T object)
    Gets the index of an object, presumably in the vector.
    boolean
    Returns true if there is nothing in this vector, false otherwise.
    Returns an iterator for this vector.
    boolean
    remove(T object)
    Removes an object from the vector, if it exists in the vector.
    removeIndex(int index)
    Removes an object from an index in the vector and shifts everything after it down an index position.
    void
    replace(int index, T object)
    Sets an object at an index.
    int
    search(T object, Comparator<? super T> comparator)
    Gets the index of an object, presumably in the vector via binary search.
    void
    setCapacity(int capacity)
    Sets this storageArray's capacity to some value.
    void
    setCapacityIncrement(int capacityIncrement)
    Sets the capacity increment value.
    void
    Clears the vector shallowly - removes no references and just sets the current size to 0.
    void
    shift(int sourceIndex, int targetIndex)
    Moves the object at an index in this vector to another index, shifting the contents between the two selected indices in this vector back or forward.
    int
    Returns the amount of objects in the vector.
    void
    Sorts this vector using NATURAL ORDERING.
    void
    sort(int startIndex, int endIndex)
    Sorts this vector using NATURAL ORDERING.
    void
    sort(Comparator<? super T> comparator)
    Sorts this vector using a comparator.
    void
    sort(Comparator<? super T> comparator, int startIndex, int endIndex)
    Sorts this vector using a comparator.
    void
    swap(int index0, int index1)
    Swaps the contents of two indices in the vector.
    void
    toArray(T[] out)
    Dumps the contents of this structure to an array.
    Returns this storageArray as a string.
    void
    Sets this vector's capacity to its current size.

    Methods inherited from class net.mtrop.doom.struct.vector.AbstractArrayStorage

    set

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface java.lang.Iterable

    forEach, spliterator
  • Field Details

    • capacityIncrement

      protected int capacityIncrement
      Capacity increment.
    • size

      protected int size
      Amount of objects in the storageArray.
  • Constructor Details

    • AbstractVector

      public AbstractVector()
      Makes a new vector.
    • AbstractVector

      public AbstractVector(int capacity)
      Makes a new vector that doubles every resize.
      Parameters:
      capacity - the initial capacity of this vector. If 0 or less, it is 1.
    • AbstractVector

      public AbstractVector(int capacity, int capacityIncrement)
      Makes a new vector.
      Parameters:
      capacity - the initial capacity of this vector.
      capacityIncrement - what to increase the capacity of this vector by if this reaches the max. if 0 or less, it will double.
  • Method Details

    • clear

      public void clear()
      Clears the vector.
    • shallowClear

      public void shallowClear()
      Clears the vector shallowly - removes no references and just sets the current size to 0. This can be prone to holding references in memory longer than necessary, so know what you are doing if you need to care about garbage collecting.
    • getCapacity

      public int getCapacity()
      Gets the capacity of this vector (size before it resizes itself).
      Returns:
      the current capacity.
    • setCapacity

      public void setCapacity(int capacity)
      Sets this storageArray's capacity to some value. If this vector is set to a capacity that is less than the current one, it will cut the vector short. If the capacity argument is 0 or less, it is set to 1.
      Parameters:
      capacity - the new capacity of this vector.
    • trim

      public void trim()
      Sets this vector's capacity to its current size. If this vector's size is 0, it will be cleared and have its capacity set to 1, since you cannot have a vector with a capacity of less than 1.
      See Also:
    • getCapacityIncrement

      public int getCapacityIncrement()
      Returns:
      the capacity increment value.
      See Also:
    • setCapacityIncrement

      public void setCapacityIncrement(int capacityIncrement)
      Sets the capacity increment value.
      Parameters:
      capacityIncrement - what to increase the capacity of this vector by if this reaches the max. if 0 or less, it will double.
    • get

      public T get(int index)
      Gets an object at an index in the vector.
      Overrides:
      get in class AbstractArrayStorage<T>
      Parameters:
      index - the desired index.
      Returns:
      null if index is out of vector bounds.
    • add

      public void add(T object)
      Adds an object to the end of the vector.
      Parameters:
      object - the object to add.
    • add

      public void add(int index, T object)
      Adds an object at an index. If index is greater than or equal to the size, it will add it at the end. If index is less than 0, it won't add it.
      Parameters:
      index - the index to add this at.
      object - the object to add.
    • replace

      public void replace(int index, T object)
      Sets an object at an index. Used for replacing contents. If index is greater than or equal to the size, it will add it at the end. If index is less than 0, this does nothing.
      Parameters:
      index - the index to set this at.
      object - the object to add.
    • remove

      public boolean remove(T object)
      Removes an object from the vector, if it exists in the vector. Sequential search.
      Parameters:
      object - the object to search for and remove.
      Returns:
      true if removed, false if not in the vector.
      Throws:
      NullPointerException - if object is null.
    • removeIndex

      public T removeIndex(int index)
      Removes an object from an index in the vector and shifts everything after it down an index position.
      Parameters:
      index - the target index.
      Returns:
      null if the index is out of bounds or the object at that index.
    • contains

      public boolean contains(T object)
      Checks if an object exists in this vector. Implementation may dictate how the object is searched.
      Parameters:
      object - the object to look for.
      Returns:
      true if an equal object exists, or false if not.
      See Also:
    • getIndexOf

      public int getIndexOf(T object)
      Gets the index of an object, presumably in the vector.
      Parameters:
      object - the object to search for.
      Returns:
      the index of the object if it is in the vector, or -1 if it is not present.
      Throws:
      NullPointerException - if object is null.
    • search

      public int search(T object, Comparator<? super T> comparator)
      Gets the index of an object, presumably in the vector via binary search. Expects the contents of this vector to be sorted.
      Parameters:
      object - the object to search for.
      comparator - the comparator to use for comparison or equivalence.
      Returns:
      the index of the object if it is in the vector, or less than 0 if it is not present. If less than 0, it is equal to where it would be added in the array. Add 1 then negate.
      Throws:
      NullPointerException - if object or comparator is null.
    • iterator

      public AbstractVector<T>.VectorIterator iterator()
      Returns an iterator for this vector.
      Specified by:
      iterator in interface Iterable<T>
    • sort

      public void sort()
      Sorts this vector using NATURAL ORDERING. Calls Arrays.sort(Object[], int, int) on the internal storage array.
    • sort

      public void sort(Comparator<? super T> comparator)
      Sorts this vector using a comparator. Calls Arrays.sort(Object[], int, int, Comparator) on the internal storage array, using the specified comparator.
      Parameters:
      comparator - the comparator to use.
    • sort

      public void sort(int startIndex, int endIndex)
      Sorts this vector using NATURAL ORDERING. Calls Arrays.sort(Object[], int, int) on the internal storage array.
      Parameters:
      startIndex - the starting index of the sort.
      endIndex - the ending index of the sort, exclusive.
    • sort

      public void sort(Comparator<? super T> comparator, int startIndex, int endIndex)
      Sorts this vector using a comparator. Calls Arrays.sort(Object[], int, int, Comparator) on the internal storage array, using the specified comparator.
      Parameters:
      comparator - the comparator to use.
      startIndex - the starting index of the sort.
      endIndex - the ending index of the sort, exclusive.
    • swap

      public void swap(int index0, int index1)
      Swaps the contents of two indices in the vector.

      If index0 is equal to index1, this does nothing.

      If one index is outside the bounds of this vector (less than 0 or greater than or equal to size()), this throws an exception.

      Parameters:
      index0 - the first index.
      index1 - the second index.
      Throws:
      IllegalArgumentException - if one index is outside the bounds of this vector (less than 0 or greater than or equal to size()).
    • shift

      public void shift(int sourceIndex, int targetIndex)
      Moves the object at an index in this vector to another index, shifting the contents between the two selected indices in this vector back or forward.

      If sourceIndex is equal to targetIndex, this does nothing.

      If one index is outside the bounds of this vector (less than 0 or greater than or equal to size()), this throws an exception.

      Parameters:
      sourceIndex - the first index.
      targetIndex - the second index.
      Throws:
      IllegalArgumentException - if one index is outside the bounds of this vector (less than 0 or greater than or equal to size()).
    • size

      public int size()
      Returns the amount of objects in the vector.
      Specified by:
      size in interface Sizable
      Returns:
      the amount of individual objects that this object contains.
    • isEmpty

      public boolean isEmpty()
      Returns true if there is nothing in this vector, false otherwise. Equivalent to size() == 0.
      Specified by:
      isEmpty in interface Sizable
      Returns:
      true if so, false otherwise.
    • toArray

      public void toArray(T[] out)
      Dumps the contents of this structure to an array. The output array must accommodate the entirety of the data in this structure.
      Parameters:
      out - the output array.
    • toString

      public String toString()
      Returns this storageArray as a string.
      Overrides:
      toString in class Object