1 //------------------------------------------------------------------------------
2 // GB_mx_mxArray_to_SelectOp
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 to a built-in GraphBLAS SelectOp.
11 
12 #include "GB_mex.h"
13 
GB_mx_mxArray_to_SelectOp(GxB_SelectOp * handle,const mxArray * op_matlab,const char * name)14 bool GB_mx_mxArray_to_SelectOp          // true if successful
15 (
16     GxB_SelectOp *handle,               // returns GraphBLAS version of op
17     const mxArray *op_matlab,           // MATLAB version of op
18     const char *name                    // name of the argument
19 )
20 {
21     (*handle) = NULL ;
22     const mxArray *opname_mx = NULL ;
23 
24     if (op_matlab == NULL || mxIsEmpty (op_matlab))
25     {
26         mexWarnMsgIdAndTxt ("GB:warn", "select op missing") ;
27         return (false) ;
28     }
29     else if (mxIsChar (op_matlab))
30     {
31         // op is a string
32         opname_mx = op_matlab ;
33     }
34     else
35     {
36         mexWarnMsgIdAndTxt ("GB:warn", "select op must be string") ;
37         return (false) ;
38     }
39 
40     // find the corresponding built-in GraphBLAS operator
41     GxB_SelectOp op ;
42 
43     // get the string
44     #define LEN 256
45     char opname [LEN+2] ;
46     int len = GB_mx_mxArray_to_string (opname, LEN, opname_mx) ;
47     if (len < 0)
48     {
49         return (false) ;
50     }
51 
52     if      (MATCH (opname, "tril"     )) { op = GxB_TRIL ; }
53     else if (MATCH (opname, "triu"     )) { op = GxB_TRIU ; }
54     else if (MATCH (opname, "diag"     )) { op = GxB_DIAG ; }
55     else if (MATCH (opname, "offdiag"  )) { op = GxB_OFFDIAG ; }
56 
57     else if (MATCH (opname, "nonzero"  )) { op = GxB_NONZERO ; }
58     else if (MATCH (opname, "eq_zero"  )) { op = GxB_EQ_ZERO ; }
59     else if (MATCH (opname, "gt_zero"  )) { op = GxB_GT_ZERO ; }
60     else if (MATCH (opname, "ge_zero"  )) { op = GxB_GE_ZERO ; }
61     else if (MATCH (opname, "lt_zero"  )) { op = GxB_LT_ZERO ; }
62     else if (MATCH (opname, "le_zero"  )) { op = GxB_LE_ZERO ; }
63 
64     else if (MATCH (opname, "ne_thunk" )) { op = GxB_NE_THUNK ; }
65     else if (MATCH (opname, "eq_thunk" )) { op = GxB_EQ_THUNK ; }
66     else if (MATCH (opname, "gt_thunk" )) { op = GxB_GT_THUNK ; }
67     else if (MATCH (opname, "ge_thunk" )) { op = GxB_GE_THUNK ; }
68     else if (MATCH (opname, "lt_thunk" )) { op = GxB_LT_THUNK ; }
69     else if (MATCH (opname, "le_thunk" )) { op = GxB_LE_THUNK ; }
70 
71     else if (MATCH (opname, "isnan"    )) { op = NULL ; }
72 
73     else
74     {
75         mexWarnMsgIdAndTxt ("GB:warn", "unknown select op") ;
76         return (false) ;
77     }
78 
79     // return the op
80     if (op != NULL)
81     {
82         ASSERT_SELECTOP_OK (op, name, GB0) ;
83     }
84     (*handle) = op ;
85     return (true) ;
86 }
87 
88