Skip to content

Commit aa28e14

Browse files
author
kroening
committed
Generalise operator-= and operator|= of guardt
1 parent e23ec34 commit aa28e14

File tree

1 file changed

+41
-23
lines changed

1 file changed

+41
-23
lines changed

src/util/guard.cpp

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Author: Daniel Kroening, [email protected]
99
#include <ostream>
1010

1111
#include "std_expr.h"
12+
#include "simplify_utils.h"
1213
#include "guard.h"
1314

1415
/*******************************************************************\
@@ -138,17 +139,23 @@ guardt &operator -= (guardt &g1, const guardt &g2)
138139
if(g1.id()!=ID_and || g2.id()!=ID_and)
139140
return g1;
140141

142+
sort_and_join(g1);
143+
guardt g2_sorted=g2;
144+
sort_and_join(g2_sorted);
145+
141146
exprt::operandst &op1=g1.operands();
142-
const exprt::operandst &op2=g2.operands();
147+
const exprt::operandst &op2=g2_sorted.operands();
143148

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)
149154
{
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);
152159
}
153160

154161
g1=conjunction(op1);
@@ -183,33 +190,43 @@ guardt &operator |= (guardt &g1, const guardt &g2)
183190
else
184191
g1=or_exprt(g1, g2);
185192

193+
// TODO: make simplify more capable and apply here
194+
186195
return g1;
187196
}
188197

189198
// find common prefix
199+
sort_and_join(g1);
200+
guardt g2_sorted=g2;
201+
sort_and_join(g2_sorted);
202+
190203
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());
192209

193210
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)
197215
{
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);
206225
}
207226

208-
if(it2==op2.end()) return g1;
227+
if(n_op2.empty()) return g1;
209228

210229
// end of common prefix
211-
exprt::operandst n_op1(it1, op1.end());
212-
exprt::operandst n_op2(it2, op2.end());
213230
exprt and_expr1=conjunction(n_op1);
214231
exprt and_expr2=conjunction(n_op2);
215232

@@ -225,6 +242,7 @@ guardt &operator |= (guardt &g1, const guardt &g2)
225242
{
226243
}
227244
else
245+
// TODO: make simplify more capable and apply here
228246
g1.add(or_exprt(and_expr1, and_expr2));
229247
}
230248

0 commit comments

Comments
 (0)