Skip to content

Commit 4adf5d0

Browse files
New class for union find replace
This has a similar interface to union_find but also allows to replace expressions. It will be used in the string solver instead of a simple replace_mapt.
1 parent 1d112d5 commit 4adf5d0

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

src/util/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ SRC = arith_tools.cpp \
9292
ui_message.cpp \
9393
unicode.cpp \
9494
union_find.cpp \
95+
union_find_replace.cpp \
9596
xml.cpp \
9697
xml_expr.cpp \
9798
xml_irep.cpp \

src/util/union_find_replace.cpp

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
}

src/util/union_find_replace.h

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*******************************************************************\
2+
3+
Module: util
4+
5+
Author: Romain Brenguier, [email protected]
6+
7+
\*******************************************************************/
8+
9+
#ifndef CPROVER_UTIL_UNION_FIND_REPLACE_H
10+
#define CPROVER_UTIL_UNION_FIND_REPLACE_H
11+
12+
#include <util/replace_expr.h>
13+
14+
/// Similar interface to union-find for expressions, with a function for
15+
/// replacing sub-expressions by their result for find.
16+
class union_find_replacet
17+
{
18+
public:
19+
bool replace_expr(exprt &expr) const;
20+
21+
exprt find(exprt expr) const;
22+
23+
exprt make_union(const exprt &a, const exprt &b);
24+
25+
std::vector<std::pair<exprt, exprt>> to_vector() const;
26+
27+
private:
28+
replace_mapt map;
29+
};
30+
31+
#endif // CPROVER_UTIL_UNION_FIND_REPLACE_H

0 commit comments

Comments
 (0)