VSelector is a templated class for storing objects associated with probabilities in a way such that, given a flat random number between 0 and 1, an object can be selected according to its relative probability. More...
#include <VSelector.h>
Public Types  
typedef vector< WeightType >  WeightVector 
A vector of weights. More...  
typedef WeightVector::const_iterator  WIterator 
The weight vector iterator type. More...  
typedef vector< T >  ObjectVector 
A vector ob objects. More...  
typedef ObjectVector::iterator  iterator 
The object vector iterator type. More...  
typedef ObjectVector::const_iterator  const_iterator 
The object vector const iterator type. More...  
typedef ObjectVector::size_type  size_type 
Size type of the underlying vector. More...  
Public Member Functions  
VSelector (size_type reserved=0)  
Default constructor.  
void  swap (VSelector &s) 
Swap the underlying representation with the argument.  
WeightType  insert (WeightType d, const T &t) 
Insert an object given a probability for this object. More...  
WeightType  reweight (WeightType, const T &) 
Reweight an object previously inserted giving it a new weight. More...  
WeightType  erase (const T &) 
Erase an object, previously inserted. More...  
void  replace (const T &told, const T &tnew) 
Replace all occurencies of told with tnew without changing the probability for the entry.  
T &  select (double rnd, double *remainder=0) 
Select an object randomly. More...  
T &  operator[] (double rnd) 
Selct an object randomly. More...  
const T &  select (double rnd, double *remainder=0) const 
Selct an object randomly. More...  
const T &  operator[] (double rnd) const 
Selct an object randomly. More...  
template<typename RNDGEN >  
T &  select (RNDGEN &rnd) 
Selct an object randomly. More...  
template<typename RNDGEN >  
const T &  select (RNDGEN &rnd) const 
Selct an object randomly. More...  
WeightType  sum () const 
Return the sum of probabilities of the objects inserted. More...  
const_iterator  begin () const 
Access to the begin() iterator of the underlying vector of objects.  
const_iterator  end () const 
Access to the end() iterator in the underlying vector of objects.  
bool  empty () const 
Returns true if the VSelector is empty.  
size_type  size () const 
Returns the number of objects in the selector.  
void  reserve (size_type reserved) 
Allocate space for a number of objects in the underlying vectors.  
void  clear () 
Erases all objects.  
template<typename OStream >  
void  output (OStream &) const 
Output to a stream.  
template<typename IStream >  
void  input (IStream &) 
Input from a stream.  
Protected Member Functions  
size_type  iselect (double rnd, double *remainder) const 
Internal selection engine.  
Private Attributes  
WeightVector  theSums 
The vector of accumulated weights for the objects in the selector.  
WeightVector  theWeights 
The vector of weights for the objects in the selector.  
ObjectVector  theObjects 
The vector of objects in the selector.  
WeightType  theSum 
The sum of all weights.  
VSelector is a templated class for storing objects associated with probabilities in a way such that, given a flat random number between 0 and 1, an object can be selected according to its relative probability.
Internally, the objects of class T
are stored in a vector parallel to a vector of the probability of the corresponding object plus the accumulated sum of probabilities of all objects before the current one in the vector. This allows for fast retreival of an object according to its probability. Where fast means that the time increases as a logarithm of the number of objects in the selector.
Here is an example on how to use the class:
double random();
// A random generator returning a number between 0 and 1.
class foo;
// Any class.
VSelector<foo*> bar;
// A selector.
foo f1, f2;
bar.insert(0.5,&f1)
// assign probability 0.5
bar.insert(0.5,&f2)
// to each of f1 and f2
foo * f = bar.select(random())
// randomly returns a pointer to f1 or f2
Definition at line 46 of file VSelector.h.
typedef ObjectVector::const_iterator ThePEG::VSelector< T, WeightType >::const_iterator 
The object vector const iterator type.
Definition at line 63 of file VSelector.h.
typedef ObjectVector::iterator ThePEG::VSelector< T, WeightType >::iterator 
The object vector iterator type.
Definition at line 60 of file VSelector.h.
typedef vector<T> ThePEG::VSelector< T, WeightType >::ObjectVector 
A vector ob objects.
Definition at line 57 of file VSelector.h.
typedef ObjectVector::size_type ThePEG::VSelector< T, WeightType >::size_type 
Size type of the underlying vector.
Definition at line 66 of file VSelector.h.
typedef vector<WeightType> ThePEG::VSelector< T, WeightType >::WeightVector 
A vector of weights.
Definition at line 51 of file VSelector.h.
typedef WeightVector::const_iterator ThePEG::VSelector< T, WeightType >::WIterator 
The weight vector iterator type.
Definition at line 54 of file VSelector.h.
WeightType ThePEG::VSelector< T, WeightType >::erase  (  const T &  ) 
Erase an object, previously inserted.
If the object had not been inserted, nothing will happen. If several copies of the object has been inserted, all will be removed removed. In all cases the sum of probabilities is returned.
Referenced by ThePEG::VSelector< pair< long, long > >::insert().

inline 
Insert an object given a probability for this object.
If the probability is zero or negative, the object will not be inserted and the probability itself is returned. Otherwise the sum of probabilities is returned.
Definition at line 94 of file VSelector.h.

inline 
Selct an object randomly.
Given a random number flatly distributed in the interval ]0,1[ Select an object according to the individual probabilities specified when they were inserted. If rnd <= 0 or if rnd >= 1 or the Selector is empty, a range_error will be thrown.
Definition at line 149 of file VSelector.h.

inline 
Selct an object randomly.
Given a random number flatly distributed in the interval ]0,1[ select an object according to the individual probabilities specified when they were inserted. If rnd <= 0 or if rnd >= 1 or the Selector is empty, a range_error will be thrown.
Definition at line 175 of file VSelector.h.
WeightType ThePEG::VSelector< T, WeightType >::reweight  (  WeightType  , 
const T &  
) 
Reweight an object previously inserted giving it a new weight.
If several equivalent objects exists, all of them will be reweighted.
Referenced by ThePEG::VSelector< pair< long, long > >::insert().

inline 
Select an object randomly.
Given a random number flatly distributed in the interval ]0,1[ Select an object according to the individual probabilities specified when they were inserted. If rnd <= 0 or if rnd >= 1 or the Selector is empty, a range_error will be thrown.
rnd  a flat random number in the interval ]0,1[ 
remainder  if nonzero the double pointed to will be set to a uniform random number in the interval ]0,1[ calculated from the fraction of rnd which was in the range of the selected object. 
Definition at line 138 of file VSelector.h.
Referenced by ThePEG::VSelector< pair< long, long > >::operator[](), and ThePEG::VSelector< pair< long, long > >::select().

inline 
Selct an object randomly.
Given a random number flatly distributed in the interval ]0,1[ Select an object according to the individual probabilities specified when they were inserted. If rnd <= 0 or if rnd >= 1 or the Selector is empty, a range_error will be thrown.
rnd  a flat random number in the interval ]0,1[ 
remainder  if nonzero the double pointed to will be set to a uniform random number in the interval ]0,1[ calculated from the fraction of rnd which was in the range of the selected object. 
Definition at line 164 of file VSelector.h.

inline 
Selct an object randomly.
Given a random number generator which generates flat random numbers in the interval ]0,1[ with the operator()()
function, select an object according to the individual probabilities specified when they were inserted. If the generated number is outside the allowed range or the Selector is empty, a range_error will be thrown. The generator should have a push_back function which will be used push back a uniform random number in the interval ]0,1[ calculated from the fraction of rnd which was in the range of the selected object.
Definition at line 192 of file VSelector.h.

inline 
Selct an object randomly.
Given a random number generator which generates flat random numbers in the interval ]0,1[ with the operator()()
function, select an object according to the individual probabilities specified when they were inserted. If the generated number is outside the allowed range or the Selector is empty, a range_error will be thrown. The generator should have a push_back function which will be used push back a uniform random number in the interval ]0,1[ calculated from the fraction of rnd which was in the range of the selected object.
Definition at line 212 of file VSelector.h.

inline 
Return the sum of probabilities of the objects inserted.
Note that probabilities specified when objects are inserted are rescaled with this number to give unit probability for 'select()'.
Definition at line 225 of file VSelector.h.