Class DoubleBag

java.lang.Object
com.github.tommyettinger.ds.DoubleList
com.github.tommyettinger.ds.DoubleBag
All Implemented Interfaces:
Arrangeable, Ordered.OfDouble, PrimitiveCollection<Double>, PrimitiveCollection.OfDouble

public class DoubleBag extends DoubleList
An unordered List of double items. This allows efficient iteration via a reused iterator or via index. This class avoids a memory copy when removing elements (the last element is moved to the removed element's position). Items are permitted to change position in the ordering when any item is removed or added. Although this won't keep an order during modifications, you can DoubleList.sort() the bag to ensure, if no modifications are made later, that the iteration will happen in sorted order.
  • Constructor Details

    • DoubleBag

      public DoubleBag()
      Creates an ordered bag with a capacity of 10.
    • DoubleBag

      public DoubleBag(int capacity)
      Creates an ordered bag with the specified capacity.
      Parameters:
      capacity -
    • DoubleBag

      public DoubleBag(DoubleList list)
      Creates a new bag containing the elements in the specific list or bag. 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 DoubleList or DoubleBag
    • DoubleBag

      public DoubleBag(double[] array)
      Creates a new bag 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 double array to add to this bag
    • DoubleBag

      public DoubleBag(double[] array, int startIndex, int count)
      Creates a new bag 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 double array to add to this bag
      startIndex - the first index in array to use
      count - how many items to use from array
    • DoubleBag

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

      public DoubleBag(DoubleIterator 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
    • DoubleBag

      public DoubleBag(Ordered.OfDouble other)
      Copies the given Ordered.OfDouble into a new bag.
      Parameters:
      other - another Ordered.OfDouble
    • DoubleBag

      public DoubleBag(Ordered.OfDouble other, int offset, int count)
      Creates a new bag by copying count items from the given Ordered, starting at offset in that Ordered, into this.
      Parameters:
      other - another Ordered.OfDouble
      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 not.
      Overrides:
      keepsOrder in class DoubleList
      Returns:
      false
    • insert

      public void insert(int index, double element)
      This always adds element to the end of this bag's ordering.
      Overrides:
      insert in class DoubleList
      Parameters:
      index - ignored
      element - element to be inserted
    • removeAt

      public double 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 DoubleList.remove(double) that removes a value, rather than an index.
      Overrides:
      removeAt in class DoubleList
      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.OfDouble
      Overrides:
      removeRange in class DoubleList
      Parameters:
      start - the first index to remove, inclusive
      end - the last index (after what should be removed), exclusive
    • hashCode

      public int hashCode()
      Specified by:
      hashCode in interface PrimitiveCollection<Double>
      Overrides:
      hashCode in class DoubleList
    • with

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

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

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

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

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

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

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

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

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

      public static DoubleBag with(double... varargs)
      Creates a new DoubleBag 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 double varargs or double array; remember that varargs allocate
      Returns:
      a new DoubleBag that holds the given items
    • parse

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