9
9
#include < ostream>
10
10
11
11
#include " std_expr.h"
12
+ #include " simplify_utils.h"
12
13
#include " guard.h"
13
14
14
15
/* ******************************************************************\
@@ -138,17 +139,23 @@ guardt &operator -= (guardt &g1, const guardt &g2)
138
139
if (g1.id ()!=ID_and || g2.id ()!=ID_and)
139
140
return g1;
140
141
142
+ sort_and_join (g1);
143
+ guardt g2_sorted=g2;
144
+ sort_and_join (g2_sorted);
145
+
141
146
exprt::operandst &op1=g1.operands ();
142
- const exprt::operandst &op2=g2 .operands ();
147
+ const exprt::operandst &op2=g2_sorted .operands ();
143
148
144
- exprt::operandst::const_iterator it2=op2 .begin ();
145
-
146
- while (!op1. empty () &&
147
- it2!=op2.end () &&
148
- op1. front ()==* it2)
149
+ exprt::operandst::iterator it1=op1 .begin ();
150
+ for (exprt::operandst::const_iterator
151
+ it2=op2. begin ();
152
+ it2!=op2.end ();
153
+ ++ it2)
149
154
{
150
- op1.erase (op1.begin ());
151
- it2++;
155
+ while (it1!=op1.end () && *it1<*it2)
156
+ ++it1;
157
+ if (it1!=op1.end () && *it1==*it2)
158
+ it1=op1.erase (it1);
152
159
}
153
160
154
161
g1=conjunction (op1);
@@ -183,33 +190,43 @@ guardt &operator |= (guardt &g1, const guardt &g2)
183
190
else
184
191
g1=or_exprt (g1, g2);
185
192
193
+ // TODO: make simplify more capable and apply here
194
+
186
195
return g1;
187
196
}
188
197
189
198
// find common prefix
199
+ sort_and_join (g1);
200
+ guardt g2_sorted=g2;
201
+ sort_and_join (g2_sorted);
202
+
190
203
exprt::operandst &op1=g1.operands ();
191
- const exprt::operandst &op2=g2.operands ();
204
+ const exprt::operandst &op2=g2_sorted.operands ();
205
+
206
+ exprt::operandst n_op1, n_op2;
207
+ n_op1.reserve (op1.size ());
208
+ n_op2.reserve (op2.size ());
192
209
193
210
exprt::operandst::iterator it1=op1.begin ();
194
- exprt::operandst::const_iterator it2=op2.begin ();
195
-
196
- while (it1!=op1.end ())
211
+ for (exprt::operandst::const_iterator
212
+ it2=op2.begin ();
213
+ it2!=op2.end ();
214
+ ++it2)
197
215
{
198
- if (it2==op2.end ())
199
- break ;
200
-
201
- if (*it1!=*it2)
202
- break ;
203
-
204
- it1++;
205
- it2++;
216
+ while (it1!=op1.end () && *it1<*it2)
217
+ {
218
+ n_op1.push_back (*it1);
219
+ it1=op1.erase (it1);
220
+ }
221
+ if (it1!=op1.end () && *it1==*it2)
222
+ ++it1;
223
+ else
224
+ n_op2.push_back (*it2);
206
225
}
207
226
208
- if (it2==op2. end ()) return g1;
227
+ if (n_op2. empty ()) return g1;
209
228
210
229
// end of common prefix
211
- exprt::operandst n_op1 (it1, op1.end ());
212
- exprt::operandst n_op2 (it2, op2.end ());
213
230
exprt and_expr1=conjunction (n_op1);
214
231
exprt and_expr2=conjunction (n_op2);
215
232
@@ -225,6 +242,7 @@ guardt &operator |= (guardt &g1, const guardt &g2)
225
242
{
226
243
}
227
244
else
245
+ // TODO: make simplify more capable and apply here
228
246
g1.add (or_exprt (and_expr1, and_expr2));
229
247
}
230
248
0 commit comments