ccgsl 2.7.2
C++wrappersforGnuScientificLibrary
qrng.hpp
Go to the documentation of this file.
1/*
2 * $Id: qrng.hpp 293 2012-12-17 20:27:36Z jdl3 $
3 * Copyright (C) 2012, 2020 John D Lamb
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20#ifndef CCGSL_QRNG_HPP
21#define CCGSL_QRNG_HPP
22
23#include<new>
24#include<gsl/gsl_qrng.h>
25#include"exception.hpp"
26
27namespace gsl {
31 class qrng {
32 public:
36 typedef gsl_qrng_type type;
41 ccgsl_pointer = 0;
42 count = 0; // initially nullptr will do
43 }
44 // Refines random access container
45 // Refines assignable
51 explicit qrng( type const* T, unsigned int const dimension ){
52 ccgsl_pointer = gsl_qrng_alloc( T, dimension );
53 // just plausibly we could allocate qrng but not count
54 try { count = new size_t; } catch( std::bad_alloc& e ){
55 // try to tidy up before rethrowing
56 gsl_qrng_free( ccgsl_pointer );
57 throw e;
58 }
59 *count = 1; // initially there is just one reference to ccgsl_pointer
60 }
67 explicit qrng( gsl_qrng* v ){
68 ccgsl_pointer = v;
69 // just plausibly we could fail to allocate count: no further action needed.
70 count = new size_t;
71 *count = 1; // initially there is just one reference to ccgsl_pointer
72 }
73 // copy constructor
79 count = v.count; if( count != 0 ) ++*count; }
80 // assignment operator
85 qrng& operator=( qrng const& v ){
86 // first, possibly delete anything pointed to by this
87 if( count == 0 or --*count == 0 ){
88 if( ccgsl_pointer != 0 ) gsl_qrng_free( ccgsl_pointer );
89 delete count;
90 } // Then copy
91 ccgsl_pointer = v.ccgsl_pointer; count = v.count; if( count != 0 ) ++*count; return *this;
92 }
93 // destructor
98 if( count == 0 or --*count == 0 ){
99 // could have allocated null pointer
100 if( ccgsl_pointer != 0 ) gsl_qrng_free( ccgsl_pointer );
101 delete count;
102 }
103 }
104#ifdef __GXX_EXPERIMENTAL_CXX0X__
109 qrng( qrng&& v ) : ccgsl_pointer( v.ccgsl_pointer ), count( nullptr ){
110 std::swap( count, v.count );
111 v.ccgsl_pointer = nullptr;
112 }
119 qrng( std::move( v ) ).swap( *this );
120 return *this;
121 }
122#endif
123 // Refines equality comparable
124 // == operator
131 bool operator==( qrng const& v ) const { return ccgsl_pointer == v.ccgsl_pointer; }
132 // != operator
139 bool operator!=( qrng const& v ) const { return not operator==( v ); }
140 // Refines forward container
141 // Refines less than comparable
142 // operator<
151 bool operator<( qrng const& v ) const { return ccgsl_pointer < v.ccgsl_pointer; }
152 // operator>
161 bool operator>( qrng const& v ) const { return ccgsl_pointer > v.ccgsl_pointer; }
162 // operator<=
171 bool operator<=( qrng const& v ) const { return ccgsl_pointer <= v.ccgsl_pointer; }
172 // operator>=
181 bool operator>=( qrng const& v ) const { return ccgsl_pointer >= v.ccgsl_pointer; }
186 bool empty() const { return ccgsl_pointer == 0; }
187 // swap() --- should work even if sizes don't match
193 void swap( qrng& v ){
194 std::swap( ccgsl_pointer, v.ccgsl_pointer );
195 std::swap( count, v.count );
196 }
197 private:
201 gsl_qrng* ccgsl_pointer;
205 size_t* count;
206 public:
207 // shared reference functions
212 gsl_qrng* get() const { return ccgsl_pointer; }
218 bool unique() const { return count != 0 and *count == 1; }
223 size_t use_count() const { return count == 0 ? 0 : *count; }
229#ifdef __GXX_EXPERIMENTAL_CXX0X__
230 explicit
231#endif
232 operator bool() const { return ccgsl_pointer != 0; }
233
239 int memcpy( qrng const& src ){ return gsl_qrng_memcpy( get(), src.get() ); }
240
245 qrng clone() const { return qrng( gsl_qrng_clone( get() ) ); }
246
250 void init(){ gsl_qrng_init( get() ); }
251
256 char const* name() const { return gsl_qrng_name( get() ); }
257
262 size_t size() const { return gsl_qrng_size( get() ); }
263
268 void* state() const { return gsl_qrng_state( get() ); }
269
270#ifndef DOXYGEN_SKIP
276 int get( double x[] ) const { return gsl_qrng_get( get(), x ); }
277#endif
284 template<typename X>
285 int get( X& x ) const {
286 if(x.size() < get()->dimension)
287 gsl_error( "x must have size at least as big as dimension of qrng.",
288 __FILE__, __LINE__, exception::GSL_EBADLEN );
289 return gsl_qrng_get( get(), x.data() );
290 }
291 // Types
296 static type const* niederreiter_2(){ return gsl_qrng_niederreiter_2; }
301 static type const* sobol(){ return gsl_qrng_sobol; }
306 static type const* halton(){ return gsl_qrng_halton; }
311 static type const* reversehalton(){ return gsl_qrng_reversehalton; }
312
313 };
314}
315#endif
@ GSL_EBADLEN
matrix, vector lengths are not conformant
Definition: exception.hpp:490
Quasi random sequences.
Definition: qrng.hpp:31
size_t * count
The shared reference count.
Definition: qrng.hpp:205
qrng(type const *T, unsigned int const dimension)
The default constructor creates a new qrng with n elements.
Definition: qrng.hpp:51
bool operator>=(qrng const &v) const
A container needs to define an ordering for sorting.
Definition: qrng.hpp:181
static type const * halton()
Quasi random number sequence static type.
Definition: qrng.hpp:306
qrng()
The default constructor is only really useful for assigning to.
Definition: qrng.hpp:40
static type const * niederreiter_2()
Quasi random number sequence static type.
Definition: qrng.hpp:296
void * state() const
C++ version of gsl_qrng_state().
Definition: qrng.hpp:268
bool operator<(qrng const &v) const
A container needs to define an ordering for sorting.
Definition: qrng.hpp:151
int memcpy(qrng const &src)
C++ version of gsl_qrng_memcpy().
Definition: qrng.hpp:239
static type const * reversehalton()
Quasi random number sequence static type.
Definition: qrng.hpp:311
bool operator>(qrng const &v) const
A container needs to define an ordering for sorting.
Definition: qrng.hpp:161
gsl_qrng_type type
Convenient typedef.
Definition: qrng.hpp:36
~qrng()
The destructor only deletes the pointers if count reaches zero.
Definition: qrng.hpp:97
gsl_qrng * get() const
Get the gsl_qrng.
Definition: qrng.hpp:212
qrng(gsl_qrng *v)
Could construct from a gsl_qrng.
Definition: qrng.hpp:67
bool empty() const
Find if the qrng is empty.
Definition: qrng.hpp:186
qrng(qrng const &v)
The copy constructor.
Definition: qrng.hpp:78
qrng & operator=(qrng &&v)
Move operator.
Definition: qrng.hpp:118
static type const * sobol()
Quasi random number sequence static type.
Definition: qrng.hpp:301
char const * name() const
C++ version of gsl_qrng_name().
Definition: qrng.hpp:256
gsl_qrng * ccgsl_pointer
The shared pointer.
Definition: qrng.hpp:201
bool operator<=(qrng const &v) const
A container needs to define an ordering for sorting.
Definition: qrng.hpp:171
qrng clone() const
C++ version of gsl_qrng_clone().
Definition: qrng.hpp:245
void swap(qrng &v)
Swap two qrng objects.
Definition: qrng.hpp:193
qrng(qrng &&v)
Move constructor.
Definition: qrng.hpp:109
int get(X &x) const
C++ version of gsl_qrng_get().
Definition: qrng.hpp:285
size_t use_count() const
Find how many qrng objects share this pointer.
Definition: qrng.hpp:223
bool unique() const
Find if this is the only object sharing the gsl_qrng.
Definition: qrng.hpp:218
void init()
C++ version of gsl_qrng_init().
Definition: qrng.hpp:250
bool operator==(qrng const &v) const
Two qrng are identically equal if their elements are identical.
Definition: qrng.hpp:131
qrng & operator=(qrng const &v)
The assignment operator.
Definition: qrng.hpp:85
bool operator!=(qrng const &v) const
Two qrng are different if their elements are not identical.
Definition: qrng.hpp:139
size_t size() const
C++ version of gsl_qrng_size().
Definition: qrng.hpp:262
The gsl package creates an interface to the GNU Scientific Library for C++.
Definition: blas.hpp:34