1 //------------------------------------------------------------------------------
2 // GB_nnz.h: macros for matrices and vectors for counting # of entries
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 #ifndef GB_NNZ_H
11 #define GB_NNZ_H
12 
13 // If A->nzmax is zero, then A->p might not be allocated.  Note the GB_NNZ
14 // macro does not count pending tuples; use GB_MATRIX_WAIT(A) first, if
15 // needed.  For both sparse and hypersparse matrices Ap [0] == 0,
16 // and nnz(A) = Ap [nvec].  For full matrices, Ap is NULL.  For bitmap
17 // matrices, GB_NNZ (A) is given by A->nvals, but the size of the space
18 // is GB_NNZ_HELD (A).
19 
20 // nnz(A) if A is sparse or hypersparse
21 #define GB_NNZ_SPARSE(A) ((A)->p [(A)->nvec])
22 
23 // nnz(A) if A is full
24 #define GB_NNZ_FULL(A) ((A)->vlen * (A)->vdim)
25 
26 // nnz(A) if A is bitmap
27 #define GB_NNZ_BITMAP(A) ((A)->nvals)
28 
29 // nnz(A) if A is full or bitmap
30 #define GB_NNZ_FULL_OR_BITMAP(A) \
31     (((A)->b == NULL) ? GB_NNZ_FULL (A) : GB_NNZ_BITMAP (A))
32 
33 // nnz(A) for all non-empty matrices
34 #define GB_NNZ_NONEMPTY(A) \
35     (((A)->p == NULL) ? GB_NNZ_FULL_OR_BITMAP (A) : GB_NNZ_SPARSE (A))
36 
37 // nnz(A) for any matrix: includes zombies for hypersparse and sparse,
38 // but excluding entries flagged as not present in a bitmap.
39 #define GB_NNZ(A) (((A)->nzmax <= 0) ? 0 : GB_NNZ_NONEMPTY (A))
40 
41 // nnz_held(A) is the number of entries held in the data structure, including
42 // zombies and all entries in a bitmap.  For hypersparse, sparse, and full,
43 // nnz(A) and nnz_held(A) are the same.  For bitmap, nnz_held(A) is the
44 // same as the # of entries in a full matrix (# rows times # columns).
45 #define GB_NNZ_HELD(A) (((A)->nzmax <= 0) ? 0 : GB_NNZ_HELD_NONEMPTY (A))
46 
47 // nnz_held(A) for all non-empty matrices
48 #define GB_NNZ_HELD_NONEMPTY(A) \
49     (((A)->p == NULL) ? GB_NNZ_FULL (A) : GB_NNZ_SPARSE (A))
50 
51 // Upper bound on nnz(A) when the matrix has zombies and pending tuples;
52 // does not need GB_MATRIX_WAIT(A) first.
53 #define GB_NNZ_UPPER_BOUND(A) ((GB_NNZ (A) - (A)->nzombies) + GB_Pending_n (A))
54 
55 #endif
56 
57