|
| 1 | +/*******************************************************************\ |
| 2 | +
|
| 3 | +Module: util |
| 4 | +
|
| 5 | +Author: Romain Brenguier, [email protected] |
| 6 | +
|
| 7 | +\*******************************************************************/ |
| 8 | + |
| 9 | +#include "union_find_replace.h" |
| 10 | + |
| 11 | +/// Keeps a map of symbols to expressions, such as none of the mapped values |
| 12 | +/// exist as a key |
| 13 | +/// \param a: an expression of type char array |
| 14 | +/// \param b: an expression to map it to, which should be either a symbol |
| 15 | +/// a string_exprt, an array_exprt, an array_of_exprt or an |
| 16 | +/// if_exprt with branches of the previous kind |
| 17 | +/// \return the new mapped value |
| 18 | +exprt union_find_replacet::make_union(const exprt &a, const exprt &b) |
| 19 | +{ |
| 20 | + const exprt &lhs_root = find(a); |
| 21 | + const exprt &rhs_root = find(b); |
| 22 | + if(lhs_root != rhs_root) |
| 23 | + map[lhs_root] = rhs_root; |
| 24 | + return rhs_root; |
| 25 | +} |
| 26 | + |
| 27 | +/// Replace subexpressions of `expr` by a canonical element of the set they |
| 28 | +/// belong to. |
| 29 | +/// \param expr: an expression, modified in place |
| 30 | +/// \return true if expr is left unchanged |
| 31 | +bool union_find_replacet::replace_expr(exprt &expr) const |
| 32 | +{ |
| 33 | + bool unchanged = ::replace_expr(map, expr); |
| 34 | + while(!unchanged && !::replace_expr(map, expr)) |
| 35 | + continue; |
| 36 | + return unchanged; |
| 37 | +} |
| 38 | + |
| 39 | +/// \param expr: an expression |
| 40 | +/// \return canonical representation for expressions which belong to the same |
| 41 | +/// set |
| 42 | +exprt union_find_replacet::find(exprt expr) const |
| 43 | +{ |
| 44 | + replace_expr(expr); |
| 45 | + return expr; |
| 46 | +} |
| 47 | + |
| 48 | +/// \return pairs of expression composed of expressions and a canonical |
| 49 | +/// expression for the set they below to. |
| 50 | +std::vector<std::pair<exprt, exprt>> union_find_replacet::to_vector() const |
| 51 | +{ |
| 52 | + std::vector<std::pair<exprt, exprt>> equations; |
| 53 | + for(const auto &pair : map) |
| 54 | + equations.emplace_back(pair.first, find(pair.second)); |
| 55 | + return equations; |
| 56 | +} |
0 commit comments