Package sim.util

Class DoubleBag

java.lang.Object
sim.util.DoubleBag
All Implemented Interfaces:
Serializable, Cloneable, Indexed

public class DoubleBag extends Object implements Serializable, Cloneable, Indexed
Maintains a simple array (objs) of doubles and the number of doubles (numObjs) in the array (the array can be bigger than this number). You are encouraged to access the doubles directly; they are stored in positions [0 ... numObjs-1]. If you wish to extend the array, you should call the resize method.

DoubleBag is approximately to double what Bag is to Object. However, for obvious reasons, DoubleBag is not a java.util.Collection subclass and is purposely simple (it doesn't have an Iterator for example).

DoubleBag is not synchronized, and so should not be accessed from different threads without locking on it or some appropriate lock double first. DoubleBag also has an unusual, fast method for removing doubles called remove(...), which removes the double simply by swapping the topmost double into its place. This means that after remove(...) is called, the DoubleBag may no longer have the same order (hence the reason it's called a "DoubleBag" rather than some variant on "Vector" or "Array" or "List"). You can guarantee order by calling removeNondestructively(...) instead if you wish, but this is O(n) in the worst case.

See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    int
     
    double[]
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
    DoubleBag(double[] other)
    Creates a DoubleBag with the given elements.
    DoubleBag(int capacity)
    Creates a DoubleBag with a given initial capacity.
    Adds the doubles from the other DoubleBag without copying them.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    add(double obj)
     
    boolean
    addAll(double[] other)
     
    boolean
    addAll(int index, double[] other)
     
    boolean
    addAll(int index, DoubleBag other)
     
    boolean
     
    void
    Removes all numbers in the DoubleBag.
     
    Should return the base component type for this Indexed object, or null if the component type should be queried via getValue(index).getClass.getComponentType()
    boolean
    contains(double o)
     
    void
    copyIntoArray(int fromStart, double[] to, int toStart, int len)
    Copies 'len' elements from the Bag into the provided array.
    void
    fill(double o)
    Replaces all elements in the bag with the provided object.
    double
    get(int index)
     
    getValue(int index)
    Throws an IndexOutOfBoundsException if index is inappropriate.
    boolean
     
    double
    pop()
    Returns 0 if the DoubleBag is empty, else removes and returns the topmost double.
    boolean
    push(double obj)
    Synonym for add(obj) -- try to use add instead unless you want to think of the DoubleBag as a stack.
    double
    remove(int index)
    Removes the double at the given index, moving the topmost double into its position.
    double
    Removes the double at the given index, shifting the other doubles down.
    void
    resize(int toAtLeast)
     
    void
    Reverses order of the elements in the DoubleBag
    double
    set(int index, double element)
     
    setValue(int index, Object value)
    Throws an IndexOutOfBoundsException if index is inappropriate, and IllegalArgumentException if the value is inappropriate.
    void
    shrink(int desiredLength)
    Resizes the objs array to max(numObjs, desiredLength), unless that value is greater than or equal to objs.length, in which case no resizing is done (this operation only shrinks -- use resize() instead).
    void
    Shuffles (randomizes the order of) the DoubleBag
    void
    shuffle(Random random)
    Shuffles (randomizes the order of) the DoubleBag
    int
     
    void
    Sorts the doubles into ascending numerical order.
    double[]
     
     
    double
    top()
    Returns 0 if the DoubleBag is empty, else returns the topmost double.

    Methods inherited from class java.lang.Object

    equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • objs

      public double[] objs
    • numObjs

      public int numObjs
  • Constructor Details

    • DoubleBag

      public DoubleBag(int capacity)
      Creates a DoubleBag with a given initial capacity.
    • DoubleBag

      public DoubleBag()
    • DoubleBag

      public DoubleBag(DoubleBag other)
      Adds the doubles from the other DoubleBag without copying them. The size of the new DoubleBag is the minimum necessary size to hold the doubles. If the Other DoubleBag is null, a new empty DoubleBag is created.
    • DoubleBag

      public DoubleBag(double[] other)
      Creates a DoubleBag with the given elements. If the Other array is null, a new empty DoubleBag is created.
  • Method Details

    • size

      public int size()
      Specified by:
      size in interface Indexed
    • isEmpty

      public boolean isEmpty()
    • addAll

      public boolean addAll(double[] other)
    • addAll

      public boolean addAll(int index, double[] other)
    • addAll

      public boolean addAll(DoubleBag other)
    • addAll

      public boolean addAll(int index, DoubleBag other)
    • clone

      public Object clone() throws CloneNotSupportedException
      Overrides:
      clone in class Object
      Throws:
      CloneNotSupportedException
    • resize

      public void resize(int toAtLeast)
    • shrink

      public void shrink(int desiredLength)
      Resizes the objs array to max(numObjs, desiredLength), unless that value is greater than or equal to objs.length, in which case no resizing is done (this operation only shrinks -- use resize() instead). This is an O(n) operation, so use it sparingly.
    • top

      public double top()
      Returns 0 if the DoubleBag is empty, else returns the topmost double.
    • pop

      public double pop()
      Returns 0 if the DoubleBag is empty, else removes and returns the topmost double.
    • push

      public boolean push(double obj)
      Synonym for add(obj) -- try to use add instead unless you want to think of the DoubleBag as a stack.
    • add

      public boolean add(double obj)
    • contains

      public boolean contains(double o)
    • get

      public double get(int index)
    • getValue

      public Object getValue(int index)
      Description copied from interface: Indexed
      Throws an IndexOutOfBoundsException if index is inappropriate. Not called get() because this would conflict with get() methods in IntBag etc. which don't return objects.
      Specified by:
      getValue in interface Indexed
    • set

      public double set(int index, double element)
    • setValue

      public Object setValue(int index, Object value)
      Description copied from interface: Indexed
      Throws an IndexOutOfBoundsException if index is inappropriate, and IllegalArgumentException if the value is inappropriate. Not called set() in order to be consistent with getValue(...)
      Specified by:
      setValue in interface Indexed
    • removeNondestructively

      public double removeNondestructively(int index)
      Removes the double at the given index, shifting the other doubles down.
    • remove

      public double remove(int index)
      Removes the double at the given index, moving the topmost double into its position.
    • sort

      public void sort()
      Sorts the doubles into ascending numerical order.
    • fill

      public void fill(double o)
      Replaces all elements in the bag with the provided object.
    • shuffle

      public void shuffle(Random random)
      Shuffles (randomizes the order of) the DoubleBag
    • shuffle

      public void shuffle(MersenneTwisterFast random)
      Shuffles (randomizes the order of) the DoubleBag
    • reverse

      public void reverse()
      Reverses order of the elements in the DoubleBag
    • clear

      public void clear()
      Removes all numbers in the DoubleBag. This is done by clearing the internal array but not replacing it with a new, smaller one.
    • copyIntoArray

      public void copyIntoArray(int fromStart, double[] to, int toStart, int len)
      Copies 'len' elements from the Bag into the provided array. The 'len' elements start at index 'fromStart' in the Bag, and are copied into the provided array starting at 'toStat'.
    • toArray

      public double[] toArray()
    • toDoubleArray

      public Double[] toDoubleArray()
    • componentType

      public Class componentType()
      Description copied from interface: Indexed
      Should return the base component type for this Indexed object, or null if the component type should be queried via getValue(index).getClass.getComponentType()
      Specified by:
      componentType in interface Indexed