Skip to content
Snippets Groups Projects
cs_fkeep.c 961 B
Newer Older
  • Learn to ignore specific revisions
  • Lubomir Riha's avatar
    Lubomir Riha committed
    #include "cs.h"
    /* drop entries for which fkeep(A(i,j)) is false; return nz if OK, else -1 */
    CS_INT cs_fkeep (cs *A, CS_INT (*fkeep) (CS_INT, CS_INT, CS_ENTRY, void *), void *other)
    {
        CS_INT j, p, nz = 0, n, *Ap, *Ai ;
        CS_ENTRY *Ax ;
        if (!CS_CSC (A) || !fkeep) return (-1) ;    /* check inputs */
        n = A->n ; Ap = A->p ; Ai = A->i ; Ax = A->x ;
        for (j = 0 ; j < n ; j++)
        {
            p = Ap [j] ;                        /* get current location of col j */
            Ap [j] = nz ;                       /* record new location of col j */
            for ( ; p < Ap [j+1] ; p++)
            {
                if (fkeep (Ai [p], j, Ax ? Ax [p] : 1, other))
                {
                    if (Ax) Ax [nz] = Ax [p] ;  /* keep A(i,j) */
                    Ai [nz++] = Ai [p] ;
                }
            }
        }
        Ap [n] = nz ;                           /* finalize A */
        cs_sprealloc (A, 0) ;                   /* remove extra space from A */
        return (nz) ;
    }