Skip to content

Commit 1009ce7

Browse files
author
kroening
committed
New iterator macro forall_literals, Forall_literals
git-svn-id: svn+ssh://svn.cprover.org/srv/svn/cbmc/trunk@1052 6afb6bc1-c8e4-404c-8f48-9ae832c5b171
1 parent f77d38f commit 1009ce7

16 files changed

+90
-85
lines changed

src/solvers/cvc/cvc_prop.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,10 @@ literalt cvc_propt::land(const bvt &bv)
205205

206206
literalt literal=def_cvc_literal();
207207

208-
for(unsigned int i=0; i<bv.size(); ++i)
208+
forall_literals(it, bv)
209209
{
210-
if(i!=0) out << " AND ";
211-
out << cvc_literal(bv[i]);
210+
if(it!=bv.begin()) out << " AND ";
211+
out << cvc_literal(*it);
212212
}
213213

214214
out << ";" << std::endl << std::endl;
@@ -234,10 +234,10 @@ literalt cvc_propt::lor(const bvt &bv)
234234

235235
literalt literal=def_cvc_literal();
236236

237-
for(unsigned int i=0; i<bv.size(); ++i)
237+
forall_literals(it, bv)
238238
{
239-
if(i!=0) out << " OR ";
240-
out << cvc_literal(bv[i]);
239+
if(it!=bv.begin()) out << " OR ";
240+
out << cvc_literal(*it);
241241
}
242242

243243
out << ";" << std::endl << std::endl;
@@ -265,8 +265,8 @@ literalt cvc_propt::lxor(const bvt &bv)
265265

266266
literalt literal=const_literal(false);
267267

268-
for(unsigned i=0; i<bv.size(); i++)
269-
literal=lxor(bv[i], literal);
268+
forall_literals(it, bv)
269+
literal=lxor(*it, literal);
270270

271271
return literal;
272272
}

src/solvers/dplib/dplib_prop.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,10 @@ literalt dplib_propt::land(const bvt &bv)
190190

191191
literalt literal=def_dplib_literal();
192192

193-
for(unsigned int i=0; i<bv.size(); ++i)
193+
forall_literals(it, bv)
194194
{
195-
if(i!=0) out << " & ";
196-
out << dplib_literal(bv[i]);
195+
if(it!=bv.begin()) out << " & ";
196+
out << dplib_literal(*it);
197197
}
198198

199199
out << std::endl << std::endl;
@@ -219,10 +219,10 @@ literalt dplib_propt::lor(const bvt &bv)
219219

220220
literalt literal=def_dplib_literal();
221221

222-
for(unsigned int i=0; i<bv.size(); ++i)
222+
forall_literals(it, bv)
223223
{
224-
if(i!=0) out << " | ";
225-
out << dplib_literal(bv[i]);
224+
if(it!=bv.begin()) out << " | ";
225+
out << dplib_literal(*it);
226226
}
227227

228228
out << std::endl << std::endl;
@@ -250,8 +250,8 @@ literalt dplib_propt::lxor(const bvt &bv)
250250

251251
literalt literal=const_literal(false);
252252

253-
for(unsigned i=0; i<bv.size(); i++)
254-
literal=lxor(bv[i], literal);
253+
forall_literals(it, bv)
254+
literal=lxor(*it, literal);
255255

256256
return literal;
257257
}

src/solvers/flattening/boolbv.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ void boolbvt::convert_bv(const exprt &expr, bvt &bv)
153153
convert_bitvector(expr, bv);
154154

155155
// check
156-
for(unsigned i=0; i<bv.size(); i++)
157-
if(bv[i].var_no()==literalt::unused_var_no())
156+
forall_literals(it, bv)
157+
if(it->var_no()==literalt::unused_var_no())
158158
{
159159
std::cout << "unused_var_no: " << expr.pretty() << std::endl;
160160
assert(false);
@@ -516,9 +516,9 @@ void boolbvt::convert_symbol(const exprt &expr, bvt &bv)
516516
for(unsigned i=0; i<width; i++)
517517
bv[i]=map.get_literal(identifier, i, expr.type());
518518

519-
for(unsigned i=0; i<width; i++)
520-
if(bv[i].var_no()>=prop.no_variables() &&
521-
!bv[i].is_constant()) { std::cout << identifier << std::endl; abort(); }
519+
forall_literals(it, bv)
520+
if(it->var_no()>=prop.no_variables() &&
521+
!it->is_constant()) { std::cout << identifier << std::endl; abort(); }
522522
}
523523
}
524524

@@ -841,8 +841,8 @@ void boolbvt::make_free_bv_expr(const typet &type, exprt &dest)
841841
bv.resize(width);
842842

843843
// make result free variables
844-
for(unsigned i=0; i<bv.size(); i++)
845-
bv[i]=prop.new_variable();
844+
Forall_literals(it, bv)
845+
*it=prop.new_variable();
846846

847847
make_bv_expr(type, bv, dest);
848848
}

src/solvers/flattening/boolbv_case.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ void boolbvt::convert_case(const exprt &expr, bvt &bv)
3434
bv.resize(width);
3535

3636
// make it free variables
37-
for(unsigned i=0; i<bv.size(); i++)
38-
bv[i]=prop.new_variable();
37+
Forall_literals(it, bv)
38+
*it=prop.new_variable();
3939

4040
if(operands.size()<3)
4141
throw "case takes at least three operands";

src/solvers/flattening/boolbv_cond.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ void boolbvt::convert_cond(const exprt &expr, bvt &bv)
3434
bv.resize(width);
3535

3636
// make it free variables
37-
for(unsigned i=0; i<bv.size(); i++)
38-
bv[i]=prop.new_variable();
37+
Forall_literals(it, bv)
38+
*it=prop.new_variable();
3939

4040
if(operands.size()<2)
4141
throw "cond takes at least two operands";

src/solvers/flattening/boolbv_typecast.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ void boolbvt::convert_typecast(const exprt &expr, bvt &bv)
135135

136136
assert(op_width==1);
137137

138-
for(unsigned i=0; i<bv.size(); i++)
139-
bv[i]=prop.land(bv[i], op_bv[0]);
138+
Forall_literals(it, bv)
139+
*it=prop.land(*it, op_bv[0]);
140140

141141
return;
142142
}

src/solvers/flattening/bv_pointers.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,8 @@ void bv_pointerst::convert_pointer_type(const exprt &expr, bvt &bv)
318318
}
319319
else if(expr.id()==ID_nondet_symbol)
320320
{
321-
for(unsigned i=0; i<bits; i++)
322-
bv[i]=prop.new_variable();
321+
Forall_literals(it, bv)
322+
*it=prop.new_variable();
323323

324324
return;
325325
}

src/solvers/flattening/bv_utils.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -707,10 +707,10 @@ void bv_utilst::incrementer(
707707
{
708708
carry_out=carry_in;
709709

710-
for(unsigned i=0; i<bv.size(); i++)
710+
Forall_literals(it, bv)
711711
{
712-
literalt new_carry=prop.land(carry_out, bv[i]);
713-
bv[i]=prop.lxor(bv[i], carry_out);
712+
literalt new_carry=prop.land(carry_out, *it);
713+
*it=prop.lxor(*it, carry_out);
714714
carry_out=new_carry;
715715
}
716716
}
@@ -750,8 +750,8 @@ Function: bv_utilst::invert
750750
bvt bv_utilst::inverted(const bvt &bv)
751751
{
752752
bvt result=bv;
753-
for(unsigned i=0; i<bv.size(); i++)
754-
result[i]=prop.lnot(result[i]);
753+
Forall_literals(it, result)
754+
*it=prop.lnot(*it);
755755
return result;
756756
}
757757

@@ -1311,8 +1311,8 @@ Function: bv_utilst::is_constant
13111311

13121312
bool bv_utilst::is_constant(const bvt &bv)
13131313
{
1314-
for(unsigned i=0; i<bv.size(); i++)
1315-
if(bv[i]!=const_literal(false) && bv[i]!=const_literal(true))
1314+
forall_literals(it, bv)
1315+
if(!it->is_constant())
13161316
return false;
13171317

13181318
return true;

src/solvers/prop/aig_prop.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ literalt aig_propt::land(const bvt &bv)
119119
{
120120
literalt literal=const_literal(true);
121121

122-
for(unsigned i=0; i<bv.size(); i++)
123-
literal=land(bv[i], literal);
122+
forall_literals(it, bv)
123+
literal=land(*it, literal);
124124

125125
return literal;
126126
}
@@ -141,8 +141,8 @@ literalt aig_propt::lor(const bvt &bv)
141141
{
142142
literalt literal=const_literal(false);
143143

144-
for(unsigned i=0; i<bv.size(); i++)
145-
literal=lor(bv[i], literal);
144+
forall_literals(it, bv)
145+
literal=lor(*it, literal);
146146

147147
return literal;
148148
}
@@ -163,8 +163,8 @@ literalt aig_propt::lxor(const bvt &bv)
163163
{
164164
literalt literal=const_literal(false);
165165

166-
for(unsigned i=0; i<bv.size(); i++)
167-
literal=lxor(bv[i], literal);
166+
forall_literals(it, bv)
167+
literal=lxor(*it, literal);
168168

169169
return literal;
170170
}

src/solvers/prop/literal.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,13 @@ literalt pos(literalt a);
185185

186186
typedef std::vector<literalt> bvt;
187187

188+
#define forall_literals(it, bv) \
189+
for(bvt::const_iterator it=(bv).begin(), it_end=(bv).end(); \
190+
it!=it_end; ++it)
191+
192+
#define Forall_literals(it, bv) \
193+
for(bvt::iterator it=(bv).begin(); \
194+
it!=(bv).end(); ++it)
195+
196+
188197
#endif

src/solvers/qbf/qbf_bdd_core.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -385,21 +385,19 @@ Function: qbf_bdd_coret::lor
385385

386386
literalt qbf_bdd_coret::lor(const bvt &bv)
387387
{
388-
for(unsigned long i=0; i<bv.size(); i++)
389-
{
390-
if(bv[i]==const_literal(true))
388+
forall_literals(it, bv)
389+
if(*it==const_literal(true))
391390
return const_literal(true);
392-
}
393391

394392
literalt nl = new_variable();
395393

396394
std::cout << "LORX" << std::endl;
397395

398396
BDD &orbdd = *bdd_variable_map[nl.var_no()];
399397

400-
for(unsigned long i=0; i<bv.size(); i++)
398+
forall_literals(it, bv)
401399
{
402-
literalt l=bv[i];
400+
literalt l=*it;
403401

404402
if(l==const_literal(false))
405403
continue;

src/solvers/sat/cnf.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,8 @@ literalt cnft::land(const bvt &bv)
296296
if(bv.size()==1) return bv[0];
297297
if(bv.size()==2) return land(bv[0], bv[1]);
298298

299-
for(unsigned i=0; i<bv.size(); i++)
300-
if(bv[i]==const_literal(false))
299+
forall_literals(it, bv)
300+
if(*it==const_literal(false))
301301
return const_literal(false);
302302

303303
if(is_all(bv, const_literal(true)))
@@ -309,20 +309,20 @@ literalt cnft::land(const bvt &bv)
309309

310310
literalt literal=new_variable();
311311

312-
for(unsigned int i=0; i<new_bv.size(); ++i)
312+
forall_literals(it, new_bv)
313313
{
314314
bvt lits;
315315
lits.reserve(2);
316-
lits.push_back(pos(new_bv[i]));
316+
lits.push_back(pos(*it));
317317
lits.push_back(neg(literal));
318318
lcnf(lits);
319319
}
320320

321321
bvt lits;
322322
lits.reserve(new_bv.size()+1);
323323

324-
for(unsigned int i=0; i<new_bv.size(); ++i)
325-
lits.push_back(neg(new_bv[i]));
324+
forall_literals(it, new_bv)
325+
lits.push_back(neg(*it));
326326

327327
lits.push_back(pos(literal));
328328
lcnf(lits);
@@ -348,8 +348,8 @@ literalt cnft::lor(const bvt &bv)
348348
if(bv.size()==1) return bv[0];
349349
if(bv.size()==2) return lor(bv[0], bv[1]);
350350

351-
for(unsigned i=0; i<bv.size(); i++)
352-
if(bv[i]==const_literal(true))
351+
forall_literals(it, bv)
352+
if(*it==const_literal(true))
353353
return const_literal(true);
354354

355355
if(is_all(bv, const_literal(false)))
@@ -361,20 +361,20 @@ literalt cnft::lor(const bvt &bv)
361361

362362
literalt literal=new_variable();
363363

364-
for(unsigned int i=0; i<new_bv.size(); ++i)
364+
forall_literals(it, new_bv)
365365
{
366366
bvt lits;
367367
lits.reserve(2);
368-
lits.push_back(neg(new_bv[i]));
368+
lits.push_back(neg(*it));
369369
lits.push_back(pos(literal));
370370
lcnf(lits);
371371
}
372372

373373
bvt lits;
374374
lits.reserve(new_bv.size()+1);
375375

376-
for(unsigned int i=0; i<new_bv.size(); ++i)
377-
lits.push_back(pos(new_bv[i]));
376+
forall_literals(it, new_bv)
377+
lits.push_back(pos(*it));
378378

379379
lits.push_back(neg(literal));
380380
lcnf(lits);
@@ -402,8 +402,8 @@ literalt cnft::lxor(const bvt &bv)
402402

403403
literalt literal=const_literal(false);
404404

405-
for(unsigned i=0; i<bv.size(); i++)
406-
literal=lxor(bv[i], literal);
405+
forall_literals(it, bv)
406+
literal=lxor(*it, literal);
407407

408408
return literal;
409409
}

src/solvers/sat/cnf.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ class cnft:public propt
5151

5252
static bool is_all(const bvt &bv, literalt l)
5353
{
54-
for(unsigned i=0; i<bv.size(); i++)
55-
if(bv[i]!=l) return false;
54+
forall_literals(it, bv)
55+
if(*it!=l) return false;
5656
return true;
5757
}
5858
};

src/solvers/sat/satcheck_minisat2.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ void convert(const bvt &bv, Minisat::vec<Minisat::Lit> &dest)
3939
{
4040
dest.growTo(bv.size());
4141

42-
for(unsigned i=0; i<bv.size(); i++)
43-
dest[i]=Minisat::mkLit(bv[i].var_no(), bv[i].sign());
42+
forall_literals(it, bv)
43+
dest[it-bv.begin()]=Minisat::mkLit(it->var_no(), it->sign());
4444
}
4545

4646
/*******************************************************************\
@@ -167,8 +167,8 @@ void satcheck_minisat2_baset<T>::lcnf(const bvt &bv)
167167
Minisat::vec<Minisat::Lit> c;
168168
convert(new_bv, c);
169169

170-
for(unsigned i=0; i<new_bv.size(); i++)
171-
assert(new_bv[i].var_no()<(unsigned)solver->nVars());
170+
forall_literals(it, new_bv)
171+
assert(it->var_no()<(unsigned)solver->nVars());
172172

173173
solver->addClause(c);
174174

@@ -375,9 +375,7 @@ void satcheck_minisat2_baset<T>::set_assumptions(const bvt &bv)
375375
{
376376
assumptions=bv;
377377

378-
for(bvt::const_iterator it=assumptions.begin();
379-
it!=assumptions.end();
380-
it++)
378+
forall_literals(it, assumptions)
381379
assert(!it->is_constant());
382380
}
383381

0 commit comments

Comments
 (0)