00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "sparse_int.h"
00012
00013
00014
00015
00016
00017 sm_row *
00018 sm_row_alloc()
00019 {
00020 register sm_row *prow;
00021
00022 #ifdef FAST_AND_LOOSE
00023 if (sm_row_freelist == NIL(sm_row)) {
00024 prow = ALLOC(sm_row, 1);
00025 } else {
00026 prow = sm_row_freelist;
00027 sm_row_freelist = prow->next_row;
00028 }
00029 #else
00030 prow = ALLOC(sm_row, 1);
00031 #endif
00032
00033 prow->row_num = 0;
00034 prow->length = 0;
00035 prow->first_col = prow->last_col = NIL(sm_element);
00036 prow->next_row = prow->prev_row = NIL(sm_row);
00037 prow->flag = 0;
00038 prow->user_word = NIL(char);
00039 return prow;
00040 }
00041
00042
00043
00044
00045
00046
00047
00048
00049 void
00050 sm_row_free(prow)
00051 register sm_row *prow;
00052 {
00053 #if defined(FAST_AND_LOOSE) && ! defined(COLS)
00054 if (prow->first_col != NIL(sm_element)) {
00055
00056 prow->last_col->next_col = sm_element_freelist;
00057 sm_element_freelist = prow->first_col;
00058 }
00059
00060
00061 prow->next_row = sm_row_freelist;
00062 sm_row_freelist = prow;
00063 #else
00064 register sm_element *p, *pnext;
00065
00066 for(p = prow->first_col; p != 0; p = pnext) {
00067 pnext = p->next_col;
00068 sm_element_free(p);
00069 }
00070 FREE(prow);
00071 #endif
00072 }
00073
00074
00075
00076
00077
00078 sm_row *
00079 sm_row_dup(prow)
00080 register sm_row *prow;
00081 {
00082 register sm_row *pnew;
00083 register sm_element *p;
00084
00085 pnew = sm_row_alloc();
00086 for(p = prow->first_col; p != 0; p = p->next_col) {
00087 (void) sm_row_insert(pnew, p->col_num);
00088 }
00089 return pnew;
00090 }
00091
00092
00093
00094
00095
00096 sm_element *
00097 sm_row_insert(prow, col)
00098 register sm_row *prow;
00099 register int col;
00100 {
00101 register sm_element *test, *element;
00102
00103
00104 sm_element_alloc(element);
00105 test = element;
00106 sorted_insert(sm_element, prow->first_col, prow->last_col, prow->length,
00107 next_col, prev_col, col_num, col, test);
00108
00109
00110 if (element != test) {
00111 sm_element_free(element);
00112 }
00113
00114
00115 return test;
00116 }
00117
00118
00119
00120
00121
00122 void
00123 sm_row_remove(prow, col)
00124 register sm_row *prow;
00125 register int col;
00126 {
00127 register sm_element *p;
00128
00129 for(p = prow->first_col; p != 0 && p->col_num < col; p = p->next_col)
00130 ;
00131 if (p != 0 && p->col_num == col) {
00132 dll_unlink(p, prow->first_col, prow->last_col,
00133 next_col, prev_col, prow->length);
00134 sm_element_free(p);
00135 }
00136 }
00137
00138
00139
00140
00141
00142 sm_element *
00143 sm_row_find(prow, col)
00144 sm_row *prow;
00145 int col;
00146 {
00147 register sm_element *p;
00148
00149 for(p = prow->first_col; p != 0 && p->col_num < col; p = p->next_col)
00150 ;
00151 if (p != 0 && p->col_num == col) {
00152 return p;
00153 } else {
00154 return NIL(sm_element);
00155 }
00156 }
00157
00158
00159
00160
00161 int
00162 sm_row_contains(p1, p2)
00163 sm_row *p1, *p2;
00164 {
00165 register sm_element *q1, *q2;
00166
00167 q1 = p1->first_col;
00168 q2 = p2->first_col;
00169 while (q1 != 0) {
00170 if (q2 == 0 || q1->col_num < q2->col_num) {
00171 return 0;
00172 } else if (q1->col_num == q2->col_num) {
00173 q1 = q1->next_col;
00174 q2 = q2->next_col;
00175 } else {
00176 q2 = q2->next_col;
00177 }
00178 }
00179 return 1;
00180 }
00181
00182
00183
00184
00185
00186 int
00187 sm_row_intersects(p1, p2)
00188 sm_row *p1, *p2;
00189 {
00190 register sm_element *q1, *q2;
00191
00192 q1 = p1->first_col;
00193 q2 = p2->first_col;
00194 if (q1 == 0 || q2 == 0) return 0;
00195 for(;;) {
00196 if (q1->col_num < q2->col_num) {
00197 if ((q1 = q1->next_col) == 0) {
00198 return 0;
00199 }
00200 } else if (q1->col_num > q2->col_num) {
00201 if ((q2 = q2->next_col) == 0) {
00202 return 0;
00203 }
00204 } else {
00205 return 1;
00206 }
00207 }
00208 }
00209
00210
00211
00212
00213
00214 int
00215 sm_row_compare(p1, p2)
00216 sm_row *p1, *p2;
00217 {
00218 register sm_element *q1, *q2;
00219
00220 q1 = p1->first_col;
00221 q2 = p2->first_col;
00222 while(q1 != 0 && q2 != 0) {
00223 if (q1->col_num != q2->col_num) {
00224 return q1->col_num - q2->col_num;
00225 }
00226 q1 = q1->next_col;
00227 q2 = q2->next_col;
00228 }
00229
00230 if (q1 != 0) {
00231 return 1;
00232 } else if (q2 != 0) {
00233 return -1;
00234 } else {
00235 return 0;
00236 }
00237 }
00238
00239
00240
00241
00242
00243 sm_row *
00244 sm_row_and(p1, p2)
00245 sm_row *p1, *p2;
00246 {
00247 register sm_element *q1, *q2;
00248 register sm_row *result;
00249
00250 result = sm_row_alloc();
00251 q1 = p1->first_col;
00252 q2 = p2->first_col;
00253 if (q1 == 0 || q2 == 0) return result;
00254 for(;;) {
00255 if (q1->col_num < q2->col_num) {
00256 if ((q1 = q1->next_col) == 0) {
00257 return result;
00258 }
00259 } else if (q1->col_num > q2->col_num) {
00260 if ((q2 = q2->next_col) == 0) {
00261 return result;
00262 }
00263 } else {
00264 (void) sm_row_insert(result, q1->col_num);
00265 if ((q1 = q1->next_col) == 0) {
00266 return result;
00267 }
00268 if ((q2 = q2->next_col) == 0) {
00269 return result;
00270 }
00271 }
00272 }
00273 }
00274
00275 int
00276 sm_row_hash(prow, modulus)
00277 sm_row *prow;
00278 int modulus;
00279 {
00280 register int sum;
00281 register sm_element *p;
00282
00283 sum = 0;
00284 for(p = prow->first_col; p != 0; p = p->next_col) {
00285 sum = (sum*17 + p->col_num) % modulus;
00286 }
00287 return sum;
00288 }
00289
00290
00291
00292
00293 void
00294 sm_row_remove_element(prow, p)
00295 register sm_row *prow;
00296 register sm_element *p;
00297 {
00298 dll_unlink(p, prow->first_col, prow->last_col,
00299 next_col, prev_col, prow->length);
00300 sm_element_free(p);
00301 }
00302
00303
00304 void
00305 sm_row_print(fp, prow)
00306 FILE *fp;
00307 sm_row *prow;
00308 {
00309 sm_element *p;
00310
00311 for(p = prow->first_col; p != 0; p = p->next_col) {
00312 (void) fprintf(fp, " %d", p->col_num);
00313 }
00314 }