-
Notifications
You must be signed in to change notification settings - Fork 0
/
AndClauseLinkedList.c~
265 lines (208 loc) · 5.6 KB
/
AndClauseLinkedList.c~
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include "ADNF.h"
#include <stdlib.h>
#include <stdio.h>
void InitAndClauseLinkedList(ADNFClause *dnf)
{
*dnf = malloc(sizeof(AAndClause));
(*dnf) -> next=0;
(*dnf) -> prev=0;
}
AAndClause * RemoveClause(AAndClause *a)
{
AAndClause *prev = a->prev;
prev->next= a->next;
if (a->next)
a->next->prev= prev;
free(a);
return prev;
}
AAndClause * AddClauseNoQuestionsAsked(AAndClause *prev, ASet clause)
{
AAndClause *next= prev->next;
prev->next= malloc(sizeof(AAndClause)); //from this point on, prev->next is the CURRENT RECORD
prev->next->set= clause; //COPY THE SET, This may change if we ever go beyond 8x8
prev->next->prev= prev;
prev->next->next= next;
return prev->next;
}
int AddClause(ADNFClause dnf, ASet clause) //returns address if really added
{
int i;
AAndClause *cur= dnf;
ASet allAtomsIntersection;
if (SET_ISFALSE(clause)) //Not adding ( a AND NOT(a) )
return 0;
#ifdef LOG_LINKEDLIST
printf("cur->next= %d\n", cur->next);
#endif
// impliesExisting=0;
while (cur->next)
{
cur=cur->next;
switch (Contains( &(cur->set), &clause) )// The one in the chain is preferred if they are equal
{
case 1: //The one in the chain contains the one to be added ( Is more specific)
// printf("head %d head->prev->next %d\n", head, head->prev->next);
cur= RemoveClause(cur);
break;
case -1: //The one to be added contains (is more specific than) the one already in the chain
#ifdef LOG_LINKEDLIST
printf("Clause %d>%d already there in wire %d at stack entry %d\n", clause, cur->set, w, cur->inQueue);
#endif
break;
case 0: //Nothing to see here
allAtomsIntersection= SET_INTERSECTION( SET_ALL_ATOMS(cur->set), SET_ALL_ATOMS(clause) );
for (i=0; i<ASET_SIZE_HALF; i++)
{
if (SET_MEMBER(allAtomsIntersection, i) ) //looking for a and ~a
{
if ( LOGIC_NORMALIZE(SET_MEMBER(cur->set, i)) != LOGIC_NORMALIZE(SET_MEMBER(clause, i)) ) //one of them has a and the other ~a
{
ASet f1, f2;
if ( SET_MEMBER(clause, i))
{
f1=clause;
f2=cur->set;
}
else
{
f2= clause;
f1= cur->set;
}
// f1 has a and f2 has ~a
SET_REMOVE(f2, SET_INDEX_NEGATE(i));
SET_REMOVE(f1, i);
//Now neither has either!!
if (f1==f2)
{
RemoveClause(cur);
clause= f1;
cur= dnf;
break; //break out of the for loop.
}
else
{
int fa= //1 if it is of form fa + ff'~a, -1 if f~a + ff'a, 0 if neither
SET_CONTAINS(f2,f1);
if (fa==1) //fa+ ff'~a = fa + ff'a. f1=f, f2=ff'
{
//if fa is in the chain, we need not touch it! Just simplify ff'~a to ff'
if ( SET_MEMBER(cur->set, i))
clause= f2;
else // if ff'~a is in the chain, we have to delete it and insert ff' in again. Then fa will be added back. (Is it necessary?)
{
RemoveClause(cur);
AddClause(dnf, f2);
}
cur= dnf;// Restart the outer while loop
break; //from the for loop
}
else if (fa==-1)//ff'a + f~a= ff' + f~a. f1=ff', f2=f
{
//if f~a is in the chain, we need not touch it. Just simplify ff'a to ff'
if (SET_MEMBER(cur->set, SET_INDEX_NEGATE(i)) )
clause= f1;
else // if ff'a is in the chain, we delete it and insert ff' in again. Then f~a will be added again.
{
RemoveClause(cur);
AddClause(dnf, f1);
}
cur= dnf;
break;
}
}//end if
}//end if
}//end if
}//end for
}//end switch
}
return AddClauseNoQuestionsAsked(cur, clause);
}
AAndClause * Last(ADNFClause head)
{
for (; head->next; head= head->next);
return head;
}
int DNFSize(ADNFClause dnf)
{
int i=0;
for (dnf=dnf->next; dnf; dnf=dnf->next)
i++;
return i;
}
void PrintDNFClause(ADNFClause dnf)
{
AAndClause *andClause= dnf;
while (andClause->next)
{
andClause = andClause->next;
PrintSet_IncNeg(andClause->set);
}
printf("\n");
}
void ClearDNFClause(ADNFClause dnf)
{
ADNFClause next, cur= dnf->next;
dnf->next=0;
for (; cur; cur=next)
{
next= cur->next;
free(cur);
}
}
void QuickSort(ASet *a, int size)
{
int p, barrier;
ASet t;
if (size<=1)
return;
//partition
p=0;
for (barrier=1; barrier<size; barrier++)
{
if (a[barrier]>= a[p])
continue; //it is where it should be!
//if it should be on the other side:
t= a[barrier];
a[barrier]= a[p+1];
a[p+1]= a[p];
a[p]= t;
p++;
}
//recursion
QuickSort(a, p);
QuickSort(a+p+1, size-p-1);
}
ASet* BinSearch(ASet v, ASet *a, int size)
{
if (size==0)
return 0;
else if (v < a[size/2])
return BinSearch( v, a, size/2);
else if (v > a[size/2])
return BinSearch( v, a+ size/2+1, size - size/2 -1);
else
return a + size/2;
}
void AddDontCare(ADNFClause dst, ADNFClause src)
{
int dontCaresSize= DNFSize(src);
ASet * dontCares= malloc(dontCaresSize);
AAndClause *curDontCare;
dontCaresSize=0;
for (src=src->next; src; src= src->next)
{
curDontCare= AddClause(dst, src->set); //add the and clause and keep a record of it
// if (curDontCare && curDontCare->set== src->set) //if it is really added and not yet proven useful
//if the set is different than what was originally added, means it has simplified.
// {
dontCares[dontCaresSize]= src->set;
dontCaresSize++;
// }
}
QuickSort(dontCares, dontCaresSize);
//Delete the ones who didn't make a difference
for (dst= dst->next; dst; dst=dst->next)
if (BinSearch(dst->set, dontCares, dontCaresSize))
dst = RemoveClause(dst); //go back to prev
}