1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
use ffi;
use enums;
use c_vec::CSlice;
pub struct QRng {
q: *mut ffi::gsl_qrng,
data: CSlice<i8>
}
impl QRng {
pub fn new(t: &QRngType, d: u32) -> Option<QRng> {
let tmp = unsafe { ffi::gsl_qrng_alloc(t.t, d) };
if tmp.is_null() {
None
} else {
Some(QRng {
q: tmp,
data: unsafe { CSlice::new(tmp as *mut i8, 0) }
})
}
}
pub fn init(&mut self) {
unsafe { ffi::gsl_qrng_init(self.q) }
}
pub fn get(&self, x: &mut [f64]) -> enums::Value {
unsafe { ffi::gsl_qrng_get(self.q, x.as_mut_ptr()) }
}
pub fn name(&self) -> Option<String> {
let tmp = unsafe { ffi::gsl_qrng_name(self.q) };
if tmp.is_null() {
None
} else {
unsafe { Some(String::from_utf8_lossy(::std::ffi::CStr::from_ptr(tmp).to_bytes()).to_string()) }
}
}
pub fn size(&self) -> usize {
unsafe { ffi::gsl_qrng_size(self.q) }
}
pub fn state<'r>(&'r mut self) -> &'r mut [i8] {
let tmp = unsafe { ffi::gsl_qrng_state(self.q) };
if !tmp.is_null() {
self.data = unsafe { CSlice::new(tmp as *mut i8, self.size() as usize) };
}
self.data.as_mut()
}
pub fn copy(&self, dest: &mut QRng) -> enums::Value {
unsafe { ffi::gsl_qrng_memcpy(dest.q, self.q) }
}
}
impl Clone for QRng {
fn clone(&self) -> QRng {
unsafe { ffi::FFI::wrap(ffi::gsl_qrng_clone(self.q)) }
}
}
impl Drop for QRng {
fn drop(&mut self) {
unsafe { ffi::gsl_qrng_free(self.q) };
self.q = ::std::ptr::null_mut();
}
}
impl ffi::FFI<ffi::gsl_qrng> for QRng {
fn wrap(q: *mut ffi::gsl_qrng) -> QRng {
QRng {
q: q,
data: unsafe { CSlice::new(q as *mut i8, 0) }
}
}
fn soft_wrap(q: *mut ffi::gsl_qrng) -> QRng {
Self::wrap(q)
}
fn unwrap_shared(q: &QRng) -> *const ffi::gsl_qrng {
q.q as *const _
}
fn unwrap_unique(q: &mut QRng) -> *mut ffi::gsl_qrng {
q.q
}
}
#[derive(Clone, Copy)]
pub struct QRngType {
t: *const ffi::gsl_qrng_type
}
impl QRngType {
pub fn niederreiter_2() -> QRngType {
unsafe {
QRngType {
t: ffi::gsl_qrng_niederreiter_2
}
}
}
pub fn sobol() -> QRngType {
unsafe {
QRngType {
t: ffi::gsl_qrng_sobol
}
}
}
pub fn halton() -> QRngType {
unsafe {
QRngType {
t: ffi::gsl_qrng_halton
}
}
}
pub fn reversehalton() -> QRngType {
unsafe {
QRngType {
t: ffi::gsl_qrng_reversehalton
}
}
}
}
impl ffi::FFI<ffi::gsl_qrng_type> for QRngType {
fn wrap(t: *mut ffi::gsl_qrng_type) -> QRngType {
QRngType {
t: t
}
}
fn soft_wrap(t: *mut ffi::gsl_qrng_type) -> QRngType {
Self::wrap(t)
}
fn unwrap_shared(t: &QRngType) -> *const ffi::gsl_qrng_type {
t.t as *const ffi::gsl_qrng_type
}
fn unwrap_unique(t: &mut QRngType) -> *mut ffi::gsl_qrng_type {
t.t as *mut ffi::gsl_qrng_type
}
}