sim.util
Class IntBag

java.lang.Object
  extended bysim.util.IntBag
All Implemented Interfaces:
java.lang.Cloneable, Indexed, java.io.Serializable

public class IntBag
extends java.lang.Object
implements java.io.Serializable, java.lang.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:
Serialized Form

Field Summary
 int numObjs
           
 int[] objs
           
 
Constructor Summary
IntBag()
           
IntBag(int capacity)
          Creates an IntBag with a given initial capacity.
IntBag(IntBag other)
          Adds the ints from the other IntBag without copying them.
 
Method Summary
 boolean add(int obj)
           
 boolean addAll(IntBag other)
           
 boolean addAll(int index, int[] other)
           
 boolean addAll(int index, IntBag other)
           
 void clear()
           
 java.lang.Object clone()
           
 java.lang.Class componentType()
          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 fill(int o)
          Replaces all elements in the bag with the provided int.
 int get(int index)
           
 java.lang.Object getValue(int index)
          Throws an IndexOutOfBoundsException if index is inappropriate.
 boolean isEmpty()
           
 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 removeNondestructively(int index)
          Removes the int at the given index, shifting the other ints down.
 void resize(int toAtLeast)
           
 void reverse()
          Reverses order of the elements in the IntBag
 int set(int index, int element)
           
 java.lang.Object setValue(int index, java.lang.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 shuffle(MersenneTwisterFast random)
          Shuffles (randomizes the order of) the IntBag
 void shuffle(java.util.Random random)
          Shuffles (randomizes the order of) the IntBag
 int size()
           
 void sort()
          Sorts the ints into ascending numerical order.
protected  void throwIndexOutOfBoundsException(int index)
           
 int[] toArray()
           
 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 Detail

objs

public int[] objs

numObjs

public int numObjs
Constructor Detail

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.

Method Detail

size

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

isEmpty

public boolean isEmpty()

addAll

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

addAll

public boolean addAll(IntBag other)

addAll

public boolean addAll(int index,
                      IntBag other)

clone

public java.lang.Object clone()
                       throws java.lang.CloneNotSupportedException
Throws:
java.lang.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 java.lang.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 java.lang.Object setValue(int index,
                                 java.lang.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(java.util.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


throwIndexOutOfBoundsException

protected void throwIndexOutOfBoundsException(int index)

clear

public void clear()

toArray

public int[] toArray()

componentType

public java.lang.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