1 #include "cs_mex.h"
2 /* [p,r] = cs_scc (A) finds the strongly connected components of A */
mexFunction(int nargout,mxArray * pargout[],int nargin,const mxArray * pargin[])3 void mexFunction
4 (
5 int nargout,
6 mxArray *pargout [ ],
7 int nargin,
8 const mxArray *pargin [ ]
9 )
10 {
11 cs_dl Amatrix, *A ;
12 cs_dld *D ;
13 CS_INT n, j, *Ap2 ;
14 if (nargout > 2 || nargin != 1)
15 {
16 mexErrMsgTxt ("Usage: [p,r] = cs_scc(A)") ;
17 }
18 A = cs_dl_mex_get_sparse (&Amatrix, 1, 0, pargin [0]) ; /* get A */
19 /* cs_scc modifies A->p and then restores it (in cs_dfs). Avoid the issue
20 * of a mexFunction modifying its input (even temporarily) by making a copy
21 * of A->p. This issue does not arise in cs_dmperm, because that function
22 * applies cs_scc to a submatrix C, not to A directly. */
23 n = A->n ;
24 Ap2 = cs_dl_malloc (n+1, sizeof (CS_INT)) ;
25 for (j = 0 ; j <= n ; j++) Ap2 [j] = A->p [j] ;
26 A->p = Ap2 ;
27 D = cs_dl_scc (A) ; /* find conn. comp. */
28 pargout [0] = cs_dl_mex_put_int (D->p, n, 1, 0) ; /* return p */
29 pargout [1] = cs_dl_mex_put_int (D->r, D->nb+1, 1, 0) ; /* return r */
30 cs_dl_dfree (D) ;
31 cs_free (Ap2) ; /* free the copy of A->p */
32 }
33