1 //------------------------------------------------------------------------------
2 // GB_hypermatrix_prune: prune empty vectors from a hypersparse 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 // On input, A->p and A->h may be shallow.  If modified, new arrays A->p and
11 // A->h are created, which are not shallow.  If these arrays are not modified,
12 // and are shallow on input, then they remain shallow on output.
13 
14 #include "GB.h"
15 
GB_hypermatrix_prune(GrB_Matrix A,GB_Context Context)16 GrB_Info GB_hypermatrix_prune
17 (
18     GrB_Matrix A,               // matrix to prune
19     GB_Context Context
20 )
21 {
22 
23     //--------------------------------------------------------------------------
24     // check inputs
25     //--------------------------------------------------------------------------
26 
27     ASSERT (A != NULL) ;
28     ASSERT (GB_ZOMBIES_OK (A)) ;        // pattern not accessed
29     ASSERT (GB_JUMBLED_OK (A)) ;
30 
31     if (!GB_IS_HYPERSPARSE (A))
32     {
33         // nothing to do
34         return (GrB_SUCCESS) ;
35     }
36 
37     //--------------------------------------------------------------------------
38     // count # of empty vectors
39     //--------------------------------------------------------------------------
40 
41     if (A->nvec_nonempty < 0)
42     {
43         A->nvec_nonempty = GB_nvec_nonempty (A, Context) ;
44     }
45 
46     //--------------------------------------------------------------------------
47     // prune empty vectors
48     //--------------------------------------------------------------------------
49 
50     if (A->nvec_nonempty < A->nvec)     // A->nvec_nonempty used here
51     {
52         // create new Ap_new and Ah_new arrays, with no empty vectors
53         int64_t *restrict Ap_new = NULL ; size_t Ap_new_size = 0 ;
54         int64_t *restrict Ah_new = NULL ; size_t Ah_new_size = 0 ;
55         int64_t nvec_new ;
56         GrB_Info info = GB_hyper_prune (&Ap_new, &Ap_new_size,
57             &Ah_new, &Ah_new_size, &nvec_new, A->p, A->h, A->nvec, Context) ;
58         if (info != GrB_SUCCESS)
59         {
60             // out of memory
61             return (info) ;
62         }
63         // free the old A->p and A->h.  If shallow, just remove them from A
64         // but do not free them since they come from another matrix.
65         GB_ph_free (A) ;
66         // A->p and A->h are now NULL and thus not shallow
67         ASSERT (!A->p_shallow) ;
68         ASSERT (!A->h_shallow) ;
69         // transplant the new hyperlist into A
70         A->p = Ap_new ; A->p_size = Ap_new_size ;
71         A->h = Ah_new ; A->h_size = Ah_new_size ;
72         A->nvec = nvec_new ;
73         A->plen = nvec_new ;
74         A->nvec_nonempty = nvec_new ;
75         A->magic = GB_MAGIC ;
76     }
77 
78     return (GrB_SUCCESS) ;
79 }
80 
81