Skip to content

Commit 2e2a2e2

Browse files
use iterators on goal and other refactoring
1 parent eb97fcc commit 2e2a2e2

8 files changed

+77
-53
lines changed

src/tactic/bv/elim_small_bv_tactic.cpp

+6-9
Original file line numberDiff line numberDiff line change
@@ -256,22 +256,19 @@ class elim_small_bv_tactic : public tactic {
256256
void operator()(goal_ref const & g,
257257
goal_ref_buffer & result) override {
258258
tactic_report report("elim-small-bv", *g);
259-
bool produce_proofs = g->proofs_enabled();
260259
fail_if_proof_generation("elim-small-bv", g);
261260
fail_if_unsat_core_generation("elim-small-bv", g);
262261
m_rw.cfg().m_produce_models = g->models_enabled();
263262

264263
expr_ref new_curr(m);
265264
proof_ref new_pr(m);
266-
unsigned size = g->size();
267-
for (unsigned idx = 0; !g->inconsistent() && idx < size; idx++) {
268-
expr * curr = g->form(idx);
265+
unsigned idx = 0;
266+
for (auto [curr, dep, pr] : *g) {
267+
if (g->inconsistent())
268+
break;
269269
m_rw(curr, new_curr, new_pr);
270-
if (produce_proofs) {
271-
proof * pr = g->pr(idx);
272-
new_pr = m.mk_modus_ponens(pr, new_pr);
273-
}
274-
g->update(idx, new_curr, new_pr, g->dep(idx));
270+
new_pr = m.mk_modus_ponens(pr, new_pr);
271+
g->update(idx++, new_curr, new_pr, dep);
275272
}
276273
g->add(m_rw.m_cfg.m_mc.get());
277274

src/tactic/core/cofactor_term_ite_tactic.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,13 @@ class cofactor_term_ite_tactic : public tactic {
2929

3030
void process(goal & g) {
3131
ast_manager & m = g.m();
32-
unsigned sz = g.size();
33-
for (unsigned i = 0; i < sz; i++) {
32+
unsigned idx = 0;
33+
for (auto [f, dep, pr] : g) {
3434
if (g.inconsistent())
3535
break;
36-
expr * f = g.form(i);
3736
expr_ref new_f(m);
3837
m_elim_ite(f, new_f);
39-
g.update(i, new_f, nullptr, g.dep(i));
38+
g.update(idx++, new_f, nullptr, dep);
4039
}
4140
}
4241

src/tactic/core/ctx_simplify_tactic.cpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -569,16 +569,15 @@ struct ctx_simplify_tactic::imp {
569569
m_occs.reset();
570570
m_occs(g);
571571
m_num_steps = 0;
572-
unsigned sz = g.size();
573572
tactic_report report("ctx-simplify", g);
574573
if (g.proofs_enabled()) {
575574
expr_ref r(m);
576-
for (unsigned i = 0; !g.inconsistent() && i < sz; ++i) {
577-
expr * t = g.form(i);
575+
unsigned idx = 0;
576+
for (auto [t, dep, pr] : g) {
578577
process(t, r);
579578
proof_ref new_pr(m.mk_rewrite(t, r), m);
580-
new_pr = m.mk_modus_ponens(g.pr(i), new_pr);
581-
g.update(i, r, new_pr, g.dep(i));
579+
new_pr = m.mk_modus_ponens(pr, new_pr);
580+
g.update(idx++, r, new_pr, dep);
582581
}
583582
}
584583
else {

src/tactic/core/der_tactic.cpp

+4-9
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,16 @@ class der_tactic : public tactic {
3434
}
3535

3636
void operator()(goal & g) {
37-
bool proofs_enabled = g.proofs_enabled();
3837
tactic_report report("der", g);
3938
expr_ref new_curr(m());
4039
proof_ref new_pr(m());
41-
unsigned size = g.size();
42-
for (unsigned idx = 0; idx < size; idx++) {
40+
unsigned idx = 0;
41+
for (auto [curr, dep, pr] : g) {
4342
if (g.inconsistent())
4443
break;
45-
expr * curr = g.form(idx);
4644
m_r(curr, new_curr, new_pr);
47-
if (proofs_enabled) {
48-
proof * pr = g.pr(idx);
49-
new_pr = m().mk_modus_ponens(pr, new_pr);
50-
}
51-
g.update(idx, new_curr, new_pr, g.dep(idx));
45+
new_pr = m().mk_modus_ponens(pr, new_pr);
46+
g.update(idx++, new_curr, new_pr, dep);
5247
}
5348
g.elim_redundancies();
5449
}

src/tactic/goal.cpp

+14-15
Original file line numberDiff line numberDiff line change
@@ -486,22 +486,21 @@ void goal::shrink(unsigned j) {
486486
/**
487487
\brief Eliminate true formulas.
488488
*/
489-
void goal::elim_true() {
490-
unsigned sz = size();
491-
unsigned j = 0;
492-
for (unsigned i = 0; i < sz; i++) {
493-
expr * f = form(i);
494-
if (m().is_true(f))
495-
continue;
496-
if (i == j) {
497-
j++;
489+
void goal::elim_true() {
490+
unsigned i = 0, j = 0;
491+
for (auto [f, dep, pr] : *this) {
492+
if (m().is_true(f)) {
493+
++i;
498494
continue;
499495
}
500-
m().set(m_forms, j, f);
501-
m().set(m_proofs, j, m().get(m_proofs, i));
502-
if (unsat_core_enabled())
503-
m().set(m_dependencies, j, m().get(m_dependencies, i));
504-
j++;
496+
if (i != j) {
497+
m().set(m_forms, j, f);
498+
m().set(m_proofs, j, pr);
499+
if (unsat_core_enabled())
500+
m().set(m_dependencies, j, dep);
501+
}
502+
++i;
503+
++j;
505504
}
506505
shrink(j);
507506
}
@@ -539,7 +538,7 @@ void goal::elim_redundancies() {
539538
expr_ref_fast_mark1 neg_lits(m());
540539
expr_ref_fast_mark2 pos_lits(m());
541540
unsigned sz = size();
542-
unsigned j = 0;
541+
unsigned j = 0;
543542
for (unsigned i = 0; i < sz; i++) {
544543
expr * f = form(i);
545544
if (m().is_true(f))

src/tactic/goal.h

+35
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,41 @@ class goal {
165165
bool is_cnf() const;
166166

167167
goal * translate(ast_translation & translator) const;
168+
169+
class iterator {
170+
public:
171+
using value_type = std::tuple<expr*, expr_dependency*, proof*>;
172+
using reference = value_type&;
173+
using pointer = value_type*;
174+
using difference_type = std::ptrdiff_t;
175+
using iterator_category = std::forward_iterator_tag;
176+
177+
iterator(goal const* g, unsigned idx) : m_goal(g), m_idx(idx) {}
178+
179+
value_type operator*() const {
180+
return std::make_tuple(m_goal->form(m_idx), m_goal->dep(m_idx), m_goal->pr(m_idx));
181+
}
182+
183+
iterator& operator++() {
184+
++m_idx;
185+
return *this;
186+
}
187+
188+
bool operator==(const iterator& other) const {
189+
return m_goal == other.m_goal && m_idx == other.m_idx;
190+
}
191+
192+
bool operator!=(const iterator& other) const {
193+
return !(*this == other);
194+
}
195+
196+
private:
197+
goal const* m_goal;
198+
unsigned m_idx;
199+
};
200+
201+
iterator begin() const { return iterator(this, 0); }
202+
iterator end() const { return iterator(this, size()); }
168203
};
169204

170205
std::ostream & operator<<(std::ostream & out, goal::precision p);

src/test/algebraic.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,18 @@ Module Name:
2525
static void display_anums(std::ostream & out, scoped_anum_vector const & rs) {
2626
out << "numbers in decimal:\n";
2727
algebraic_numbers::manager & m = rs.m();
28-
for (const auto& r : rs) {
29-
m.display_decimal(out, r, 10);
28+
for (unsigned i = 0; i < rs.size(); i++) {
29+
m.display_decimal(out, rs[i], 10);
3030
out << "\n";
3131
}
3232
out << "numbers as root objects\n";
33-
for (const auto& r : rs) {
34-
m.display_root(out, r);
33+
for (unsigned i = 0; i < rs.size(); i++) {
34+
m.display_root(out, rs[i]);
3535
out << "\n";
3636
}
3737
out << "numbers as intervals\n";
38-
for (const auto& r : rs) {
39-
m.display_interval(out, r);
38+
for (unsigned i = 0; i < rs.size(); i++) {
39+
m.display_interval(out, rs[i]);
4040
out << "\n";
4141
}
4242
}

src/test/lp/gomory_test.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ struct gomory_test {
158158
TRACE("gomory_cut_detail", tout << "pol.size() > 1" << std::endl;);
159159
lcm_den = lcm(lcm_den, denominator(k));
160160
TRACE("gomory_cut_detail", tout << "k: " << k << " lcm_den: " << lcm_den << "\n";
161-
for (const auto& p : pol) {
162-
tout << p.first << " " << p.second << "\n";
161+
for (unsigned i = 0; i < pol.size(); i++) {
162+
tout << pol[i].first << " " << pol[i].second << "\n";
163163
}
164164
tout << "k: " << k << "\n";);
165165
lp_assert(lcm_den.is_pos());
@@ -172,8 +172,8 @@ struct gomory_test {
172172
k *= lcm_den;
173173
}
174174
TRACE("gomory_cut_detail", tout << "after *lcm\n";
175-
for (const auto& p : pol) {
176-
tout << p.first << " * v" << p.second << "\n";
175+
for (unsigned i = 0; i < pol.size(); i++) {
176+
tout << pol[i].first << " * v" << pol[i].second << "\n";
177177
}
178178
tout << "k: " << k << "\n";);
179179

@@ -210,7 +210,7 @@ struct gomory_test {
210210
bool some_int_columns = false;
211211
mpq f_0 = fractional_part(get_value(inf_col));
212212
mpq one_min_f_0 = 1 - f_0;
213-
for (const auto& pp : row) {
213+
for ( auto pp : row) {
214214
a = pp.first;
215215
x_j = pp.second;
216216
if (x_j == inf_col)

0 commit comments

Comments
 (0)