File: GB_mx_mxArray_to_indices.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 (117 lines) | stat: -rw-r--r-- 3,766 bytes parent folder | download | duplicates (3)
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
//------------------------------------------------------------------------------
// GB_mx_mxArray_to_indices
//------------------------------------------------------------------------------

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

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

// Get a list of indices from a built-in array

#include "GB_mex.h"

bool GB_mx_mxArray_to_indices       // true if successful, false otherwise
(
    GrB_Index **handle,             // index array returned
    const mxArray *I_builtin,       // built-in mxArray to get
    GrB_Index *ni,                  // length of I, or special
    GrB_Index Icolon [3],           // for all but GB_LIST
    bool *I_is_list                 // true if GB_LIST
)
{

    (*handle) = NULL ;

    mxArray *X ;
    GrB_Index *I ;

    if (I_builtin == NULL || mxIsEmpty (I_builtin))
    {
        I = (GrB_Index *) GrB_ALL ;       // like the ":" in C=A(:,j)
        (*ni) = 0 ;
        (*handle) = I ;
        (*I_is_list) = false ;
        // Icolon not used
    }
    else
    {
        if (mxIsStruct (I_builtin))
        {
            // a struct with 3 integers: I.begin, I.inc, I.end
            (*I_is_list) = false ;

            // look for I.begin (required)
            int fieldnumber = mxGetFieldNumber (I_builtin, "begin") ;
            if (fieldnumber < 0)
            {
                mexWarnMsgIdAndTxt ("GB:warn","I.begin missing") ;
                return (false) ;
            }
            X = mxGetFieldByNumber (I_builtin, 0, fieldnumber) ;
            Icolon [GxB_BEGIN] = (int64_t) mxGetScalar (X) ;

            // look for I.end (required)
            fieldnumber = mxGetFieldNumber (I_builtin, "end") ;
            if (fieldnumber < 0)
            {
                mexWarnMsgIdAndTxt ("GB:warn","I.end missing") ;
                return (false) ;
            }
            mxArray *X ;
            X = mxGetFieldByNumber (I_builtin, 0, fieldnumber) ;
            Icolon [GxB_END] = (int64_t) mxGetScalar (X) ;

            // look for I.inc (optional)
            fieldnumber = mxGetFieldNumber (I_builtin, "inc") ;
            if (fieldnumber < 0)
            {
                (*ni) = GxB_RANGE ;
                Icolon [GxB_INC] = 1 ;
            }
            else
            {
                // 
                X = mxGetFieldByNumber (I_builtin, 0, fieldnumber) ;
                int64_t iinc = (int64_t) mxGetScalar (X) ;
                if (iinc == 0)
                {
                    // this can be either a stride, or backwards.  Either 
                    // one works the same, but try a mixture, just for testing.
                    (*ni) = (Icolon [GxB_BEGIN] % 2) ?
                        GxB_STRIDE : GxB_BACKWARDS ;
                    Icolon [GxB_INC] = 0 ;
                }
                else if (iinc > 0)
                {
                    (*ni) = GxB_STRIDE ;
                    Icolon [GxB_INC] = iinc ;
                }
                else
                {
                    // GraphBLAS must be given the magnitude of the stride
                    (*ni) = GxB_BACKWARDS ;
                    Icolon [GxB_INC] = -iinc ;
                }
            }
            (*handle) = Icolon ;

        }
        else
        {
            if (!mxIsClass (I_builtin, "uint64"))
            {
                mexWarnMsgIdAndTxt ("GB:warn","indices must be uint64") ;
                return (false) ;
            }

            (*I_is_list) = true ;
            I = mxGetData (I_builtin) ;
            (*ni) = (uint64_t) mxGetNumberOfElements (I_builtin) ;
            (*handle) = I ;
        }
    }

    return (true) ;
}