File: gb_mxstring_to_format.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 (122 lines) | stat: -rw-r--r-- 3,439 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//------------------------------------------------------------------------------
// gb_mxstring_to_format: get the format from a built-in string
//------------------------------------------------------------------------------

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

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

// Valid format strings:

//  'by row'            auto sparsity for these 2 strings
//  'by col'

//  'sparse by row'
//  'hypersparse by row'
//  'bitmap by row'
//  'full by row'

//  'sparse by col'
//  'hypersparse by col'
//  'bitmap by col'
//  'full by col'

//  'sparse'            fmt is GxB_BY_COL for these four strings
//  'hypersparse'
//  'bitmap'
//  'full'

// The sparsity formats can be combined as well, such as:
//  'sparse/hyper by row'

// hypersparse can be abbreviated as 'hyper'

#include "gb_interface.h"

bool gb_mxstring_to_format      // true if a valid format is found
(
    // input
    const mxArray *mxformat,    // built-in string, 'by row' or 'by col'
    // output
    GxB_Format_Value *fmt,
    int *sparsity
)
{

    bool valid = false ;
    (*fmt) = GxB_BY_COL ;
    (*sparsity) = 0 ;
    #define LEN 256
    char format_string [LEN+2] ;
    gb_mxstring_to_string (format_string, LEN, mxformat, "format") ;

    //--------------------------------------------------------------------------
    // look for trailing "by row" or "by col", and set format if found
    //--------------------------------------------------------------------------

    int len = strlen (format_string) ;
    if (len >= 6)
    {
        if (MATCH (format_string + len - 6, "by row"))
        { 
            valid = true ;
            (*fmt) = GxB_BY_ROW ;
            len = len - 6 ;
            format_string [GB_IMAX (0, len-1)] = '\0' ;
        }
        else if (MATCH (format_string + len - 6, "by col"))
        { 
            valid = true ;
            (*fmt) = GxB_BY_COL ;
            len = len - 6 ;
            format_string [GB_IMAX (0, len-1)] = '\0' ;
        }
    }

    //--------------------------------------------------------------------------
    // parse the format for hypersparse/sparse/bitmap/full sparsity tokens
    //--------------------------------------------------------------------------

    int s = 0 ;
    int kstart = 0 ;
    for (int k = 0 ; k <= len ; k++)
    {
        if (format_string [k] == '/' || format_string [k] == '\0')
        {
            // mark the end of prior token
            format_string [k] = '\0' ;

            // null-terminated token is contained in format_string [kstart:k]
            if (MATCH (format_string + kstart, "sparse"))
            { 
                s += GxB_SPARSE ;
            }
            else if (MATCH (format_string + kstart, "hypersparse") ||
                     MATCH (format_string + kstart, "hyper"))
            { 
                s += GxB_HYPERSPARSE ;
            }
            else if (MATCH (format_string + kstart, "bitmap"))
            { 
                s += GxB_BITMAP ;
            }
            else if (MATCH (format_string + kstart, "full"))
            { 
                s += GxB_FULL ;
            }

            // advance to the next token
            kstart = k+1 ;
        }
    }

    if (s > 0)
    {
        valid = true ;
        (*sparsity) = s ;
    }

    return (valid) ;
}