Logo ROOT   6.13/01
Reference Guide
GSLQRngWrapper.h
Go to the documentation of this file.
1 // @(#)root/mathmore:$Id$
2 // Author: L. Moneta Fri Aug 24 17:20:45 2007
3 
4 /**********************************************************************
5  * *
6  * Copyright (c) 2006 LCG ROOT Math Team, CERN/PH-SFT *
7  * *
8  * *
9  **********************************************************************/
10 
11 // Header file for class GSLQRngWrapper
12 
13 #ifndef ROOT_Math_GSLQRngWrapper
14 #define ROOT_Math_GSLQRngWrapper
15 
16 #include "gsl/gsl_qrng.h"
17 
18 namespace ROOT {
19 
20  namespace Math {
21 
22 
23 /**
24  GSLQRngWrapper class to wrap gsl_qrng structure
25 */
27 
28 public:
29 
30 
31  /**
32  Default constructor
33  */
35  fOwn(0),
36  fRng(0),
37  fRngType(0)
38  {
39  }
40 
41  /**
42  Constructor with type
43  */
44  GSLQRngWrapper(const gsl_qrng_type * type) :
45  fOwn(1),
46  fRng(0),
47  fRngType(type)
48  {
49  }
50 
51  /**
52  construct from an existing gsl_qrng
53  it is managed externally - so will not be deleted at the end
54  */
55  GSLQRngWrapper(const gsl_qrng * r ) :
56  fOwn(0),
57  fRngType(0)
58  {
59  fRng = const_cast<gsl_qrng *>(r);
60  }
61 
62  /**
63  Copy constructor - clone the GSL object and manage it
64  */
66  fOwn(1),
68  {
69  fRng = gsl_qrng_clone(r.fRng);
70  }
71 
72  /**
73  Assignment operator
74  */
76  if (this == &rhs) return *this; // time saving self-test
77  fRngType = rhs.fRngType;
78  int iret = 0;
79  if (fRngType == rhs.fRngType) {
80  iret = gsl_qrng_memcpy(fRng, rhs.fRng);
81  if (!iret) return *this;
82  }
83  // otherwise create a new copy
84  if (fOwn) Free();
85  fRng = gsl_qrng_clone(rhs.fRng);
86  fOwn = true;
87  return *this;
88  }
89 
90  /**
91  Destructor (free the rng if not done before)
92  */
94  if (fOwn) Free();
95  }
96 
97  void Allocate(unsigned int dimension) {
98  if (fRngType == 0) SetDefaultType();
99  if (fRng != 0 && fOwn) Free();
100  fRng = gsl_qrng_alloc( fRngType, dimension );
101  //std::cout << " allocate " << fRng << std::endl;
102  }
103 
104  void Free() {
105  if (!fOwn) return; // no operation if pointer is not own
106  //std::cout << "free gslrng " << fRngType << " " << fRng << std::endl;
107  if (fRng != 0) gsl_qrng_free(fRng);
108  fRng = 0;
109  }
110 
111 
112  void SetType(const gsl_qrng_type * type) {
113  fRngType = type;
114  }
115 
116  void SetDefaultType() {
117  // construct default engine (Sobol)
118  fRngType = gsl_qrng_sobol;
119  }
120 
121 
122  unsigned int Dimension() const { return fRng->dimension; }
123 
124  inline gsl_qrng * Rng() { return fRng; }
125 
126  inline const gsl_qrng * Rng() const { return fRng; }
127 
128 
129 
130 private:
131 
132  bool fOwn; // ownership of contained pointer
133  gsl_qrng * fRng;
134  const gsl_qrng_type * fRngType;
135 };
136 
137 
138  } // end namespace Math
139 
140 } // end namespace ROOT
141 
142 
143 #endif /* ROOT_Math_GSLQRngWrapper */
Namespace for new ROOT classes and functions.
Definition: TFoamSampler.h:19
void Allocate(unsigned int dimension)
GSLQRngWrapper(GSLQRngWrapper &r)
Copy constructor - clone the GSL object and manage it.
GSLQRngWrapper()
Default constructor.
GSLQRngWrapper(const gsl_qrng_type *type)
Constructor with type.
GSLQRngWrapper & operator=(const GSLQRngWrapper &rhs)
Assignment operator.
void SetType(const gsl_qrng_type *type)
~GSLQRngWrapper()
Destructor (free the rng if not done before)
GSLQRngWrapper(const gsl_qrng *r)
construct from an existing gsl_qrng it is managed externally - so will not be deleted at the end ...
unsigned int Dimension() const
Namespace for new Math classes and functions.
GSLQRngWrapper class to wrap gsl_qrng structure.
const gsl_qrng_type * fRngType
const gsl_qrng * Rng() const