summaryrefslogtreecommitdiffstats
path: root/src/misc/espresso/part.c
blob: 42843aebd209d24356d345763468140b2147fafa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
 * Revision Control Information
 *
 * $Source$
 * $Author$
 * $Revision$
 * $Date$
 *
 */
#include "mincov_int.h"

static int visit_col();

static void
copy_row(A, prow)
register sm_matrix *A;
register sm_row *prow;
{
    register sm_element *p;

    for(p = prow->first_col; p != 0; p = p->next_col) {
    (void) sm_insert(A, p->row_num, p->col_num);
    }
}


static int
visit_row(A, prow, rows_visited, cols_visited)
sm_matrix *A;
sm_row *prow;
int *rows_visited;
int *cols_visited;
{
    sm_element *p;
    sm_col *pcol;

    if (! prow->flag) {
    prow->flag = 1;
    (*rows_visited)++;
    if (*rows_visited == A->nrows) {
        return 1;
    }
    for(p = prow->first_col; p != 0; p = p->next_col) {
        pcol = sm_get_col(A, p->col_num);
        if (! pcol->flag) {
        if (visit_col(A, pcol, rows_visited, cols_visited)) {
            return 1;
        }
        }
    }
    }
    return 0;
}


static int
visit_col(A, pcol, rows_visited, cols_visited)
sm_matrix *A;
sm_col *pcol;
int *rows_visited;
int *cols_visited;
{
    sm_element *p;
    sm_row *prow;

    if (! pcol->flag) {
    pcol->flag = 1;
    (*cols_visited)++;
    if (*cols_visited == A->ncols) {
        return 1;
    }
    for(p = pcol->first_row; p != 0; p = p->next_row) {
        prow = sm_get_row(A, p->row_num);
        if (! prow->flag) {
        if (visit_row(A, prow, rows_visited, cols_visited)) {
            return 1;
        }
        }
    }
    }
    return 0;
}

int
sm_block_partition(A, L, R)
sm_matrix *A;
sm_matrix **L, **R;
{
    int cols_visited, rows_visited;
    register sm_row *prow;
    register sm_col *pcol;

    /* Avoid the trivial case */
    if (A->nrows == 0) {
    return 0;
    }

    /* Reset the visited flags for each row and column */
    for(prow = A->first_row; prow != 0; prow = prow->next_row) {
    prow->flag = 0;
    }
    for(pcol = A->first_col; pcol != 0; pcol = pcol->next_col) {
    pcol->flag = 0;
    }

    cols_visited = rows_visited = 0;
    if (visit_row(A, A->first_row, &rows_visited, &cols_visited)) {
    /* we found all of the rows */
    return 0;
    } else {
    *L = sm_alloc();
    *R = sm_alloc();
    for(prow = A->first_row; prow != 0; prow = prow->next_row) {
        if (prow->flag) {
        copy_row(*L, prow);
        } else {
        copy_row(*R, prow);
        }
    }
    return 1;
    }
}