graphblas_sparse_linear_algebra/operators/options/
options_for_operator_with_matrix_as_second_argument.rs

1use suitesparse_graphblas_sys::GrB_Descriptor;
2
3use super::{
4    graphblas_descriptor, GetClearOutputBeforeUse, GetGraphblasDescriptor, GetOperatorMaskOptions,
5    GetOperatorOptions, GetTransposeSecondMatrixArgument, WithTransposeMatrixArgument,
6};
7
8// Implemented methods do not provide mutable access to GraphBLAS operators or options.
9// Code review must consider that no mtable access is provided.
10// https://doc.rust-lang.org/nomicon/send-and-sync.html
11unsafe impl Send for OptionsForOperatorWithMatrixAsSecondArgument {}
12unsafe impl Sync for OptionsForOperatorWithMatrixAsSecondArgument {}
13
14#[derive(Debug, Clone)]
15pub struct OptionsForOperatorWithMatrixAsSecondArgument {
16    clear_output_before_use: bool,
17    use_mask_structure_of_stored_values_as_mask: bool,
18    use_mask_complement: bool,
19    transpose_matrix_argument: bool,
20
21    graphblas_descriptor: GrB_Descriptor,
22}
23
24pub trait GetOptionsForOperatorWithMatrixAsSecondArgument:
25    GetOperatorOptions + GetTransposeSecondMatrixArgument
26{
27}
28
29impl GetOperatorOptions for OptionsForOperatorWithMatrixAsSecondArgument {}
30impl GetOptionsForOperatorWithMatrixAsSecondArgument
31    for OptionsForOperatorWithMatrixAsSecondArgument
32{
33}
34
35impl GetClearOutputBeforeUse for OptionsForOperatorWithMatrixAsSecondArgument {
36    fn clear_output_before_use(&self) -> bool {
37        self.clear_output_before_use
38    }
39}
40
41impl GetOperatorMaskOptions for OptionsForOperatorWithMatrixAsSecondArgument {
42    fn use_mask_structure_of_stored_values_as_mask(&self) -> bool {
43        self.use_mask_structure_of_stored_values_as_mask
44    }
45
46    fn use_mask_complement(&self) -> bool {
47        self.use_mask_complement
48    }
49}
50
51impl GetTransposeSecondMatrixArgument for OptionsForOperatorWithMatrixAsSecondArgument {
52    fn transpose_second_matrix_argument(&self) -> bool {
53        self.transpose_matrix_argument
54    }
55}
56
57impl GetGraphblasDescriptor for OptionsForOperatorWithMatrixAsSecondArgument {
58    fn graphblas_descriptor(&self) -> GrB_Descriptor {
59        self.graphblas_descriptor
60    }
61}
62
63impl WithTransposeMatrixArgument for OptionsForOperatorWithMatrixAsSecondArgument {
64    fn with_negated_transpose_matrix_argument(&self) -> Self {
65        OptionsForOperatorWithMatrixAsSecondArgument::new(
66            self.clear_output_before_use,
67            self.use_mask_structure_of_stored_values_as_mask,
68            self.use_mask_complement,
69            !self.transpose_matrix_argument,
70        )
71    }
72
73    fn with_transpose_matrix_argument(&self, transpose_matrix: bool) -> Self {
74        if transpose_matrix == self.transpose_matrix_argument {
75            self.clone()
76        } else {
77            OptionsForOperatorWithMatrixAsSecondArgument::new(
78                self.clear_output_before_use,
79                self.use_mask_structure_of_stored_values_as_mask,
80                self.use_mask_complement,
81                transpose_matrix,
82            )
83        }
84    }
85}
86
87impl OptionsForOperatorWithMatrixAsSecondArgument {
88    pub fn new(
89        clear_output_before_use: bool,
90        use_mask_structure_of_stored_values_as_mask: bool,
91        use_mask_complement: bool,
92        transpose_matrix_argument: bool,
93    ) -> Self {
94        Self {
95            clear_output_before_use,
96            use_mask_structure_of_stored_values_as_mask,
97            use_mask_complement,
98            transpose_matrix_argument,
99
100            graphblas_descriptor: graphblas_descriptor(
101                clear_output_before_use,
102                false,
103                false,
104                false,
105                transpose_matrix_argument,
106            ),
107        }
108    }
109
110    pub fn new_default() -> Self {
111        let clear_output_before_use = false;
112        let use_mask_structure_of_stored_values_as_mask = false;
113        let use_mask_complement = false;
114        let transpose_matrix_argument = false;
115
116        Self {
117            clear_output_before_use,
118            use_mask_structure_of_stored_values_as_mask,
119            use_mask_complement,
120            transpose_matrix_argument,
121
122            graphblas_descriptor: graphblas_descriptor(
123                clear_output_before_use,
124                false,
125                false,
126                false,
127                transpose_matrix_argument,
128            ),
129        }
130    }
131}
132
133#[cfg(test)]
134mod tests {
135    use std::ptr;
136
137    use super::*;
138
139    #[test]
140    fn test_options() {
141        let default_options = OptionsForOperatorWithMatrixAsSecondArgument::new_default();
142        let expected_value: GrB_Descriptor = ptr::null_mut();
143        assert_eq!(default_options.graphblas_descriptor(), expected_value)
144    }
145}