ThePEG::VSelector< T, WeightType > Class Template Reference

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>

List of all members.

Public Types

typedef vector< WeightType > WeightVector
 A vector of weights.
typedef
WeightVector::const_iterator 
WIterator
 The weight vector iterator type.
typedef vector< T > ObjectVector
 A vector ob objects.
typedef ObjectVector::iterator iterator
 The object vector iterator type.
typedef
ObjectVector::const_iterator 
const_iterator
 The object vector const iterator type.
typedef ObjectVector::size_type size_type
 Size type of the underlying vector.

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.
WeightType reweight (WeightType, const T &)
 Reweight an object previously inserted giving it a new weight.
WeightType erase (const T &)
 Erase an object, previously inserted.
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) throw (range_error)
 Select an object randomly.
T & operator[] (double rnd) throw (range_error)
 Selct an object randomly.
const T & select (double rnd, double *remainder=0) const throw (range_error)
 Selct an object randomly.
const T & operator[] (double rnd) const throw (range_error)
 Selct an object randomly.
template<typename RNDGEN>
T & select (RNDGEN &rnd) throw (range_error)
 Selct an object randomly.
template<typename RNDGEN>
const T & select (RNDGEN &rnd) const throw (range_error)
 Selct an object randomly.
WeightType sum () const
 Return the sum of probabilities of the objects inserted.
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 throw (range_error)
 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.


Detailed Description

template<typename T, typename WeightType = double>
class ThePEG::VSelector< T, WeightType >

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

See also:
Selector

Definition at line 46 of file VSelector.h.


Member Typedef Documentation

template<typename T, typename WeightType = double>
typedef vector<WeightType> ThePEG::VSelector< T, WeightType >::WeightVector

A vector of weights.

Definition at line 51 of file VSelector.h.

template<typename T, typename WeightType = double>
typedef WeightVector::const_iterator ThePEG::VSelector< T, WeightType >::WIterator

The weight vector iterator type.

Definition at line 54 of file VSelector.h.

template<typename T, typename WeightType = double>
typedef vector<T> ThePEG::VSelector< T, WeightType >::ObjectVector

A vector ob objects.

Definition at line 57 of file VSelector.h.

template<typename T, typename WeightType = double>
typedef ObjectVector::iterator ThePEG::VSelector< T, WeightType >::iterator

The object vector iterator type.

Definition at line 60 of file VSelector.h.

template<typename T, typename WeightType = double>
typedef ObjectVector::const_iterator ThePEG::VSelector< T, WeightType >::const_iterator

The object vector const iterator type.

Definition at line 63 of file VSelector.h.

template<typename T, typename WeightType = double>
typedef ObjectVector::size_type ThePEG::VSelector< T, WeightType >::size_type

Size type of the underlying vector.

Definition at line 66 of file VSelector.h.


Member Function Documentation

template<typename T, typename WeightType = double>
WeightType ThePEG::VSelector< T, WeightType >::insert ( WeightType  d,
const T &  t 
) [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.

template<typename T, typename WeightType = double>
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.

template<typename T, typename WeightType = double>
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.

template<typename T, typename WeightType = double>
T& ThePEG::VSelector< T, WeightType >::select ( double  rnd,
double *  remainder = 0 
) throw (range_error) [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.

Parameters:
rnd a flat random number in the interval ]0,1[
remainder if non-zero 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< int, int > >::operator[](), and ThePEG::VSelector< pair< int, int > >::select().

template<typename T, typename WeightType = double>
T& ThePEG::VSelector< T, WeightType >::operator[] ( double  rnd  )  throw (range_error) [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.

template<typename T, typename WeightType = double>
const T& ThePEG::VSelector< T, WeightType >::select ( double  rnd,
double *  remainder = 0 
) const throw (range_error) [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.

Parameters:
rnd a flat random number in the interval ]0,1[
remainder if non-zero 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.

template<typename T, typename WeightType = double>
const T& ThePEG::VSelector< T, WeightType >::operator[] ( double  rnd  )  const throw (range_error) [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.

template<typename T, typename WeightType = double>
template<typename RNDGEN>
T& ThePEG::VSelector< T, WeightType >::select ( RNDGEN &  rnd  )  throw (range_error) [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.

template<typename T, typename WeightType = double>
template<typename RNDGEN>
const T& ThePEG::VSelector< T, WeightType >::select ( RNDGEN &  rnd  )  const throw (range_error) [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.

template<typename T, typename WeightType = double>
WeightType ThePEG::VSelector< T, WeightType >::sum (  )  const [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.


The documentation for this class was generated from the following file:

Generated on Thu Apr 29 12:43:25 2010 for ThePEG by  doxygen 1.5.5