mech_set/relations/
disjoint.rs1
2use crate::*;
3
4use indexmap::set::IndexSet;
5use mech_core::set::MechSet;
6
7#[derive(Debug)]
10struct SetDisjointFxn {
11 lhs: Ref<MechSet>,
12 rhs: Ref<MechSet>,
13 out: Ref<bool>,
14}
15impl MechFunctionFactory for SetDisjointFxn {
16 fn new(args: FunctionArgs) -> MResult<Box<dyn MechFunction>> {
17 match args {
18 FunctionArgs::Binary(out, arg1, arg2) => {
19 let lhs: Ref<MechSet> = unsafe { arg1.as_unchecked() }.clone();
20 let rhs: Ref<MechSet> = unsafe { arg2.as_unchecked() }.clone();
21 let out: Ref<bool> = unsafe { out.as_unchecked() }.clone();
22 Ok(Box::new(SetDisjointFxn {lhs, rhs, out }))
23 },
24 _ => Err(MechError2::new(IncorrectNumberOfArguments { expected: 2, found: args.len() }, None).with_compiler_loc()),
25 }
26 }
27}
28impl MechFunctionImpl for SetDisjointFxn {
29 fn solve(&self) {
30 unsafe {
31 let mut out_ptr: &mut bool = &mut *(self.out.as_mut_ptr());
33
34 let lhs_ptr: &MechSet = &*(self.lhs.as_ptr());
36 let rhs_ptr: &MechSet = &*(self.rhs.as_ptr());
37
38 *out_ptr = lhs_ptr.set.is_disjoint(&(rhs_ptr.set));
40 }
41 }
42 fn out(&self) -> Value { Value::Bool(self.out.clone()) }
43 fn to_string(&self) -> String { format!("{:#?}", self) }
44}
45#[cfg(feature = "compiler")]
46impl MechFunctionCompiler for SetDisjointFxn {
47 fn compile(&self, ctx: &mut CompileCtx) -> MResult<Register> {
48 let name = format!("SetDisjointFxn");
49 compile_binop!(name, self.out, self.lhs, self.rhs, ctx, FeatureFlag::Custom(hash_str("set/disjoint") ));
50 }
51}
52register_descriptor! {
53 FunctionDescriptor {
54 name: "SetDisjointFxn",
55 ptr: SetDisjointFxn::new,
56 }
57}
58
59fn set_disjoint_fxn(lhs: Value, rhs: Value) -> MResult<Box<dyn MechFunction>> {
60 match (lhs, rhs) {
61 (Value::Set(lhs), Value::Set(rhs)) => {
62 Ok(Box::new(SetDisjointFxn { lhs: lhs.clone(), rhs: rhs.clone(), out: Ref::new(false) }))
63 },
64 x => Err(MechError2::new(
65 UnhandledFunctionArgumentKind2 {
66 arg: (x.0.kind(), x.1.kind()),
67 fxn_name: "set/disjoint".to_string(),
68 }, None
69 ).with_compiler_loc()),
70 }
71}
72
73pub struct SetDisjoint {}
74impl NativeFunctionCompiler for SetDisjoint {
75 fn compile(&self, arguments: &Vec<Value>) -> MResult<Box<dyn MechFunction>> {
76 if arguments.len() != 2 {
77 return Err(MechError2::new(IncorrectNumberOfArguments { expected: 2, found: arguments.len() }, None).with_compiler_loc());
78 }
79 let lhs = arguments[0].clone();
80 let rhs = arguments[1].clone();
81 match set_disjoint_fxn(lhs.clone(),rhs.clone()) {
82 Ok(fxn) => Ok(fxn),
83 Err(x) => {
84 match (lhs,rhs) {
85 (Value::MutableReference(lhs),Value::MutableReference(rhs)) => { set_disjoint_fxn(lhs.borrow().clone(),rhs.borrow().clone()) },
86 (lhs,Value::MutableReference(rhs)) => { set_disjoint_fxn(lhs.clone(),rhs.borrow().clone()) },
87 (Value::MutableReference(lhs),rhs) => { set_disjoint_fxn(lhs.borrow().clone(),rhs.clone()) },
88 x => Err(MechError2::new(
89 UnhandledFunctionArgumentKind2 { arg: (x.0.kind(), x.1.kind()), fxn_name: "set/disjoint".to_string() },
90 None
91 ).with_compiler_loc()),
92 }
93 }
94 }
95 }
96}
97
98register_descriptor! {
99 FunctionCompilerDescriptor {
100 name: "set/disjoint",
101 ptr: &SetDisjoint{},
102 }
103}