1 //------------------------------------------------------------------------------
2 // GB_Monoid_check: check and print a monoid
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 #include "GB.h"
11 
12 GB_PUBLIC   // accessed by the MATLAB tests in GraphBLAS/Test only
GB_Monoid_check(const GrB_Monoid monoid,const char * name,int pr,FILE * f)13 GrB_Info GB_Monoid_check        // check a GraphBLAS monoid
14 (
15     const GrB_Monoid monoid,    // GraphBLAS monoid to print and check
16     const char *name,           // name of the monoid, optional
17     int pr,                     // print level
18     FILE *f                     // file for output
19 )
20 {
21 
22     //--------------------------------------------------------------------------
23     // check inputs
24     //--------------------------------------------------------------------------
25 
26     GBPR0 ("\n    GraphBLAS Monoid: %s ", ((name != NULL) ? name : "")) ;
27 
28     if (monoid == NULL)
29     {
30         GBPR0 ("NULL\n") ;
31         return (GrB_NULL_POINTER) ;
32     }
33 
34     //--------------------------------------------------------------------------
35     // check object
36     //--------------------------------------------------------------------------
37 
38     GB_CHECK_MAGIC (monoid, "Monoid") ;
39     GBPR0 (monoid->header_size > 0 ? "(user-defined)" : "(built-in)") ;
40 
41     GrB_Info info = GB_BinaryOp_check (monoid->op, "monoid->op", pr, f) ;
42     if (info != GrB_SUCCESS || GB_OP_IS_POSITIONAL (monoid->op))
43     {
44         GBPR0 ("    Monoid contains an invalid operator\n") ;
45         return (GrB_INVALID_OBJECT) ;
46     }
47 
48     if (monoid->op->xtype != monoid->op->ztype ||
49         monoid->op->ytype != monoid->op->ztype)
50     {
51         GBPR0 ("    All domains of operator must be the same\n") ;
52         return (GrB_INVALID_OBJECT) ;
53     }
54 
55     if (monoid->identity == NULL)
56     {
57         GBPR0 ("    Identity value is missing\n") ;
58         return (GrB_INVALID_OBJECT) ;
59     }
60 
61     // print the identity and terminal values
62     if (pr != GxB_SILENT)
63     {
64         // print the identity value, if present
65         GBPR ("    identity: [ ") ;
66         info = GB_entry_check (monoid->op->ztype, monoid->identity, pr, f) ;
67         if (info != GrB_SUCCESS) return (info) ;
68         GBPR (" ] ") ;
69 
70         // print the terminal value, if present
71         if (monoid->terminal != NULL)
72         {
73             GBPR ("terminal: [ ") ;
74             info = GB_entry_check (monoid->op->ztype, monoid->terminal, pr, f) ;
75             if (info != GrB_SUCCESS) return (info) ;
76             GBPR (" ]") ;
77         }
78         GBPR ("\n") ;
79     }
80 
81     return (GrB_SUCCESS) ;
82 }
83 
84