1 //------------------------------------------------------------------------------
2 // GB_mx_mxArray_to_BinaryOp
3 //------------------------------------------------------------------------------
4 
5 // SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
6 // SPDX-License-Identifier: Apache-2.0
7 
8 //------------------------------------------------------------------------------
9 
10 // Convert a MATLAB string or struct to a built-in GraphBLAS BinaryOp.  The
11 // mxArray is either a struct containing two terms: opname (an operator name),
12 // and an optional MATLAB string optype (a string, 'logical', 'double', etc).
13 // If not present, the default type is used (provided on input).
14 //
15 // That is:
16 // op = 'plus' ;    % the GrB_PLUS_*, type determined by default_optype.
17 //
18 // op.opname = 'plus' ; op.type = 'int8' ; % the GrB_PLUS_INT8 operator.
19 
20 #include "GB_mex.h"
21 
GB_mx_mxArray_to_BinaryOp(GrB_BinaryOp * op_handle,const mxArray * op_matlab,const char * name,const GrB_Type default_optype,const bool user_complex)22 bool GB_mx_mxArray_to_BinaryOp          // true if successful, false otherwise
23 (
24     GrB_BinaryOp *op_handle,            // the binary op
25     const mxArray *op_matlab,           // MATLAB version of op
26     const char *name,                   // name of the argument
27     const GrB_Type default_optype,      // default operator type
28     const bool user_complex             // if true, use user-defined Complex
29 )
30 {
31 
32     (*op_handle) = NULL ;
33 
34     const mxArray *opname_mx = NULL, *optype_mx = NULL ;
35 
36     if (op_matlab == NULL || mxIsEmpty (op_matlab))
37     {
38         // op is not present; defaults will be used
39         ;
40     }
41     else if (mxIsStruct (op_matlab))
42     {
43         // look for op.opname
44         int fieldnumber = mxGetFieldNumber (op_matlab, "opname") ;
45         if (fieldnumber >= 0)
46         {
47             opname_mx = mxGetFieldByNumber (op_matlab, 0, fieldnumber) ;
48         }
49         // look for op.optype
50         fieldnumber = mxGetFieldNumber (op_matlab, "optype") ;
51         if (fieldnumber >= 0)
52         {
53             optype_mx = mxGetFieldByNumber (op_matlab, 0, fieldnumber) ;
54         }
55     }
56     else
57     {
58         // op must be a string.  default type will be used
59         opname_mx = op_matlab ;
60     }
61 
62     // find the corresponding built-in GraphBLAS operator
63     GrB_BinaryOp op ;
64     if (!GB_mx_string_to_BinaryOp (&op, default_optype, opname_mx, optype_mx,
65         user_complex))
66     {
67         return (false) ;
68     }
69 
70     // return the op
71     ASSERT_BINARYOP_OK_OR_NULL (op, name, GB0) ;
72     (*op_handle) = op ;
73     return (true) ;
74 }
75 
76