1 //------------------------------------------------------------------------------
2 // GB_BinaryOp_new: create a new user-defined binary operator
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 binary function signature
11 // must be void f (void *z, const void *x, const void *y), and then it must
12 // recast its input and output arguments internally as needed.
13
14 // This function is not directly user-callable. Use GrB_BinaryOp_new instead.
15
16 #include "GB.h"
17 #include "GB_binop.h"
18
GB_BinaryOp_new(GrB_BinaryOp * op,GxB_binary_function function,GrB_Type ztype,GrB_Type xtype,GrB_Type ytype,const char * name)19 GrB_Info GB_BinaryOp_new
20 (
21 GrB_BinaryOp *op, // handle for the new binary operator
22 GxB_binary_function function, // pointer to the binary function
23 GrB_Type ztype, // type of output z
24 GrB_Type xtype, // type of input x
25 GrB_Type ytype, // type of input y
26 const char *name // name of the function (may be NULL)
27 )
28 {
29
30 //--------------------------------------------------------------------------
31 // check inputs
32 //--------------------------------------------------------------------------
33
34 GB_WHERE1 ("GrB_BinaryOp_new (op, function, ztype, xtype, ytype)") ;
35 GB_RETURN_IF_NULL (op) ;
36 (*op) = NULL ;
37 GB_RETURN_IF_NULL (function) ;
38 GB_RETURN_IF_NULL_OR_FAULTY (ztype) ;
39 GB_RETURN_IF_NULL_OR_FAULTY (xtype) ;
40 GB_RETURN_IF_NULL_OR_FAULTY (ytype) ;
41
42 //--------------------------------------------------------------------------
43 // allocate the binary op
44 //--------------------------------------------------------------------------
45
46 size_t header_size ;
47 (*op) = GB_MALLOC (1, struct GB_BinaryOp_opaque, &header_size) ;
48 if (*op == NULL)
49 {
50 // out of memory
51 return (GrB_OUT_OF_MEMORY) ;
52 }
53 (*op)->header_size = header_size ;
54
55 //--------------------------------------------------------------------------
56 // create the binary op
57 //--------------------------------------------------------------------------
58
59 GB_binop_new (*op, function, ztype, xtype, ytype, name, GB_USER_opcode) ;
60 return (GrB_SUCCESS) ;
61 }
62
63