Line data Source code
1 : //------------------------------------------------------------------------------ 2 : // LAGraph_New: create a new graph 3 : //------------------------------------------------------------------------------ 4 : 5 : // LAGraph, (c) 2019-2022 by The LAGraph Contributors, All Rights Reserved. 6 : // SPDX-License-Identifier: BSD-2-Clause 7 : // 8 : // For additional details (including references to third party source code and 9 : // other files) see the LICENSE file or contact permission@sei.cmu.edu. See 10 : // Contributors.txt for a full list of contributors. Created, in part, with 11 : // funding and support from the U.S. Government (see Acknowledgments.txt file). 12 : // DM22-0790 13 : 14 : // Contributed by Timothy A. Davis, Texas A&M University 15 : 16 : //------------------------------------------------------------------------------ 17 : 18 : // If succesful, the matrix A is moved into G->A, and the caller's A is set 19 : // to NULL. 20 : 21 : #include "LG_internal.h" 22 : 23 1146 : int LAGraph_New 24 : ( 25 : // output: 26 : LAGraph_Graph *G, // the graph to create, NULL if failure 27 : // input/output: 28 : GrB_Matrix *A, // the adjacency matrix of the graph, may be NULL. 29 : // A is moved into G as G->A, and A itself is set 30 : // to NULL to denote that is now a part of G. 31 : // That is, { G->A = A ; A = NULL ; } is performed. 32 : // When G is deleted, G->A is freed. If A is NULL, 33 : // the graph is invalid until G->A is set. 34 : // input: 35 : LAGraph_Kind kind, // the kind of graph. This may be LAGRAPH_UNKNOWN, 36 : // which must then be revised later before the 37 : // graph can be used. 38 : char *msg 39 : ) 40 : { 41 : 42 : //-------------------------------------------------------------------------- 43 : // check inputs 44 : //-------------------------------------------------------------------------- 45 : 46 1146 : LG_CLEAR_MSG ; 47 1146 : LG_ASSERT (G != NULL, GrB_NULL_POINTER) ; 48 : 49 : //-------------------------------------------------------------------------- 50 : // allocate the graph 51 : //-------------------------------------------------------------------------- 52 : 53 1144 : LG_TRY (LAGraph_Malloc ((void **) G, 1, 54 : sizeof (struct LAGraph_Graph_struct), msg)) ; 55 : 56 : //-------------------------------------------------------------------------- 57 : // initialize its members 58 : //-------------------------------------------------------------------------- 59 : 60 1136 : (*G)->A = NULL ; 61 1136 : (*G)->kind = LAGraph_KIND_UNKNOWN ; 62 1136 : (*G)->AT = NULL ; 63 1136 : (*G)->out_degree = NULL ; 64 1136 : (*G)->in_degree = NULL ; 65 1136 : (*G)->is_symmetric_structure = LAGRAPH_UNKNOWN ; 66 1136 : (*G)->nself_edges = LAGRAPH_UNKNOWN ; 67 1136 : (*G)->emin = NULL ; 68 1136 : (*G)->emin_state = LAGRAPH_UNKNOWN ; 69 1136 : (*G)->emax = NULL ; 70 1136 : (*G)->emax_state = LAGRAPH_UNKNOWN ; 71 : 72 : //-------------------------------------------------------------------------- 73 : // assign its primary components 74 : //-------------------------------------------------------------------------- 75 : 76 1136 : if ((A != NULL) && (*A != NULL)) 77 : { 78 : // move &A into the graph and set &A to NULL to denote to the caller 79 : // that it is now a component of G. The graph G is not opaque, so the 80 : // caller can get A back with A = G->A, but this helps with memory 81 : // management, since LAGraph_Delete (&G,msg) frees G->A, and if the 82 : // caller also does GrB_free (&A), a double-free would occur if this 83 : // move does not set A to NULL. 84 1134 : (*G)->A = (*A) ; 85 1134 : (*A) = NULL ; 86 : 87 1134 : (*G)->kind = kind ; 88 1134 : (*G)->is_symmetric_structure = 89 : (kind == LAGraph_ADJACENCY_UNDIRECTED) 90 : ? LAGraph_TRUE 91 1134 : : LAGRAPH_UNKNOWN ; 92 : } 93 : 94 1136 : return (GrB_SUCCESS) ; 95 : }