1 /* Author: G. Jungman
2 */
3 #include "gsl__config.h"
4 #include <stdlib.h>
5 #include <string.h>
6 #include "gsl_errno.h"
7 #include "gsl_qrng.h"
8
9
10 gsl_qrng *
gsl_qrng_alloc(const gsl_qrng_type * T,unsigned int dimension)11 gsl_qrng_alloc (const gsl_qrng_type * T, unsigned int dimension)
12 {
13
14 gsl_qrng * q = (gsl_qrng *) malloc (sizeof (gsl_qrng));
15
16 if (q == 0)
17 {
18 GSL_ERROR_VAL ("allocation failed for qrng struct",
19 GSL_ENOMEM, 0);
20 };
21
22 q->dimension = dimension;
23 q->state_size = T->state_size(dimension);
24 q->state = malloc (q->state_size);
25
26 if (q->state == 0)
27 {
28 free (q);
29 GSL_ERROR_VAL ("allocation failed for qrng state",
30 GSL_ENOMEM, 0);
31 };
32
33 q->type = T;
34
35 T->init_state(q->state, q->dimension);
36
37 return q;
38 }
39
40 void
gsl_qrng_init(gsl_qrng * q)41 gsl_qrng_init (gsl_qrng * q)
42 {
43 (q->type->init_state) (q->state, q->dimension);
44 }
45
46 int
gsl_qrng_memcpy(gsl_qrng * dest,const gsl_qrng * src)47 gsl_qrng_memcpy (gsl_qrng * dest, const gsl_qrng * src)
48 {
49 if (dest->type != src->type)
50 {
51 GSL_ERROR ("generators must be of the same type", GSL_EINVAL);
52 }
53
54 dest->dimension = src->dimension;
55 dest->state_size = src->state_size;
56 memcpy (dest->state, src->state, src->state_size);
57
58 return GSL_SUCCESS;
59 }
60
61
62 gsl_qrng *
gsl_qrng_clone(const gsl_qrng * q)63 gsl_qrng_clone (const gsl_qrng * q)
64 {
65 gsl_qrng * r = (gsl_qrng *) malloc (sizeof (gsl_qrng));
66
67 if (r == 0)
68 {
69 GSL_ERROR_VAL ("failed to allocate space for rng struct",
70 GSL_ENOMEM, 0);
71 };
72
73 r->dimension = q->dimension;
74 r->state_size = q->state_size;
75 r->state = malloc (r->state_size);
76
77 if (r->state == 0)
78 {
79 free (r);
80 GSL_ERROR_VAL ("failed to allocate space for rng state",
81 GSL_ENOMEM, 0);
82 };
83
84 r->type = q->type;
85
86 memcpy (r->state, q->state, q->state_size);
87
88 return r;
89 }
90
91 #ifndef HIDE_INLINE_STATIC
92 int
gsl_qrng_get(const gsl_qrng * q,double x[])93 gsl_qrng_get (const gsl_qrng * q, double x[])
94 {
95 return (q->type->get) (q->state, q->dimension, x);
96 }
97 #endif
98
99 const char *
gsl_qrng_name(const gsl_qrng * q)100 gsl_qrng_name (const gsl_qrng * q)
101 {
102 return q->type->name;
103 }
104
105
106 size_t
gsl_qrng_size(const gsl_qrng * q)107 gsl_qrng_size (const gsl_qrng * q)
108 {
109 return q->state_size;
110 }
111
112
113 void *
gsl_qrng_state(const gsl_qrng * q)114 gsl_qrng_state (const gsl_qrng * q)
115 {
116 return q->state;
117 }
118
119
120 void
gsl_qrng_free(gsl_qrng * q)121 gsl_qrng_free (gsl_qrng * q)
122 {
123 if(q != 0) {
124 if(q->state != 0) free (q->state);
125 free (q);
126 }
127 }
128