thepeg is hosted by Hepforge, IPPP Durham
ThePEG 2.3.0
Selector.h
1// -*- C++ -*-
2//
3// Selector.h is a part of ThePEG - Toolkit for HEP Event Generation
4// Copyright (C) 1999-2019 Leif Lonnblad
5//
6// ThePEG is licenced under version 3 of the GPL, see COPYING for details.
7// Please respect the MCnet academic guidelines, see GUIDELINES for details.
8//
9#ifndef ThePEG_Selector_H
10#define ThePEG_Selector_H
11// This is the declaration of the Selector class.
12
14#include <stdexcept>
15#include <algorithm>
16#include <stdexcept>
17
18namespace ThePEG {
19
45template <typename T, typename WeightType = double>
46class Selector {
47
48public:
49
51 typedef map<WeightType, T, less<WeightType> > MapType;
52
54 typedef typename MapType::const_iterator const_iterator;
55
57 typedef typename MapType::iterator iterator;
58
60 typedef typename MapType::size_type size_type;
61
62public:
63
67 Selector() : theSum(WeightType()) {}
68
72 void swap(Selector & s)
73 {
74 theMap.swap(s.theMap);
75 std::swap(theSum, s.theSum);
76 }
77
84 WeightType insert(WeightType d, const T & t) {
85 typedef typename MapType::value_type value_type;
86 WeightType newSum = theSum + d;
87 if ( newSum <= theSum ) return d;
88 theMap.insert(theMap.end(), value_type((theSum = newSum), t));
89 return theSum;
90 }
91
97 WeightType reweight(WeightType d, const T & t)
98 {
99 erase(t);
100 return insert(d, t);
101 }
102
109 WeightType erase(const T &);
110
115 void replace(const T & oldObject, const T & newObject) {
116 for ( iterator it = theMap.begin(); it != theMap.end(); ++it )
117 if ( it->second == oldObject ) it->second = newObject;
118 }
119
131 T & select(double rnd, double * remainder = 0);
132
140 T & operator[](double rnd) { return select(rnd); }
141
153 const T & select(double rnd, double * remainder = 0) const;
154
162 const T & operator[](double rnd) const { return select(rnd); }
163
176 template <typename RNDGEN>
177 T & select(RNDGEN & rnd) {
178 double rem = 0.0;
179 T & t = select(rnd(), &rem);
180 rnd.push_back(rem);
181 return t;
182 }
183
196 template <typename RNDGEN>
197 const T & select(RNDGEN & rnd) const {
198 double rem = 0.0;
199 const T & t = select(rnd(), &rem);
200 rnd.push_back(rem);
201 return t;
202 }
203
210 WeightType sum() const { return theSum; }
211
218 const_iterator begin() const { return theMap.begin(); }
219
224 const_iterator end() const { return theMap.end(); }
225
229 bool empty() const { return theMap.empty(); }
230
234 size_type size() const { return theMap.size(); }
235
239 void clear() { theMap.clear(); theSum = WeightType(); }
240
244 template <typename OStream>
245 void output(OStream &, DimensionT) const;
246
250 template <typename IStream>
251 void input(IStream &, DimensionT);
252
256 template <typename OStream>
257 void output(OStream &, StandardT) const;
258
262 template <typename IStream>
263 void input(IStream &, StandardT);
264
265private:
266
271
275 WeightType theSum;
276
277};
278
282template <typename OStream, typename T, typename WeightType>
283OStream & operator<<(OStream & os, const Selector<T,WeightType> & s)
284{
285 s.output(os, typename TypeTraits<WeightType>::DimType());
286 return os;
287}
288
292template <typename IStream, typename T, typename WeightType>
293IStream & operator>>(IStream & is, Selector<T,WeightType> & s)
294{
295 s.input(is, typename TypeTraits<WeightType>::DimType());
296 return is;
297}
298
299
300}
301
302#ifndef ThePEG_TEMPLATES_IN_CC_FILE
303#include "Selector.tcc"
304#endif
305
306#endif /* ThePEG_Selector_H */
This is the main config header file for ThePEG.
Selector is a templated class for storing objects associated with probabilities in a way such that,...
Definition: Selector.h:46
MapType theMap
The underlying map relating sums of probabilities to inserted objects.
Definition: Selector.h:270
void output(OStream &, DimensionT) const
Output to a stream for dimensionful units.
void output(OStream &, StandardT) const
Output to a stream.
T & select(double rnd, double *remainder=0)
Select an object randomly.
WeightType sum() const
Return the sum of probabilities of the objects inserted.
Definition: Selector.h:210
WeightType erase(const T &)
Erase an object, previously inserted.
const_iterator end() const
Access to the end() iterator in the underlying map.
Definition: Selector.h:224
const_iterator begin() const
Access to the begin() iterator of the underlying map.
Definition: Selector.h:218
MapType::iterator iterator
Iterator corresponding to the underlying map.
Definition: Selector.h:57
void input(IStream &, StandardT)
Input from a stream.
map< WeightType, T, less< WeightType > > MapType
Map doubles to objects.
Definition: Selector.h:51
WeightType insert(WeightType d, const T &t)
Insert an object given a probability for this object.
Definition: Selector.h:84
void replace(const T &oldObject, const T &newObject)
Replace all occurencies of oldObject with newObject without changing the probability for the entry.
Definition: Selector.h:115
const T & operator[](double rnd) const
Selct an object randomly.
Definition: Selector.h:162
T & operator[](double rnd)
Selct an object randomly.
Definition: Selector.h:140
WeightType reweight(WeightType d, const T &t)
Reweight an object previously inserted giving it a new weight.
Definition: Selector.h:97
bool empty() const
Returns true if the Selector is empty.
Definition: Selector.h:229
size_type size() const
Returns the number of objects in the selector.
Definition: Selector.h:234
T & select(RNDGEN &rnd)
Selct an object randomly.
Definition: Selector.h:177
WeightType theSum
The sum of all probabilities assicialted with inserted objects.
Definition: Selector.h:275
void swap(Selector &s)
Swap the underlying representation with the argument.
Definition: Selector.h:72
MapType::const_iterator const_iterator
Iterator corresponding to the underlying map.
Definition: Selector.h:54
void clear()
Erases all objects.
Definition: Selector.h:239
void input(IStream &, DimensionT)
Input from a stream for dimensionful units.
Selector()
Default constructor.
Definition: Selector.h:67
const T & select(RNDGEN &rnd) const
Selct an object randomly.
Definition: Selector.h:197
const T & select(double rnd, double *remainder=0) const
Selct an object randomly.
MapType::size_type size_type
Size type of the underlying map.
Definition: Selector.h:60
This is the main namespace within which all identifiers in ThePEG are declared.
Definition: FactoryBase.h:28
vector< T > & operator>>(vector< T > &tv, U &u)
Overload the right shift operator for vector to pop objects from a vector.
Definition: Containers.h:192
vector< T > & operator<<(vector< T > &tv, const U &u)
Overload the left shift operator for vector to push_back objects to a vector.
Definition: Containers.h:179
void swap(ThePEG::Pointer::RCPtr< T > &t1, ThePEG::Pointer::RCPtr< T > &t2)
Specialization of std::swap to avoid unnecessary (in/de)crements of the reference count.
Definition: RCPtr.h:1154
Conversion between integers and types.
Definition: TemplateTools.h:24