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
use ffi;
use enums;
use std::fmt;
use std::fmt::{Formatter, Debug};
use c_vec::CSlice;
pub struct Combination {
c: *mut ffi::gsl_combination,
data: CSlice<usize>
}
impl Combination {
pub fn new(n: usize, k: usize) -> Option<Combination> {
let tmp = unsafe { ffi::gsl_combination_alloc(n, k) };
if tmp.is_null() {
None
} else {
unsafe {
if !(*tmp).data.is_null() {
Some(Combination {
c: tmp,
data: CSlice::new((*tmp).data, (*tmp).k as usize)
})
} else {
Some(Combination {
c: tmp,
data: CSlice::new(tmp as *mut usize, 0usize)
})
}
}
}
}
pub fn new_init_first(n: usize, k: usize) -> Option<Combination> {
let tmp = unsafe { ffi::gsl_combination_calloc(n, k) };
if tmp.is_null() {
None
} else {
unsafe {
if !(*tmp).data.is_null() {
Some(Combination {
c: tmp,
data: CSlice::new((*tmp).data, (*tmp).k as usize)
})
} else {
Some(Combination {
c: tmp,
data: CSlice::new(tmp as *mut usize, 0usize)
})
}
}
}
}
pub fn init_first(&self) {
unsafe { ffi::gsl_combination_init_first(self.c) }
}
pub fn init_last(&self) {
unsafe { ffi::gsl_combination_init_last(self.c) }
}
pub fn copy(&self, dest: &Combination) -> enums::Value {
unsafe { ffi::gsl_combination_memcpy(dest.c, self.c) }
}
pub fn get(&self, i: usize) -> usize {
unsafe { ffi::gsl_combination_get(self.c, i) }
}
pub fn n(&self) -> usize {
unsafe { ffi::gsl_combination_n(self.c) }
}
pub fn k(&self) -> usize {
unsafe { ffi::gsl_combination_k(self.c) }
}
pub fn as_slice<'r>(&'r self) -> &'r [usize] {
self.data.as_ref()
}
pub fn as_mut_slice<'r>(&'r mut self) -> &'r mut [usize] {
self.data.as_mut()
}
pub fn is_valid(&self) -> enums::Value {
unsafe { ffi::gsl_combination_valid(self.c) }
}
pub fn next(&self) -> enums::Value {
unsafe { ffi::gsl_combination_next(self.c) }
}
pub fn prev(&self) -> enums::Value {
unsafe { ffi::gsl_combination_prev(self.c) }
}
}
impl Drop for Combination {
fn drop(&mut self) {
unsafe { ffi::gsl_combination_free(self.c) };
self.c = ::std::ptr::null_mut();
}
}
impl ffi::FFI<ffi::gsl_combination> for Combination {
fn wrap(c: *mut ffi::gsl_combination) -> Combination {
unsafe {
Combination {
c: c,
data: CSlice::new((*c).data, (*c).k as usize)
}
}
}
fn unwrap(c: &Combination) -> *mut ffi::gsl_combination {
c.c
}
}
impl Debug for Combination {
#[allow(unused_must_use)]
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "[");
for tmp in 0..self.data.len() {
if tmp == 0 {
write!(f, "{}", self.data.get(tmp).unwrap());
} else {
write!(f, ", {}", self.data.get(tmp).unwrap());
}
}
write!(f, "]")
}
}