edu.colorado.collections
Class IntLinkedBag

java.lang.Object
  extended by edu.colorado.collections.IntLinkedBag
All Implemented Interfaces:
java.lang.Cloneable

public class IntLinkedBag
extends java.lang.Object
implements java.lang.Cloneable

An IntLinkedBag is a collection of int numbers.

See Also:
Java Source Code for this class (www.cs.colorado.edu/~main/edu/colorado/collections/IntLinkedBag.java) , IntArrayBag, LinkedBag
Note:
(1) Beyond Int.MAX_VALUE elements, countOccurrences, size, and grab are wrong.

(2) Because of the slow linear algorithms of this class, large bags will have poor performance.


Constructor Summary
IntLinkedBag()
          Initialize an empty bag.
 
Method Summary
 void add(int element)
          Add a new element to this bag.
 void addAll(IntLinkedBag addend)
          Add the contents of another bag to this bag.
 void addMany(int... elements)
          Add new elements to this bag.
 java.lang.Object clone()
          Generate a copy of this bag.
 int countOccurrences(int target)
          Accessor method to count the number of occurrences of a particular element in this bag.
 int grab()
          Accessor method to retrieve a random element from this bag.
 boolean remove(int target)
          Remove one copy of a specified element from this bag.
 int size()
          Determine the number of elements in this bag.
static IntLinkedBag union(IntLinkedBag b1, IntLinkedBag b2)
          Create a new bag that contains all the elements from two other bags.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

IntLinkedBag

public IntLinkedBag()
Initialize an empty bag.

Parameters:
- - none
Postcondition:
This bag is empty.
Method Detail

add

public void add(int element)
Add a new element to this bag.

Parameters:
element - the new element that is being added
Postcondition:
A new copy of the element has been added to this bag.
Throws:
java.lang.OutOfMemoryError - Indicates insufficient memory a new IntNode.

addAll

public void addAll(IntLinkedBag addend)
Add the contents of another bag to this bag.

Parameters:
addend - a bag whose contents will be added to this bag
Precondition:
The parameter, addend, is not null.
Postcondition:
The elements from addend have been added to this bag.
Throws:
java.lang.NullPointerException - Indicates that addend is null.
java.lang.OutOfMemoryError - Indicates insufficient memory to increase the size of the bag.

addMany

public void addMany(int... elements)
Add new elements to this bag. If the new elements would take this bag beyond its current capacity, then the capacity is increased before adding the new elements.

Parameters:
elements - (a variable-arity argument) one or more new elements that are being inserted
Postcondition:
A new copy of the element has been added to this bag.
Throws:
java.lang.OutOfMemoryError - Indicates insufficient memory to increase the size of the bag.

clone

public java.lang.Object clone()
Generate a copy of this bag.

Overrides:
clone in class java.lang.Object
Parameters:
- - none
Returns:
The return value is a copy of this bag. Subsequent changes to the copy will not affect the original, nor vice versa. Note that the return value must be type cast to an IntLinkedBag before it can be used.
Throws:
java.lang.OutOfMemoryError - Indicates insufficient memory for creating the clone.

countOccurrences

public int countOccurrences(int target)
Accessor method to count the number of occurrences of a particular element in this bag.

Parameters:
target - the element that needs to be counted
Returns:
the number of times that target occurs in this bag

grab

public int grab()
Accessor method to retrieve a random element from this bag.

Parameters:
- - none
Precondition:
This bag is not empty.
Returns:
a randomly selected element from this bag
Throws:
java.lang.IllegalStateException - Indicates that the bag is empty.

remove

public boolean remove(int target)
Remove one copy of a specified element from this bag.

Parameters:
target - the element to remove from the bag
Postcondition:
If target was found in the bag, then one copy of target has been removed and the method returns true. Otherwise the bag remains unchanged and the method returns false.

size

public int size()
Determine the number of elements in this bag.

Parameters:
- - none
Returns:
the number of elements in this bag

union

public static IntLinkedBag union(IntLinkedBag b1,
                                 IntLinkedBag b2)
Create a new bag that contains all the elements from two other bags.

Parameters:
b1 - the first of two bags
b2 - the second of two bags
Precondition:
Neither b1 nor b2 is null.
Returns:
the union of b1 and b2
Throws:
java.lang.IllegalArgumentException - Indicates that one of the arguments is null.
java.lang.OutOfMemoryError - Indicates insufficient memory for the new bag.