File: GB_hypermatrix_prune.c

package info (click to toggle)
suitesparse-graphblas 7.4.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 67,112 kB
  • sloc: ansic: 1,072,243; cpp: 8,081; sh: 512; makefile: 506; asm: 369; python: 125; awk: 10
file content (88 lines) | stat: -rw-r--r-- 3,140 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//------------------------------------------------------------------------------
// GB_hypermatrix_prune: prune empty vectors from a hypersparse matrix
//------------------------------------------------------------------------------

// SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2022, All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

//------------------------------------------------------------------------------

// On input, A->p and A->h may be shallow.  If modified, new arrays A->p and
// A->h are created, which are not shallow.  If these arrays are not modified,
// and are shallow on input, then they remain shallow on output.

#include "GB.h"

GrB_Info GB_hypermatrix_prune
(
    GrB_Matrix A,               // matrix to prune
    GB_Context Context
)
{

    //--------------------------------------------------------------------------
    // check inputs
    //--------------------------------------------------------------------------

    ASSERT (A != NULL) ;
    ASSERT (GB_ZOMBIES_OK (A)) ;        // pattern not accessed
    ASSERT (GB_JUMBLED_OK (A)) ;
    ASSERT_MATRIX_OK (A, "A before hypermatrix_prune", GB0) ;

    if (!GB_IS_HYPERSPARSE (A))
    { 
        // nothing to do
        return (GrB_SUCCESS) ;
    }

    //--------------------------------------------------------------------------
    // count # of empty vectors
    //--------------------------------------------------------------------------

    if (A->nvec_nonempty < 0)
    { 
        // A->nvec_nonempty is needed to prune the hyperlist
        A->nvec_nonempty = GB_nvec_nonempty (A, Context) ;
    }

    //--------------------------------------------------------------------------
    // prune empty vectors
    //--------------------------------------------------------------------------

    if (A->nvec_nonempty < A->nvec)     // A->nvec_nonempty used here
    {
        // create new Ap_new and Ah_new arrays, with no empty vectors
        int64_t *restrict Ap_new = NULL ; size_t Ap_new_size = 0 ;
        int64_t *restrict Ah_new = NULL ; size_t Ah_new_size = 0 ;
        int64_t nvec_new, plen_new ;
        int64_t anz = A->nvals ;
        ASSERT (anz == A->p [A->nvec]) ;
        GrB_Info info = GB_hyper_prune (&Ap_new, &Ap_new_size,
            &Ah_new, &Ah_new_size, &nvec_new, &plen_new,
            A->p, A->h, A->nvec, Context) ;
        if (info != GrB_SUCCESS)
        { 
            // out of memory
            return (info) ;
        }
        // free the old A->p, A->h, and A->Y
        GB_phy_free (A) ;
        // A->p, A->h, A->Y are now NULL and thus not shallow
        ASSERT (!A->p_shallow) ;
        ASSERT (!A->h_shallow) ;
        ASSERT (!A->Y_shallow) ;
        // transplant the new hyperlist into A
        A->p = Ap_new ; A->p_size = Ap_new_size ;
        A->h = Ah_new ; A->h_size = Ah_new_size ;
        A->nvec = nvec_new ;
        A->plen = plen_new ;
        A->nvec_nonempty = nvec_new ;
        A->nvals = anz ;
        ASSERT (anz == A->p [A->nvec]) ;
        A->magic = GB_MAGIC ;
    }

    ASSERT_MATRIX_OK (A, "A after hypermatrix_prune", GB0) ;
    return (GrB_SUCCESS) ;
}