1 //------------------------------------------------------------------------------
2 // GB_mex_Matrix_subref: C=A(I,J)
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_mex.h"
11 
12 #define USAGE "C = GB_mex_Matrix_subref (A, I, J)"
13 
14 #define FREE_ALL                        \
15 {                                       \
16     GrB_Matrix_free_(&A) ;              \
17     GrB_Matrix_free_(&C) ;              \
18     GB_mx_put_global (true) ;           \
19 }
20 
mexFunction(int nargout,mxArray * pargout[],int nargin,const mxArray * pargin[])21 void mexFunction
22 (
23     int nargout,
24     mxArray *pargout [ ],
25     int nargin,
26     const mxArray *pargin [ ]
27 )
28 {
29     struct GB_Matrix_opaque C_header ;
30     GrB_Matrix C = GB_clear_static_header (&C_header) ;
31 
32     bool malloc_debug = GB_mx_get_global (true) ;
33     GrB_Matrix A = NULL ;
34     GrB_Index *I = NULL, ni = 0, I_range [3] ;
35     GrB_Index *J = NULL, nj = 0, J_range [3] ;
36     bool ignore ;
37 
38     // check inputs
39     GB_CONTEXT (USAGE) ;
40     if (nargout > 1 || nargin != 3)
41     {
42         mexErrMsgTxt ("Usage: " USAGE) ;
43     }
44 
45     #define GET_DEEP_COPY ;
46     #define FREE_DEEP_COPY ;
47 
48     // get A (shallow copy)
49     A = GB_mx_mxArray_to_Matrix (pargin [0], "A", false, true) ;
50     if (A == NULL)
51     {
52         FREE_ALL ;
53         mexErrMsgTxt ("A failed") ;
54     }
55 
56     // get I
57     if (!GB_mx_mxArray_to_indices (&I, pargin [1], &ni, I_range, &ignore))
58     {
59         FREE_ALL ;
60         mexErrMsgTxt ("I failed") ;
61     }
62 
63     // get J
64     if (!GB_mx_mxArray_to_indices (&J, pargin [2], &nj, J_range, &ignore))
65     {
66         FREE_ALL ;
67         mexErrMsgTxt ("J failed") ;
68     }
69 
70     // C = A(I,J), numeric not symbolic
71     METHOD (GB_subref (C, true, A, I, ni, J, nj, false, Context)) ;
72 
73     // return C to MATLAB
74     pargout [0] = GB_mx_Matrix_to_mxArray (&C, "C subref result", false) ;
75 
76     FREE_ALL ;
77 }
78 
79