1 | #ifndef SPARSE_H |
---|
2 | #define SPARSE_H |
---|
3 | |
---|
4 | #include "util.h" |
---|
5 | |
---|
6 | /* hack to fix conflict with libX11.a */ |
---|
7 | #define sm_alloc sm_allocate |
---|
8 | #define sm_free sm_free_space |
---|
9 | |
---|
10 | /* |
---|
11 | * sparse.h -- sparse matrix package header file |
---|
12 | */ |
---|
13 | |
---|
14 | typedef struct sm_element_struct sm_element; |
---|
15 | typedef struct sm_row_struct sm_row; |
---|
16 | typedef struct sm_col_struct sm_col; |
---|
17 | typedef struct sm_matrix_struct sm_matrix; |
---|
18 | |
---|
19 | |
---|
20 | /* |
---|
21 | * sparse matrix element |
---|
22 | */ |
---|
23 | struct sm_element_struct { |
---|
24 | int row_num; /* row number of this element */ |
---|
25 | int col_num; /* column number of this element */ |
---|
26 | sm_element *next_row; /* next row in this column */ |
---|
27 | sm_element *prev_row; /* previous row in this column */ |
---|
28 | sm_element *next_col; /* next column in this row */ |
---|
29 | sm_element *prev_col; /* previous column in this row */ |
---|
30 | char *user_word; /* user-defined word */ |
---|
31 | }; |
---|
32 | |
---|
33 | |
---|
34 | /* |
---|
35 | * row header |
---|
36 | */ |
---|
37 | struct sm_row_struct { |
---|
38 | int row_num; /* the row number */ |
---|
39 | int length; /* number of elements in this row */ |
---|
40 | int flag; /* user-defined word */ |
---|
41 | sm_element *first_col; /* first element in this row */ |
---|
42 | sm_element *last_col; /* last element in this row */ |
---|
43 | sm_row *next_row; /* next row (in sm_matrix linked list) */ |
---|
44 | sm_row *prev_row; /* previous row (in sm_matrix linked list) */ |
---|
45 | char *user_word; /* user-defined word */ |
---|
46 | }; |
---|
47 | |
---|
48 | |
---|
49 | /* |
---|
50 | * column header |
---|
51 | */ |
---|
52 | struct sm_col_struct { |
---|
53 | int col_num; /* the column number */ |
---|
54 | int length; /* number of elements in this column */ |
---|
55 | int flag; /* user-defined word */ |
---|
56 | sm_element *first_row; /* first element in this column */ |
---|
57 | sm_element *last_row; /* last element in this column */ |
---|
58 | sm_col *next_col; /* next column (in sm_matrix linked list) */ |
---|
59 | sm_col *prev_col; /* prev column (in sm_matrix linked list) */ |
---|
60 | char *user_word; /* user-defined word */ |
---|
61 | }; |
---|
62 | |
---|
63 | |
---|
64 | /* |
---|
65 | * A sparse matrix |
---|
66 | */ |
---|
67 | struct sm_matrix_struct { |
---|
68 | sm_row **rows; /* pointer to row headers (by row #) */ |
---|
69 | int rows_size; /* alloc'ed size of above array */ |
---|
70 | sm_col **cols; /* pointer to column headers (by col #) */ |
---|
71 | int cols_size; /* alloc'ed size of above array */ |
---|
72 | sm_row *first_row; /* first row (linked list of all rows) */ |
---|
73 | sm_row *last_row; /* last row (linked list of all rows) */ |
---|
74 | int nrows; /* number of rows */ |
---|
75 | sm_col *first_col; /* first column (linked list of columns) */ |
---|
76 | sm_col *last_col; /* last column (linked list of columns) */ |
---|
77 | int ncols; /* number of columns */ |
---|
78 | char *user_word; /* user-defined word */ |
---|
79 | }; |
---|
80 | |
---|
81 | |
---|
82 | #define sm_get_col(A, colnum) \ |
---|
83 | (((colnum) >= 0 && (colnum) < (A)->cols_size) ? \ |
---|
84 | (A)->cols[colnum] : (sm_col *) 0) |
---|
85 | |
---|
86 | #define sm_get_row(A, rownum) \ |
---|
87 | (((rownum) >= 0 && (rownum) < (A)->rows_size) ? \ |
---|
88 | (A)->rows[rownum] : (sm_row *) 0) |
---|
89 | |
---|
90 | #define sm_foreach_row(A, prow) \ |
---|
91 | for(prow = A->first_row; prow != 0; prow = prow->next_row) |
---|
92 | |
---|
93 | #define sm_foreach_col(A, pcol) \ |
---|
94 | for(pcol = A->first_col; pcol != 0; pcol = pcol->next_col) |
---|
95 | |
---|
96 | #define sm_foreach_row_element(prow, p) \ |
---|
97 | for(p = (prow == 0) ? 0 : prow->first_col; p != 0; p = p->next_col) |
---|
98 | |
---|
99 | #define sm_foreach_col_element(pcol, p) \ |
---|
100 | for(p = (pcol == 0) ? 0 : pcol->first_row; p != 0; p = p->next_row) |
---|
101 | |
---|
102 | #define sm_put(x, val) \ |
---|
103 | (x->user_word = (char *) val) |
---|
104 | |
---|
105 | #define sm_get(type, x) \ |
---|
106 | ((type) (x->user_word)) |
---|
107 | |
---|
108 | EXTERN sm_matrix *sm_allocate ARGS((void)); |
---|
109 | EXTERN sm_matrix *sm_alloc_size ARGS((int, int)); |
---|
110 | EXTERN void sm_free_space ARGS((sm_matrix *)); |
---|
111 | EXTERN sm_matrix *sm_dup ARGS((sm_matrix *)); |
---|
112 | EXTERN void sm_resize ARGS((sm_matrix *, int, int)); |
---|
113 | EXTERN sm_element *sm_insert ARGS((sm_matrix *, int, int)); |
---|
114 | EXTERN sm_element *sm_find ARGS((sm_matrix *, int, int)); |
---|
115 | EXTERN void sm_remove ARGS((sm_matrix *, int, int)); |
---|
116 | EXTERN void sm_remove_element ARGS((sm_matrix *, sm_element *)); |
---|
117 | EXTERN void sm_delrow ARGS((sm_matrix *, int)); |
---|
118 | EXTERN void sm_delcol ARGS((sm_matrix *, int)); |
---|
119 | EXTERN void sm_copy_row ARGS((sm_matrix *, int, sm_row *)); |
---|
120 | EXTERN void sm_copy_col ARGS((sm_matrix *, int, sm_col *)); |
---|
121 | EXTERN sm_row *sm_longest_row ARGS((sm_matrix *)); |
---|
122 | EXTERN sm_col *sm_longest_col ARGS((sm_matrix *)); |
---|
123 | EXTERN int sm_num_elements ARGS((sm_matrix *)); |
---|
124 | EXTERN int sm_read ARGS((FILE *, sm_matrix **)); |
---|
125 | EXTERN int sm_read_compressed ARGS((FILE *, sm_matrix **)); |
---|
126 | EXTERN void sm_write ARGS((FILE *, sm_matrix *)); |
---|
127 | EXTERN void sm_print ARGS((FILE *, sm_matrix *)); |
---|
128 | EXTERN void sm_dump ARGS((sm_matrix *, char *, int)); |
---|
129 | EXTERN void sm_cleanup ARGS((void)); |
---|
130 | |
---|
131 | EXTERN sm_col *sm_col_alloc ARGS((void)); |
---|
132 | EXTERN void sm_col_free ARGS((sm_col *)); |
---|
133 | EXTERN sm_col *sm_col_dup ARGS((sm_col *)); |
---|
134 | EXTERN sm_element *sm_col_insert ARGS((sm_col *, int)); |
---|
135 | EXTERN void sm_col_remove ARGS((sm_col *, int)); |
---|
136 | EXTERN sm_element *sm_col_find ARGS((sm_col *, int)); |
---|
137 | EXTERN int sm_col_contains ARGS((sm_col *, sm_col *)); |
---|
138 | EXTERN int sm_col_intersects ARGS((sm_col *, sm_col *)); |
---|
139 | EXTERN int sm_col_compare ARGS((sm_col *, sm_col *)); |
---|
140 | EXTERN sm_col *sm_col_and ARGS((sm_col *, sm_col *)); |
---|
141 | EXTERN int sm_col_hash ARGS((sm_col *, int)); |
---|
142 | EXTERN void sm_col_remove_element ARGS((sm_col *, sm_element *)); |
---|
143 | EXTERN void sm_col_print ARGS((FILE *, sm_col *)); |
---|
144 | |
---|
145 | EXTERN sm_row *sm_row_alloc ARGS((void)); |
---|
146 | EXTERN void sm_row_free ARGS((sm_row *)); |
---|
147 | EXTERN sm_row *sm_row_dup ARGS((sm_row *)); |
---|
148 | EXTERN sm_element *sm_row_insert ARGS((sm_row *, int)); |
---|
149 | EXTERN void sm_row_remove ARGS((sm_row *, int)); |
---|
150 | EXTERN sm_element *sm_row_find ARGS((sm_row *, int)); |
---|
151 | EXTERN int sm_row_contains ARGS((sm_row *, sm_row *)); |
---|
152 | EXTERN int sm_row_intersects ARGS((sm_row *, sm_row *)); |
---|
153 | EXTERN int sm_row_compare ARGS((sm_row *, sm_row *)); |
---|
154 | EXTERN sm_row *sm_row_and ARGS((sm_row *, sm_row *)); |
---|
155 | EXTERN int sm_row_hash ARGS((sm_row *, int)); |
---|
156 | EXTERN void sm_row_remove_element ARGS((sm_row *, sm_element *)); |
---|
157 | EXTERN void sm_row_print ARGS((FILE *, sm_row *)); |
---|
158 | |
---|
159 | #endif |
---|