public class IntBag extends java.lang.Object implements java.io.Serializable, java.lang.Cloneable, Indexed
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.
Constructor and Description |
---|
IntBag() |
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.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(int obj) |
boolean |
addAll(int[] other) |
boolean |
addAll(IntBag other) |
boolean |
addAll(int index,
int[] other) |
boolean |
addAll(int index,
IntBag other) |
void |
clear()
Removes all numbers in the IntBag.
|
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 |
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) |
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.
|
int[] |
toArray() |
java.lang.Double[] |
toDoubleArray() |
java.lang.Integer[] |
toIntegerArray() |
java.lang.Long[] |
toLongArray() |
int |
top()
Returns 0 if the IntBag is empty, else returns the topmost int.
|
public IntBag(int capacity)
public IntBag()
public IntBag(IntBag other)
public IntBag(int[] other)
public boolean isEmpty()
public boolean addAll(int[] other)
public boolean addAll(int index, int[] other)
public boolean addAll(IntBag other)
public boolean addAll(int index, IntBag other)
public java.lang.Object clone() throws java.lang.CloneNotSupportedException
clone
in class java.lang.Object
java.lang.CloneNotSupportedException
public void resize(int toAtLeast)
public void shrink(int desiredLength)
public int top()
public int pop()
public boolean push(int obj)
public boolean add(int obj)
public boolean contains(int o)
public int get(int index)
public java.lang.Object getValue(int index)
Indexed
public int set(int index, int element)
public java.lang.Object setValue(int index, java.lang.Object value)
Indexed
public int removeNondestructively(int index)
public int remove(int index)
public void sort()
public void fill(int o)
public void shuffle(java.util.Random random)
public void shuffle(MersenneTwisterFast random)
public void reverse()
public void clear()
public void copyIntoArray(int fromStart, int[] to, int toStart, int len)
public int[] toArray()
public java.lang.Integer[] toIntegerArray()
public java.lang.Long[] toLongArray()
public java.lang.Double[] toDoubleArray()
public java.lang.Class componentType()
Indexed
componentType
in interface Indexed