Skip to content

Commit f01cbe9

Browse files
Yakir Vizelyvizel
authored andcommitted
Moving from std::set to std::unordered_set
1 parent 5a02d55 commit f01cbe9

File tree

2 files changed

+72
-51
lines changed

2 files changed

+72
-51
lines changed

src/cprover/chc_db.cpp

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,58 +6,62 @@
66

77
#include <iostream>
88

9-
chc_db::chc_sett chc_db::m_empty_set;
10-
std::set<exprt> chc_graph::m_expr_empty_set;
9+
chc_dbt::chc_sett chc_dbt::m_empty_set;
10+
std::unordered_set<exprt, irep_hash> chc_grapht::m_expr_empty_set;
1111

12-
void chc_db::reset_indices()
12+
void chc_dbt::reset_indices()
1313
{
1414
m_body_idx.clear();
1515
m_head_idx.clear();
1616
}
1717

18-
void chc_db::build_indices()
18+
void chc_dbt::build_indices()
1919
{
2020
reset_indices();
2121

22-
for (auto &r: m_clauses) {
22+
for (std::size_t i = 0; i < m_clauses.size(); i++)
23+
{
24+
auto & r = m_clauses[i];
2325
if (!can_cast_expr<function_application_exprt>(*r.head()))
2426
{
2527
continue;
2628
}
2729
exprt func = to_function_application_expr(*r.head()).function();
28-
m_head_idx[func].insert(&r);
30+
m_head_idx[func].insert(i);
2931

3032
std::vector<symbol_exprt> use;
3133
r.used_relations(*this,std::back_inserter(use));
3234
for (auto & symb : use)
3335
{
34-
m_body_idx[symb].insert(&r);
36+
m_body_idx[symb].insert(i);
3537
}
3638
}
3739
}
3840

39-
void chc_graph::build_graph()
41+
void chc_grapht::build_graph()
4042
{
4143
m_db.build_indices();
4244

4345
for (auto & sp : m_db.get_state_preds())
4446
{
45-
std::set<exprt> outgoing;
46-
const std::set<const horn_clause *> &uses = m_db.use(sp);
47-
for (const horn_clause *r : uses) {
48-
const exprt * head = r->head();
47+
std::unordered_set<exprt, irep_hash> outgoing;
48+
const chc_dbt::chc_sett &uses = m_db.use(sp);
49+
for (auto idx: uses) {
50+
const horn_clauset & r = m_db.get_clause(idx);
51+
const exprt * head = r.head();
4952
if (can_cast_expr<function_application_exprt>(*head))
5053
{
5154
outgoing.insert(to_function_application_expr(*head).function());
5255
}
5356
}
5457
m_outgoing.insert(std::make_pair(sp, outgoing));
5558

56-
std::set<exprt> incoming;
57-
const std::set<const horn_clause *> &defs = m_db.def(sp);
58-
chc_db::is_state_pred isStatePred(m_db);
59-
for (const horn_clause *r : defs) {
60-
std::set<symbol_exprt> symbols = find_symbols(*r->body());
59+
std::unordered_set<exprt, irep_hash> incoming;
60+
const chc_dbt::chc_sett &defs = m_db.def(sp);
61+
chc_dbt::is_state_pred isStatePred(m_db);
62+
for (auto idx : defs) {
63+
const horn_clauset & r = m_db.get_clause(idx);
64+
std::set<symbol_exprt> symbols = find_symbols(*r.body());
6165
for (auto & s : symbols)
6266
if (isStatePred(s))
6367
incoming.insert(s);

src/cprover/chc_db.h

Lines changed: 51 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,25 @@
1515
#include <set>
1616
#include <functional>
1717

18-
class chc_db;
19-
class horn_clause
18+
class chc_dbt;
19+
20+
/*
21+
* A horn clause.
22+
* This class is simply a wrapper around a forall_exprt with a few utilities:
23+
* 1. Getting the body of a clause
24+
* 2. Getting the head of a clause
25+
* 3. Checking if a clause is a fact or a query
26+
* 4. Getting used relations (the predicates) or function applications
27+
* (their instantiations) in a clause.
28+
*/
29+
class horn_clauset
2030
{
2131
forall_exprt m_chc;
2232

2333
public:
24-
horn_clause(forall_exprt f) : m_chc(f) {}
34+
horn_clauset(forall_exprt f) : m_chc(f) {}
2535

26-
horn_clause(std::vector<symbol_exprt> & vars, exprt clause) : m_chc(vars, clause) {
36+
horn_clauset(std::vector<symbol_exprt> & vars, exprt clause) : m_chc(vars, clause) {
2737

2838
}
2939

@@ -76,74 +86,75 @@ class horn_clause
7686
return false;
7787
}
7888

79-
bool operator==(const horn_clause &other) const
89+
bool operator==(const horn_clauset &other) const
8090
{
8191
return m_chc == other.m_chc;
8292
}
8393

84-
bool operator!=(const horn_clause &other) const
94+
bool operator!=(const horn_clauset &other) const
8595
{
8696
return !(*this==other);
8797
}
8898

89-
bool operator<(const horn_clause &other) const
99+
bool operator<(const horn_clauset &other) const
90100
{
91101
return m_chc < other.m_chc;
92102
}
93103

94104
template <typename OutputIterator>
95-
void used_relations(chc_db &db, OutputIterator out) const;
105+
void used_relations(chc_dbt &db, OutputIterator out) const;
96106
template <typename OutputIterator>
97-
void used_func_app(chc_db &db, OutputIterator out) const;
107+
void used_func_app(chc_dbt &db, OutputIterator out) const;
98108
};
99109

100110
/*
101111
* A database of CHCs.
102112
* Uninterpreted relations need to be registered.
103113
*/
104-
class chc_db
114+
class chc_dbt
105115
{
106-
friend class horn_clause;
116+
friend class horn_clauset;
107117
public:
108118
struct is_state_pred : public std::__unary_function<exprt, bool> {
109-
const chc_db &m_db;
110-
is_state_pred(const chc_db &db) : m_db(db) {}
119+
const chc_dbt &m_db;
120+
is_state_pred(const chc_dbt &db) : m_db(db) {}
111121

112122
bool operator()(symbol_exprt state) { return m_db.has_state_pred(state); }
113123
};
114124

125+
typedef std::unordered_set<std::size_t> chc_sett;
126+
115127
private:
116-
using chcst = std::vector<horn_clause>;
128+
using chcst = std::vector<horn_clauset>;
117129
chcst m_clauses;
118130

119-
std::set<symbol_exprt> m_state_preds;
131+
std::unordered_set<symbol_exprt, irep_hash> m_state_preds;
120132

121-
typedef std::set<const horn_clause *> chc_sett;
122-
typedef std::map<exprt, std::set<const horn_clause *>> chc_indext;
133+
typedef std::map<exprt, chc_sett> chc_indext;
123134
chc_indext m_body_idx;
124135
chc_indext m_head_idx;
125136

126137
// representing the empty set
127138
static chc_sett m_empty_set;
128139

129140
public:
130-
chc_db() {}
141+
chc_dbt() {}
131142

132143
void add_state_pred(const symbol_exprt & state) { m_state_preds.insert(state); }
133-
const std::set<symbol_exprt> &get_state_preds() { return m_state_preds; }
144+
const std::unordered_set<symbol_exprt, irep_hash> &get_state_preds() { return m_state_preds; }
134145
bool has_state_pred(const symbol_exprt & state) const { return m_state_preds.count(state) > 0; }
135146

136147
void build_indices();
137148
void reset_indices();
138149

139-
const std::set<const horn_clause *> & use(const exprt & state) const {
150+
const chc_sett & use(const exprt & state) const {
140151
auto it = m_body_idx.find(state);
141152
if (it == m_body_idx.end())
142153
return m_empty_set;
143154
return it->second;
144155
}
145156

146-
const std::set<const horn_clause *> & def(const exprt & state) const {
157+
const chc_sett & def(const exprt & state) const {
147158
auto it = m_head_idx.find(state);
148159
if (it == m_head_idx.end())
149160
return m_empty_set;
@@ -157,24 +168,30 @@ class chc_db
157168
for (auto & c : m_clauses) {
158169
if (c.get_chc()==f) return;
159170
}
160-
m_clauses.push_back(horn_clause(f));
171+
m_clauses.push_back(horn_clauset(f));
161172
reset_indices();
162173
}
163174

175+
[[nodiscard]] const horn_clauset & get_clause(std::size_t idx) const
176+
{
177+
INVARIANT(idx < m_clauses.size(), "Index in range");
178+
return m_clauses[idx];
179+
}
180+
164181
chcst::iterator begin() { return m_clauses.begin(); }
165182
chcst::iterator end() { return m_clauses.end(); }
166183
chcst::const_iterator begin() const { return m_clauses.begin(); }
167184
chcst::const_iterator end() const { return m_clauses.end(); }
168185
};
169186

170187
template <typename OutputIterator>
171-
void horn_clause::used_relations(chc_db &db, OutputIterator out) const
188+
void horn_clauset::used_relations(chc_dbt &db, OutputIterator out) const
172189
{
173190
const exprt *body = this->body();
174191
if (body == nullptr) return;
175192
std::set<symbol_exprt> symbols = find_symbols(*body);
176193

177-
chc_db::is_state_pred filter(db);
194+
chc_dbt::is_state_pred filter(db);
178195
for (auto & symb : symbols) {
179196
if (filter(symb)) {
180197
*out = symb;
@@ -183,12 +200,12 @@ void horn_clause::used_relations(chc_db &db, OutputIterator out) const
183200
}
184201

185202
template <typename OutputIterator>
186-
void horn_clause::used_func_app(chc_db &db, OutputIterator out) const
203+
void horn_clauset::used_func_app(chc_dbt &db, OutputIterator out) const
187204
{
188205
const exprt *body = this->body();
189206
if (body == nullptr) return;
190207

191-
std::set<function_application_exprt> funcs;
208+
std::unordered_set<function_application_exprt, irep_hash> funcs;
192209
body->visit_pre([&funcs](const exprt &expr) {
193210
if (can_cast_expr<function_application_exprt>(expr))
194211
{
@@ -197,7 +214,7 @@ void horn_clause::used_func_app(chc_db &db, OutputIterator out) const
197214
}
198215
});
199216

200-
chc_db::is_state_pred filter(db);
217+
chc_dbt::is_state_pred filter(db);
201218
for (auto & f : funcs) {
202219
if (filter(to_symbol_expr(f.function()))) {
203220
*out = f;
@@ -210,19 +227,19 @@ void horn_clause::used_func_app(chc_db &db, OutputIterator out) const
210227
* Uninterpreted relations are vertices, dependency is based on clauses:
211228
* relations in the body have an edge to the relation in the head.
212229
*/
213-
class chc_graph
230+
class chc_grapht
214231
{
215-
chc_db & m_db;
216-
typedef std::map<exprt, std::set<exprt>> grapht;
232+
chc_dbt & m_db;
233+
typedef std::map<exprt, std::unordered_set<exprt, irep_hash>> grapht;
217234
grapht m_incoming;
218235
grapht m_outgoing;
219236
const symbol_exprt *m_entry;
220237

221238
// representing the empty set
222-
static std::set<exprt> m_expr_empty_set;
239+
static std::unordered_set<exprt, irep_hash> m_expr_empty_set;
223240

224241
public:
225-
chc_graph(chc_db & db) : m_db(db), m_entry(nullptr) {}
242+
chc_grapht(chc_dbt & db) : m_db(db), m_entry(nullptr) {}
226243

227244
void build_graph();
228245

@@ -231,14 +248,14 @@ class chc_graph
231248
INVARIANT(has_entry(), "Entry must exist.");
232249
return m_entry; }
233250

234-
const std::set<exprt> &outgoing(const symbol_exprt &state) const {
251+
const std::unordered_set<exprt, irep_hash> &outgoing(const symbol_exprt &state) const {
235252
auto it = m_outgoing.find(state);
236253
if (it == m_outgoing.end())
237254
return m_expr_empty_set;
238255
return it->second;
239256
}
240257

241-
const std::set<exprt> &incoming(const symbol_exprt &state) const {
258+
const std::unordered_set<exprt, irep_hash> &incoming(const symbol_exprt &state) const {
242259
auto it = m_incoming.find(state);
243260
if (it == m_incoming.end())
244261
return m_expr_empty_set;

0 commit comments

Comments
 (0)