1 #include "cs.h"
2 /* allocate a sparse matrix (triplet form or compressed-column form) */
cs_spalloc(CS_INT m,CS_INT n,CS_INT nzmax,CS_INT values,CS_INT triplet)3 cs *cs_spalloc (CS_INT m, CS_INT n, CS_INT nzmax, CS_INT values, CS_INT triplet)
4 {
5     cs *A = cs_calloc (1, sizeof (cs)) ;    /* allocate the cs struct */
6     if (!A) return (NULL) ;                 /* out of memory */
7     A->m = m ;                              /* define dimensions and nzmax */
8     A->n = n ;
9     A->nzmax = nzmax = CS_MAX (nzmax, 1) ;
10     A->nz = triplet ? 0 : -1 ;              /* allocate triplet or comp.col */
11     A->p = cs_malloc (triplet ? nzmax : n+1, sizeof (CS_INT)) ;
12     A->i = cs_malloc (nzmax, sizeof (CS_INT)) ;
13     A->x = values ? cs_malloc (nzmax, sizeof (CS_ENTRY)) : NULL ;
14     return ((!A->p || !A->i || (values && !A->x)) ? cs_spfree (A) : A) ;
15 }
16 
17 /* change the max # of entries sparse matrix */
cs_sprealloc(cs * A,CS_INT nzmax)18 CS_INT cs_sprealloc (cs *A, CS_INT nzmax)
19 {
20     CS_INT ok, oki, okj = 1, okx = 1 ;
21     if (!A) return (0) ;
22     if (nzmax <= 0) nzmax = (CS_CSC (A)) ? (A->p [A->n]) : A->nz ;
23     nzmax = CS_MAX (nzmax, 1) ;
24     A->i = cs_realloc (A->i, nzmax, sizeof (CS_INT), &oki) ;
25     if (CS_TRIPLET (A)) A->p = cs_realloc (A->p, nzmax, sizeof (CS_INT), &okj) ;
26     if (A->x) A->x = cs_realloc (A->x, nzmax, sizeof (CS_ENTRY), &okx) ;
27     ok = (oki && okj && okx) ;
28     if (ok) A->nzmax = nzmax ;
29     return (ok) ;
30 }
31 
32 /* free a sparse matrix */
cs_spfree(cs * A)33 cs *cs_spfree (cs *A)
34 {
35     if (!A) return (NULL) ;     /* do nothing if A already NULL */
36     cs_free (A->p) ;
37     cs_free (A->i) ;
38     cs_free (A->x) ;
39     return ((cs *) cs_free (A)) ;   /* free the cs struct and return NULL */
40 }
41 
42 /* free a numeric factorization */
cs_nfree(csn * N)43 csn *cs_nfree (csn *N)
44 {
45     if (!N) return (NULL) ;     /* do nothing if N already NULL */
46     cs_spfree (N->L) ;
47     cs_spfree (N->U) ;
48     cs_free (N->pinv) ;
49     cs_free (N->B) ;
50     return ((csn *) cs_free (N)) ;  /* free the csn struct and return NULL */
51 }
52 
53 /* free a symbolic factorization */
cs_sfree(css * S)54 css *cs_sfree (css *S)
55 {
56     if (!S) return (NULL) ;     /* do nothing if S already NULL */
57     cs_free (S->pinv) ;
58     cs_free (S->q) ;
59     cs_free (S->parent) ;
60     cs_free (S->cp) ;
61     cs_free (S->leftmost) ;
62     return ((css *) cs_free (S)) ;  /* free the css struct and return NULL */
63 }
64 
65 /* allocate a cs_dmperm or cs_scc result */
cs_dalloc(CS_INT m,CS_INT n)66 csd *cs_dalloc (CS_INT m, CS_INT n)
67 {
68     csd *D ;
69     D = cs_calloc (1, sizeof (csd)) ;
70     if (!D) return (NULL) ;
71     D->p = cs_malloc (m, sizeof (CS_INT)) ;
72     D->r = cs_malloc (m+6, sizeof (CS_INT)) ;
73     D->q = cs_malloc (n, sizeof (CS_INT)) ;
74     D->s = cs_malloc (n+6, sizeof (CS_INT)) ;
75     return ((!D->p || !D->r || !D->q || !D->s) ? cs_dfree (D) : D) ;
76 }
77 
78 /* free a cs_dmperm or cs_scc result */
cs_dfree(csd * D)79 csd *cs_dfree (csd *D)
80 {
81     if (!D) return (NULL) ;     /* do nothing if D already NULL */
82     cs_free (D->p) ;
83     cs_free (D->q) ;
84     cs_free (D->r) ;
85     cs_free (D->s) ;
86     return ((csd *) cs_free (D)) ;  /* free the csd struct and return NULL */
87 }
88 
89 /* free workspace and return a sparse matrix result */
cs_done(cs * C,void * w,void * x,CS_INT ok)90 cs *cs_done (cs *C, void *w, void *x, CS_INT ok)
91 {
92     cs_free (w) ;                       /* free workspace */
93     cs_free (x) ;
94     return (ok ? C : cs_spfree (C)) ;   /* return result if OK, else free it */
95 }
96 
97 /* free workspace and return CS_INT array result */
cs_idone(CS_INT * p,cs * C,void * w,CS_INT ok)98 CS_INT *cs_idone (CS_INT *p, cs *C, void *w, CS_INT ok)
99 {
100     cs_spfree (C) ;                     /* free temporary matrix */
101     cs_free (w) ;                       /* free workspace */
102     return (ok ? p : (CS_INT *) cs_free (p)) ; /* return result, or free it */
103 }
104 
105 /* free workspace and return a numeric factorization (Cholesky, LU, or QR) */
cs_ndone(csn * N,cs * C,void * w,void * x,CS_INT ok)106 csn *cs_ndone (csn *N, cs *C, void *w, void *x, CS_INT ok)
107 {
108     cs_spfree (C) ;                     /* free temporary matrix */
109     cs_free (w) ;                       /* free workspace */
110     cs_free (x) ;
111     return (ok ? N : cs_nfree (N)) ;    /* return result if OK, else free it */
112 }
113 
114 /* free workspace and return a csd result */
cs_ddone(csd * D,cs * C,void * w,CS_INT ok)115 csd *cs_ddone (csd *D, cs *C, void *w, CS_INT ok)
116 {
117     cs_spfree (C) ;                     /* free temporary matrix */
118     cs_free (w) ;                       /* free workspace */
119     return (ok ? D : cs_dfree (D)) ;    /* return result if OK, else free it */
120 }
121