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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
use std::marker::PhantomData;

use crate::graphblas_bindings::*;
use crate::value_type::utilities_to_implement_traits_for_all_value_types::{
    implement_macro_with_1_type_trait_and_typed_graphblas_function_for_all_value_types_except_bool,
    implement_macro_with_1_type_trait_and_typed_graphblas_function_for_all_value_types_with_postfix,
    implement_macro_with_1_type_trait_and_typed_graphblas_function_for_unsigned_integers_with_postfix,
};
use crate::value_type::ValueType;

pub trait Monoid<T>
where
    T: ValueType,
{
    fn graphblas_type(&self) -> GrB_Monoid;
}

macro_rules! implement_monoid_operator {
    ($monoid_operator_name:ident, $graphblas_operator_trait_name:ident) => {
        pub trait $graphblas_operator_trait_name<T: ValueType> {
            fn graphblas_type() -> GrB_Monoid;
        }

        impl<T: ValueType + $graphblas_operator_trait_name<T>> Monoid<T>
            for $monoid_operator_name<T>
        {
            fn graphblas_type(&self) -> GrB_Monoid {
                T::graphblas_type()
            }
        }

        impl<T: ValueType> $monoid_operator_name<T> {
            pub fn new() -> Self {
                Self {
                    _value_type: PhantomData,
                }
            }
        }
    };
}

macro_rules! implement_typed_monoid_operator {
    ($operator_trait_name:ident,
        $graphblas_operator_name:ident,
        $value_type:ty
    ) => {
        impl $operator_trait_name<$value_type> for $value_type {
            fn graphblas_type() -> GrB_Monoid {
                unsafe { $graphblas_operator_name }
            }
        }
    };
}

#[derive(Debug, Clone)]
pub struct Min<T: ValueType> {
    _value_type: PhantomData<T>,
}

implement_monoid_operator!(Min, MinMonoidTyped);

implement_macro_with_1_type_trait_and_typed_graphblas_function_for_all_value_types_except_bool!(
    implement_typed_monoid_operator,
    MinMonoidTyped,
    GrB_MIN_MONOID
);

#[derive(Debug, Clone)]
pub struct Max<T: ValueType> {
    _value_type: PhantomData<T>,
}

implement_monoid_operator!(Max, MaxMonoidTyped);

implement_macro_with_1_type_trait_and_typed_graphblas_function_for_all_value_types_except_bool!(
    implement_typed_monoid_operator,
    MaxMonoidTyped,
    GrB_MAX_MONOID
);

#[derive(Debug, Clone)]
pub struct Plus<T: ValueType> {
    _value_type: PhantomData<T>,
}

implement_monoid_operator!(Plus, PlusMonoidTyped);

implement_macro_with_1_type_trait_and_typed_graphblas_function_for_all_value_types_except_bool!(
    implement_typed_monoid_operator,
    PlusMonoidTyped,
    GrB_PLUS_MONOID
);

#[derive(Debug, Clone)]
pub struct Times<T: ValueType> {
    _value_type: PhantomData<T>,
}

implement_monoid_operator!(Times, TimesMonoidTyped);

implement_macro_with_1_type_trait_and_typed_graphblas_function_for_all_value_types_except_bool!(
    implement_typed_monoid_operator,
    TimesMonoidTyped,
    GrB_TIMES_MONOID
);

#[derive(Debug, Clone)]
pub struct Any<T: ValueType> {
    _value_type: PhantomData<T>,
}

implement_monoid_operator!(Any, AnyMonoidTyped);

implement_macro_with_1_type_trait_and_typed_graphblas_function_for_all_value_types_with_postfix!(
    implement_typed_monoid_operator,
    AnyMonoidTyped,
    GxB_ANY,
    MONOID
);

#[derive(Debug, Clone)]
pub struct LogicalOr<T: ValueType> {
    _value_type: PhantomData<T>,
}

#[derive(Debug, Clone)]
pub struct LogicalAnd<T: ValueType> {
    _value_type: PhantomData<T>,
}

#[derive(Debug, Clone)]
pub struct LogicalExclusiveOr<T: ValueType> {
    _value_type: PhantomData<T>,
}

#[derive(Debug, Clone)]
pub struct Equal<T: ValueType> {
    _value_type: PhantomData<T>,
}

implement_monoid_operator!(LogicalOr, LogicalOrMonoidTyped);
implement_monoid_operator!(LogicalAnd, LogicalAndMonoidTyped);
implement_monoid_operator!(LogicalExclusiveOr, LogicalExclusiveOrMonoidTyped);
implement_monoid_operator!(Equal, EqualMonoidTyped);

implement_typed_monoid_operator!(LogicalOrMonoidTyped, GrB_LOR_MONOID_BOOL, bool);
implement_typed_monoid_operator!(LogicalAndMonoidTyped, GrB_LAND_MONOID_BOOL, bool);
implement_typed_monoid_operator!(LogicalExclusiveOrMonoidTyped, GrB_LXOR_MONOID_BOOL, bool);
implement_typed_monoid_operator!(EqualMonoidTyped, GrB_LXNOR_MONOID_BOOL, bool);

#[derive(Debug, Clone)]
pub struct BitwiseLogicalOr<T: ValueType> {
    _value_type: PhantomData<T>,
}

#[derive(Debug, Clone)]
pub struct BitwiseLogicalAnd<T: ValueType> {
    _value_type: PhantomData<T>,
}

#[derive(Debug, Clone)]
pub struct BitwiseLogicalExclusiveOr<T: ValueType> {
    _value_type: PhantomData<T>,
}

implement_monoid_operator!(BitwiseLogicalOr, BitwiseLogicalOrMonoidTyped);
implement_monoid_operator!(BitwiseLogicalAnd, BitwiseLogicalAndMonoidTyped);
implement_monoid_operator!(
    BitwiseLogicalExclusiveOr,
    BitwiseLogicalExclusiveOrMonoidTyped
);

implement_macro_with_1_type_trait_and_typed_graphblas_function_for_unsigned_integers_with_postfix!(
    implement_typed_monoid_operator,
    BitwiseLogicalOrMonoidTyped,
    GxB_BOR,
    MONOID
);

implement_macro_with_1_type_trait_and_typed_graphblas_function_for_unsigned_integers_with_postfix!(
    implement_typed_monoid_operator,
    BitwiseLogicalAndMonoidTyped,
    GxB_BAND,
    MONOID
);

implement_macro_with_1_type_trait_and_typed_graphblas_function_for_unsigned_integers_with_postfix!(
    implement_typed_monoid_operator,
    BitwiseLogicalExclusiveOrMonoidTyped,
    GxB_BXOR,
    MONOID
);

#[cfg(test)]
mod tests {
    use super::*;

    use crate::collections::sparse_vector::operations::{
        FromVectorElementList, GetSparseVectorElementList, GetSparseVectorElementValue,
    };
    use crate::collections::sparse_vector::{SparseVector, VectorElementList};
    use crate::context::Context;
    use crate::operators::binary_operator::{Assignment, First};
    use crate::operators::element_wise_addition::{
        ApplyElementWiseVectorAdditionMonoidOperator, ElementWiseVectorAdditionMonoidOperator,
    };
    use crate::operators::mask::SelectEntireVector;
    use crate::operators::options::OptionsForOperatorWithMatrixArguments;

    #[test]
    fn new_binary_operator() {
        let min_monoid = Min::<f32>::new();
        let _graphblas_type = min_monoid.graphblas_type();
    }

    #[test]
    fn test_element_wise_addition_with_equality_operator() {
        let context = Context::init_default().unwrap();

        let operator = Equal::<bool>::new();
        let options = OptionsForOperatorWithMatrixArguments::new_default();
        let equality_operator = ElementWiseVectorAdditionMonoidOperator::new();

        let length = 7;

        let multiplier = SparseVector::<bool>::new(context.clone(), length).unwrap();
        let multiplicant = multiplier.clone();
        let mut product = multiplier.clone();

        // Test multiplication of empty matrices
        equality_operator
            .apply(
                &multiplier,
                &operator,
                &multiplicant,
                &Assignment::new(),
                &mut product,
                &SelectEntireVector::new(context.clone()),
                &options,
            )
            .unwrap();
        let element_list = product.get_element_list().unwrap();

        assert_eq!(element_list.length(), 0);

        let multiplier_element_list = VectorElementList::<bool>::from_element_vector(vec![
            (1, false).into(),
            (3, true).into(),
            (5, false).into(),
            (6, true).into(),
        ]);
        let multiplier = SparseVector::<bool>::from_element_list(
            context.clone(),
            length,
            multiplier_element_list,
            &First::<bool>::new(),
        )
        .unwrap();

        let multiplicant_element_list = VectorElementList::<bool>::from_element_vector(vec![
            (3, true).into(),
            (4, true).into(),
            (5, false).into(),
            (6, false).into(),
        ]);
        let multiplicant = SparseVector::<bool>::from_element_list(
            context.clone(),
            length,
            multiplicant_element_list,
            &First::<bool>::new(),
        )
        .unwrap();

        // Test multiplication of full matrices
        equality_operator
            .apply(
                &multiplier,
                &operator,
                &multiplicant,
                &Assignment::new(),
                &mut product,
                &SelectEntireVector::new(context.clone()),
                &options,
            )
            .unwrap();

        assert_eq!(product.element_value_or_default(&0).unwrap(), false); // operator does not apply on empty
        assert_eq!(product.element_value_or_default(&1).unwrap(), false); // false unequal to empty
        assert_eq!(product.element_value_or_default(&2).unwrap(), false); // operator does not apply on empty
        assert_eq!(product.element_value_or_default(&3).unwrap(), true);
        assert_eq!(product.element_value_or_default(&4).unwrap(), true); // true and empty => true
        assert_eq!(product.element_value_or_default(&5).unwrap(), true); // true and false => true
        assert_eq!(product.element_value_or_default(&6).unwrap(), false); // false and true => false
    }
}