Class FloatList

java.lang.Object
com.github.tommyettinger.ds.FloatList
All Implemented Interfaces:
Arrangeable, Ordered.OfFloat, PrimitiveCollection<Float>, PrimitiveCollection.OfFloat
Direct Known Subclasses:
FloatBag, FloatDeque

public class FloatList extends Object implements PrimitiveCollection.OfFloat, Ordered.OfFloat, Arrangeable
A resizable, insertion-ordered float list. Primitive-backed, so it avoids the boxing that occurs with an ArrayList of Float. This tries to imitate most of the List interface, though it can't implement it without boxing its items. Has a primitive iterator accessible via iterator().
See Also:
  • Field Details

  • Constructor Details

    • FloatList

      public FloatList()
      Creates an ordered list with a capacity of 10.
    • FloatList

      public FloatList(int capacity)
      Creates an ordered list with the specified capacity.
      Parameters:
      capacity - Any elements added beyond this will cause the backing array to be grown.
    • FloatList

      @Deprecated public FloatList(boolean ordered, int capacity)
      Deprecated.
      FloatList is always ordered; for an unordered list use FloatBag
      Creates an ordered list with the specified capacity.
      Parameters:
      ordered - ignored; for an unordered list use FloatBag
      capacity - Any elements added beyond this will cause the backing array to be grown.
    • FloatList

      public FloatList(FloatList list)
      Creates a new list containing the elements in the given list. The new list will be ordered. The capacity is set to the number of elements, so any subsequent elements added will cause the backing array to be grown.
      Parameters:
      list - another FloatList (or FloatBag) to copy from
    • FloatList

      public FloatList(float[] array)
      Creates a new list containing the elements in the specified array. The capacity is set to the number of elements, so any subsequent elements added will cause the backing array to be grown.
      Parameters:
      array - a float array to copy from
    • FloatList

      public FloatList(float[] array, int startIndex, int count)
      Creates a new list containing the elements in the specified array. The capacity is set to the number of elements, so any subsequent elements added will cause the backing array to be grown.
      Parameters:
      array - a non-null float array to add to this list
      startIndex - the first index in array to use
      count - how many items to use from array
    • FloatList

      @Deprecated public FloatList(boolean ordered, float[] array, int startIndex, int count)
      Deprecated.
      FloatList is always ordered; for an unordered list use FloatBag
      Creates a new list containing the elements in the specified array. The capacity is set to the number of elements, so any subsequent elements added will cause the backing array to be grown.
      Parameters:
      ordered - ignored; for an unordered list use FloatBag
      array - a non-null float array to add to this list
      startIndex - the first index in array to use
      count - how many items to use from array
    • FloatList

      public FloatList(PrimitiveCollection.OfFloat coll)
      Creates a new list containing the items in the specified PrimitiveCollection.OfFloat.
      Parameters:
      coll - a primitive collection that will have its contents added to this
    • FloatList

      public FloatList(FloatIterator coll)
      Creates a new instance containing the items in the specified iterator.
      Parameters:
      coll - an iterator that will have its remaining contents added to this
    • FloatList

      public FloatList(Ordered.OfFloat other)
      Copies the given Ordered.OfFloat into a new FloatList.
      Parameters:
      other - another Ordered.OfFloat that will have its contents copied into this
    • FloatList

      public FloatList(Ordered.OfFloat other, int offset, int count)
      Creates a new list by copying count items from the given Ordered, starting at offset in that Ordered, into this.
      Parameters:
      other - another Ordered.OfFloat
      offset - the first index in other's ordering to draw an item from
      count - how many items to copy from other
  • Method Details

    • keepsOrder

      public boolean keepsOrder()
      Returns true if this implementation retains order, which it does.
      Returns:
      true
    • size

      public int size()
      Description copied from interface: Arrangeable
      Returns the number of elements in this Arrangeable. Often this is shared with Collection.size(), but isn't always.
      Specified by:
      size in interface Arrangeable
      Specified by:
      size in interface PrimitiveCollection<Float>
      Returns:
      the number of elements in this Arrangeable
    • add

      public boolean add(float value)
      Specified by:
      add in interface PrimitiveCollection.OfFloat
    • add

      public void add(float value1, float value2)
    • add

      public void add(float value1, float value2, float value3)
    • add

      public void add(float value1, float value2, float value3, float value4)
    • addAll

      public boolean addAll(FloatList list)
    • addAll

      public boolean addAll(FloatList list, int offset, int count)
    • addAll

      public boolean addAll(Ordered.OfFloat other, int offset, int count)
      Adds up to count items, starting from offset, in the Ordered.OfFloat other to this list, inserting at the end of the iteration order.
      Parameters:
      other - a non-null Ordered.OfFloat
      offset - the first index in other to use
      count - how many indices in other to use
      Returns:
      true if this is modified by this call, as addAll(FloatList) does
    • addAll

      public boolean addAll(int insertionIndex, Ordered.OfFloat other, int offset, int count)
      Adds up to count items, starting from offset, in the Ordered.OfFloat other to this list, inserting starting at insertionIndex in the iteration order.
      Parameters:
      insertionIndex - where to insert into the iteration order
      other - a non-null Ordered.OfFloat
      offset - the first index in other to use
      count - how many indices in other to use
      Returns:
      true if this is modified by this call, as addAll(FloatList) does
    • addAll

      public boolean addAll(float... array)
      Specified by:
      addAll in interface PrimitiveCollection.OfFloat
    • addAll

      public boolean addAll(float[] array, int offset, int length)
      Specified by:
      addAll in interface PrimitiveCollection.OfFloat
    • get

      public float get(int index)
    • set

      public void set(int index, float value)
    • plus

      public void plus(int index, float value)
    • plus

      public FloatList plus(float value)
      Adds value to each item in this FloatList, 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 FloatList).
      Parameters:
      value - each item in this will be assigned item + value
      Returns:
      this for chaining and Kotlin compatibility
    • times

      public void times(int index, float value)
    • times

      public FloatList times(float value)
      Multiplies each item in this FloatList 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 FloatList).
      Parameters:
      value - each item in this will be assigned item * value
      Returns:
      this for chaining and Kotlin compatibility
    • minus

      public void minus(int index, float value)
    • minus

      public FloatList minus(float value)
      Takes each item in this FloatList 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 FloatList).
      Parameters:
      value - each item in this will be assigned item - value
      Returns:
      this for chaining and Kotlin compatibility
    • div

      public void div(int index, float value)
    • div

      public FloatList div(float value)
      Divides each item in this FloatList 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 FloatList).
      Parameters:
      value - each item in this will be assigned item / value
      Returns:
      this for chaining and Kotlin compatibility
    • rem

      public void rem(int index, float value)
    • rem

      public FloatList rem(float value)
      Gets the remainder of each item in this FloatList 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 FloatList).
      Parameters:
      value - each item in this will be assigned item % value
      Returns:
      this for chaining and Kotlin compatibility
    • insert

      public void insert(int index, float value)
    • 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.
      Parameters:
      index - the first index to duplicate
      count - how many items to duplicate
    • order

      public FloatList order()
      Returns this FloatList, since it is its own order. This is only here to satisfy the Ordered.OfFloat interface.
      Specified by:
      order in interface Ordered.OfFloat
      Returns:
      this FloatList
    • swap

      public void swap(int first, int second)
      Description copied from interface: Ordered.OfFloat
      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.OfFloat
      Parameters:
      first - the first position, must not be negative and must be less than Arrangeable.size()
      second - the second position, must not be negative and must be less than Arrangeable.size()
    • contains

      public boolean contains(float value)
      Specified by:
      contains in interface PrimitiveCollection.OfFloat
    • containsAll

      public boolean containsAll(FloatList other)
      Returns true if this FloatList contains, at least once, every item in other; otherwise returns false.
      Parameters:
      other - an FloatList
      Returns:
      true if this contains every item in other, otherwise false
    • indexOf

      public int indexOf(float value)
      Returns the first index in this list that contains the specified value, or -1 if it is not present.
      Parameters:
      value - a float value to search for
      Returns:
      the first index of the given value, or -1 if it is not present
    • lastIndexOf

      public int lastIndexOf(float value)
      Returns the last index in this list that contains the specified value, or -1 if it is not present.
      Parameters:
      value - a float value to search for
      Returns:
      the last index of the given value, or -1 if it is not present
    • remove

      public boolean remove(float value)
      Removes the first occurrence of value from this FloatList, returning true if anything was removed. Otherwise, this returns false.
      Specified by:
      remove in interface PrimitiveCollection.OfFloat
      Parameters:
      value - the value to (attempt to) remove
      Returns:
      true if a value was removed, false if the FloatList is unchanged
    • removeAt

      public float removeAt(int index)
      Removes and returns the item at the specified index. Note that this is equivalent to List.remove(int), but can't have that name because we also have remove(float) that removes a value, rather than an index.
      Parameters:
      index - the index of the item to remove and return
      Returns:
      the removed item
    • removeRange

      public void removeRange(int start, int end)
      Removes the items between the specified start index, inclusive, and end index, exclusive. Note that this takes different arguments than some other range-related methods; this needs a start index and an end index, rather than a count of items. This matches the behavior in the JDK collections.
      Specified by:
      removeRange in interface Ordered.OfFloat
      Parameters:
      start - the first index to remove, inclusive
      end - the last index (after what should be removed), exclusive
    • removeAll

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

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

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

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

      public boolean replaceFirst(float find, float 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.
      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(float find, float 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.
      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
    • replaceFirst

      public boolean replaceFirst(float find, float replace, float tolerance)
      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. Takes a tolerance to permit some floating-point imprecision in the search.
      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(float find, float replace, float tolerance)
      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. Takes a tolerance to permit some floating-point imprecision in the search.
      Parameters:
      find - the item to search for
      replace - the item to replace find with, if possible
      tolerance - how much an item can be different from find and still be replaced
      Returns:
      the number of replacements that occurred; 0 if nothing was found or replaced
    • replaceFirstNaN

      public boolean replaceFirstNaN(float replace)
      Replaces the first occurrence of NaN with replace. Returns true if it performed the replacement, or false if there was nothing to replace.
      Parameters:
      replace - the item to replace NaN with, if possible
      Returns:
      true if this changed, or false otherwise
    • replaceAllNaN

      public int replaceAllNaN(float replace)
      Replaces every occurrence of NaN with replace. Returns the number of changed items, which is 0 if nothing was found.
      Parameters:
      replace - the item to replace NaN with, if possible
      Returns:
      the number of replacements that occurred; 0 if nothing was found or replaced
    • pop

      public float pop()
      Removes and returns the last item.
      Returns:
      the last item, removed from this
    • peek

      public float peek()
      Returns the last item.
      Returns:
      the last item, without modifying this
    • first

      public float first()
      Returns the first item.
      Specified by:
      first in interface PrimitiveCollection.OfFloat
      Returns:
      the first item, without modifying this
    • notEmpty

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

      public boolean isEmpty()
      Returns true if the list is empty.
      Specified by:
      isEmpty in interface PrimitiveCollection<Float>
      Returns:
      true if the list is empty, or false if it has any items
    • clear

      public void clear()
      Effectively removes all items from this FloatList. This is done simply by setting size to 0; because a float item isn't a reference, it doesn't need to be set to null.
      Specified by:
      clear in interface PrimitiveCollection<Float>
    • shrink

      public float[] shrink()
      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.
      Returns:
      items; this will be a different reference if this resized
    • trimToSize

      public void trimToSize()
    • ensureCapacity

      public float[] ensureCapacity(int additionalCapacity)
      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.
      Returns:
      items; this will be a different reference if this resized
    • setSize

      public float[] setSize(int newSize)
      Sets the list size, leaving any values beyond the current size undefined.
      Returns:
      items; this will be a different reference if this resized to a larger capacity
    • resize

      protected float[] resize(int newSize)
    • sort

      public void sort()
      Sorts this entire collection using Arrays.sort(float[], int, int) in ascending order.
    • sort

      public void sort(int from, int to)
      Uses Arrays.sort(float[], int, int) to sort a (clamped) subrange of this collection in ascending order.
      Parameters:
      from - the index of the first element (inclusive) to be sorted
      to - the index of the last element (exclusive) to be sorted
    • sort

      public void sort(FloatComparator c)
      Sorts all elements according to the order induced by the specified comparator using FloatComparators.sort(float[], int, int, FloatComparator). If c is null, this instead delegates to sort(), which uses Arrays.sort(float[]), and does not always run in-place.

      This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort. The sorting algorithm is an in-place mergesort that is significantly slower than a standard mergesort, as its running time is O(n (log n)2), but it does not allocate additional memory; as a result, it can be used as a generic sorting algorithm.

      Specified by:
      sort in interface Ordered.OfFloat
      Parameters:
      c - the comparator to determine the order of the FloatList
    • sort

      public void sort(int from, int to, FloatComparator c)
      Sorts the specified range of elements according to the order induced by the specified comparator using mergesort, or Arrays.sort(float[], int, int) if c is null. This purely uses FloatComparators.sort(float[], int, int, FloatComparator), and you can see its docs for more information. This clamps from and to to the valid range.
      Parameters:
      from - the index of the first element (inclusive) to be sorted
      to - the index of the last element (exclusive) to be sorted
      c - the comparator to determine the order of the FloatList
    • reverse

      public void reverse()
      Description copied from interface: Ordered.OfFloat
      Reverses the order of this Ordered in-place.
      Specified by:
      reverse in interface Arrangeable
      Specified by:
      reverse in interface Ordered.OfFloat
    • shuffle

      public void shuffle(Random random)
      Description copied from interface: Ordered.OfFloat
      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.OfFloat
      Parameters:
      random - any Random class, such as one from juniper
    • truncate

      public void truncate(int newSize)
      Reduces the size of the list to the specified size. If the list is already smaller than the specified size, no action is taken.
    • random

      public float random(Random random)
      Returns a random item from the list, or zero if the list is empty.
      Specified by:
      random in interface Ordered.OfFloat
      Parameters:
      random - a Random or a subclass, such as any from juniper
      Returns:
      a randomly selected item from this, or 0 if this is empty
    • toArray

      public float[] toArray()
      Allocates a new float array with size elements and fills it with the items in this.
      Specified by:
      toArray in interface PrimitiveCollection.OfFloat
      Returns:
      a new float array with the same contents as this
    • toArray

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

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

      public boolean equals(Object object)
      Specified by:
      equals in interface PrimitiveCollection<Float>
      Overrides:
      equals in class Object
    • equals

      public boolean equals(Object object, float tolerance)
      Compares float items with the given tolerance for error.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • iterator

      public FloatList.FloatListIterator iterator()
      Returns a Java 8 primitive iterator over the int items in this FloatList. Iterates in order if keepsOrder() returns true, which it does for a FloatList but not a FloatBag.
      This will reuse one of two iterators in this FloatList; this does not allow nested iteration. Use FloatListIterator(FloatList) to nest iterators.
      Specified by:
      iterator in interface PrimitiveCollection<Float>
      Specified by:
      iterator in interface PrimitiveCollection.OfFloat
      Returns:
      a FloatIterator; use its nextFloat() method instead of next()
    • with

      public static FloatList with()
      Constructs an empty list. 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 list containing nothing
    • with

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

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

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

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

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

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

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

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

      public static FloatList with(float... varargs)
      Creates a new FloatList that holds only the given items, but can be resized. This overload will only be used when an array is supplied and the type of the items requested is the component type of the array, or if varargs are used and there are 9 or more arguments.
      Parameters:
      varargs - a float varargs or float array; remember that varargs allocate
      Returns:
      a new FloatList that holds the given items
    • parse

      public static FloatList 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 FloatList parse(String str, String delimiter, boolean brackets)
      Creates a new collection and fills it by calling PrimitiveCollection.OfFloat.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 FloatList parse(String str, String delimiter, int offset, int length)
      Creates a new collection and fills it by calling PrimitiveCollection.OfFloat.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