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