1 #include "cs.h"
2 /* drop entries for which fkeep(A(i,j)) is false; return nz if OK, else -1 */
cs_fkeep(cs * A,CS_INT (* fkeep)(CS_INT,CS_INT,CS_ENTRY,void *),void * other)3 CS_INT cs_fkeep (cs *A, CS_INT (*fkeep) (CS_INT, CS_INT, CS_ENTRY, void *), void *other)
4 {
5     CS_INT j, p, nz = 0, n, *Ap, *Ai ;
6     CS_ENTRY *Ax ;
7     if (!CS_CSC (A) || !fkeep) return (-1) ;    /* check inputs */
8     n = A->n ; Ap = A->p ; Ai = A->i ; Ax = A->x ;
9     for (j = 0 ; j < n ; j++)
10     {
11         p = Ap [j] ;                        /* get current location of col j */
12         Ap [j] = nz ;                       /* record new location of col j */
13         for ( ; p < Ap [j+1] ; p++)
14         {
15             if (fkeep (Ai [p], j, Ax ? Ax [p] : 1, other))
16             {
17                 if (Ax) Ax [nz] = Ax [p] ;  /* keep A(i,j) */
18                 Ai [nz++] = Ai [p] ;
19             }
20         }
21     }
22     Ap [n] = nz ;                           /* finalize A */
23     cs_sprealloc (A, 0) ;                   /* remove extra space from A */
24     return (nz) ;
25 }
26