-
Notifications
You must be signed in to change notification settings - Fork 80
/
nodegraph.rs
181 lines (144 loc) · 3.47 KB
/
nodegraph.rs
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
use std::ffi::CStr;
use std::os::raw::c_char;
use std::slice;
use niffler::get_input;
use crate::sketch::minhash::KmerMinHash;
use crate::sketch::nodegraph::Nodegraph;
#[no_mangle]
pub unsafe extern "C" fn nodegraph_new() -> *mut Nodegraph {
Box::into_raw(Box::new(Nodegraph::default())) as _
}
#[no_mangle]
pub unsafe extern "C" fn nodegraph_free(ptr: *mut Nodegraph) {
if ptr.is_null() {
return;
}
Box::from_raw(ptr);
}
#[no_mangle]
pub unsafe extern "C" fn nodegraph_with_tables(
ksize: usize,
starting_size: usize,
n_tables: usize,
) -> *mut Nodegraph {
Box::into_raw(Box::new(Nodegraph::with_tables(
starting_size,
n_tables,
ksize,
))) as _
}
#[no_mangle]
pub unsafe extern "C" fn nodegraph_count(ptr: *mut Nodegraph, h: u64) -> bool {
let ng = {
assert!(!ptr.is_null());
&mut *ptr
};
ng.count(h)
}
#[no_mangle]
pub unsafe extern "C" fn nodegraph_get(ptr: *mut Nodegraph, h: u64) -> usize {
let ng = {
assert!(!ptr.is_null());
&mut *ptr
};
ng.get(h)
}
#[no_mangle]
pub unsafe extern "C" fn nodegraph_expected_collisions(ptr: *mut Nodegraph) -> f64 {
let ng = {
assert!(!ptr.is_null());
&mut *ptr
};
ng.expected_collisions()
}
#[no_mangle]
pub unsafe extern "C" fn nodegraph_ksize(ptr: *mut Nodegraph) -> usize {
let ng = {
assert!(!ptr.is_null());
&mut *ptr
};
ng.ksize()
}
#[no_mangle]
pub unsafe extern "C" fn nodegraph_tablesize(ptr: *mut Nodegraph) -> usize {
let ng = {
assert!(!ptr.is_null());
&mut *ptr
};
ng.tablesize()
}
#[no_mangle]
pub unsafe extern "C" fn nodegraph_ntables(ptr: *mut Nodegraph) -> usize {
let ng = {
assert!(!ptr.is_null());
&mut *ptr
};
ng.ntables()
}
#[no_mangle]
pub unsafe extern "C" fn nodegraph_noccupied(ptr: *mut Nodegraph) -> usize {
let ng = {
assert!(!ptr.is_null());
&mut *ptr
};
ng.noccupied()
}
#[no_mangle]
pub unsafe extern "C" fn nodegraph_matches(ptr: *mut Nodegraph, mh_ptr: *mut KmerMinHash) -> usize {
let ng = {
assert!(!ptr.is_null());
&mut *ptr
};
let mh = {
assert!(!ptr.is_null());
&mut *mh_ptr
};
ng.matches(mh)
}
#[no_mangle]
pub unsafe extern "C" fn nodegraph_update(ptr: *mut Nodegraph, optr: *mut Nodegraph) {
let ng = {
assert!(!ptr.is_null());
&mut *ptr
};
let ong = {
assert!(!optr.is_null());
&mut *optr
};
ng.update(ong);
}
ffi_fn! {
unsafe fn nodegraph_from_path(filename: *const c_char) -> Result<*mut Nodegraph> {
let c_str = {
assert!(!filename.is_null());
CStr::from_ptr(filename)
};
let (mut input, _) = get_input(c_str.to_str()?)?;
let ng = Nodegraph::from_reader(&mut input)?;
Ok(Box::into_raw(Box::new(ng)))
}
}
ffi_fn! {
unsafe fn nodegraph_from_buffer(ptr: *const c_char, insize: usize) -> Result<*mut Nodegraph> {
let buf = {
assert!(!ptr.is_null());
slice::from_raw_parts(ptr as *mut u8, insize)
};
let ng = Nodegraph::from_reader(&mut &buf[..])?;
Ok(Box::into_raw(Box::new(ng)))
}
}
ffi_fn! {
unsafe fn nodegraph_save(ptr: *mut Nodegraph, filename: *const c_char) -> Result<()> {
let ng = {
assert!(!ptr.is_null());
&mut *ptr
};
let c_str = {
assert!(!filename.is_null());
CStr::from_ptr(filename)
};
ng.save(c_str.to_str()?)?;
Ok(())
}
}