1 //------------------------------------------------------------------------------
2 // GB_sparsity_control: ensure the sparsity control is in the proper range
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 #include "GB.h"
11
GB_sparsity_control(int sparsity,int64_t vdim)12 int GB_sparsity_control // revised sparsity control
13 (
14 int sparsity, // sparsity control
15 int64_t vdim // A->vdim, or -1 to ignore this condition
16 )
17 {
18
19 //--------------------------------------------------------------------------
20 // ensure the sparsity control is in range 1 to 15
21 //--------------------------------------------------------------------------
22
23 sparsity = sparsity & GxB_ANY_SPARSITY ;
24 if (sparsity == GxB_DEFAULT)
25 {
26 // if zero, set to auto sparsity
27 sparsity = GxB_AUTO_SPARSITY ;
28 }
29
30 //--------------------------------------------------------------------------
31 // ensure vectors and scalars cannot become hypersparse
32 //--------------------------------------------------------------------------
33
34 if ((vdim == 0 || vdim == 1) && (sparsity & GxB_HYPERSPARSE))
35 {
36 // a GxB_Scalar, GrB_Vector, or a GrB_Matrix with a single vector,
37 // cannot be converted to hypersparse. If the sparsity control
38 // allows for the hypersparse case, disable it and enable the
39 // sparse case instead.
40 sparsity = sparsity & (~GxB_HYPERSPARSE) ;
41 sparsity = sparsity | GxB_SPARSE ;
42 }
43
44 //--------------------------------------------------------------------------
45 // return revised sparsity control
46 //--------------------------------------------------------------------------
47
48 return (sparsity) ;
49 }
50
51