Package sim.util

Class IntBag

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

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

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

IntBag is not synchronized, and so should not be accessed from different threads without locking on it or some appropriate lock int first. IntBag also has an unusual, fast method for removing ints called remove(...), which removes the int simply by swapping the topmost int into its place. This means that after remove(...) is called, the IntBag may no longer have the same order (hence the reason it's called a "IntBag" 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
     
    int[]
     
  • Constructor Summary

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

    Modifier and Type
    Method
    Description
    boolean
    add(int obj)
     
    boolean
    addAll(int[] other)
     
    boolean
    addAll(int index, int[] other)
     
    boolean
    addAll(int index, IntBag other)
     
    boolean
    addAll(IntBag other)
     
    void
    Removes all numbers in the IntBag.
     
    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(int o)
     
    void
    copyIntoArray(int fromStart, int[] to, int toStart, int len)
    Copies 'len' elements from the Bag into the provided array.
    void
    fill(int o)
    Replaces all elements in the bag with the provided int.
    int
    get(int index)
     
    getValue(int index)
    Throws an IndexOutOfBoundsException if index is inappropriate.
    boolean
     
    int
    pop()
    Returns 0 if the IntBag is empty, else removes and returns the topmost int.
    boolean
    push(int obj)
    Synonym for add(obj) -- try to use add instead unless you want to think of the IntBag as a stack.
    int
    remove(int index)
    Removes the int at the given index, moving the topmost int into its position.
    int
    Removes the int at the given index, shifting the other ints down.
    void
    resize(int toAtLeast)
     
    void
    Reverses order of the elements in the IntBag
    int
    set(int index, int 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 IntBag
    void
    shuffle(Random random)
    Shuffles (randomizes the order of) the IntBag
    int
     
    void
    Sorts the ints into ascending numerical order.
    int[]
     
     
     
     
    int
    top()
    Returns 0 if the IntBag is empty, else returns the topmost int.

    Methods inherited from class java.lang.Object

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

    • objs

      public int[] objs
    • numObjs

      public int numObjs
  • Constructor Details

    • IntBag

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

      public IntBag()
    • IntBag

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

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

    • size

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

      public boolean isEmpty()
    • addAll

      public boolean addAll(int[] other)
    • addAll

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

      public boolean addAll(IntBag other)
    • addAll

      public boolean addAll(int index, IntBag 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 int top()
      Returns 0 if the IntBag is empty, else returns the topmost int.
    • pop

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

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

      public boolean add(int obj)
    • contains

      public boolean contains(int o)
    • get

      public int 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 int set(int index, int 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 int removeNondestructively(int index)
      Removes the int at the given index, shifting the other ints down.
    • remove

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

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

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

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

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

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

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

      public void copyIntoArray(int fromStart, int[] 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 int[] toArray()
    • toIntegerArray

      public Integer[] toIntegerArray()
    • toLongArray

      public Long[] toLongArray()
    • 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