1 //------------------------------------------------------------------------------
2 // GB_new_bix: create a matrix and allocate space
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 // Creates a matrix (with GB_new), then allocates a given space for indices and
11 // values.
12 
13 // Ahandle must be non-NULL on input.
14 
15 // If *Ahandle is NULL on input:
16 
17 //      A new, dynamically allocated header for the matrix A is allocated.  If
18 //      successful, *Ahandle points to the new handle, and its contents, on
19 //      output.  If an out-of-memory condition occurs, the header is freed and
20 //      *Ahandle is NULL on output.  If successful, (*Ahandle)->static_header
21 //      will always be false (A_static_header is ignored).
22 
23 // If *Ahandle is not NULL on input:
24 
25 //      The static header for A is used.  The pointer *Ahandle itself is not
26 //      modified on output, either on success or failure.  If successful, the
27 //      content of A has been created.  If an out-of-memory condition occurs,
28 //      the preexisting header is not freed and *Ahandle is unmodified on
29 //      output. (*Ahandle)->static_header is determined from the input
30 //      parameter, A_static_header.
31 
32 #include "GB.h"
33 
GB_new_bix(GrB_Matrix * Ahandle,const bool A_static_header,const GrB_Type type,const int64_t vlen,const int64_t vdim,const GB_Ap_code Ap_option,const bool is_csc,const int sparsity,const bool bitmap_calloc,const float hyper_switch,const int64_t plen,const int64_t anz,const bool numeric,GB_Context Context)34 GrB_Info GB_new_bix             // create a new matrix, incl. A->b, A->i, A->x
35 (
36     GrB_Matrix *Ahandle,        // output matrix to create
37     const bool A_static_header, // true if Ahandle is statically allocated.
38     const GrB_Type type,        // type of output matrix
39     const int64_t vlen,         // length of each vector
40     const int64_t vdim,         // number of vectors
41     const GB_Ap_code Ap_option, // allocate A->p and A->h, or leave NULL
42     const bool is_csc,          // true if CSC, false if CSR
43     const int sparsity,         // hyper, sparse, bitmap, full, or auto
44     const bool bitmap_calloc,   // if true, calloc A->b, otherwise use malloc
45     const float hyper_switch,   // A->hyper_switch, unless auto
46     const int64_t plen,         // size of A->p and A->h, if hypersparse
47     const int64_t anz,          // number of nonzeros the matrix must hold
48     const bool numeric,         // if true, allocate A->x, else A->x is NULL
49     GB_Context Context
50 )
51 {
52 
53     //--------------------------------------------------------------------------
54     // check inputs
55     //--------------------------------------------------------------------------
56 
57     ASSERT (Ahandle != NULL) ;
58 
59     //--------------------------------------------------------------------------
60     // allocate the header and the vector pointers
61     //--------------------------------------------------------------------------
62 
63     bool preexisting_header = (*Ahandle != NULL) ;
64     GrB_Info info = GB_new (Ahandle, A_static_header,
65         type, vlen, vdim, Ap_option,
66         is_csc, sparsity, hyper_switch, plen, Context) ;
67     if (info != GrB_SUCCESS)
68     {
69         // out of memory.
70         // If *Ahandle was non-NULL on input, it has not been freed.
71         ASSERT (preexisting_header == (*Ahandle != NULL)) ;
72         return (info) ;
73     }
74 
75     GrB_Matrix A = (*Ahandle) ;
76 
77     //--------------------------------------------------------------------------
78     // allocate the bitmap (A->b), indices (A->i), and values (A->x)
79     //--------------------------------------------------------------------------
80 
81     info = GB_bix_alloc (A, anz, sparsity == GxB_BITMAP, bitmap_calloc,
82         ! (sparsity == GxB_FULL || sparsity == GxB_BITMAP),
83         numeric, Context) ;
84     if (info != GrB_SUCCESS)
85     {
86         // out of memory
87         // GB_bix_alloc has already freed all content of A
88         if (!preexisting_header)
89         {
90             // also free the header *Ahandle itself
91             GB_Matrix_free (Ahandle) ;
92             ASSERT (*Ahandle == NULL) ;
93         }
94         return (info) ;
95     }
96 
97     //--------------------------------------------------------------------------
98     // return result
99     //--------------------------------------------------------------------------
100 
101     return (GrB_SUCCESS) ;
102 }
103 
104