Class LongDeque

java.lang.Object
com.github.tommyettinger.ds.LongList
com.github.tommyettinger.ds.LongDeque
All Implemented Interfaces:
Arrangeable, Ordered.OfLong, PrimitiveCollection<Long>, PrimitiveCollection.OfLong, RandomAccess

public class LongDeque extends LongList implements RandomAccess, Arrangeable, PrimitiveCollection.OfLong, Ordered.OfLong
A resizable, insertion-ordered double-ended queue of primitive long with efficient add and remove at the beginning and end. This extends LongList and supports RandomAccess. Like LongList, it is a PrimitiveCollection.OfLong, Arrangeable, and Ordered.OfLong. Values in the backing array may wrap back to the beginning, making add and remove at the beginning and end O(1) (unless the backing array needs to resize when adding). Deque functionality is provided via removeLast() and addFirst(long).
Unlike most Deque implementations in the JDK, you can get and set items anywhere in the deque in constant time with get(int) and set(int, long). Relative to a LongList, get(int) has slightly higher overhead, but it still runs in constant time. Unlike ArrayDeque in the JDK, this implements equals(Object) and hashCode(). This can provide what are effectively ListIterators for iteration from an index or in reverse order.
Unlike ArrayDeque or ArrayList, most methods that take an index here try to be "forgiving;" that is, they treat negative indices as index 0, and too-large indices as the last index, rather than throwing an Exception, except in some cases where the LongDeque is empty and an item from it is required. An exception is in set(int, long), which allows prepending by setting a negative index, or appending by setting a too-large index. This isn't a standard JDK behavior, and it doesn't always act how Deque or List is documented.
Some new methods are present here, or have been made public when they weren't before. removeRange(int, int), for instance, is now public, as is resize(int). New APIs include Deque-like methods that affect the middle of the deque, such as peekAt(int) and pollAt(int). There are more bulk methods that work at the head or tail region of the deque, such as addAllFirst(OfLong) and truncateFirst(int). There are the methods from Arrangeable, and many default methods from PrimitiveCollection and Ordered.
In general, this is an improvement over ArrayDeque in every type of functionality, and is mostly equivalent to LongList as long as the performance of get(int) is adequate. Because it is array-backed, it should usually be much faster than LinkedList, as well; only periodic resizing and modifications in the middle of the List using an iterator should be typically faster for LinkedList.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    A LongIterator over the elements of a LongDeque.

    Nested classes/interfaces inherited from class com.github.tommyettinger.ds.LongList

    LongList.LongListIterator

    Nested classes/interfaces inherited from interface com.github.tommyettinger.ds.Arrangeable

    Arrangeable.ArrangeableList<T>
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    long
    The value returned when nothing can be obtained from this deque and an exception is not meant to be thrown, such as when calling peek() on an empty deque.
     
     
    protected int
    Index of first element.
    protected int
    Index of last element.

    Fields inherited from class com.github.tommyettinger.ds.LongList

    items, iterator1, iterator2, size
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new LongDeque which can hold 16 values without needing to resize the backing array.
    LongDeque(int initialSize)
    Creates a new LongDeque which can hold the specified number of values without needing to resize the backing array.
    LongDeque(long[] a)
    Creates a new LongDeque using all the contents of the given array.
    LongDeque(long[] a, int offset, int count)
    Creates a new LongDeque using count items from a, starting at offset.
    Copies the given LongDeque exactly into this one.
     
    LongDeque(Ordered.OfLong other, int offset, int count)
     
    Creates a new LongDeque using all the contents of the given Collection.
    Creates a new instance containing the items in the specified iterator.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    add(long t)
    Inserts the specified element into the deque represented by this deque (in other words, at the tail of this deque) if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is currently available.
    void
    add(long value1, long value2)
     
    void
    add(long value1, long value2, long value3)
     
    void
    add(long value1, long value2, long value3, long value4)
     
    boolean
    addAll(int index, long[] array)
    Like addAll(int, OfLong), but takes an array instead of a PrimitiveCollection.OfLong and inserts it so the first item will be at the given index.
    boolean
    addAll(int index, long[] array, int offset, int length)
    Like addAll(int, OfLong), but takes an array instead of a PrimitiveCollection.OfLong, gets items starting at offset from that array, using length items, and inserts them so the item at the given offset will be at the given index.
    boolean
    addAll(int index, Ordered.OfLong ord)
    Like addAll(int, OfLong), but takes an ord instead of a PrimitiveCollection.OfLong and inserts it so the first item will be at the given index.
    boolean
    addAll(int index, Ordered.OfLong ord, int offset, int length)
    Like addAll(int, OfLong), but takes an array instead of a PrimitiveCollection.OfLong, gets items starting at offset from that array, using length items, and inserts them so the item at the given offset will be at the given index.
    boolean
     
    boolean
    addAll(long[] array)
    Exactly like addAll(OfLong), but takes an array instead of a PrimitiveCollection.OfLong.
    boolean
    addAll(long[] array, int offset, int length)
    Like addAll(long[]), but only uses at most length items from array, starting at offset.
    boolean
     
    boolean
    addAll(LongList list, int offset, int count)
     
    boolean
    Exactly like addAll(OfLong), but takes an Ordered.OfLong instead of a PrimitiveCollection.OfLong.
    boolean
    addAll(Ordered.OfLong ord, int offset, int length)
    Like addAll(long[]), but only uses at most length items from ord, starting at offset.
    boolean
    Adds all the elements in the specified collection at the end of this deque, as if by calling addLast(long) on each one, in the order that they are returned by the collection's iterator.
    boolean
    addAllFirst(long[] array)
    Exactly like addAllFirst(OfLong), but takes an array instead of a PrimitiveCollection.OfLong.
    boolean
    addAllFirst(long[] array, int offset, int length)
    Like addAllFirst(long[]), but only uses at most length items from array, starting at offset.
    boolean
    Exactly like addAllFirst(OfLong), but takes an ord instead of a PrimitiveCollection.OfLong.
    boolean
    addAllFirst(Ordered.OfLong ord, int offset, int length)
    Like addAllFirst(Ordered.OfLong), but only uses at most length items from ord, starting at offset.
    boolean
    Adds every item in c to this in order at the start.
    boolean
    addAllLast(long[] array)
    An alias for addAll(long[]).
    boolean
    addAllLast(long[] array, int offset, int length)
    boolean
    boolean
    addAllLast(Ordered.OfLong ord, int offset, int length)
    boolean
    An alias for addAll(OfLong), this adds every item in c to this in order at the end.
    void
    addFirst(long value)
    Prepend given value to the head (enqueue to head).
    void
    addFirst(long value1, long value2)
     
    void
    addFirst(long value1, long value2, long value3)
     
    void
    addFirst(long value1, long value2, long value3, long value4)
     
    void
    addLast(long value)
    Appends given long to the tail (enqueue to tail).
    void
    addLast(long value1, long value2)
     
    void
    addLast(long value1, long value2, long value3)
     
    void
    addLast(long value1, long value2, long value3, long value4)
     
    long
    assign(int index, long item)
    Replaces the element at the specified position in this list with the specified element.
    void
    Removes all values from this deque.
    boolean
    contains(long o)
    Returns true if this deque contains the specified element.
    boolean
    Returns true if this LongList contains, at least once, every item in other; otherwise returns false.
    Returns an iterator over the elements in this deque in reverse sequential order.
    descendingIterator(int index)
    Returns an iterator over the elements in this deque in reverse sequential order.
    void
    div(int index, long value)
     
    div(long value)
    Divides each item in this LongList by value, stores it in this and returns it.
    boolean
    duplicateRange(int index, int count)
    Inserts the specified number of items at the specified index.
    long
    Retrieves, but does not remove, the head of the queue represented by this deque (in other words, the first element of this deque).
    long[]
    ensureCapacity(int additional)
    Increases the size of the backing array to accommodate the specified number of additional items.
    protected int
    ensureGap(int index, int gapSize)
    Make sure there is a "gap" of exactly gapSize values starting at index.
    boolean
    Using == between each item in order, compares for equality with other subtypes of LongList.
    long
    Returns the first (head) item in the deque (without removing it).
    long
    get(int index)
    Returns the element at the specified position in this deque.
    long
    Gets the default value, which is the value returned when nothing can be obtained from this deque and an exception is not meant to be thrown, such as when calling peek() on an empty deque.
    long
    Retrieves, but does not remove, the first element of this deque.
    long
    Retrieves, but does not remove, the last element of this deque.
    long
    getRandom(Random random)
    Gets a randomly selected item from this LongDeque, using the given random number generator.
    int
     
    int
    indexOf(long value)
    Returns the index of the first occurrence of value in the deque, or -1 if no such value exists.
    int
    indexOf(long value, int fromIndex)
    Returns the index of the first occurrence of value in the deque, or -1 if no such value exists.
    void
    insert(int index, long item)
    Inserts the specified element into this deque at the specified index.
    boolean
    insertAll(int index, long[] array)
    boolean
    insertAll(int index, long[] array, int offset, int length)
    boolean
    insertAll(int index, Ordered.OfLong ord)
    boolean
    insertAll(int index, Ordered.OfLong ord, int offset, int length)
    boolean
    An alias for addAll(int, OfLong); inserts all elements in the specified collection into this list at the specified position.
    boolean
    Returns true if the deque is empty.
    Returns an iterator for the items in the deque.
    long
    Returns the last (tail) item in the deque (without removing it).
    int
    lastIndexOf(long value)
    Returns the index of the last occurrence of value in the deque, or -1 if no such value exists.
    int
    lastIndexOf(long value, int fromIndex)
    Returns the index of last occurrence of value in the deque, starting from fromIndex and going backwards, or -1 if no such value exists.
     
    listIterator(int index)
    Gets an iterator over this deque that starts at the given index.
    void
    minus(int index, long value)
     
    minus(long value)
    Takes each item in this LongList and subtracts value, stores it in this and returns it.
    boolean
    Returns true if the deque has one or more items.
    boolean
    offer(long t)
    Inserts the specified element into the queue represented by this deque (in other words, at the tail of this deque) if it is possible to do so immediately without violating capacity restrictions, returning true upon success and false if no space is currently available.
    boolean
    offerFirst(long t)
    Inserts the specified element at the front of this deque unless it would violate capacity restrictions.
    boolean
    offerLast(long t)
    Inserts the specified element at the end of this deque unless it would violate capacity restrictions.
    Returns this LongList, since it is its own order.
    static LongDeque
    parse(String str, String delimiter)
    Calls parse(String, String, boolean) with brackets set to false.
    static LongDeque
    parse(String str, String delimiter, boolean brackets)
    Creates a new collection and fills it by calling PrimitiveCollection.OfLong.addLegible(String, String, int, int) on either all of str (if brackets is false) or str without its first and last chars (if brackets is true).
    static LongDeque
    parse(String str, String delimiter, int offset, int length)
    Creates a new collection and fills it by calling PrimitiveCollection.OfLong.addLegible(String, String, int, int) with the given four parameters as-is.
    long
    Retrieves, but does not remove, the head of the queue represented by this deque (in other words, the first element of this deque), or returns defaultValue if this deque is empty.
    long
    peekAt(int index)
    Returns the element at the specified position in this deque.
    long
    Retrieves, but does not remove, the first element of this deque, or returns defaultValue if this deque is empty.
    long
    Retrieves, but does not remove, the last element of this deque, or returns defaultValue if this deque is empty.
    void
    plus(int index, long value)
     
    plus(long value)
    Adds value to each item in this LongList, stores it in this and returns it.
    long
    Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque), or returns defaultValue if this deque is empty.
    long
    poll(int index)
    Removes the element at the specified position in this deque.
    long
    pollAt(int index)
    Removes the element at the specified position in this deque.
    long
    Retrieves and removes the first element of this deque, or returns defaultValue if this deque is empty.
    long
    Retrieves and removes the last element of this deque, or returns defaultValue if this deque is empty.
    long
    pop()
    Pops an element from the stack represented by this deque.
    void
    push(long t)
    Pushes an element onto the stack represented by this deque (in other words, at the head of this deque).
    long
    random(Random random)
    Gets a randomly selected item from this LongDeque, using the given random number generator.
    void
    rem(int index, long value)
     
    rem(long value)
    Gets the remainder of each item in this LongList with value, stores it in this and returns it.
    long
    Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque).
    boolean
    remove(long o)
    Removes the first occurrence of the specified element from this deque.
    boolean
    Removes from this LongList all occurrences of any elements contained in the specified collection.
    long
    removeAt(int index)
    Removes the element at the specified position in this deque.
    boolean
    Removes from this LongList element-wise occurrences of elements contained in the specified collection.
    long
    Remove the first item from the deque.
    boolean
    Removes the first occurrence of the specified element from this deque.
    long
    Remove the last item from the deque.
    boolean
    Removes the last occurrence of the specified element from this deque.
    boolean
    removeLastValue(long value)
    Removes the last instance of the specified value in the deque.
    void
    removeRange(int fromIndex, int toIndex)
    Removes from this list all the elements whose index is between fromIndex, inclusive, and toIndex, exclusive.
    boolean
    removeValue(long value)
    Removes the first instance of the specified value in the deque.
    int
    replaceAll(long find, long replace)
    Replaces every occurrence of find with replace.
    void
    replaceAll(com.github.tommyettinger.function.LongToLongFunction operator)
    Replaces each element of this list with the result of applying the given operator to that element.
    boolean
    replaceFirst(long find, long replace)
    Replaces the first occurrence of find with replace.
    long[]
    resize(int newSize)
    Resizes the backing array.
    boolean
    Removes all items from this LongList that are not present somewhere in other, any number of times.
    void
    Reverses this LongDeque in-place.
    void
    set(int index, long item)
    Replaces the element at the specified position in this list with the specified element.
    void
    setDefaultValue(long defaultValue)
    Sets the default value, which is the value returned when nothing can be obtained from this deque and an exception is not meant to be thrown, such as when calling peek() on an empty deque.
    long[]
    setSize(int newSize)
    Sets the list size, leaving any values beyond the current size undefined.
    long[]
    Reduces the size of the backing array to the size of the actual items.
    void
    Pseudo-randomly shuffles the order of this Ordered in-place.
    int
    Returns the number of elements in this deque.
    void
    Sorts this deque in-place using Arrays.sort(long[], int, int) in ascending order.
    void
    sort(int from, int to)
    Uses Arrays.sort(long[], int, int) to sort a (clamped) subrange of this deque.
    void
    sort(int from, int to, LongComparator comparator)
    Sorts the specified range of elements according to the order induced by the specified comparator using mergesort, or Arrays.sort(long[], int, int) if c is null.
    void
    sort(LongComparator comparator)
    void
    swap(int first, int second)
    Switches the ordering of positions first and second, without changing any items beyond that.
    void
    times(int index, long value)
     
    times(long value)
    Multiplies each item in this LongList by value, stores it in this and returns it.
    long[]
    Returns an array containing all the elements in this collection.
    long[]
    toArray(long[] array)
    If array.length at least equal to LongList.size(), this copies the contents of this into array and returns it; otherwise, it allocates a new long array that can fit all the items in this, and proceeds to copy into that and return that.
    Delegates to PrimitiveCollection.OfLong.toString(String, boolean) with a delimiter of ", " and square brackets enabled.
    void
    Trims the capacity of this LongDeque instance to be the deque's current size.
    void
    truncate(int newSize)
    Reduces the size of the deque to the specified size by bulk-removing items from the tail end.
    void
    truncateFirst(int newSize)
    Reduces the size of the deque to the specified size by bulk-removing from the head.
    void
    truncateLast(int newSize)
    Alias for truncate(int).
    static LongDeque
    Constructs an empty deque.
    static LongDeque
    with(long item)
    Creates a new LongDeque that holds only the given item, but can be resized.
    static LongDeque
    with(long... varargs)
    Creates a new LongDeque that will hold the items in the given array or varargs.
    static LongDeque
    with(long item0, long item1)
    Creates a new LongDeque that holds only the given items, but can be resized.
    static LongDeque
    with(long item0, long item1, long item2)
    Creates a new LongDeque that holds only the given items, but can be resized.
    static LongDeque
    with(long item0, long item1, long item2, long item3)
    Creates a new LongDeque that holds only the given items, but can be resized.
    static LongDeque
    with(long item0, long item1, long item2, long item3, long item4)
    Creates a new LongDeque that holds only the given items, but can be resized.
    static LongDeque
    with(long item0, long item1, long item2, long item3, long item4, long item5)
    Creates a new LongDeque that holds only the given items, but can be resized.
    static LongDeque
    with(long item0, long item1, long item2, long item3, long item4, long item5, long item6)
    Creates a new LongDeque that holds only the given items, but can be resized.
    static LongDeque
    with(long item0, long item1, long item2, long item3, long item4, long item5, long item6, long item7)
    Creates a new LongDeque that holds only the given items, but can be resized.

    Methods inherited from class com.github.tommyettinger.ds.LongList

    keepsOrder

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface com.github.tommyettinger.ds.Arrangeable

    rearrange, shuffle

    Methods inherited from interface com.github.tommyettinger.ds.Ordered.OfLong

    getOrderType, random, selectRanked, selectRankedIndex
  • Field Details

    • defaultValue

      public long defaultValue
      The value returned when nothing can be obtained from this deque and an exception is not meant to be thrown, such as when calling peek() on an empty deque.
    • tail

      protected int tail
      Index of last element. Logically bigger than head. Unless empty, it points to a valid element inside the deque. This may be the same as head, and is if there is one element in the deque (or none), that will be the case.
    • descendingIterator1

      protected transient LongDeque.LongDequeIterator descendingIterator1
    • descendingIterator2

      protected transient LongDeque.LongDequeIterator descendingIterator2
  • Constructor Details

    • LongDeque

      public LongDeque()
      Creates a new LongDeque which can hold 16 values without needing to resize the backing array.
    • LongDeque

      public LongDeque(int initialSize)
      Creates a new LongDeque which can hold the specified number of values without needing to resize the backing array.
      Parameters:
      initialSize - how large the backing array should be, without any padding
    • LongDeque

      public LongDeque(PrimitiveCollection.OfLong coll)
      Creates a new LongDeque using all the contents of the given Collection.
      Parameters:
      coll - a Collection of long that will be copied into this and used in full
      Throws:
      NullPointerException - if coll is null
    • LongDeque

      public LongDeque(LongIterator iter)
      Creates a new instance containing the items in the specified iterator.
      Parameters:
      iter - an iterator that will have its remaining contents added to this
      Throws:
      NullPointerException - if iter is null
    • LongDeque

      public LongDeque(LongDeque deque)
      Copies the given LongDeque exactly into this one. Individual values will be shallow-copied.
      Parameters:
      deque - another LongDeque to copy
      Throws:
      NullPointerException - if deque is null
    • LongDeque

      public LongDeque(Ordered.OfLong other, int offset, int count)
    • LongDeque

      public LongDeque(long[] a)
      Creates a new LongDeque using all the contents of the given array.
      Parameters:
      a - an array of long that will be copied into this and used in full
      Throws:
      NullPointerException - if a is null
    • LongDeque

      public LongDeque(long[] a, int offset, int count)
      Creates a new LongDeque using count items from a, starting at offset. If count is 0 or less, this will create an empty LongDeque with capacity 1.
      Parameters:
      a - an array of long
      offset - where in a to start using items
      count - how many items to use from a
      Throws:
      NullPointerException - if a is null
    • LongDeque

      public LongDeque(Ordered.OfLong other)
  • Method Details

    • order

      public LongDeque order()
      Description copied from class: LongList
      Returns this LongList, since it is its own order. This is only here to satisfy the Ordered.OfLong interface.
      Specified by:
      order in interface Ordered.OfLong
      Overrides:
      order in class LongList
      Returns:
      this LongList
    • getDefaultValue

      public long getDefaultValue()
      Gets the default value, which is the value returned when nothing can be obtained from this deque and an exception is not meant to be thrown, such as when calling peek() on an empty deque. Unless changed, the default value is usually null.
      Returns:
      the current default value
    • setDefaultValue

      public void setDefaultValue(long defaultValue)
      Sets the default value, which is the value returned when nothing can be obtained from this deque and an exception is not meant to be thrown, such as when calling peek() on an empty deque. Unless changed, the default value is usually null.
      Parameters:
      defaultValue - any long this can return instead of throwing an Exception, or null
    • addLast

      public void addLast(long value)
      Appends given long to the tail (enqueue to tail). Unless the backing array needs resizing, operates in O(1) time.
      Parameters:
      value - can be null
    • addLast

      public void addLast(long value1, long value2)
    • addLast

      public void addLast(long value1, long value2, long value3)
    • addLast

      public void addLast(long value1, long value2, long value3, long value4)
    • addFirst

      public void addFirst(long value)
      Prepend given value to the head (enqueue to head). Unless backing array needs resizing, operates in O(1) time.
      Parameters:
      value - can be null
      See Also:
    • addFirst

      public void addFirst(long value1, long value2)
    • addFirst

      public void addFirst(long value1, long value2, long value3)
    • addFirst

      public void addFirst(long value1, long value2, long value3, long value4)
    • trimToSize

      public void trimToSize()
      Trims the capacity of this LongDeque instance to be the deque's current size. An application can use this operation to minimize the storage of a LongDeque instance.
      Overrides:
      trimToSize in class LongList
    • shrink

      public long[] shrink()
      Description copied from class: LongList
      Reduces the size of the backing array to the size of the actual items. This is useful to release memory when many items have been removed, or if it is known that more items will not be added.
      Overrides:
      shrink in class LongList
      Returns:
      LongList.items; this will be a different reference if this resized
    • ensureCapacity

      public long[] ensureCapacity(int additional)
      Increases the size of the backing array to accommodate the specified number of additional items. Useful before adding many items to avoid multiple backing array resizes.
      Overrides:
      ensureCapacity in class LongList
      Returns:
      the backing array this will use after this call
    • resize

      public long[] resize(int newSize)
      Resizes the backing array. newSize should be greater than the current size; otherwise, newSize will be set to size and the resize to the same size will (for most purposes) be wasted effort. If this is not empty, this will rearrange the items internally to be linear and have the head at index 0, with the tail at size - 1. This always allocates a new internal backing array.
      Overrides:
      resize in class LongList
      Returns:
      the new backing array, as a direct reference
    • ensureGap

      protected int ensureGap(int index, int gapSize)
      Make sure there is a "gap" of exactly gapSize values starting at index. This can resize the backing array to achieve this goal. If possible, this will keep the same backing array and modify it in-place. The "gap" is not assigned null, and may contain old/duplicate references; calling code must overwrite the entire gap with additional values to ensure GC correctness.
      Parameters:
      index - the 0-based index in the iteration order where the gap will be present
      gapSize - the number of items that will need filling in the gap, and can be filled without issues.
      Returns:
      the position in the array where the gap will begin, which is unrelated to the index
    • addAll

      public boolean addAll(LongList list)
      Overrides:
      addAll in class LongList
    • addAll

      public boolean addAll(LongList list, int offset, int count)
      Overrides:
      addAll in class LongList
    • removeFirst

      public long removeFirst()
      Remove the first item from the deque. (dequeue from head) Always O(1).
      Returns:
      removed long
      Throws:
      NoSuchElementException - when the deque is empty
    • removeLast

      public long removeLast()
      Remove the last item from the deque. (dequeue from tail) Always O(1).
      Returns:
      removed long
      Throws:
      NoSuchElementException - when the deque is empty
      See Also:
    • offerFirst

      public boolean offerFirst(long t)
      Inserts the specified element at the front of this deque unless it would violate capacity restrictions. When using a capacity-restricted deque, this method is generally preferable to the addFirst(long) method, which can fail to insert an element only by throwing an exception.
      Parameters:
      t - the element to add
      Returns:
      true if the element was added to this deque, else false
      Throws:
      ClassCastException - if the class of the specified element prevents it from being added to this deque
      NullPointerException - if the specified element is null and this deque does not permit null elements
      IllegalArgumentException - if some property of the specified element prevents it from being added to this deque
    • offerLast

      public boolean offerLast(long t)
      Inserts the specified element at the end of this deque unless it would violate capacity restrictions. When using a capacity-restricted deque, this method is generally preferable to the addLast(long) method, which can fail to insert an element only by throwing an exception.
      Parameters:
      t - the element to add
      Returns:
      true if the element was added to this deque, else false
      Throws:
      ClassCastException - if the class of the specified element prevents it from being added to this deque
      NullPointerException - if the specified element is null and this deque does not permit null elements
      IllegalArgumentException - if some property of the specified element prevents it from being added to this deque
    • pollFirst

      public long pollFirst()
      Retrieves and removes the first element of this deque, or returns defaultValue if this deque is empty. The default value is usually null unless it has been changed with setDefaultValue(long).
      Returns:
      the head of this deque, or defaultValue if this deque is empty
      See Also:
    • pollLast

      public long pollLast()
      Retrieves and removes the last element of this deque, or returns defaultValue if this deque is empty. The default value is usually null unless it has been changed with setDefaultValue(long).
      Returns:
      the tail of this deque, or defaultValue if this deque is empty
      See Also:
    • getFirst

      public long getFirst()
      Retrieves, but does not remove, the first element of this deque.

      This method differs from peekFirst only in that it throws an exception if this deque is empty.

      Returns:
      the head of this deque
      Throws:
      NoSuchElementException - if this deque is empty
    • getLast

      public long getLast()
      Retrieves, but does not remove, the last element of this deque. This method differs from peekLast only in that it throws an exception if this deque is empty.
      Returns:
      the tail of this deque
      Throws:
      NoSuchElementException - if this deque is empty
    • peekFirst

      public long peekFirst()
      Retrieves, but does not remove, the first element of this deque, or returns defaultValue if this deque is empty.
      Returns:
      the head of this deque, or defaultValue if this deque is empty
    • peekLast

      public long peekLast()
      Retrieves, but does not remove, the last element of this deque, or returns defaultValue if this deque is empty.
      Returns:
      the tail of this deque, or defaultValue if this deque is empty
    • removeFirstOccurrence

      public boolean removeFirstOccurrence(long o)
      Removes the first occurrence of the specified element from this deque. If the deque does not contain the element, it is unchanged. More formally, removes the first element e such that o == e (if such an element exists). Returns true if this deque contained the specified element (or equivalently, if this deque changed as a result of the call).
      Parameters:
      o - element to be removed from this deque, if present
      Returns:
      true if an element was removed as a result of this call
    • removeLastOccurrence

      public boolean removeLastOccurrence(long o)
      Removes the last occurrence of the specified element from this deque. If the deque does not contain the element, it is unchanged. More formally, removes the last element e such that o == e (if such an element exists). Returns true if this deque contained the specified element (or equivalently, if this deque changed as a result of the call).
      Parameters:
      o - element to be removed from this deque, if present
      Returns:
      true if an element was removed as a result of this call
    • add

      public boolean add(long t)
      Inserts the specified element into the deque represented by this deque (in other words, at the tail of this deque) if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is currently available. When using a capacity-restricted deque, it is generally preferable to use offer.

      This method is equivalent to addLast(long).

      Specified by:
      add in interface PrimitiveCollection.OfLong
      Overrides:
      add in class LongList
      Parameters:
      t - the element to add
      Returns:
      true (as specified by Collection.add(E))
    • add

      public void add(long value1, long value2)
      Overrides:
      add in class LongList
    • add

      public void add(long value1, long value2, long value3)
      Overrides:
      add in class LongList
    • add

      public void add(long value1, long value2, long value3, long value4)
      Overrides:
      add in class LongList
    • insert

      public void insert(int index, long item)
      Inserts the specified element into this deque at the specified index. Unlike offerFirst(long) and offerLast(long), this does not run in expected constant time unless the index is less than or equal to 0 (where it acts like offerFirst()) or greater than or equal to size() (where it acts like offerLast()).
      Overrides:
      insert in class LongList
      Parameters:
      index - the index in the deque's insertion order to insert the item
      item - a long item to insert; may be null
    • offer

      public boolean offer(long t)
      Inserts the specified element into the queue represented by this deque (in other words, at the tail of this deque) if it is possible to do so immediately without violating capacity restrictions, returning true upon success and false if no space is currently available. When using a capacity-restricted deque, this method is generally preferable to the add(long) method, which can fail to insert an element only by throwing an exception.

      This method is equivalent to offerLast(long).

      Parameters:
      t - the element to add
      Returns:
      true if the element was added to this deque, else false
    • remove

      public long remove()
      Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque). This method differs from poll() only in that it throws an exception if this deque is empty.

      This method is equivalent to removeFirst().

      Returns:
      the head of the queue represented by this deque
      Throws:
      NoSuchElementException - if this deque is empty
    • poll

      public long poll()
      Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque), or returns defaultValue if this deque is empty.

      This method is equivalent to pollFirst().

      Returns:
      the first element of this deque, or defaultValue if this deque is empty
    • element

      public long element()
      Retrieves, but does not remove, the head of the queue represented by this deque (in other words, the first element of this deque). This method differs from peek only in that it throws an exception if this deque is empty.

      This method is equivalent to getFirst().

      Returns:
      the head of the queue represented by this deque
      Throws:
      NoSuchElementException - if this deque is empty
    • peek

      public long peek()
      Retrieves, but does not remove, the head of the queue represented by this deque (in other words, the first element of this deque), or returns defaultValue if this deque is empty.

      This method is equivalent to peekFirst().

      Overrides:
      peek in class LongList
      Returns:
      the head of the queue represented by this deque, or defaultValue if this deque is empty
    • addAll

      public boolean addAll(PrimitiveCollection.OfLong c)
      Adds all the elements in the specified collection at the end of this deque, as if by calling addLast(long) on each one, in the order that they are returned by the collection's iterator.

      When using a capacity-restricted deque, it is generally preferable to call offer separately on each element.

      An exception encountered while trying to add an element may result in only some of the elements having been successfully added when the associated exception is thrown.

      Specified by:
      addAll in interface PrimitiveCollection.OfLong
      Parameters:
      c - the elements to be inserted into this deque
      Returns:
      true if this deque changed as a result of the call
    • addAllLast

      public boolean addAllLast(PrimitiveCollection.OfLong c)
      An alias for addAll(OfLong), this adds every item in c to this in order at the end.
      Parameters:
      c - the elements to be inserted into this deque
      Returns:
      true if this deque changed as a result of the call
    • addAllFirst

      public boolean addAllFirst(PrimitiveCollection.OfLong c)
      Adds every item in c to this in order at the start. The iteration order of c will be preserved for the added items.
      Parameters:
      c - the elements to be inserted into this deque
      Returns:
      true if this deque changed as a result of the call
    • insertAll

      public boolean insertAll(int index, PrimitiveCollection.OfLong c)
      An alias for addAll(int, OfLong); inserts all elements in the specified collection into this list at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in 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. (Note that this will occur if the specified collection is this list, and it's nonempty.)
      Parameters:
      index - index at which to insert the first element from the specified collection
      c - collection containing elements to be added to this list
      Returns:
      true if this list changed as a result of the call
    • addAll

      public boolean addAll(int index, PrimitiveCollection.OfLong c)
    • addAll

      public boolean addAll(long[] array)
      Exactly like addAll(OfLong), but takes an array instead of a PrimitiveCollection.OfLong.
      Specified by:
      addAll in interface PrimitiveCollection.OfLong
      Overrides:
      addAll in class LongList
      Parameters:
      array - the elements to be inserted into this deque
      Returns:
      true if this deque changed as a result of the call
      See Also:
    • addAll

      public boolean addAll(long[] array, int offset, int length)
      Like addAll(long[]), but only uses at most length items from array, starting at offset.
      Specified by:
      addAll in interface PrimitiveCollection.OfLong
      Overrides:
      addAll in class LongList
      Parameters:
      array - the elements to be inserted into this deque
      offset - the index of the first item in array to add
      length - how many items, at most, to add from array into this
      Returns:
      true if this deque changed as a result of the call
      See Also:
    • addAllLast

      public boolean addAllLast(long[] array)
      An alias for addAll(long[]).
      Parameters:
      array - the elements to be inserted into this deque
      Returns:
      true if this deque changed as a result of the call
      See Also:
    • addAllLast

      public boolean addAllLast(long[] array, int offset, int length)
      Parameters:
      array - the elements to be inserted into this deque
      offset - the index of the first item in array to add
      length - how many items, at most, to add from array into this
      Returns:
      true if this deque changed as a result of the call
      See Also:
    • addAllFirst

      public boolean addAllFirst(long[] array)
      Exactly like addAllFirst(OfLong), but takes an array instead of a PrimitiveCollection.OfLong.
      Parameters:
      array - the elements to be inserted into this deque
      Returns:
      true if this deque changed as a result of the call
      See Also:
    • addAllFirst

      public boolean addAllFirst(long[] array, int offset, int length)
      Like addAllFirst(long[]), but only uses at most length items from array, starting at offset. The order of array will be preserved, starting at the head of the deque.
      Parameters:
      array - the elements to be inserted into this deque
      offset - the index of the first item in array to add
      length - how many items, at most, to add from array into this
      Returns:
      true if this deque changed as a result of the call
      See Also:
    • insertAll

      public boolean insertAll(int index, long[] array)
      Parameters:
      index - the index in this deque's iteration order to place the first item in array
      array - the elements to be inserted into this deque
      Returns:
      true if this deque changed as a result of the call
    • insertAll

      public boolean insertAll(int index, long[] array, int offset, int length)
      Parameters:
      index - the index in this deque's iteration order to place the first item in array
      array - the elements to be inserted into this deque
      offset - the index of the first item in array to add
      length - how many items, at most, to add from array into this
      Returns:
      true if this deque changed as a result of the call
    • addAll

      public boolean addAll(int index, long[] array)
      Like addAll(int, OfLong), but takes an array instead of a PrimitiveCollection.OfLong and inserts it so the first item will be at the given index. The order of array will be preserved, starting at the given index in this deque.
      Parameters:
      index - the index in this deque's iteration order to place the first item in array
      array - the elements to be inserted into this deque
      Returns:
      true if this deque changed as a result of the call
      See Also:
    • addAll

      public boolean addAll(int index, long[] array, int offset, int length)
      Like addAll(int, OfLong), but takes an array instead of a PrimitiveCollection.OfLong, gets items starting at offset from that array, using length items, and inserts them so the item at the given offset will be at the given index. The order of array will be preserved, starting at the given index in this deque.
      Parameters:
      index - the index in this deque's iteration order to place the first item in array
      array - the elements to be inserted into this deque
      offset - the index of the first item in array to add
      length - how many items, at most, to add from array into this
      Returns:
      true if this deque changed as a result of the call
      See Also:
    • addAll

      public boolean addAll(Ordered.OfLong ord)
      Exactly like addAll(OfLong), but takes an Ordered.OfLong instead of a PrimitiveCollection.OfLong.
      Parameters:
      ord - the elements to be inserted into this deque
      Returns:
      true if this deque changed as a result of the call
      See Also:
    • addAll

      public boolean addAll(Ordered.OfLong ord, int offset, int length)
      Like addAll(long[]), but only uses at most length items from ord, starting at offset.
      Overrides:
      addAll in class LongList
      Parameters:
      ord - the elements to be inserted into this deque
      offset - the index of the first item in ord to add
      length - how many items, at most, to add from ord into this
      Returns:
      true if this deque changed as a result of the call
      See Also:
    • addAll

      public boolean addAll(int index, Ordered.OfLong ord)
      Like addAll(int, OfLong), but takes an ord instead of a PrimitiveCollection.OfLong and inserts it so the first item will be at the given index. The order of ord will be preserved, starting at the given index in this deque.
      Parameters:
      index - the index in this deque's iteration order to place the first item in ord
      ord - the elements to be inserted into this deque
      Returns:
      true if this deque changed as a result of the call
      See Also:
    • addAll

      public boolean addAll(int index, Ordered.OfLong ord, int offset, int length)
      Like addAll(int, OfLong), but takes an array instead of a PrimitiveCollection.OfLong, gets items starting at offset from that array, using length items, and inserts them so the item at the given offset will be at the given index. The order of array will be preserved, starting at the given index in this deque.
      Overrides:
      addAll in class LongList
      Parameters:
      index - the index in this deque's iteration order to place the first item in array
      ord - the elements to be inserted into this deque
      offset - the index of the first item in array to add
      length - how many items, at most, to add from array into this
      Returns:
      true if this deque changed as a result of the call
      See Also:
    • addAllLast

      public boolean addAllLast(Ordered.OfLong ord)
      Parameters:
      ord - the elements to be inserted into this deque
      Returns:
      true if this deque changed as a result of the call
      See Also:
    • addAllLast

      public boolean addAllLast(Ordered.OfLong ord, int offset, int length)
      Parameters:
      ord - the elements to be inserted into this deque
      offset - the index of the first item in ord to add
      length - how many items, at most, to add from ord into this
      Returns:
      true if this deque changed as a result of the call
      See Also:
    • addAllFirst

      public boolean addAllFirst(Ordered.OfLong ord)
      Exactly like addAllFirst(OfLong), but takes an ord instead of a PrimitiveCollection.OfLong.
      Parameters:
      ord - the elements to be inserted into this deque
      Returns:
      true if this deque changed as a result of the call
      See Also:
    • addAllFirst

      public boolean addAllFirst(Ordered.OfLong ord, int offset, int length)
      Like addAllFirst(Ordered.OfLong), but only uses at most length items from ord, starting at offset. The order of ord will be preserved, starting at the head of the deque.
      Parameters:
      ord - the elements to be inserted into this deque
      offset - the index of the first item in ord to add
      length - how many items, at most, to add from ord into this
      Returns:
      true if this deque changed as a result of the call
      See Also:
    • insertAll

      public boolean insertAll(int index, Ordered.OfLong ord)
      Parameters:
      index - the index in this deque's iteration order to place the first item in ord
      ord - the elements to be inserted into this deque
      Returns:
      true if this deque changed as a result of the call
    • insertAll

      public boolean insertAll(int index, Ordered.OfLong ord, int offset, int length)
      Parameters:
      index - the index in this deque's iteration order to place the first item in ord
      ord - the elements to be inserted into this deque
      offset - the index of the first item in ord to add
      length - how many items, at most, to add from ord into this
      Returns:
      true if this deque changed as a result of the call
    • push

      public void push(long t)
      Pushes an element onto the stack represented by this deque (in other words, at the head of this deque).

      This method is equivalent to addFirst(long).

      Parameters:
      t - the element to push
    • pop

      public long pop()
      Pops an element from the stack represented by this deque. In other words, removes and returns the first element of this deque.

      This method is equivalent to removeFirst().

      Overrides:
      pop in class LongList
      Returns:
      the element at the front of this deque (which is the top of the stack represented by this deque)
      Throws:
      NoSuchElementException - if this deque is empty
    • remove

      public boolean remove(long o)
      Removes the first occurrence of the specified element from this deque. If the deque does not contain the element, it is unchanged. More formally, removes the first element e such that o == e (if such an element exists). Returns true if this deque contained the specified element (or equivalently, if this deque changed as a result of the call).

      This method is equivalent to removeFirstOccurrence(long).

      Specified by:
      remove in interface PrimitiveCollection.OfLong
      Overrides:
      remove in class LongList
      Parameters:
      o - element to be removed from this deque, if present
      Returns:
      true if an element was removed as a result of this call
    • contains

      public boolean contains(long o)
      Returns true if this deque contains the specified element. More formally, returns true if and only if this deque contains at least one element e such that o == e.
      Specified by:
      contains in interface PrimitiveCollection.OfLong
      Overrides:
      contains in class LongList
      Parameters:
      o - element whose presence in this deque is to be tested
      Returns:
      true if this deque contains the specified element
    • containsAll

      public boolean containsAll(LongList other)
      Description copied from class: LongList
      Returns true if this LongList contains, at least once, every item in other; otherwise returns false.
      Overrides:
      containsAll in class LongList
      Parameters:
      other - an LongList
      Returns:
      true if this contains every item in other, otherwise false
    • size

      public int size()
      Returns the number of elements in this deque.
      Specified by:
      size in interface Arrangeable
      Specified by:
      size in interface PrimitiveCollection<Long>
      Overrides:
      size in class LongList
      Returns:
      the number of elements in this deque
    • toArray

      public long[] toArray()
      Returns an array containing all the elements in this collection. If this collection makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order. The returned array's runtime component type is long.

      The returned array will be "safe" in that no references to it are maintained by this collection. (In other words, this method must allocate a new array even if this collection is backed by an array). The caller is thus free to modify the returned array.

      Specified by:
      toArray in interface PrimitiveCollection.OfLong
      Overrides:
      toArray in class LongList
      Returns:
      an array, whose runtime component type is long, containing all the elements in this collection
    • toArray

      public long[] toArray(long[] array)
      Description copied from class: LongList
      If array.length at least equal to LongList.size(), this copies the contents of this into array and returns it; otherwise, it allocates a new long array that can fit all the items in this, and proceeds to copy into that and return that.
      Specified by:
      toArray in interface PrimitiveCollection.OfLong
      Overrides:
      toArray in class LongList
      Parameters:
      array - a long array that will be modified if it can fit LongList.size() items
      Returns:
      array, if it had sufficient size, or a new array otherwise, either with a copy of this
    • setSize

      public long[] setSize(int newSize)
      Description copied from class: LongList
      Sets the list size, leaving any values beyond the current size undefined.
      Overrides:
      setSize in class LongList
      Returns:
      LongList.items; this will be a different reference if this resized to a larger capacity
    • truncateLast

      public void truncateLast(int newSize)
      Alias for truncate(int).
      Parameters:
      newSize - the size this deque should have after this call completes, if smaller than the current size
    • truncate

      public void truncate(int newSize)
      Reduces the size of the deque to the specified size by bulk-removing items from the tail end. If the deque is already smaller than the specified size, no action is taken.
      Overrides:
      truncate in class LongList
      Parameters:
      newSize - the size this deque should have after this call completes, if smaller than the current size
    • truncateFirst

      public void truncateFirst(int newSize)
      Reduces the size of the deque to the specified size by bulk-removing from the head. If the deque is already smaller than the specified size, no action is taken.
      Parameters:
      newSize - the size this deque should have after this call completes, if smaller than the current size
    • removeRange

      public void removeRange(int fromIndex, int toIndex)
      Removes from this list all the elements whose index is between fromIndex, inclusive, and toIndex, exclusive. Shifts any succeeding elements to the left (reduces their index). This call shrinks the list by (toIndex - fromIndex) elements. If toIndex==fromIndex, this operation has no effect. If fromIndex is 0 or less, this delegates to truncateFirst(int); if toIndex is equal to or greater than the size of this collection, this delegates to truncate(int).
      This is public here, not protected as in most JDK collections, because there are actually sometimes needs for this in user code.
      Specified by:
      removeRange in interface Ordered.OfLong
      Overrides:
      removeRange in class LongList
      Parameters:
      fromIndex - index of first element to be removed (inclusive)
      toIndex - index after last element to be removed (exclusive)
    • indexOf

      public int indexOf(long value)
      Returns the index of the first occurrence of value in the deque, or -1 if no such value exists.
      Overrides:
      indexOf in class LongList
      Parameters:
      value - the long to look for
      Returns:
      An index of the first occurrence of value in the deque or -1 if no such value exists
    • indexOf

      public int indexOf(long value, int fromIndex)
      Returns the index of the first occurrence of value in the deque, or -1 if no such value exists. This returns fromIndex if value is present at that point, so if you chain calls to indexOf(), the subsequent fromIndex should be larger than the last-returned index.
      Parameters:
      value - the long to look for
      fromIndex - the initial index to check (zero-indexed, starts at the head, inclusive)
      Returns:
      An index of first occurrence of value at or after fromIndex in the deque, or -1 if no such value exists
    • lastIndexOf

      public int lastIndexOf(long value)
      Returns the index of the last occurrence of value in the deque, or -1 if no such value exists.
      Overrides:
      lastIndexOf in class LongList
      Parameters:
      value - the long to look for
      Returns:
      An index of the last occurrence of value in the deque or -1 if no such value exists
    • lastIndexOf

      public int lastIndexOf(long value, int fromIndex)
      Returns the index of last occurrence of value in the deque, starting from fromIndex and going backwards, or -1 if no such value exists. This returns fromIndex if value is present at that point, so if you chain calls to indexOf(), the subsequent fromIndex should be smaller than the last-returned index.
      Parameters:
      value - the long to look for
      fromIndex - the initial index to check (zero-indexed, starts at the head, inclusive)
      Returns:
      An index of last occurrence of value at or before fromIndex in the deque, or -1 if no such value exists
    • listIterator

      public LongList.LongListIterator listIterator()
    • listIterator

      public LongList.LongListIterator listIterator(int index)
      Gets an iterator over this deque that starts at the given index.
      Parameters:
      index - the index to start iterating from in this deque
      Returns:
      a reused iterator starting at the given index
    • removeValue

      public boolean removeValue(long value)
      Removes the first instance of the specified value in the deque.
      Parameters:
      value - the long to remove
      Returns:
      true if value was found and removed, false otherwise
    • removeLastValue

      public boolean removeLastValue(long value)
      Removes the last instance of the specified value in the deque.
      Parameters:
      value - the long to remove
      Returns:
      true if value was found and removed, false otherwise
    • removeAt

      public long removeAt(int index)
      Removes the element at the specified position in this deque. Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the deque.
      Overrides:
      removeAt in class LongList
      Parameters:
      index - the index of the element to be removed
      Returns:
      the element previously at the specified position
    • pollAt

      public long pollAt(int index)
      Removes the element at the specified position in this deque. Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the deque, or the default value if this is empty. This will not throw an Exception in normal usage, even if index is negative (which makes this simply return pollFirst()) or greater than or equal to size() (which makes this return pollLast()).
      This is an alias for poll(int) for compatibility with primitive-backed lists and deques; poll(int) can refer to the method that removes an item by value, not by index, in those types.
      Parameters:
      index - the index of the element to be removed
      Returns:
      the element previously at the specified position
    • poll

      public long poll(int index)
      Removes the element at the specified position in this deque. Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the deque, or the default value if this is empty. This will not throw an Exception in normal usage, even if index is negative (which makes this simply return pollFirst()) or greater than or equal to size() (which makes this return pollLast()).
      Parameters:
      index - the index of the element to be removed
      Returns:
      the element previously at the specified position
    • removeAll

      public boolean removeAll(PrimitiveCollection.OfLong c)
      Description copied from class: LongList
      Removes from this LongList all occurrences of any elements contained in the specified collection.
      Specified by:
      removeAll in interface PrimitiveCollection.OfLong
      Overrides:
      removeAll in class LongList
      Parameters:
      c - a primitive collection of int items to remove fully, such as another LongList or a LongDeque
      Returns:
      true if this list was modified.
    • removeEach

      public boolean removeEach(PrimitiveCollection.OfLong c)
      Description copied from class: LongList
      Removes from this LongList element-wise occurrences of elements contained in the specified collection. Note that if a value is present more than once in this LongList, only one of those occurrences will be removed for each occurrence of that value in c. If c has the same contents as this LongList or has additional items, then removing each of c will clear this.
      Specified by:
      removeEach in interface PrimitiveCollection.OfLong
      Overrides:
      removeEach in class LongList
      Parameters:
      c - a primitive collection of int items to remove one-by-one, such as another LongList or a LongDeque
      Returns:
      true if this list was modified.
    • retainAll

      public boolean retainAll(PrimitiveCollection.OfLong other)
      Description copied from class: LongList
      Removes all items from this LongList that are not present somewhere in other, any number of times.
      Specified by:
      retainAll in interface PrimitiveCollection.OfLong
      Overrides:
      retainAll in class LongList
      Parameters:
      other - a PrimitiveCollection.OfLong that contains the items that this should keep, whenever present
      Returns:
      true if this LongList changed as a result of this call, otherwise false
    • notEmpty

      public boolean notEmpty()
      Returns true if the deque has one or more items.
      Specified by:
      notEmpty in interface PrimitiveCollection<Long>
      Overrides:
      notEmpty in class LongList
      Returns:
      true if the list has one or more items, or false otherwise
    • isEmpty

      public boolean isEmpty()
      Returns true if the deque is empty.
      Specified by:
      isEmpty in interface PrimitiveCollection<Long>
      Overrides:
      isEmpty in class LongList
      Returns:
      true if the list is empty, or false if it has any items
    • first

      public long first()
      Returns the first (head) item in the deque (without removing it).
      Specified by:
      first in interface PrimitiveCollection.OfLong
      Overrides:
      first in class LongList
      Returns:
      the first item, without modifying this
      Throws:
      NoSuchElementException - when the deque is empty
      See Also:
    • last

      public long last()
      Returns the last (tail) item in the deque (without removing it).
      Throws:
      NoSuchElementException - when the deque is empty
      See Also:
    • get

      public long get(int index)
      Returns the element at the specified position in this deque. Like ArrayList or LongList, but unlike LinkedList, this runs in O(1) time. It is expected to be slightly slower than LongList.get(int), which also runs in O(1) time. Unlike get() in ArrayList or LongList, this considers negative indices to refer to the first item, and too-large indices to refer to the last item. That means it delegates to getFirst() or getLast() in those cases instead of throwing an IndexOutOfBoundsException, though it may throw a NoSuchElementException if the deque is empty and there is no item it can get.
      Overrides:
      get in class LongList
      Parameters:
      index - index of the element to return
      Returns:
      the element at the specified position in this deque
      Throws:
      NoSuchElementException - if the deque is empty
    • peekAt

      public long peekAt(int index)
      Returns the element at the specified position in this deque. Like ArrayList or LongList, but unlike LinkedList, this runs in O(1) time. It is expected to be slightly slower than LongList.get(int), which also runs in O(1) time. Unlike get() in ArrayList or LongList, this considers negative indices to refer to the first item, and too-large indices to refer to the last item. That means it delegates to peekFirst() or peekLast() in those cases instead of throwing an IndexOutOfBoundsException, and it will return the default value if the deque is empty. Unless changed, the default value is usually null.
      Parameters:
      index - index of the element to return
      Returns:
      the element at the specified position in this deque
    • assign

      public long assign(int index, long item)
      Replaces the element at the specified position in this list with the specified element. If this deque is empty or the index is larger than the largest index currently in this deque, this delegates to addLast(long) and returns the default value. If the index is negative, this delegates to addFirst(long) and returns the default value.
      Parameters:
      index - index of the element to replace
      item - element to be stored at the specified position
      Returns:
      the element previously at the specified position
    • set

      public void set(int index, long item)
      Replaces the element at the specified position in this list with the specified element. If this deque is empty or the index is larger than the largest index currently in this deque, this delegates to addLast(long) and returns the default value. If the index is negative, this delegates to addFirst(long) and returns the default value.
      Overrides:
      set in class LongList
      Parameters:
      index - index of the element to replace
      item - element to be stored at the specified position
    • plus

      public void plus(int index, long value)
      Overrides:
      plus in class LongList
    • plus

      public LongList plus(long value)
      Description copied from class: LongList
      Adds value to each item in this LongList, stores it in this and returns it. The presence of this method allows Kotlin code to use the + operator (though it shouldn't be used more than once in an expression, because this method modifies this LongList).
      Overrides:
      plus in class LongList
      Parameters:
      value - each item in this will be assigned item + value
      Returns:
      this for chaining and Kotlin compatibility
    • times

      public void times(int index, long value)
      Overrides:
      times in class LongList
    • times

      public LongList times(long value)
      Description copied from class: LongList
      Multiplies each item in this LongList by value, stores it in this and returns it. The presence of this method allows Kotlin code to use the * operator (though it shouldn't be used more than once in an expression, because this method modifies this LongList).
      Overrides:
      times in class LongList
      Parameters:
      value - each item in this will be assigned item * value
      Returns:
      this for chaining and Kotlin compatibility
    • minus

      public void minus(int index, long value)
      Overrides:
      minus in class LongList
    • minus

      public LongList minus(long value)
      Description copied from class: LongList
      Takes each item in this LongList and subtracts value, stores it in this and returns it. This is just a minor convenience in Java, but the presence of this method allows Kotlin code to use the - operator (though it shouldn't be used more than once in an expression, because this method modifies this LongList).
      Overrides:
      minus in class LongList
      Parameters:
      value - each item in this will be assigned item - value
      Returns:
      this for chaining and Kotlin compatibility
    • div

      public void div(int index, long value)
      Overrides:
      div in class LongList
    • div

      public LongList div(long value)
      Description copied from class: LongList
      Divides each item in this LongList by value, stores it in this and returns it. The presence of this method allows Kotlin code to use the / operator (though it shouldn't be used more than once in an expression, because this method modifies this LongList).
      Overrides:
      div in class LongList
      Parameters:
      value - each item in this will be assigned item / value
      Returns:
      this for chaining and Kotlin compatibility
    • rem

      public void rem(int index, long value)
      Overrides:
      rem in class LongList
    • rem

      public LongList rem(long value)
      Description copied from class: LongList
      Gets the remainder of each item in this LongList with value, stores it in this and returns it. The presence of this method allows Kotlin code to use the % operator (though it shouldn't be used more than once in an expression, because this method modifies this LongList).
      Overrides:
      rem in class LongList
      Parameters:
      value - each item in this will be assigned item % value
      Returns:
      this for chaining and Kotlin compatibility
    • replaceAll

      public void replaceAll(com.github.tommyettinger.function.LongToLongFunction operator)
      Description copied from class: LongList
      Replaces each element of this list with the result of applying the given operator to that element.
      Overrides:
      replaceAll in class LongList
      Parameters:
      operator - a LongToLongFunction (a functional interface defined in funderby)
    • replaceFirst

      public boolean replaceFirst(long find, long replace)
      Replaces the first occurrence of find with replace. Returns true if it performed the replacement, or false if there was nothing to replace. This also returns false if find and replace are the same.
      Overrides:
      replaceFirst in class LongList
      Parameters:
      find - the item to search for
      replace - the item to replace find with, if possible
      Returns:
      true if this changed, or false otherwise
    • replaceAll

      public int replaceAll(long find, long replace)
      Replaces every occurrence of find with replace. Returns the number of changed items, which is 0 if nothing was found or in the case that find and replace are the same.
      Overrides:
      replaceAll in class LongList
      Parameters:
      find - the item to search for
      replace - the item to replace find with, if possible
      Returns:
      the number of replacements that occurred; 0 if nothing was found or replaced
    • duplicateRange

      public boolean duplicateRange(int index, int count)
      Inserts the specified number of items at the specified index. The new items will have values equal to the values at those indices before the insertion, and the previous values will be pushed to after the duplicated range.
      Overrides:
      duplicateRange in class LongList
      Parameters:
      index - the first index to duplicate
      count - how many items to duplicate
    • clear

      public void clear()
      Removes all values from this deque. This operates in O(1) time.
      Specified by:
      clear in interface PrimitiveCollection<Long>
      Overrides:
      clear in class LongList
    • iterator

      public LongList.LongListIterator iterator()
      Returns an iterator for the items in the deque. Remove is supported.
      Reuses one of two iterators for this deque. For nested or multithreaded iteration, use LongDequeIterator(LongDeque).
      Specified by:
      iterator in interface PrimitiveCollection<Long>
      Specified by:
      iterator in interface PrimitiveCollection.OfLong
      Overrides:
      iterator in class LongList
      Returns:
      a LongIterator; use its nextLong() method instead of next()
    • descendingIterator

      public LongList.LongListIterator descendingIterator()
      Returns an iterator over the elements in this deque in reverse sequential order. The elements will be returned in order from last (tail) to first (head).
      Reuses one of two descending iterators for this deque. For nested or multithreaded iteration, use LongDequeIterator(LongDeque, boolean).
      Returns:
      an iterator over the elements in this deque in reverse sequence
    • descendingIterator

      public LongList.LongListIterator descendingIterator(int index)
      Returns an iterator over the elements in this deque in reverse sequential order. The elements will be returned in order from index backwards to first (head).
      Reuses one of two descending iterators for this deque. For nested or multithreaded iteration, use LongDequeIterator(LongDeque, boolean).
      Parameters:
      index - the index to start iterating from in this deque
      Returns:
      an iterator over the elements in this deque in reverse sequence
    • toString

      public String toString()
      Delegates to PrimitiveCollection.OfLong.toString(String, boolean) with a delimiter of ", " and square brackets enabled.
      Overrides:
      toString in class LongList
      Returns:
      the square-bracketed String representation of this LongDeque, with items separated by ", "
    • hashCode

      public int hashCode()
      Specified by:
      hashCode in interface PrimitiveCollection<Long>
      Overrides:
      hashCode in class LongList
    • equals

      public boolean equals(Object o)
      Using == between each item in order, compares for equality with other subtypes of LongList. If o is not a LongList (and is also not somehow reference-equivalent to this collection), this returns false. This uses the PrimitiveCollection.OfLong.iterator() of both this and o, so if either is in the middle of a concurrent iteration that modifies the collection, this may fail.
      Specified by:
      equals in interface PrimitiveCollection<Long>
      Overrides:
      equals in class LongList
      Parameters:
      o - object to be compared for equality with this collection
      Returns:
      true if this is equal to o, or false otherwise
    • swap

      public void swap(int first, int second)
      Switches the ordering of positions first and second, without changing any items beyond that.
      Specified by:
      swap in interface Arrangeable
      Specified by:
      swap in interface Ordered.OfLong
      Overrides:
      swap in class LongList
      Parameters:
      first - the first position, must not be negative and must be less than size()
      second - the second position, must not be negative and must be less than size()
    • reverse

      public void reverse()
      Reverses this LongDeque in-place.
      Specified by:
      reverse in interface Arrangeable
      Specified by:
      reverse in interface Ordered.OfLong
      Overrides:
      reverse in class LongList
    • shuffle

      public void shuffle(Random rng)
      Description copied from interface: Ordered.OfLong
      Pseudo-randomly shuffles the order of this Ordered in-place. You can seed rng, the random number generator, with an identical seed to reproduce a shuffle on two Ordered with the same Arrangeable.size().
      Specified by:
      shuffle in interface Arrangeable
      Specified by:
      shuffle in interface Ordered.OfLong
      Overrides:
      shuffle in class LongList
      Parameters:
      rng - any Random class, such as one from juniper
    • sort

      public void sort()
      Sorts this deque in-place using Arrays.sort(long[], int, int) in ascending order.
      Overrides:
      sort in class LongList
    • sort

      public void sort(int from, int to)
      Uses Arrays.sort(long[], int, int) to sort a (clamped) subrange of this deque.
      Overrides:
      sort in class LongList
      Parameters:
      from - first index to use, inclusive
      to - last index to use, exclusive
    • sort

      public void sort(LongComparator comparator)
      Sorts this deque in-place using LongComparators.sort(long[], int, int, LongComparator). This should operate in O(n log(n)) time or less when the internals of the deque are continuous (the head is before the tail in the array). If the internals are not continuous, this takes an additional O(n) step (where n is less than the size of the deque) to rearrange the internals before sorting. You can pass null as the value for comparator, which will make this use the natural ordering for long.
      Specified by:
      sort in interface Ordered.OfLong
      Overrides:
      sort in class LongList
      Parameters:
      comparator - the Comparator to use for long items; may be null to use the natural order of long items when long implements Comparable of long
    • sort

      public void sort(int from, int to, LongComparator comparator)
      Description copied from class: LongList
      Sorts the specified range of elements according to the order induced by the specified comparator using mergesort, or Arrays.sort(long[], int, int) if c is null. This purely uses LongComparators.sort(long[], int, int, LongComparator), and you can see its docs for more information. This clamps from and to to the valid range.
      Overrides:
      sort in class LongList
      Parameters:
      from - the index of the first element (inclusive) to be sorted
      to - the index of the last element (exclusive) to be sorted
      comparator - the comparator to determine the order of the LongList
    • getRandom

      public long getRandom(Random random)
      Gets a randomly selected item from this LongDeque, using the given random number generator. Throws a NoSuchElementException if empty.
      Parameters:
      random - any Random or subclass of it, such as AlternateRandom.
      Returns:
      a randomly selected item from this deque
    • random

      public long random(Random random)
      Gets a randomly selected item from this LongDeque, using the given random number generator.
      Specified by:
      random in interface Ordered.OfLong
      Overrides:
      random in class LongList
      Parameters:
      random - any Random or subclass of it, such as AlternateRandom.
      Returns:
      a randomly selected item from this deque, or the default value if empty
    • with

      public static LongDeque with()
      Constructs an empty deque. This is usually less useful than just using the constructor, but can be handy in some code-generation scenarios when you don't know how many arguments you will have.
      Returns:
      a new deque containing nothing
    • with

      public static LongDeque with(long item)
      Creates a new LongDeque that holds only the given item, but can be resized.
      Parameters:
      item - one long item
      Returns:
      a new LongDeque that holds the given item
    • with

      public static LongDeque with(long item0, long item1)
      Creates a new LongDeque that holds only the given items, but can be resized.
      Parameters:
      item0 - a long item
      item1 - a long item
      Returns:
      a new LongDeque that holds the given items
    • with

      public static LongDeque with(long item0, long item1, long item2)
      Creates a new LongDeque that holds only the given items, but can be resized.
      Parameters:
      item0 - a long item
      item1 - a long item
      item2 - a long item
      Returns:
      a new LongDeque that holds the given items
    • with

      public static LongDeque with(long item0, long item1, long item2, long item3)
      Creates a new LongDeque that holds only the given items, but can be resized.
      Parameters:
      item0 - a long item
      item1 - a long item
      item2 - a long item
      item3 - a long item
      Returns:
      a new LongDeque that holds the given items
    • with

      public static LongDeque with(long item0, long item1, long item2, long item3, long item4)
      Creates a new LongDeque that holds only the given items, but can be resized.
      Parameters:
      item0 - a long item
      item1 - a long item
      item2 - a long item
      item3 - a long item
      item4 - a long item
      Returns:
      a new LongDeque that holds the given items
    • with

      public static LongDeque with(long item0, long item1, long item2, long item3, long item4, long item5)
      Creates a new LongDeque that holds only the given items, but can be resized.
      Parameters:
      item0 - a long item
      item1 - a long item
      item2 - a long item
      item3 - a long item
      item4 - a long item
      item5 - a long item
      Returns:
      a new LongDeque that holds the given items
    • with

      public static LongDeque with(long item0, long item1, long item2, long item3, long item4, long item5, long item6)
      Creates a new LongDeque that holds only the given items, but can be resized.
      Parameters:
      item0 - a long item
      item1 - a long item
      item2 - a long item
      item3 - a long item
      item4 - a long item
      item5 - a long item
      item6 - a long item
      Returns:
      a new LongDeque that holds the given items
    • with

      public static LongDeque with(long item0, long item1, long item2, long item3, long item4, long item5, long item6, long item7)
      Creates a new LongDeque that holds only the given items, but can be resized.
      Parameters:
      item0 - a long item
      item1 - a long item
      item2 - a long item
      item3 - a long item
      item4 - a long item
      item5 - a long item
      item6 - a long item
      item7 - a long item
      Returns:
      a new LongDeque that holds the given items
    • with

      public static LongDeque with(long... varargs)
      Creates a new LongDeque that will hold the items in the given array or varargs. This overload will only be used when a long array is supplied, or if varargs are used and there are 9 or more arguments.
      Parameters:
      varargs - either 0 or more long items, or an array of long
      Returns:
      a new LongDeque that holds the given long items
    • parse

      public static LongDeque parse(String str, String delimiter)
      Calls parse(String, String, boolean) with brackets set to false.
      Parameters:
      str - a String that will be parsed in full
      delimiter - the delimiter between items in str
      Returns:
      a new collection parsed from str
    • parse

      public static LongDeque parse(String str, String delimiter, boolean brackets)
      Creates a new collection and fills it by calling PrimitiveCollection.OfLong.addLegible(String, String, int, int) on either all of str (if brackets is false) or str without its first and last chars (if brackets is true). Each item is expected to be separated by delimiter.
      Parameters:
      str - a String that will be parsed in full (depending on brackets)
      delimiter - the delimiter between items in str
      brackets - if true, the first and last chars in str will be ignored
      Returns:
      a new collection parsed from str
    • parse

      public static LongDeque parse(String str, String delimiter, int offset, int length)
      Creates a new collection and fills it by calling PrimitiveCollection.OfLong.addLegible(String, String, int, int) with the given four parameters as-is.
      Parameters:
      str - a String that will have the given section parsed
      delimiter - the delimiter between items in str
      offset - the first position to parse in str, inclusive
      length - how many chars to parse, starting from offset
      Returns:
      a new collection parsed from str