Skip to content

Commit 97c87a6

Browse files
Improve sum_over_map code
Declare variables after a return that does not use it. Rename `second` to `factor` which reflects better what it represents. Improve comment about the constant case.
1 parent 046e9b1 commit 97c87a6

File tree

2 files changed

+42
-43
lines changed

2 files changed

+42
-43
lines changed

src/solvers/strings/string_constraint_instantiation.cpp

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -88,69 +88,66 @@ std::map<exprt, int> map_representation_of_sum(const exprt &f)
8888
return elems;
8989
}
9090

91-
exprt
92-
sum_over_map(std::map<exprt, int> &m, const typet &type, bool negated)
91+
exprt sum_over_map(std::map<exprt, int> &m, const typet &type, bool negated)
9392
{
94-
exprt sum = nil_exprt();
95-
mp_integer constants = 0;
96-
typet index_type;
9793
if(m.empty())
9894
return from_integer(0, type);
99-
else
100-
index_type = m.begin()->first.type();
95+
96+
exprt sum = nil_exprt();
97+
mp_integer constants = 0;
98+
typet index_type = m.begin()->first.type();
10199

102100
for(const auto &term : m)
103101
{
104-
// We should group constants together...
105102
const exprt &t = term.first;
106-
int second = negated ? (-term.second) : term.second;
103+
int factor = negated ? (-term.second) : term.second;
107104
if(t.id() == ID_constant)
108105
{
106+
// Constants are accumulated in the variable \c constants
109107
const auto int_value = numeric_cast_v<mp_integer>(to_constant_expr(t));
110-
constants += int_value * second;
108+
constants += int_value * factor;
111109
}
112110
else
113111
{
114-
switch(second)
112+
switch(factor)
115113
{
116-
case -1:
117-
if(sum.is_nil())
118-
sum = unary_minus_exprt(t);
119-
else
120-
sum = minus_exprt(sum, t);
121-
break;
114+
case -1:
115+
if(sum.is_nil())
116+
sum = unary_minus_exprt(t);
117+
else
118+
sum = minus_exprt(sum, t);
119+
break;
120+
121+
case 1:
122+
if(sum.is_nil())
123+
sum = t;
124+
else
125+
sum = plus_exprt(sum, t);
126+
break;
122127

123-
case 1:
128+
default:
129+
if(factor > 1)
130+
{
124131
if(sum.is_nil())
125132
sum = t;
126133
else
127-
sum = plus_exprt(sum, t);
128-
break;
129-
130-
default:
131-
if(second > 1)
132-
{
133-
if(sum.is_nil())
134-
sum = t;
135-
else
136-
plus_exprt(sum, t);
137-
for(int i = 1; i < second; i++)
138-
sum = plus_exprt(sum, t);
139-
}
140-
else if(second < -1)
141-
{
142-
if(sum.is_nil())
143-
sum = unary_minus_exprt(t);
144-
else
145-
sum = minus_exprt(sum, t);
146-
for(int i = -1; i > second; i--)
147-
sum = minus_exprt(sum, t);
148-
}
134+
plus_exprt(sum, t);
135+
for(int i = 1; i < factor; i++) sum = plus_exprt(sum, t);
136+
}
137+
else if(factor < -1)
138+
{
139+
if(sum.is_nil())
140+
sum = unary_minus_exprt(t);
141+
else
142+
sum = minus_exprt(sum, t);
143+
for(int i = -1; i > factor; i--)
144+
sum = minus_exprt(sum, t);
145+
}
149146
}
150147
}
151148
}
152149

153-
exprt index_const = from_integer(constants, index_type);
150+
const exprt index_const = from_integer(constants, index_type);
154151
if(sum.is_not_nil())
155152
return plus_exprt(sum, index_const);
156153
else

src/solvers/strings/string_constraint_instantiation.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ std::map<exprt, int> map_representation_of_sum(const exprt &f);
4747
/// \return a expression for the sum of each element in the map a number of
4848
/// times given by the corresponding integer in the map. For a map x -> 2, y
4949
/// -> -1 would give an expression $x + x - y$.
50-
exprt
51-
sum_over_map(std::map<exprt, int> &m, const typet &type, bool negated = false);
50+
exprt sum_over_map(
51+
std::map<exprt, int> &m,
52+
const typet &type,
53+
bool negated = false);
5254

5355
#endif // CPROVER_SOLVERS_REFINEMENT_STRING_CONSTRAINT_INSTANTIATION_H

0 commit comments

Comments
 (0)