Class IntVector

  • All Implemented Interfaces:
    java.io.Serializable
    Direct Known Subclasses:
    IntStack

    public class IntVector
    extends java.lang.Object
    implements java.io.Serializable
    Like java.util.Vector, but elements are ints. This is a bare-bones implementation. May add features as I need them.
    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected int[] array  
      protected int pos  
    • Constructor Summary

      Constructors 
      Constructor Description
      IntVector()
      Default constructor.
      IntVector​(int capacity)
      Specify the initial capacity of this vector.
      IntVector​(int[] array)
      Construct an IntVector from an array.
      IntVector​(int capacity, int growth_factor, int multiplication_limit)
      Specify the initial capacity, growth factor and multiplication limit of this vector.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(int element)
      Add an element at the end of vector.
      void add​(int[] elements)
      Add an array of elements to the end.
      void add​(int[] elements, int startpos, int endpos)
      Add a slice of elements to the end
      void add​(int index, int element)
      Add an element at a certain position in the vector.
      void addBulk​(IntVector elements)  
      boolean contains​(int elem)
      Tests if the specified int is a component of this IntVector.
      IntVector copy()  
      void ensure_size​(int req)  
      boolean equals​(java.lang.Object o)
      Compares the specified Object with this IntVector for equality.
      void fill​(int value)
      Set every element of the vector to some value.
      int get​(int index)
      Retrieve the element at index.
      int[] getArray()
      Return the internal array.
      int hashCode()  
      int indexOf​(int element)
      Returns the index of the first occurrence of the element specified in this vector.
      int indexOfOptimizeAscending​(int element)
      Returns the index of some occurrence of the element specified in this vector.
      void multiAdd​(int element, int count)  
      void multiAdd​(int index, int element, int count)  
      int position​(int elem)
      Return the position of the first occurrence of elem in the IntVector, if it exists.
      void put​(int index, int element)
      Set an element at a certain position in the vector.
      int remove​(int index)
      Remove the element at a certain index.
      void removeAllElements()
      Remove all elements and set size to 0.
      void removeAllElementsAdjustSizeDown()  
      void resetSize​(int capacity)  
      void set​(int index, int element)
      Set an element at a certain position in the vector.
      void setSize​(int size)  
      int size()  
      IntVector sortDedup()  
      int[] toArray()  
      int[] toArrayCopy()  
      int[] toIntArray()  
      java.lang.String toString()  
      void trimToSize()
      Reduce the size of the internal array to the number of current elements.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • pos

        protected int pos
      • array

        protected int[] array
    • Constructor Detail

      • IntVector

        public IntVector()
        Default constructor.
      • IntVector

        public IntVector​(int[] array)
        Construct an IntVector from an array. The array is not copied and may subsequently be modified.
        Parameters:
        array - The array the IntVector is initialized from.
      • IntVector

        public IntVector​(int capacity)
        Specify the initial capacity of this vector. Use to avoid internal copying if you know ahead of time how large your vector is going to get (at least).
        Parameters:
        capacity - Initial capacity of vector.
      • IntVector

        public IntVector​(int capacity,
                         int growth_factor,
                         int multiplication_limit)
        Specify the initial capacity, growth factor and multiplication limit of this vector. Use to avoid internal copying if you know ahead of time how large your vector is going to get (at least).
        Parameters:
        capacity - Initial capacity of vector.
        growth_factor - Growth factor.
        multiplication_limit - Multiplication limit.
    • Method Detail

      • resetSize

        public void resetSize​(int capacity)
      • setSize

        public void setSize​(int size)
      • add

        public void add​(int[] elements)
        Add an array of elements to the end.
        Parameters:
        elements - -
      • addBulk

        public void addBulk​(IntVector elements)
      • add

        public void add​(int[] elements,
                        int startpos,
                        int endpos)
        Add a slice of elements to the end
        Parameters:
        elements - -
        startpos - -
        endpos - -
      • add

        public void add​(int element)
        Add an element at the end of vector. Behaves like add(Object o) of Vector.
        Parameters:
        element - -
      • multiAdd

        public void multiAdd​(int element,
                             int count)
      • add

        public void add​(int index,
                        int element)
        Add an element at a certain position in the vector. Elements later in the vector are shifted right by one. If the position is past the end of the current vector, new 0-valued elements are added.
        Parameters:
        index - -
        element - -
      • multiAdd

        public void multiAdd​(int index,
                             int element,
                             int count)
      • set

        public void set​(int index,
                        int element)
        Set an element at a certain position in the vector.
        Parameters:
        index - -
        element - -
      • put

        public void put​(int index,
                        int element)
        Set an element at a certain position in the vector. Vector will grow. Not apparently used (2014) Seems for purposes of having pairs of adjacent elements, (e.g. map).
        Parameters:
        index - -
        element - -
      • get

        public int get​(int index)
        Retrieve the element at index.
        Parameters:
        index - -
        Returns:
        The element at index.
        Throws:
        java.lang.ArrayIndexOutOfBoundsException - If index is not a valid index.
      • remove

        public int remove​(int index)
        Remove the element at a certain index.
        Parameters:
        index - The index of the element to be removed.
        Returns:
        The element at index.
        Throws:
        java.lang.ArrayIndexOutOfBoundsException - If index is not a valid index.
      • removeAllElements

        public void removeAllElements()
        Remove all elements and set size to 0. Will not change current capacity.
      • removeAllElementsAdjustSizeDown

        public void removeAllElementsAdjustSizeDown()
      • equals

        public boolean equals​(java.lang.Object o)
        Compares the specified Object with this IntVector for equality. Two IntVectors are equal if and only if the object passed in o is of type IntVector, this.size() == o.size(), and the n-th element in this IntVector is equal to the n-th element in o for all n < this.size().
        Overrides:
        equals in class java.lang.Object
        Parameters:
        o - -
        Returns:
        true if the IntVectors are equal, false otherwise.
      • size

        public int size()
        Returns:
        The number of elements in the vector.
      • contains

        public boolean contains​(int elem)
        Tests if the specified int is a component of this IntVector.
        Parameters:
        elem - -
        Returns:
        true if and only if the int is an element of this IntVector, false otherwise.
      • position

        public int position​(int elem)
        Return the position of the first occurrence of elem in the IntVector, if it exists.
        Parameters:
        elem - The element we're looking for.
        Returns:
        The position, or -1 if it doesn't exist.
      • fill

        public void fill​(int value)
        Set every element of the vector to some value. Not used (2014)
        Parameters:
        value - The fill value.
      • toArray

        public int[] toArray()
        Returns:
        the underlying int array, where the length of the returned array is equal to the vector's size. This is not a copy!
      • sortDedup

        public IntVector sortDedup()
        Returns:
        an updated value for this vector, with the values sorted and duplicates removed
      • toArrayCopy

        public int[] toArrayCopy()
        Returns:
        a copy of the underlying array.
      • getArray

        public int[] getArray()
        Return the internal array.
        Returns:
        -
      • indexOf

        public int indexOf​(int element)
        Returns the index of the first occurrence of the element specified in this vector.
        Parameters:
        element - -
        Returns:
        the index or -1 if the element was not found.
      • indexOfOptimizeAscending

        public int indexOfOptimizeAscending​(int element)
        Returns the index of some occurrence of the element specified in this vector. optimization: this is used only in bag index implementations or cases where which element among potentially many is picked, such as sets (at most one element) or "contains" (don't care which one is found) Other optimizations for that are done for the major use case that the order of adding elements results in the elements being more-or-less ordered, ascending. Exploit this by assuming ascending, and testing if the element is above or below the mid-element, and ordering the direction of the search.
        Parameters:
        element - -
        Returns:
        the index or -1 if the element was not found.
      • trimToSize

        public void trimToSize()
        Reduce the size of the internal array to the number of current elements. You should only use this if you know that your vector will not grow anymore.
      • toIntArray

        public int[] toIntArray()
        Returns:
        a copy of the internal int array, trimmed
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • ensure_size

        public void ensure_size​(int req)
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object