Module logicng

Class LNGVector<T>

  • Type Parameters:
    T - the type of the elements
    All Implemented Interfaces:
    Iterable<T>

    public final class LNGVector<T>
    extends Object
    implements Iterable<T>
    A simple vector implementation (inspired by MiniSat, CleaneLing, Sat4J).
    Since:
    1.0
    Version:
    1.0
    • Constructor Summary

      Constructors 
      Constructor Description
      LNGVector()
      Creates a vector with an initial capacity of 5 elements.
      LNGVector​(int size)
      Creates a vector with a given capacity.
      LNGVector​(int size, T pad)
      Creates a vector with a given capacity and a given initial element.
      LNGVector​(Collection<T> elems)
      Creates a vector with the given elements
      LNGVector​(T... elems)
      Creates a vector with the given elements.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      T back()
      Returns the last element of the vector and leaves it on the vector.
      void clear()
      Clears the vector.
      boolean empty()
      Returns whether the vector is empty or not.
      T get​(int position)
      Returns the element at a given position in the vector.
      void growTo​(int size)
      Grows the vector to a new size and initializes the new elements with null.
      void growTo​(int size, T pad)
      Grows the vector to a new size and initializes the new elements with a given value.
      Iterator<T> iterator()  
      void manualSort​(Comparator<T> comparator)
      Sorts this vector with a given comparator (with manual implemented merge sort).
      void pop()
      Removes the last element of the vector.
      void push​(T element)
      Pushes an element at the end of the vector.
      void release()
      Clears the vector and sets all elements to null.
      boolean remove​(T element)
      Removes the first occurrence of a given element from the vector (the element is compared by equals).
      void removeElements​(int num)
      Removes a given number of elements from the vector.
      void replaceInplace​(LNGVector<? extends T> other)
      Replaces the contents of this vector with the contents of another vector in-place.
      void set​(int position, T element)
      Sets an element at a given position in the vector.
      void shrinkTo​(int newSize)
      Shrinks the vector to a given size if the new size is less than the current size.
      int size()
      Returns the size of the vector.
      void sort​(Comparator<T> comparator)
      Sorts this vector with a given comparator (with JDK sorting).
      void sortReverse​(Comparator<T> comparator)
      Sorts this vector in reverse order.
      String toString()  
      void unsafePush​(T element)
      Pushes an element and assumes that there is enough space on the vector.
    • Constructor Detail

      • LNGVector

        public LNGVector()
        Creates a vector with an initial capacity of 5 elements.
      • LNGVector

        public LNGVector​(int size)
        Creates a vector with a given capacity.
        Parameters:
        size - the capacity of the vector.
      • LNGVector

        public LNGVector​(int size,
                         T pad)
        Creates a vector with a given capacity and a given initial element.
        Parameters:
        size - the capacity of the vector
        pad - the initial element
      • LNGVector

        @SafeVarargs
        public LNGVector​(T... elems)
        Creates a vector with the given elements.
        Parameters:
        elems - the elements
      • LNGVector

        public LNGVector​(Collection<T> elems)
        Creates a vector with the given elements
        Parameters:
        elems - the elements
    • Method Detail

      • empty

        public boolean empty()
        Returns whether the vector is empty or not.
        Returns:
        true if the vector is empty, false otherwise
      • size

        public int size()
        Returns the size of the vector.
        Returns:
        the size of the vector
      • back

        public T back()
        Returns the last element of the vector and leaves it on the vector.
        Returns:
        the last element of the vector
      • push

        public void push​(T element)
        Pushes an element at the end of the vector.
        Parameters:
        element - the element
      • unsafePush

        public void unsafePush​(T element)
        Pushes an element and assumes that there is enough space on the vector.
        Parameters:
        element - the element to push
        Throws:
        ArrayIndexOutOfBoundsException - if there was not enough space on the vector
      • get

        public T get​(int position)
        Returns the element at a given position in the vector.
        Parameters:
        position - the position
        Returns:
        the element at the position
        Throws:
        ArrayIndexOutOfBoundsException - if the position is not found in the vector
      • set

        public void set​(int position,
                        T element)
        Sets an element at a given position in the vector.
        Parameters:
        position - the position
        element - the element
        Throws:
        ArrayIndexOutOfBoundsException - if the position is not found in the vector
      • pop

        public void pop()
        Removes the last element of the vector.
      • shrinkTo

        public void shrinkTo​(int newSize)
        Shrinks the vector to a given size if the new size is less than the current size. Otherwise, the size remains the same.
        Parameters:
        newSize - the new size
      • growTo

        public void growTo​(int size,
                           T pad)
        Grows the vector to a new size and initializes the new elements with a given value.
        Parameters:
        size - the new size
        pad - the value for new elements
      • growTo

        public void growTo​(int size)
        Grows the vector to a new size and initializes the new elements with null.
        Parameters:
        size - the new size
      • removeElements

        public void removeElements​(int num)
        Removes a given number of elements from the vector.
        Parameters:
        num - the number of elements to remove.
        Throws:
        ArrayIndexOutOfBoundsException - if the number of elements to remove is larger than the size of the vector
      • remove

        public boolean remove​(T element)
        Removes the first occurrence of a given element from the vector (the element is compared by equals).
        Parameters:
        element - the element to remove
        Returns:
        true if the element was removed, false if the element was not in the vector
      • replaceInplace

        public void replaceInplace​(LNGVector<? extends T> other)
        Replaces the contents of this vector with the contents of another vector in-place.
        Parameters:
        other - the other vector
        Throws:
        IllegalArgumentException - if you try to replace a vector with itself
      • clear

        public void clear()
        Clears the vector. This method only sets the size to 0. The elements are not nulled out. Use release() for this purpose.
      • release

        public void release()
        Clears the vector and sets all elements to null.
      • sort

        public void sort​(Comparator<T> comparator)
        Sorts this vector with a given comparator (with JDK sorting).
        Parameters:
        comparator - the comparator
      • manualSort

        public void manualSort​(Comparator<T> comparator)
        Sorts this vector with a given comparator (with manual implemented merge sort). This method is required for sorting clauses based on activity, since not all requirements of the JDK sorting are met.
        Parameters:
        comparator - the comparator
      • sortReverse

        public void sortReverse​(Comparator<T> comparator)
        Sorts this vector in reverse order.
        Parameters:
        comparator - the comparator