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
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_graphblas_index_integer_value_types,
    implement_macro_with_1_type_trait_and_typed_graphblas_function_for_all_value_types,
};
use crate::value_type::ValueType;

// TODO: review EvaluationDomain. Typecasting may not work as expected, e.g. for less-than ot greater-than operators.
pub trait IndexUnaryOperator<EvaluationDomain>
where
    EvaluationDomain: ValueType,
{
    fn graphblas_type(&self) -> GrB_IndexUnaryOp;
}

macro_rules! implement_index_unary_operator {
    ($operator_name:ident, $graphblas_operator_trait_name:ident) => {
        pub trait $graphblas_operator_trait_name<T: ValueType> {
            fn graphblas_type() -> GrB_IndexUnaryOp;
        }

        impl<T: ValueType + $graphblas_operator_trait_name<T>> IndexUnaryOperator<T>
            for $operator_name<T>
        {
            fn graphblas_type(&self) -> GrB_IndexUnaryOp {
                T::graphblas_type()
            }
        }

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

macro_rules! implement_typed_index_unary_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_IndexUnaryOp {
                unsafe { $graphblas_operator_name }
            }
        }
    };
}

macro_rules! implement_generic_index_unary_operator {
    ($operator_name:ident,
        $graphblas_operator_name:ident
    ) => {
        impl<EvaluationDomain: ValueType> IndexUnaryOperator<EvaluationDomain>
            for $operator_name<EvaluationDomain>
        {
            fn graphblas_type(&self) -> GrB_IndexUnaryOp {
                unsafe { $graphblas_operator_name }
            }
        }

        impl<EvaluationDomain: ValueType> $operator_name<EvaluationDomain> {
            pub fn new() -> Self {
                Self {
                    _evaluation_domain: PhantomData,
                }
            }
        }
    };
}

macro_rules! define_index_unary_operator {
    ($identifier: ident) => {
        #[derive(Debug, Clone)]
        pub struct $identifier<EvaluationDomain: ValueType> {
            _evaluation_domain: PhantomData<EvaluationDomain>,
        }
    };
}

// z = i + y
define_index_unary_operator!(PlusRowIndex);
implement_index_unary_operator!(PlusRowIndex, PlusRowIndexTyped);
implement_macro_with_1_type_trait_and_typed_graphblas_function_for_all_graphblas_index_integer_value_types!(
    implement_typed_index_unary_operator,
    PlusRowIndexTyped,
    GrB_ROWINDEX
);

// z = j - i + y
define_index_unary_operator!(PlusDiagonalIndex);
implement_index_unary_operator!(PlusDiagonalIndex, PlusDiagonalIndexTyped);
implement_macro_with_1_type_trait_and_typed_graphblas_function_for_all_graphblas_index_integer_value_types!(
    implement_typed_index_unary_operator,
    PlusDiagonalIndexTyped,
    GrB_DIAGINDEX
);

// z=(j<=(i+y))
// true for entries on or below the yth diagonal
define_index_unary_operator!(IsOnOrBelowDiagonal);
implement_generic_index_unary_operator!(IsOnOrBelowDiagonal, GrB_TRIL);

// z=(j>=(i+y))
// true for entries on or above the yth diagonal
define_index_unary_operator!(IsOnOrAboveDiagonal);
implement_generic_index_unary_operator!(IsOnOrAboveDiagonal, GrB_TRIU);

// z=(j==(i+y))
// true for entries on the yth diagonal
define_index_unary_operator!(IsOnDiagonal);
implement_generic_index_unary_operator!(IsOnDiagonal, GrB_DIAG);

// z=(j<=y)
// true for entries in columns 0 to y
define_index_unary_operator!(IsUpToAndIncludingColumn);
implement_generic_index_unary_operator!(IsUpToAndIncludingColumn, GrB_COLLE);

// z=(j>y)
// true for entries in columns y+1 and above
define_index_unary_operator!(IsAfterColumn);
implement_generic_index_unary_operator!(IsAfterColumn, GrB_COLGT);

// z=(i<=y)
// true for entries in rows 0 to y
define_index_unary_operator!(IsUpToAndIncludingRow);
implement_generic_index_unary_operator!(IsUpToAndIncludingRow, GrB_ROWLE);

// z=(i>y)
// true for entries in rows y+1 and above
define_index_unary_operator!(IsAfterRow);
implement_generic_index_unary_operator!(IsAfterRow, GrB_ROWGT);

define_index_unary_operator!(IsValueNotEqualTo);
implement_index_unary_operator!(IsValueNotEqualTo, IsValueNotEqualToTyped);
implement_macro_with_1_type_trait_and_typed_graphblas_function_for_all_value_types!(
    implement_typed_index_unary_operator,
    IsValueNotEqualToTyped,
    GrB_VALUENE
);

define_index_unary_operator!(IsValueEqualTo);
implement_index_unary_operator!(IsValueEqualTo, IsValueEqualToTyped);
implement_macro_with_1_type_trait_and_typed_graphblas_function_for_all_value_types!(
    implement_typed_index_unary_operator,
    IsValueEqualToTyped,
    GrB_VALUEEQ
);

define_index_unary_operator!(IsValueGreaterThan);
implement_index_unary_operator!(IsValueGreaterThan, IsValueGreaterThanTyped);
implement_macro_with_1_type_trait_and_typed_graphblas_function_for_all_value_types!(
    implement_typed_index_unary_operator,
    IsValueGreaterThanTyped,
    GrB_VALUEGT
);

define_index_unary_operator!(IsValueLessThan);
implement_index_unary_operator!(IsValueLessThan, IsValueLessThanTyped);
implement_macro_with_1_type_trait_and_typed_graphblas_function_for_all_value_types!(
    implement_typed_index_unary_operator,
    IsValueLessThanTyped,
    GrB_VALUELT
);

define_index_unary_operator!(IsValueLessThanOrEqualTo);
implement_index_unary_operator!(IsValueLessThanOrEqualTo, IsValueLessThanOrEqualToTyped);
implement_macro_with_1_type_trait_and_typed_graphblas_function_for_all_value_types!(
    implement_typed_index_unary_operator,
    IsValueLessThanOrEqualToTyped,
    GrB_VALUELE
);

#[cfg(test)]
mod tests {}