|
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||
java.lang.ObjectGShuffleBag<E>
public class GShuffleBag<E>
An enlargeable continuous shuffled sequence generator. Basically it's like a List, Collections.shuffle(), remove(), and refilling/reshuffling once it's empty. However, it only uses a single array and the shuffling happens on the go (each next operation swapps two elements). Additionally, it's easier to use and less error prone.
The size, capacity, and next operations run in constant time. The add operation runs in amortized constant time (adding n elements requires O(n) time).
Single elements can only reoccur once this pass is finished. So, if an element was only added once it can be at most be drawn twice - at the end of one pass and at the beginning of the next pass.
IntShuffleBag,
ShuffleBag| Constructor Summary | |
|---|---|
GShuffleBag()
Constructs an empty bag with an initial capacity of 10 and the default source of randomness. |
|
GShuffleBag(int capacity)
Constructs an empty bag with the specified initial capacity and the default source of randomness. |
|
GShuffleBag(int capacity,
java.util.Random gen)
Constructs an empty bag with the specified initial capacity and the specified source of randomness. |
|
GShuffleBag(java.util.Random gen)
Constructs an empty bag with an initial capacity of 10 and the specified source of randomness. |
|
| Method Summary | |
|---|---|
void |
add(E e)
Puts the specified element into the bag and sets the cursor to the last position. |
void |
add(E e,
int quantity)
Puts the specified element several times into the bag and sets the cursor to the last position. |
int |
capacity()
Returns the capacity of this bag. |
E |
next()
Returns the next item from this bag. |
int |
size()
Returns the number of elements in this bag. |
void |
trimToSize()
Trims the capacity of this GShuffleBag instance to be the bag's current size. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public GShuffleBag()
java.lang.IllegalArgumentException - if the specified initial capacity is negativepublic GShuffleBag(java.util.Random gen)
gen - the source of randomness to use to shuffle the bag
java.lang.IllegalArgumentException - if the specified initial capacity is negativepublic GShuffleBag(int capacity)
capacity - the initial capacity of this bag
java.lang.IllegalArgumentException - if the specified initial capacity is negative
public GShuffleBag(int capacity,
java.util.Random gen)
capacity - the initial capacity of this baggen - the source of randomness to use to shuffle this bag
java.lang.IllegalArgumentException - if the specified initial capacity is negative| Method Detail |
|---|
public void add(E e)
e - element to be added
public void add(E e,
int quantity)
e - element to be addedquantity - the quantitypublic E next()
The cursor moves from back to front. If the cursor position is smaller than 1 the element at index 0 is returned and the curor is reset to size-1.
Otherwise randomly picked elements from index 0 to the cursor position (inclusive) are swapped with the element at the cursor position. Afterwards the cursor gets moved one step and the element at the old cursor position is returned.
public void trimToSize()
public int capacity()
public int size()
|
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||