Class LongArrayDeque

    • Field Summary

      Fields 
      Modifier and Type Field Description
      long[] buffer
      Internal array for storing elements of the deque.
      int head
      The index of the element at the head of the deque or an arbitrary number equal to tail if the deque is empty.
      protected ArraySizingStrategy resizer
      Buffer resizing strategy.
      int tail
      The index at which the next element would be added to the tail of the deque.
    • Constructor Summary

      Constructors 
      Constructor Description
      LongArrayDeque()
      New instance with sane defaults.
      LongArrayDeque​(int expectedElements)
      New instance with sane defaults.
      LongArrayDeque​(int expectedElements, ArraySizingStrategy resizer)
      New instance with sane defaults.
      LongArrayDeque​(LongContainer container)
      Creates a new deque from elements of another container, appending elements at the end of the deque in the iteration order.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addFirst​(long e1)
      Inserts the specified element at the front of this deque.
      void addFirst​(long... elements)
      Vararg-signature method for adding elements at the front of this deque.
      int addFirst​(LongContainer container)
      Inserts all elements from the given container to the front of this deque.
      int addFirst​(java.lang.Iterable<? extends LongCursor> iterable)
      Inserts all elements from the given iterable to the front of this deque.
      void addLast​(long e1)
      Inserts the specified element at the end of this deque.
      void addLast​(long... elements)
      Vararg-signature method for adding elements at the end of this deque.
      int addLast​(LongContainer container)
      Inserts all elements from the given container to the end of this deque.
      int addLast​(java.lang.Iterable<? extends LongCursor> iterable)
      Inserts all elements from the given iterable to the end of this deque.
      int bufferIndexOf​(long e1)
      Return the index of the first (counting from head) element equal to e1.
      void clear()
      Removes all elements from this collection.
      LongArrayDeque clone()
      Clone this object.
      boolean contains​(long e)
      Lookup a given element in the container.
      <T extends LongProcedure>
      T
      descendingForEach​(T procedure)
      Applies procedure to all elements of this deque, tail to head.
      java.util.Iterator<LongCursor> descendingIterator()
      Returns a cursor over the values of this deque (in tail to head order).
      protected void ensureBufferSpace​(int expectedAdditions)
      Ensures the internal buffer has enough free slots to store expectedAdditions.
      void ensureCapacity​(int expectedElements)
      Ensure this container can hold at least the given number of elements without resizing its buffers.
      protected boolean equalElements​(LongArrayDeque other)
      Compare order-aligned elements against another LongDeque.
      boolean equals​(java.lang.Object obj)
      Returns true only if the other object is an instance of the same class and with the same elements.
      <T extends LongProcedure>
      T
      forEach​(T procedure)
      Applies a procedure to all container elements.
      static LongArrayDeque from​(long... elements)
      Create a new deque by pushing a variable number of arguments to the end of it.
      long getFirst()
      Retrieves the first element of this deque but does not remove it.
      long getLast()
      Retrieves the last element of this deque but does not remove it.
      int hashCode()
      boolean isEmpty()
      Shortcut for size() == 0.
      java.util.Iterator<LongCursor> iterator()
      Returns a cursor over the values of this deque (in head to tail order).
      int lastBufferIndexOf​(long e1)
      Return the index of the last (counting from tail) element equal to e1.
      protected static int oneLeft​(int index, int modulus)
      Move one index to the left, wrapping around buffer.
      protected static int oneRight​(int index, int modulus)
      Move one index to the right, wrapping around buffer.
      long ramBytesAllocated()
      Allocated memory estimation
      long ramBytesUsed()
      Bytes that is actually been used
      void release()
      Release internal buffers of this deque and reallocate with the default buffer.
      int removeAll​(long e1)
      Removes all occurrences of e from this collection.
      int removeAll​(LongLookupContainer c)
      Default implementation uses a predicate for removal.
      int removeAll​(LongPredicate predicate)
      Removes all elements in this collection for which the given predicate returns true.
      void removeAtBufferIndex​(int index)
      Removes the element at index in the internal {#link buffer array, returning its value.
      long removeFirst()
      Retrieves and removes the first element of this deque.
      int removeFirst​(long e1)
      Removes the first element that equals e.
      long removeLast()
      Retrieves and removes the last element of this deque.
      int removeLast​(long e1)
      Removes the last element that equals e.
      int retainAll​(LongLookupContainer c)
      Default implementation uses a predicate for retaining.
      int retainAll​(LongPredicate predicate)
      Default implementation redirects to LongCollection.removeAll(LongPredicate) and negates the predicate.
      int size()
      Return the current number of elements in this container.
      long[] toArray()
      Default implementation of copying to an array.
      long[] toArray​(long[] target)
      Copies elements of this deque to an array.
      java.lang.String toString()
      Convert the contents of this container to a human-friendly string.
      • Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
    • Field Detail

      • buffer

        public long[] buffer
        Internal array for storing elements of the deque.
      • head

        public int head
        The index of the element at the head of the deque or an arbitrary number equal to tail if the deque is empty.
      • tail

        public int tail
        The index at which the next element would be added to the tail of the deque.
    • Constructor Detail

      • LongArrayDeque

        public LongArrayDeque()
        New instance with sane defaults.
      • LongArrayDeque

        public LongArrayDeque​(int expectedElements)
        New instance with sane defaults.
        Parameters:
        expectedElements - The expected number of elements guaranteed not to cause buffer expansion (inclusive).
      • LongArrayDeque

        public LongArrayDeque​(int expectedElements,
                              ArraySizingStrategy resizer)
        New instance with sane defaults.
        Parameters:
        expectedElements - The expected number of elements guaranteed not to cause buffer expansion (inclusive).
        resizer - Underlying buffer sizing strategy.
      • LongArrayDeque

        public LongArrayDeque​(LongContainer container)
        Creates a new deque from elements of another container, appending elements at the end of the deque in the iteration order.
    • Method Detail

      • addFirst

        public void addFirst​(long e1)
        Inserts the specified element at the front of this deque.
        Specified by:
        addFirst in interface LongDeque
      • addFirst

        public final void addFirst​(long... elements)
        Vararg-signature method for adding elements at the front of this deque.

        This method is handy, but costly if used in tight loops (anonymous array passing)

        Parameters:
        elements - The elements to add.
      • addFirst

        public int addFirst​(LongContainer container)
        Inserts all elements from the given container to the front of this deque.
        Parameters:
        container - The container to iterate over.
        Returns:
        Returns the number of elements actually added as a result of this call.
      • addFirst

        public int addFirst​(java.lang.Iterable<? extends LongCursor> iterable)
        Inserts all elements from the given iterable to the front of this deque.
        Parameters:
        iterable - The iterable to iterate over.
        Returns:
        Returns the number of elements actually added as a result of this call.
      • addLast

        public void addLast​(long e1)
        Inserts the specified element at the end of this deque.
        Specified by:
        addLast in interface LongDeque
      • addLast

        public final void addLast​(long... elements)
        Vararg-signature method for adding elements at the end of this deque.

        This method is handy, but costly if used in tight loops (anonymous array passing)

        Parameters:
        elements - The elements to iterate over.
      • addLast

        public int addLast​(LongContainer container)
        Inserts all elements from the given container to the end of this deque.
        Parameters:
        container - The container to iterate over.
        Returns:
        Returns the number of elements actually added as a result of this call.
      • addLast

        public int addLast​(java.lang.Iterable<? extends LongCursor> iterable)
        Inserts all elements from the given iterable to the end of this deque.
        Parameters:
        iterable - The iterable to iterate over.
        Returns:
        Returns the number of elements actually added as a result of this call.
      • removeFirst

        public long removeFirst()
        Retrieves and removes the first element of this deque.
        Specified by:
        removeFirst in interface LongDeque
        Returns:
        the head (first) element of this deque.
      • removeLast

        public long removeLast()
        Retrieves and removes the last element of this deque.
        Specified by:
        removeLast in interface LongDeque
        Returns:
        the tail of this deque.
      • getFirst

        public long getFirst()
        Retrieves the first element of this deque but does not remove it.
        Specified by:
        getFirst in interface LongDeque
        Returns:
        the head of this deque.
      • getLast

        public long getLast()
        Retrieves the last element of this deque but does not remove it.
        Specified by:
        getLast in interface LongDeque
        Returns:
        the head of this deque.
      • removeFirst

        public int removeFirst​(long e1)
        Removes the first element that equals e.
        Specified by:
        removeFirst in interface LongDeque
        Returns:
        The deleted element's index or -1 if the element was not found.
      • bufferIndexOf

        public int bufferIndexOf​(long e1)
        Return the index of the first (counting from head) element equal to e1. The index points to the buffer array.
        Parameters:
        e1 - The element to look for.
        Returns:
        Returns the index of the first element equal to e1 or -1 if not found.
      • removeLast

        public int removeLast​(long e1)
        Removes the last element that equals e.
        Specified by:
        removeLast in interface LongDeque
        Returns:
        The deleted element's index or -1 if the element was not found.
      • lastBufferIndexOf

        public int lastBufferIndexOf​(long e1)
        Return the index of the last (counting from tail) element equal to e1. The index points to the buffer array.
        Parameters:
        e1 - The element to look for.
        Returns:
        Returns the index of the first element equal to e1 or -1 if not found.
      • removeAll

        public int removeAll​(long e1)
        Removes all occurrences of e from this collection.
        Specified by:
        removeAll in interface LongCollection
        Parameters:
        e1 - Element to be removed from this collection, if present.
        Returns:
        The number of removed elements as a result of this call.
      • removeAtBufferIndex

        public void removeAtBufferIndex​(int index)
        Removes the element at index in the internal {#link buffer array, returning its value.
        Parameters:
        index - Index of the element to remove. The index must be located between head and tail in modulo buffer arithmetic.
      • isEmpty

        public boolean isEmpty()
        Shortcut for size() == 0.
        Specified by:
        isEmpty in interface LongContainer
      • size

        public int size()
        Return the current number of elements in this container. The time for calculating the container's size may take O(n) time, although implementing classes should try to maintain the current size and return in constant time.
        Specified by:
        size in interface LongContainer
      • clear

        public void clear()
        Removes all elements from this collection.

        The internal array buffers are not released as a result of this call.

        Specified by:
        clear in interface LongCollection
        See Also:
        release()
      • ensureCapacity

        public void ensureCapacity​(int expectedElements)
        Ensure this container can hold at least the given number of elements without resizing its buffers.
        Specified by:
        ensureCapacity in interface Preallocable
        Parameters:
        expectedElements - The total number of elements, inclusive.
      • ensureBufferSpace

        protected void ensureBufferSpace​(int expectedAdditions)
        Ensures the internal buffer has enough free slots to store expectedAdditions. Increases internal buffer size if needed.
      • toArray

        public long[] toArray()
        Default implementation of copying to an array.
        Specified by:
        toArray in interface LongContainer
      • toArray

        public long[] toArray​(long[] target)
        Copies elements of this deque to an array. The content of the target array is filled from index 0 (head of the queue) to index size() - 1 (tail of the queue).
        Parameters:
        target - The target array must be large enough to hold all elements.
        Returns:
        Returns the target argument for chaining.
      • clone

        public LongArrayDeque clone()
        Clone this object. The returned clone will reuse the same hash function and array resizing strategy.
        Overrides:
        clone in class java.lang.Object
      • oneLeft

        protected static int oneLeft​(int index,
                                     int modulus)
        Move one index to the left, wrapping around buffer.
      • oneRight

        protected static int oneRight​(int index,
                                      int modulus)
        Move one index to the right, wrapping around buffer.
      • ramBytesAllocated

        public long ramBytesAllocated()
        Description copied from interface: Accountable
        Allocated memory estimation
        Specified by:
        ramBytesAllocated in interface Accountable
        Returns:
        Ram allocated in bytes
      • ramBytesUsed

        public long ramBytesUsed()
        Description copied from interface: Accountable
        Bytes that is actually been used
        Specified by:
        ramBytesUsed in interface Accountable
        Returns:
        Ram used in bytes
      • iterator

        public java.util.Iterator<LongCursor> iterator()
        Returns a cursor over the values of this deque (in head to tail order). The iterator is implemented as a cursor and it returns the same cursor instance on every call to Iterator.next() (to avoid boxing of primitive types). To read the current value (or index in the deque's buffer) use the cursor's public fields. An example is shown below.
         for (IntValueCursor c : intDeque) {
           System.out.println("buffer index=" + c.index + " value=" + c.value);
         }
         
        Specified by:
        iterator in interface java.lang.Iterable<LongCursor>
        Specified by:
        iterator in interface LongContainer
      • descendingIterator

        public java.util.Iterator<LongCursor> descendingIterator()
        Returns a cursor over the values of this deque (in tail to head order). The iterator is implemented as a cursor and it returns the same cursor instance on every call to Iterator.next() (to avoid boxing of primitive types). To read the current value (or index in the deque's buffer) use the cursor's public fields. An example is shown below.
         for (Iterator<IntCursor> i = intDeque.descendingIterator(); i.hasNext();) {
           final IntCursor c = i.next();
           System.out.println("buffer index=" + c.index + " value=" + c.value);
         }
         
        Specified by:
        descendingIterator in interface LongDeque
        Returns:
        An iterator over elements in this deque in tail-to-head order.
      • forEach

        public <T extends LongProcedure> T forEach​(T procedure)
        Applies a procedure to all container elements. Returns the argument (any subclass of LongProcedure. This lets the caller to call methods of the argument by chaining the call (even if the argument is an anonymous type) to retrieve computed values, for example (IntContainer):
         int count = container.forEach(new IntProcedure() {
           int count; // this is a field declaration in an anonymous class.
         
           public void apply(int value) {
             count++;
           }
         }).count;
         
        Specified by:
        forEach in interface LongContainer
      • forEach

        public <T extends LongPredicate> T forEach​(T predicate)
        Applies a predicate to container elements as long, as the predicate returns true. The iteration is interrupted otherwise.
        Specified by:
        forEach in interface LongContainer
      • descendingForEach

        public <T extends LongProcedure> T descendingForEach​(T procedure)
        Applies procedure to all elements of this deque, tail to head.
        Specified by:
        descendingForEach in interface LongDeque
      • descendingForEach

        public <T extends LongPredicate> T descendingForEach​(T predicate)
        Applies a predicate to container elements as long, as the predicate returns true. The iteration is interrupted otherwise.
        Specified by:
        descendingForEach in interface LongDeque
      • removeAll

        public int removeAll​(LongPredicate predicate)
        Removes all elements in this collection for which the given predicate returns true.
        Specified by:
        removeAll in interface LongCollection
        Returns:
        Returns the number of removed elements.
      • contains

        public boolean contains​(long e)
        Lookup a given element in the container. This operation has no speed guarantees (may be linear with respect to the size of this container).
        Specified by:
        contains in interface LongContainer
        Returns:
        Returns true if this container has an element equal to e.
      • hashCode

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

        public boolean equals​(java.lang.Object obj)
        Returns true only if the other object is an instance of the same class and with the same elements.
        Overrides:
        equals in class java.lang.Object
      • equalElements

        protected boolean equalElements​(LongArrayDeque other)
        Compare order-aligned elements against another LongDeque.
      • from

        public static LongArrayDeque from​(long... elements)
        Create a new deque by pushing a variable number of arguments to the end of it.
      • removeAll

        public int removeAll​(LongLookupContainer c)
        Default implementation uses a predicate for removal.
        Specified by:
        removeAll in interface LongCollection
        Returns:
        Returns the number of removed elements.
      • retainAll

        public int retainAll​(LongLookupContainer c)
        Default implementation uses a predicate for retaining.
        Specified by:
        retainAll in interface LongCollection
        Returns:
        Returns the number of removed elements.
      • toString

        public java.lang.String toString()
        Convert the contents of this container to a human-friendly string.
        Overrides:
        toString in class java.lang.Object