1 //------------------------------------------------------------------------------
2 // GB_binop_new: create a new operator (user-defined or internal)
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 // Create a new a binary operator: z = f (x,y).  The function pointer may
11 // be NULL, for implied functions (FIRST and SECOND).  It may not be NULL
12 // otherwise.
13 
14 // The binary op header is allocated by the caller, and passed in
15 // uninitialized.
16 
17 #include "GB.h"
18 #include "GB_binop.h"
19 #include <ctype.h>
20 
GB_binop_new(GrB_BinaryOp op,GxB_binary_function function,GrB_Type ztype,GrB_Type xtype,GrB_Type ytype,const char * name,const GB_Opcode opcode)21 void GB_binop_new
22 (
23     GrB_BinaryOp op,                // new binary operator
24     GxB_binary_function function,   // binary function (may be NULL)
25     GrB_Type ztype,                 // type of output z
26     GrB_Type xtype,                 // type of input x
27     GrB_Type ytype,                 // type of input y
28     const char *name,               // name of the function (may be NULL)
29     const GB_Opcode opcode          // opcode for the function
30 )
31 {
32 
33     //--------------------------------------------------------------------------
34     // check inputs
35     //--------------------------------------------------------------------------
36 
37     ASSERT (op != NULL) ;
38     ASSERT (ztype != NULL) ;
39     ASSERT (xtype != NULL) ;
40     ASSERT (ytype != NULL) ;
41 
42     //--------------------------------------------------------------------------
43     // initialize the binary operator
44     //--------------------------------------------------------------------------
45 
46     op->magic = GB_MAGIC ;
47     op->xtype = xtype ;
48     op->ytype = ytype ;
49     op->ztype = ztype ;
50     op->function = function ;       // may be NULL
51     op->opcode = opcode ;
52     op->name [0] = '\0' ;
53 
54     //--------------------------------------------------------------------------
55     // find the name of the operator
56     //--------------------------------------------------------------------------
57 
58     if (name != NULL)
59     {
60         // see if the typecast "(GxB_binary_function)" appears in the name
61         char *p = NULL ;
62         p = strstr ((char *) name, "GxB_binary_function") ;
63         if (p != NULL)
64         {
65             // skip past the typecast, the left parenthesis, and any whitespace
66             p += 19 ;
67             while (isspace (*p)) p++ ;
68             if (*p == ')') p++ ;
69             while (isspace (*p)) p++ ;
70             strncpy (op->name, p, GB_LEN-1) ;
71         }
72         else
73         {
74             // copy the entire name as-is
75             strncpy (op->name, name, GB_LEN-1) ;
76         }
77     }
78 
79     //--------------------------------------------------------------------------
80     // return result
81     //--------------------------------------------------------------------------
82 
83     ASSERT_BINARYOP_OK (op, "new binary op", GB0) ;
84 }
85 
86