00001 #include "cs.h"
00002
00003 #define HEAD(k,j) (ata ? head [k] : j)
00004 #define NEXT(J) (ata ? next [J] : -1)
00005 static void init_ata (cs *AT, const int *post, int *w, int **head, int **next)
00006 {
00007 int i, k, p, m = AT->n, n = AT->m, *ATp = AT->p, *ATi = AT->i ;
00008 *head = w+4*n, *next = w+5*n+1 ;
00009 for (k = 0 ; k < n ; k++) w [post [k]] = k ;
00010 for (i = 0 ; i < m ; i++)
00011 {
00012 for (k = n, p = ATp[i] ; p < ATp[i+1] ; p++) k = CS_MIN (k, w [ATi[p]]);
00013 (*next) [i] = (*head) [k] ;
00014 (*head) [k] = i ;
00015 }
00016 }
00017 int *cs_counts (const cs *A, const int *parent, const int *post, int ata)
00018 {
00019 int i, j, k, n, m, J, s, p, q, jleaf, *ATp, *ATi, *maxfirst, *prevleaf,
00020 *ancestor, *head = NULL, *next = NULL, *colcount, *w, *first, *delta ;
00021 cs *AT ;
00022 if (!CS_CSC (A) || !parent || !post) return (NULL) ;
00023 m = A->m ; n = A->n ;
00024 s = 4*n + (ata ? (n+m+1) : 0) ;
00025 delta = colcount = (int*) cs_malloc (n, sizeof (int)) ;
00026 w = (int*) cs_malloc (s, sizeof (int)) ;
00027 AT = cs_transpose (A, 0) ;
00028 if (!AT || !colcount || !w) return (cs_idone (colcount, AT, w, 0)) ;
00029 ancestor = w ; maxfirst = w+n ; prevleaf = w+2*n ; first = w+3*n ;
00030 for (k = 0 ; k < s ; k++) w [k] = -1 ;
00031 for (k = 0 ; k < n ; k++)
00032 {
00033 j = post [k] ;
00034 delta [j] = (first [j] == -1) ? 1 : 0 ;
00035 for ( ; j != -1 && first [j] == -1 ; j = parent [j]) first [j] = k ;
00036 }
00037 ATp = AT->p ; ATi = AT->i ;
00038 if (ata) init_ata (AT, post, w, &head, &next) ;
00039 for (i = 0 ; i < n ; i++) ancestor [i] = i ;
00040 for (k = 0 ; k < n ; k++)
00041 {
00042 j = post [k] ;
00043 if (parent [j] != -1) delta [parent [j]]-- ;
00044 for (J = HEAD (k,j) ; J != -1 ; J = NEXT (J))
00045 {
00046 for (p = ATp [J] ; p < ATp [J+1] ; p++)
00047 {
00048 i = ATi [p] ;
00049 q = cs_leaf (i, j, first, maxfirst, prevleaf, ancestor, &jleaf);
00050 if (jleaf >= 1) delta [j]++ ;
00051 if (jleaf == 2) delta [q]-- ;
00052 }
00053 }
00054 if (parent [j] != -1) ancestor [j] = parent [j] ;
00055 }
00056 for (j = 0 ; j < n ; j++)
00057 {
00058 if (parent [j] != -1) colcount [parent [j]] += colcount [j] ;
00059 }
00060 return (cs_idone (colcount, AT, w, 1)) ;
00061 }