Skip to content

Commit 021f971

Browse files
author
Daniel Kroening
committed
simplifier: introduce ranged for
Way prettier.
1 parent de989ce commit 021f971

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/util/simplify_expr.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Author: Daniel Kroening, [email protected]
2222
#include "namespace.h"
2323
#include "pointer_offset_size.h"
2424
#include "pointer_offset_sum.h"
25+
#include "range.h"
2526
#include "rational.h"
2627
#include "rational_tools.h"
2728
#include "simplify_utils.h"
@@ -1392,24 +1393,29 @@ bool simplify_exprt::simplify_if(if_exprt &expr)
13921393
return false;
13931394
}
13941395

1396+
// this pushes the if-then-else into struct and array constructors
13951397
if(((truevalue.id()==ID_struct && falsevalue.id()==ID_struct) ||
13961398
(truevalue.id()==ID_array && falsevalue.id()==ID_array)) &&
13971399
truevalue.operands().size()==falsevalue.operands().size())
13981400
{
13991401
exprt cond_copy=cond;
14001402
exprt falsevalue_copy=falsevalue;
1401-
expr.swap(truevalue);
1403+
exprt truevalue_copy = truevalue;
14021404

1403-
exprt::operandst::const_iterator f_it=
1404-
falsevalue_copy.operands().begin();
1405-
Forall_operands(it, expr)
1405+
auto range_false = make_range(falsevalue_copy.operands());
1406+
auto range_true = make_range(truevalue_copy.operands());
1407+
auto new_expr = truevalue;
1408+
new_expr.operands().clear();
1409+
1410+
for(const auto &pair : range_true.zip(range_false))
14061411
{
1407-
if_exprt if_expr(cond_copy, *it, *f_it);
1408-
it->swap(if_expr);
1409-
simplify_if(to_if_expr(*it));
1410-
++f_it;
1412+
if_exprt if_expr(cond_copy, pair.first, pair.second);
1413+
simplify_if(if_expr);
1414+
new_expr.operands().push_back(std::move(if_expr));
14111415
}
14121416

1417+
expr.swap(new_expr);
1418+
14131419
return false;
14141420
}
14151421

0 commit comments

Comments
 (0)