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
use crate::backend::sdsl_c;
use anyhow::Result;

use crate::interface::common;

#[derive(Clone)]
pub struct Interface {
    set_to_value: extern "C" fn(common::VoidPtr, u64),
    set_to_id: extern "C" fn(common::VoidPtr),
    set_random_bits: extern "C" fn(common::VoidPtr),
    util_mod: extern "C" fn(common::VoidPtr, u64),
    bit_compress: extern "C" fn(common::VoidPtr),
    expand_width: extern "C" fn(common::VoidPtr, u8),

    _lib: std::sync::Arc<sharedlib::Lib>,
}

impl Interface {
    pub fn new(id: &str) -> Result<Self> {
        let lib = sdsl_c::LIB.clone();
        let builder = sdsl_c::FunctionBuilder::new(None, id, lib.clone());

        Ok(Self {
            set_to_value: builder.get("set_to_value")?,
            set_to_id: builder.get("set_to_id")?,
            set_random_bits: builder.get("set_random_bits")?,
            util_mod: builder.get("mod")?,
            bit_compress: builder.get("bit_compress")?,
            expand_width: builder.get("expand_width")?,

            _lib: lib.clone(),
        })
    }
}

pub trait Util {
    fn util(&self) -> &Interface;
}

pub fn set_to_value<T: Util + common::Ptr>(structure: &mut T, value: u64) {
    (structure.util().set_to_value)(*structure.ptr(), value)
}

pub fn set_to_id<T: Util + common::Ptr>(structure: &mut T) {
    (structure.util().set_to_id)(*structure.ptr())
}

pub fn set_random_bits<T: Util + common::Ptr>(structure: &mut T) {
    (structure.util().set_random_bits)(*structure.ptr())
}

pub fn modulus<T: Util + common::Ptr>(structure: &mut T, value: u64) {
    (structure.util().util_mod)(*structure.ptr(), value)
}

pub fn bit_compress<T: Util + common::Ptr>(structure: &mut T) {
    (structure.util().bit_compress)(*structure.ptr())
}

pub fn expand_width<T: Util + common::Ptr>(structure: &mut T, new_width: u8) {
    (structure.util().expand_width)(*structure.ptr(), new_width)
}

pub mod crate_export {
    pub use super::{
        bit_compress, expand_width, modulus, set_random_bits, set_to_id, set_to_value,
    };
}