1 //------------------------------------------------------------------------------
2 // GB_shallow_copy: create a shallow copy of a matrix
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 purely shallow copy of a matrix.  No typecasting is done.
11 
12 // The CSR/CSC format of C and A can differ, but they have they same vlen and
13 // vdim.  This function is CSR/CSC agnostic, except that C_is_csc is used to
14 // set the C->is_csc state in C.
15 
16 // No errors are checked except for out-of-memory conditions.  This function is
17 // not user-callable.  Shallow matrices are never passed back to the user.
18 
19 // Compare this function with GB_shallow_op.c
20 // A has any sparsity structure (hypersparse, sparse, bitmap, or full)
21 
22 #include "GB_transpose.h"
23 
24 #define GB_FREE_ALL ;
25 
26 GB_PUBLIC                   // used by GraphBLAS MATLAB interface
GB_shallow_copy(GrB_Matrix C,const bool C_is_csc,const GrB_Matrix A,GB_Context Context)27 GrB_Info GB_shallow_copy    // create a purely shallow matrix
28 (
29     GrB_Matrix C,           // output matrix C, with a static header
30     const bool C_is_csc,    // desired CSR/CSC format of C
31     const GrB_Matrix A,     // input matrix
32     GB_Context Context
33 )
34 {
35 
36     //--------------------------------------------------------------------------
37     // check inputs
38     //--------------------------------------------------------------------------
39 
40     ASSERT (C != NULL) ;
41     ASSERT (C->static_header) ;
42     ASSERT_MATRIX_OK (A, "A for shallow copy", GB0) ;
43     GB_MATRIX_WAIT_IF_PENDING_OR_ZOMBIES (A) ;
44     ASSERT (!GB_PENDING (A)) ;
45     ASSERT (GB_JUMBLED_OK (A)) ;
46     ASSERT (!GB_ZOMBIES (A)) ;
47 
48     //--------------------------------------------------------------------------
49     // construct a shallow copy of A for the pattern of C
50     //--------------------------------------------------------------------------
51 
52     // allocate the struct for C, but do not allocate C->[p,h,b,i,x].
53     // C has the exact same sparsity structure as A.
54     GrB_Info info ;
55     info = GB_new (&C, true, // sparse or hyper, static header
56         A->type, A->vlen, A->vdim, GB_Ap_null, C_is_csc,
57         GB_sparsity (A), A->hyper_switch, 0, Context) ;
58     ASSERT (info == GrB_SUCCESS) ;
59 
60     //--------------------------------------------------------------------------
61     // make a shallow copy of the vector pointers
62     //--------------------------------------------------------------------------
63 
64     ASSERT (C->magic == GB_MAGIC2) ;    // C not yet initialized
65     C->p_shallow = (A->p != NULL) ;     // C->p not freed when freeing C
66     C->h_shallow = (A->h != NULL) ;     // C->h not freed when freeing C
67     C->p = A->p ;                       // C->p is of size A->plen + 1
68     C->h = A->h ;                       // C->h is of size A->plen
69     C->plen = A->plen ;                 // C and A have the same hyperlist size
70     C->nvec = A->nvec ;
71     C->nvec_nonempty = A->nvec_nonempty ;
72     C->jumbled = A->jumbled ;           // C is jumbled if A is jumbled
73     C->nvals = A->nvals ;
74     C->magic = GB_MAGIC ;
75 
76     //--------------------------------------------------------------------------
77     // check for empty matrix
78     //--------------------------------------------------------------------------
79 
80     if (A->nzmax == 0)
81     {
82         // C->p and C->h are shallow but the rest is empty
83         C->nzmax = 0 ;
84         C->b = NULL ;
85         C->i = NULL ;
86         C->x = NULL ;
87         C->b_shallow = false ;
88         C->i_shallow = false ;
89         C->x_shallow = false ;
90         C->jumbled = false ;
91         ASSERT_MATRIX_OK (C, "C = quick copy of empty A", GB0) ;
92         return (GrB_SUCCESS) ;
93     }
94 
95     //--------------------------------------------------------------------------
96     // make a shallow copy of the pattern
97     //--------------------------------------------------------------------------
98 
99     C->b = A->b ;                   // of size A->nzmax
100     C->b_shallow = (A->b != NULL) ; // C->b will not be freed when freeing C
101 
102     C->i = A->i ;                   // of size A->nzmax
103     C->i_shallow = (A->i != NULL) ; // C->i will not be freed when freeing C
104 
105     //--------------------------------------------------------------------------
106     // make a shallow copy of the values
107     //--------------------------------------------------------------------------
108 
109     C->nzmax = A->nzmax ;
110     C->x = A->x ;
111     C->x_shallow = (A->x != NULL) ; // C->x will not be freed when freeing C
112     ASSERT (C->x_size == 0) ;       // C->x is shallow
113     ASSERT_MATRIX_OK (C, "C = pure shallow (A)", GB0) ;
114     return (GrB_SUCCESS) ;
115 }
116 
117