1 //------------------------------------------------------------------------------
2 // gbextractvalues: extract all entries from a GraphBLAS matrix
3 //------------------------------------------------------------------------------
4 
5 // SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
6 // SPDX-License-Identifier: GPL-3.0-or-later
7 
8 //------------------------------------------------------------------------------
9 
10 // Usage:
11 
12 // X = gbextractvalues (A)
13 
14 #include "gb_matlab.h"
15 
16 #define USAGE "usage: X = GrB.extractvalues (A)"
17 
mexFunction(int nargout,mxArray * pargout[],int nargin,const mxArray * pargin[])18 void mexFunction
19 (
20     int nargout,
21     mxArray *pargout [ ],
22     int nargin,
23     const mxArray *pargin [ ]
24 )
25 {
26 
27     //--------------------------------------------------------------------------
28     // check inputs
29     //--------------------------------------------------------------------------
30 
31     gb_usage (nargin == 1 && nargout <= 1, USAGE) ;
32 
33     //--------------------------------------------------------------------------
34     // get the matrix
35     //--------------------------------------------------------------------------
36 
37     GrB_Matrix A = gb_get_shallow (pargin [0]) ;
38     GrB_Index nvals ;
39     OK (GrB_Matrix_nvals (&nvals, A)) ;
40     GrB_Type xtype ;
41     OK (GxB_Matrix_type (&xtype, A)) ;
42     GrB_Index s = MAX (nvals, 1) ;
43 
44     //--------------------------------------------------------------------------
45     // extract the tuples
46     //--------------------------------------------------------------------------
47 
48     if (xtype == GrB_BOOL)
49     {
50         bool *X = mxMalloc (s * sizeof (bool)) ;
51         OK (GrB_Matrix_extractTuples_BOOL (NULL, NULL, X, &nvals, A)) ;
52         pargout [0] = gb_export_to_mxfull (&X, nvals, 1, GrB_BOOL) ;
53     }
54     else if (xtype == GrB_INT8)
55     {
56         int8_t *X = mxMalloc (s * sizeof (int8_t)) ;
57         OK (GrB_Matrix_extractTuples_INT8 (NULL, NULL, X, &nvals, A)) ;
58         pargout [0] = gb_export_to_mxfull (&X, nvals, 1, GrB_INT8) ;
59     }
60     else if (xtype == GrB_INT16)
61     {
62         int16_t *X = mxMalloc (s * sizeof (int16_t)) ;
63         OK (GrB_Matrix_extractTuples_INT16 (NULL, NULL, X, &nvals, A)) ;
64         pargout [0] = gb_export_to_mxfull (&X, nvals, 1, GrB_INT16) ;
65     }
66     else if (xtype == GrB_INT32)
67     {
68         int32_t *X = mxMalloc (s * sizeof (int32_t)) ;
69         OK (GrB_Matrix_extractTuples_INT32 (NULL, NULL, X, &nvals, A)) ;
70         pargout [0] = gb_export_to_mxfull (&X, nvals, 1, GrB_INT32) ;
71     }
72     else if (xtype == GrB_INT64)
73     {
74         int64_t *X = mxMalloc (s * sizeof (int64_t)) ;
75         OK (GrB_Matrix_extractTuples_INT64 (NULL, NULL, X, &nvals, A)) ;
76         pargout [0] = gb_export_to_mxfull (&X, nvals, 1, GrB_INT64) ;
77     }
78     else if (xtype == GrB_UINT8)
79     {
80         uint8_t *X = mxMalloc (s * sizeof (uint8_t)) ;
81         OK (GrB_Matrix_extractTuples_UINT8 (NULL, NULL, X, &nvals, A)) ;
82         pargout [0] = gb_export_to_mxfull (&X, nvals, 1, GrB_UINT8) ;
83     }
84     else if (xtype == GrB_UINT16)
85     {
86         uint16_t *X = mxMalloc (s * sizeof (uint16_t)) ;
87         OK (GrB_Matrix_extractTuples_UINT16 (NULL, NULL, X, &nvals, A)) ;
88         pargout [0] = gb_export_to_mxfull (&X, nvals, 1, GrB_UINT16) ;
89     }
90     else if (xtype == GrB_UINT32)
91     {
92         uint32_t *X = mxMalloc (s * sizeof (uint32_t)) ;
93         OK (GrB_Matrix_extractTuples_UINT32 (NULL, NULL, X, &nvals, A)) ;
94         pargout [0] = gb_export_to_mxfull (&X, nvals, 1, GrB_UINT32) ;
95     }
96     else if (xtype == GrB_UINT64)
97     {
98         uint64_t *X = mxMalloc (s * sizeof (uint64_t)) ;
99         OK (GrB_Matrix_extractTuples_UINT64 (NULL, NULL, X, &nvals, A)) ;
100         pargout [0] = gb_export_to_mxfull (&X, nvals, 1, GrB_UINT64) ;
101     }
102 
103     else if (xtype == GrB_FP32)
104     {
105         float *X = mxMalloc (s * sizeof (float)) ;
106         OK (GrB_Matrix_extractTuples_FP32 (NULL, NULL, X, &nvals, A)) ;
107         pargout [0] = gb_export_to_mxfull (&X, nvals, 1, GrB_FP32) ;
108     }
109     else if (xtype == GrB_FP64)
110     {
111         double *X = mxMalloc (s * sizeof (double)) ;
112         OK (GrB_Matrix_extractTuples_FP64 (NULL, NULL, X, &nvals, A)) ;
113         pargout [0] = gb_export_to_mxfull (&X, nvals, 1, GrB_FP64) ;
114     }
115     else if (xtype == GxB_FC32)
116     {
117         GxB_FC32_t *X = mxMalloc (s * sizeof (GxB_FC32_t)) ;
118         OK (GxB_Matrix_extractTuples_FC32 (NULL, NULL, X, &nvals, A)) ;
119         pargout [0] = gb_export_to_mxfull (&X, nvals, 1, GxB_FC32) ;
120     }
121     else if (xtype == GxB_FC64)
122     {
123         GxB_FC64_t *X = mxMalloc (s * sizeof (GxB_FC64_t)) ;
124         OK (GxB_Matrix_extractTuples_FC64 (NULL, NULL, X, &nvals, A)) ;
125         pargout [0] = gb_export_to_mxfull (&X, nvals, 1, GxB_FC64) ;
126     }
127     else
128     {
129         ERROR ("unsupported type") ;
130     }
131 
132     //--------------------------------------------------------------------------
133     // free workspace
134     //--------------------------------------------------------------------------
135 
136     OK (GrB_Matrix_free (&A)) ;
137     GB_WRAPUP ;
138 }
139 
140