Class IntBag
- All Implemented Interfaces:
Indexed
,Serializable
,Cloneable
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
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
add
(int obj) boolean
addAll
(int[] other) boolean
addAll
(int index, int[] other) boolean
boolean
void
clear()
Removes all numbers in the IntBag.clone()
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
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 IntBagint
set
(int index, int element) 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 IntBagvoid
Shuffles (randomizes the order of) the IntBagint
size()
void
sort()
Sorts the ints into ascending numerical order.int[]
toArray()
Double[]
Integer[]
Long[]
int
top()
Returns 0 if the IntBag is empty, else returns the topmost int.
-
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
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() -
isEmpty
public boolean isEmpty() -
addAll
public boolean addAll(int[] other) -
addAll
public boolean addAll(int index, int[] other) -
addAll
-
addAll
-
clone
- Overrides:
clone
in classObject
- 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
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. -
set
public int set(int index, int element) -
setValue
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(...) -
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
Shuffles (randomizes the order of) the IntBag -
shuffle
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
-
toLongArray
-
toDoubleArray
-
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 interfaceIndexed
-