ecologylab.collections
Class WeightSet<E extends AbstractSetElement>

java.lang.Object
  extended by java.util.Observable
      extended by ecologylab.generic.ObservableDebug
          extended by ecologylab.collections.WeightSet<E>
All Implemented Interfaces:
java.lang.Iterable<E>

public class WeightSet<E extends AbstractSetElement>
extends ObservableDebug
implements java.lang.Iterable<E>

Provides the facility of efficient weighted random selection from a set elements, each of whicn includes a characterizing floating point weight.

Works in cooperation w SetElements; requiring that user object to keep an integer index slot, which only we should be accessing. This impure oo style is an adaption to Java's lack of multiple inheritance.

Gotta be careful to be thread safe. Seems no operations can safely occur concurrently. There are a bunch of synchronized methods to affect this.


Nested Class Summary
 class WeightSet.FloatWeightComparator
           
 
Constructor Summary
WeightSet(int maxSize, int setSize, WeightingStrategy<E> weightingStrategy)
           
WeightSet(int maxSize, ThreadMaster threadMaster, WeightingStrategy<E> weightStrategy)
           
WeightSet(WeightingStrategy<E> getWeightStrategy)
           
 
Method Summary
 E at(int i)
          Fetches the i'th element in the sorted list.
 void clear(boolean doRecycleElements)
          Delete all the elements in the set, as fast as possible.
 E get(int i)
           
 double getWeight(E e)
          Fetches the weight of the passed in element.
 WeightingStrategy<E> getWeightStrategy()
           
 void insert(E el)
           
 boolean isEmpty()
          Check to see if the set has any elements.
 boolean isRunnable()
          Method Overriden by VisualPool to return true
 java.util.Iterator<E> iterator()
           
 E maxPeek()
           
 E maxSelect()
          Selects the top weighted element from the set.
This method removes the selected from the set.
 double mean()
           
 void prune(int numToKeep)
           
 E pruneAndMaxSelect()
          Prune to the set's specified maxSize, if necessary, then do a maxSelect().
 void remove(E el)
           
 int size()
           
 java.lang.String toString()
           
 java.lang.Double weightAt(int i)
          Fetches the weight of the i'th element in the sorted list.
 
Methods inherited from class ecologylab.generic.ObservableDebug
debug, debug, debugA, debugA, debugI, debugI, error, getClassName, print, println, println, println, show, superString
 
Methods inherited from class java.util.Observable
addObserver, countObservers, deleteObserver, deleteObservers, hasChanged, notifyObservers, notifyObservers
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

WeightSet

public WeightSet(int maxSize,
                 int setSize,
                 WeightingStrategy<E> weightingStrategy)

WeightSet

public WeightSet(WeightingStrategy<E> getWeightStrategy)

WeightSet

public WeightSet(int maxSize,
                 ThreadMaster threadMaster,
                 WeightingStrategy<E> weightStrategy)
Method Detail

mean

public double mean()

maxSelect

public E maxSelect()
Selects the top weighted element from the set.
This method removes the selected from the set.

Returns:

maxPeek

public E maxPeek()

prune

public void prune(int numToKeep)

insert

public void insert(E el)

remove

public void remove(E el)

get

public E get(int i)

clear

public void clear(boolean doRecycleElements)
Delete all the elements in the set, as fast as possible.

Parameters:
doRecycleElements - TODO

pruneAndMaxSelect

public E pruneAndMaxSelect()
Prune to the set's specified maxSize, if necessary, then do a maxSelect(). The reason for doing these operations together is because both require sorting.

Returns:
element in the set with the highest weight, or in case of a tie, a random pick of those.

size

public int size()

toString

public java.lang.String toString()
Overrides:
toString in class ObservableDebug

isEmpty

public boolean isEmpty()
Check to see if the set has any elements.

Returns:

at

public E at(int i)
Fetches the i'th element in the sorted list.

Parameters:
i - index into the list
Returns:
the element at index i

weightAt

public java.lang.Double weightAt(int i)
Fetches the weight of the i'th element in the sorted list.

Parameters:
i - index into the list
Returns:
the weight of the element at index i

getWeight

public double getWeight(E e)
Fetches the weight of the passed in element.

Parameters:
e - Element to weigh. Doesn't have to be a member of the set.
Returns:
the weight of the element

isRunnable

public boolean isRunnable()
Method Overriden by VisualPool to return true

Returns:

getWeightStrategy

public WeightingStrategy<E> getWeightStrategy()

iterator

public java.util.Iterator<E> iterator()
Specified by:
iterator in interface java.lang.Iterable<E extends AbstractSetElement>