thepeg is hosted by Hepforge, IPPP Durham
ThePEG 2.3.0
VSelector.h
1// -*- C++ -*-
2//
3// VSelector.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_VSelector_H
10#define ThePEG_VSelector_H
11// This is the definition of the ThePEG::VSelector class.
12
14#include <stdexcept>
15#include <algorithm>
16#include <stdexcept>
17
18namespace ThePEG {
19
20template <typename T, typename WeightType = double>
46class VSelector {
47
48public:
49
51 typedef vector<WeightType> WeightVector;
52
54 typedef typename WeightVector::const_iterator WIterator;
55
57 typedef vector<T> ObjectVector;
58
60 typedef typename ObjectVector::iterator iterator;
61
63 typedef typename ObjectVector::const_iterator const_iterator;
64
66 typedef typename ObjectVector::size_type size_type;
67
68public:
69
73 VSelector(size_type reserved = 0) : theSum()
74 {
75 reserve(reserved);
76 }
77
81 void swap(VSelector & s) {
82 theSums.swap(s.theSums);
83 theWeights.swap(s.theWeights);
84 theObjects.swap(s.theObjects);
85 std::swap(theSum, s.theSum);
86 }
87
94 WeightType insert(WeightType d, const T & t) {
95 WeightType newSum = theSum + d;
96 if ( newSum <= theSum ) return d;
97 theSums.push_back(theSum = newSum);
98 theWeights.push_back(d);
99 theObjects.push_back(t);
100 return theSum;
101 }
102
108 WeightType reweight(WeightType, const T &);
109
116 WeightType erase(const T &);
117
122 void replace(const T & told, const T & tnew) {
123 for ( iterator it = theObjects.begin(); it != theObjects.end(); ++it )
124 if ( *it == told ) *it = tnew;
125 }
126
138 T & select(double rnd, double * remainder = 0) {
139 return theObjects[iselect(rnd, remainder)];
140 }
141
149 T & operator[](double rnd) {
150 return select(rnd, 0);
151 }
152
164 const T & select(double rnd, double * remainder = 0) const {
165 return theObjects[iselect(rnd, remainder)];
166 }
167
175 const T & operator[](double rnd) const {
176 return select(rnd, 0);
177 }
178
191 template <typename RNDGEN>
192 T & select(RNDGEN & rnd) {
193 double rem = 0.0;
194 T & t = select(rnd(), &rem);
195 rnd.push_back(rem);
196 return t;
197 }
198
211 template <typename RNDGEN>
212 const T & select(RNDGEN & rnd) const {
213 double rem = 0.0;
214 const T & t = select(rnd(), &rem);
215 rnd.push_back(rem);
216 return t;
217 }
218
225 WeightType sum() const { return theSum; }
226
231 const_iterator begin() const { return theObjects.begin(); }
232
237 const_iterator end() const { return theObjects.end(); }
238
242 bool empty() const { return theObjects.empty(); }
243
247 size_type size() const { return theObjects.size(); }
248
252 void reserve(size_type reserved) {
253 theSums.reserve(reserved);
254 theWeights.reserve(reserved);
255 theObjects.reserve(reserved);
256 }
257
261 void clear() {
262 theSums.clear();
263 theWeights.clear();
264 theObjects.clear();
265 theSum = WeightType();
266 }
267
271 template <typename OStream>
272 void output(OStream &) const;
273
277 template <typename IStream>
278 void input(IStream &);
279
280protected:
281
285 size_type iselect(double rnd, double * remainder) const;
286
287private:
288
293
298
303
307 WeightType theSum;
308
309};
310
314template <typename OStream, typename T, typename WeightType>
315inline OStream & operator<<(OStream & os,
316 const VSelector<T,WeightType> & s) {
317 s.output(os);
318 return os;
319}
320
324template <typename IStream, typename T, typename WeightType>
325inline IStream & operator>>(IStream & is,
327 s.input(is);
328 return is;
329}
330
331
332}
333
334#include "VSelector.tcc"
335
336#endif /* ThePEG_VSelector_H */
This is the main config header file for ThePEG.
VSelector is a templated class for storing objects associated with probabilities in a way such that,...
Definition: VSelector.h:46
WeightVector theSums
The vector of accumulated weights for the objects in the selector.
Definition: VSelector.h:292
T & select(RNDGEN &rnd)
Selct an object randomly.
Definition: VSelector.h:192
ObjectVector::iterator iterator
The object vector iterator type.
Definition: VSelector.h:60
WeightType theSum
The sum of all weights.
Definition: VSelector.h:307
void input(IStream &)
Input from a stream.
const T & select(RNDGEN &rnd) const
Selct an object randomly.
Definition: VSelector.h:212
WeightType reweight(WeightType, const T &)
Reweight an object previously inserted giving it a new weight.
WeightType erase(const T &)
Erase an object, previously inserted.
const T & select(double rnd, double *remainder=0) const
Selct an object randomly.
Definition: VSelector.h:164
WeightVector::const_iterator WIterator
The weight vector iterator type.
Definition: VSelector.h:54
const T & operator[](double rnd) const
Selct an object randomly.
Definition: VSelector.h:175
const_iterator end() const
Access to the end() iterator in the underlying vector of objects.
Definition: VSelector.h:237
vector< T > ObjectVector
A vector ob objects.
Definition: VSelector.h:57
WeightVector theWeights
The vector of weights for the objects in the selector.
Definition: VSelector.h:297
void reserve(size_type reserved)
Allocate space for a number of objects in the underlying vectors.
Definition: VSelector.h:252
WeightType insert(WeightType d, const T &t)
Insert an object given a probability for this object.
Definition: VSelector.h:94
VSelector(size_type reserved=0)
Default constructor.
Definition: VSelector.h:73
size_type size() const
Returns the number of objects in the selector.
Definition: VSelector.h:247
WeightType sum() const
Return the sum of probabilities of the objects inserted.
Definition: VSelector.h:225
void output(OStream &) const
Output to a stream.
const_iterator begin() const
Access to the begin() iterator of the underlying vector of objects.
Definition: VSelector.h:231
void replace(const T &told, const T &tnew)
Replace all occurencies of told with tnew without changing the probability for the entry.
Definition: VSelector.h:122
T & operator[](double rnd)
Selct an object randomly.
Definition: VSelector.h:149
bool empty() const
Returns true if the VSelector is empty.
Definition: VSelector.h:242
vector< WeightType > WeightVector
A vector of weights.
Definition: VSelector.h:51
ObjectVector::const_iterator const_iterator
The object vector const iterator type.
Definition: VSelector.h:63
size_type iselect(double rnd, double *remainder) const
Internal selection engine.
ObjectVector theObjects
The vector of objects in the selector.
Definition: VSelector.h:302
void clear()
Erases all objects.
Definition: VSelector.h:261
T & select(double rnd, double *remainder=0)
Select an object randomly.
Definition: VSelector.h:138
void swap(VSelector &s)
Swap the underlying representation with the argument.
Definition: VSelector.h:81
ObjectVector::size_type size_type
Size type of the underlying vector.
Definition: VSelector.h:66
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