diff options
| author | Andrew Guschin <guschin@altlinux.org> | 2024-04-01 00:47:17 +0400 |
|---|---|---|
| committer | Andrew Guschin <guschin@altlinux.org> | 2024-04-01 00:52:51 +0400 |
| commit | 950c3cfe266453a2a723c0d490baf1cd330a9c1e (patch) | |
| tree | 5216eaad2730c631678a1eac8924bfd6a48dedd9 /graph-checker/src/geng.rs | |
| parent | f7aa97e10a2fbddb76e1893b7deb193ad56e7192 (diff) | |
feat: replace stdin with geng-generator
Diffstat (limited to 'graph-checker/src/geng.rs')
| -rw-r--r-- | graph-checker/src/geng.rs | 43 |
1 files changed, 31 insertions, 12 deletions
diff --git a/graph-checker/src/geng.rs b/graph-checker/src/geng.rs index a998b28..872c6ec 100644 --- a/graph-checker/src/geng.rs +++ b/graph-checker/src/geng.rs @@ -1,6 +1,9 @@ +use std::ffi::{c_char, CStr}; use std::iter::Iterator; use std::ptr; +use crate::graph::Graph; + #[allow(non_camel_case_types)] enum geng_iterator {} @@ -12,13 +15,15 @@ extern "C" { ); fn geng_iterator_next(iter: *const geng_iterator, g: *mut u32) -> bool; fn geng_iterator_destroy(iter: *const geng_iterator); - fn printgraph(g: *const u32, n: usize); + fn fill_matrix(g: *const u32, n: usize, mat: *mut *mut u32); + fn ntog6(g: *const u32, m: usize, n: usize) -> *const c_char; } -fn print_graph(g: Vec<u32>, n: usize) { - unsafe { - printgraph(g.as_ptr(), n); - } +pub fn get_g6(g: Vec<u32>, n: usize) -> String { + let s = unsafe { ntog6(g.as_ptr(), 1, n) }; + // TODO: free allocated s? ^ + let s = unsafe { CStr::from_ptr(s) }; + s.to_str().unwrap().trim().to_string() } pub struct GengIterator { @@ -27,7 +32,7 @@ pub struct GengIterator { } impl GengIterator { - fn new(n: usize) -> GengIterator { + pub fn new(n: usize) -> GengIterator { let iter = unsafe { let iter: *mut geng_iterator = ptr::null_mut(); geng_iterator_create(&iter, n, 10000); @@ -38,17 +43,31 @@ impl GengIterator { } impl Iterator for &GengIterator { - type Item = Vec<u32>; + type Item = Graph; fn next(&mut self) -> Option<Self::Item> { let mut g = vec![0; self.size]; - let res; - unsafe { + let res = unsafe { let ptr: *const geng_iterator = &*self.iter; - res = geng_iterator_next(ptr, g.as_mut_ptr()) - } + geng_iterator_next(ptr, g.as_mut_ptr()) + }; if res { - Some(g) + let mut mat = vec![vec![0; self.size]; self.size]; + unsafe { + fill_matrix( + g.as_ptr(), + self.size, + mat.iter_mut() + .map(Vec::as_mut_ptr) + .collect::<Vec<*mut u32>>() + .as_mut_ptr(), + ); + } + + Some(Graph { + size: self.size, + matrix: mat, + }) } else { None } |