abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
cols.c
Go to the documentation of this file.
1 /*
2  * Revision Control Information
3  *
4  * $Source$
5  * $Author$
6  * $Revision$
7  * $Date$
8  *
9  */
10 //#include "port.h"
11 #include "sparse_int.h"
12 
14 
15 
16 
17 /*
18  * allocate a new col vector
19  */
20 sm_col *
22 {
23  register sm_col *pcol;
24 
25 #ifdef FAST_AND_LOOSE
26  if (sm_col_freelist == NIL(sm_col)) {
27  pcol = ALLOC(sm_col, 1);
28  } else {
29  pcol = sm_col_freelist;
30  sm_col_freelist = pcol->next_col;
31  }
32 #else
33  pcol = ALLOC(sm_col, 1);
34 #endif
35 
36  pcol->col_num = 0;
37  pcol->length = 0;
38  pcol->first_row = pcol->last_row = NIL(sm_element);
39  pcol->next_col = pcol->prev_col = NIL(sm_col);
40  pcol->flag = 0;
41  pcol->user_word = NIL(char); /* for our user ... */
42  return pcol;
43 }
44 
45 
46 /*
47  * free a col vector -- for FAST_AND_LOOSE, this is real cheap for cols;
48  * however, freeing a rowumn must still walk down the rowumn discarding
49  * the elements one-by-one; that is the only use for the extra '-DCOLS'
50  * compile flag ...
51  */
52 void
54 register sm_col *pcol;
55 {
56 #if defined(FAST_AND_LOOSE) && ! defined(COLS)
57  if (pcol->first_row != NIL(sm_element)) {
58  /* Add the linked list of col items to the free list */
59  pcol->last_row->next_row = sm_element_freelist;
60  sm_element_freelist = pcol->first_row;
61  }
62 
63  /* Add the col to the free list of cols */
64  pcol->next_col = sm_col_freelist;
65  sm_col_freelist = pcol;
66 #else
67  register sm_element *p, *pnext;
68 
69  for(p = pcol->first_row; p != 0; p = pnext) {
70  pnext = p->next_row;
71  sm_element_free(p);
72  }
73  FREE(pcol);
74 #endif
75 }
76 
77 
78 /*
79  * duplicate an existing col
80  */
81 sm_col *
83 register sm_col *pcol;
84 {
85  register sm_col *pnew;
86  register sm_element *p;
87 
88  pnew = sm_col_alloc();
89  for(p = pcol->first_row; p != 0; p = p->next_row) {
90  (void) sm_col_insert(pnew, p->row_num);
91  }
92  return pnew;
93 }
94 
95 
96 /*
97  * insert an element into a col vector
98  */
99 sm_element *
100 sm_col_insert(pcol, row)
101 register sm_col *pcol;
102 register int row;
103 {
104  register sm_element *test, *element;
105 
106  /* get a new item, save its address */
107  sm_element_alloc(element);
108  test = element;
109  sorted_insert(sm_element, pcol->first_row, pcol->last_row, pcol->length,
110  next_row, prev_row, row_num, row, test);
111 
112  /* if item was not used, free it */
113  if (element != test) {
114  sm_element_free(element);
115  }
116 
117  /* either way, return the current new value */
118  return test;
119 }
120 
121 
122 /*
123  * remove an element from a col vector
124  */
125 void
126 sm_col_remove(pcol, row)
127 register sm_col *pcol;
128 register int row;
129 {
130  register sm_element *p;
131 
132  for(p = pcol->first_row; p != 0 && p->row_num < row; p = p->next_row)
133  ;
134  if (p != 0 && p->row_num == row) {
135  dll_unlink(p, pcol->first_row, pcol->last_row,
136  next_row, prev_row, pcol->length);
137  sm_element_free(p);
138  }
139 }
140 
141 
142 /*
143  * find an element (if it is in the col vector)
144  */
145 sm_element *
146 sm_col_find(pcol, row)
147 sm_col *pcol;
148 int row;
149 {
150  register sm_element *p;
151 
152  for(p = pcol->first_row; p != 0 && p->row_num < row; p = p->next_row)
153  ;
154  if (p != 0 && p->row_num == row) {
155  return p;
156  } else {
157  return NIL(sm_element);
158  }
159 }
160 
161 /*
162  * return 1 if col p2 contains col p1; 0 otherwise
163  */
164 int
166 sm_col *p1, *p2;
167 {
168  register sm_element *q1, *q2;
169 
170  q1 = p1->first_row;
171  q2 = p2->first_row;
172  while (q1 != 0) {
173  if (q2 == 0 || q1->row_num < q2->row_num) {
174  return 0;
175  } else if (q1->row_num == q2->row_num) {
176  q1 = q1->next_row;
177  q2 = q2->next_row;
178  } else {
179  q2 = q2->next_row;
180  }
181  }
182  return 1;
183 }
184 
185 
186 /*
187  * return 1 if col p1 and col p2 share an element in common
188  */
189 int
191 sm_col *p1, *p2;
192 {
193  register sm_element *q1, *q2;
194 
195  q1 = p1->first_row;
196  q2 = p2->first_row;
197  if (q1 == 0 || q2 == 0) return 0;
198  for(;;) {
199  if (q1->row_num < q2->row_num) {
200  if ((q1 = q1->next_row) == 0) {
201  return 0;
202  }
203  } else if (q1->row_num > q2->row_num) {
204  if ((q2 = q2->next_row) == 0) {
205  return 0;
206  }
207  } else {
208  return 1;
209  }
210  }
211 }
212 
213 
214 /*
215  * compare two cols, lexical ordering
216  */
217 int
219 sm_col *p1, *p2;
220 {
221  register sm_element *q1, *q2;
222 
223  q1 = p1->first_row;
224  q2 = p2->first_row;
225  while(q1 != 0 && q2 != 0) {
226  if (q1->row_num != q2->row_num) {
227  return q1->row_num - q2->row_num;
228  }
229  q1 = q1->next_row;
230  q2 = q2->next_row;
231  }
232 
233  if (q1 != 0) {
234  return 1;
235  } else if (q2 != 0) {
236  return -1;
237  } else {
238  return 0;
239  }
240 }
241 
242 
243 /*
244  * return the intersection
245  */
246 sm_col *
247 sm_col_and(p1, p2)
248 sm_col *p1, *p2;
249 {
250  register sm_element *q1, *q2;
251  register sm_col *result;
252 
253  result = sm_col_alloc();
254  q1 = p1->first_row;
255  q2 = p2->first_row;
256  if (q1 == 0 || q2 == 0) return result;
257  for(;;) {
258  if (q1->row_num < q2->row_num) {
259  if ((q1 = q1->next_row) == 0) {
260  return result;
261  }
262  } else if (q1->row_num > q2->row_num) {
263  if ((q2 = q2->next_row) == 0) {
264  return result;
265  }
266  } else {
267  (void) sm_col_insert(result, q1->row_num);
268  if ((q1 = q1->next_row) == 0) {
269  return result;
270  }
271  if ((q2 = q2->next_row) == 0) {
272  return result;
273  }
274  }
275  }
276 }
277 
278 int
279 sm_col_hash(pcol, modulus)
280 sm_col *pcol;
281 int modulus;
282 {
283  register int sum;
284  register sm_element *p;
285 
286  sum = 0;
287  for(p = pcol->first_row; p != 0; p = p->next_row) {
288  sum = (sum*17 + p->row_num) % modulus;
289  }
290  return sum;
291 }
292 
293 /*
294  * remove an element from a col vector (given a pointer to the element)
295  */
296 void
298 register sm_col *pcol;
299 register sm_element *p;
300 {
301  dll_unlink(p, pcol->first_row, pcol->last_row,
302  next_row, prev_row, pcol->length);
303  sm_element_free(p);
304 }
305 
306 
307 void
308 sm_col_print(fp, pcol)
309 FILE *fp;
310 sm_col *pcol;
311 {
312  sm_element *p;
313 
314  for(p = pcol->first_row; p != 0; p = p->next_row) {
315  (void) fprintf(fp, " %d", p->row_num);
316  }
317 }
319 
sm_col * sm_col_and(sm_col *p1, sm_col *p2)
Definition: cols.c:247
static Llb_Mgr_t * p
Definition: llb3Image.c:950
sm_col * sm_col_dup(sm_col *pcol)
Definition: cols.c:82
#define sm_element_free(e)
Definition: sparse_int.h:113
sm_col * next_col
Definition: sparse.h:65
int sm_col_compare(sm_col *p1, sm_col *p2)
Definition: cols.c:218
sm_element * last_row
Definition: sparse.h:64
int sm_col_hash(sm_col *pcol, int modulus)
Definition: cols.c:279
int length
Definition: sparse.h:61
char * user_word
Definition: sparse.h:67
void sm_col_remove(sm_col *pcol, int row)
Definition: cols.c:126
#define NIL(type)
Definition: avl.h:25
#define ALLOC(type, num)
Definition: avl.h:27
#define dll_unlink(p, first, last, next, prev, count)
Definition: sparse_int.h:76
sm_element * sm_col_find(sm_col *pcol, int row)
Definition: cols.c:146
sm_element * first_row
Definition: sparse.h:63
int flag
Definition: sparse.h:62
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
#define FREE(obj)
Definition: avl.h:31
int sm_col_contains(sm_col *p1, sm_col *p2)
Definition: cols.c:165
int col_num
Definition: sparse.h:60
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
void sm_col_remove_element(sm_col *pcol, sm_element *p)
Definition: cols.c:297
int sm_col_intersects(sm_col *p1, sm_col *p2)
Definition: cols.c:190
typedefABC_NAMESPACE_HEADER_START struct sm_element_struct sm_element
Definition: sparse.h:21
static int result
Definition: cuddGenetic.c:125
#define sm_element_alloc(newobj)
Definition: sparse_int.h:110
sm_col * prev_col
Definition: sparse.h:66
void sm_col_free(sm_col *pcol)
Definition: cols.c:53
sm_element * sm_col_insert(sm_col *pcol, int row)
Definition: cols.c:100
void sm_col_print(FILE *fp, sm_col *pcol)
Definition: cols.c:308
ABC_NAMESPACE_IMPL_START sm_col * sm_col_alloc()
Definition: cols.c:21