Skip to content

Commit cb10550

Browse files
Use sparse arrays in substitute_array_access
The obtained expression can be exponentially smaller, because sparse array representation avoids repetitions.
1 parent ce4c008 commit cb10550

File tree

1 file changed

+5
-23
lines changed

1 file changed

+5
-23
lines changed

src/solvers/refinement/string_refinement.cpp

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,36 +1188,18 @@ exprt fill_in_array_expr(const array_exprt &expr, std::size_t string_max_length)
11881188
/// expressions: for instance in array access `arr[index]`, where:
11891189
/// `arr := {12, 24, 48}` the constructed expression will be:
11901190
/// `index==0 ? 12 : index==1 ? 24 : 48`
1191+
/// Avoids repetition so `arr := {12, 12, 24, 48}` will give
1192+
/// `index<=1 ? 12 : index==1 ? 24 : 48`
11911193
static exprt substitute_array_access(
11921194
const array_exprt &array_expr,
11931195
const exprt &index,
11941196
const std::function<symbol_exprt(const irep_idt &, const typet &)>
11951197
&symbol_generator)
11961198
{
11971199
const typet &char_type = array_expr.type().subtype();
1198-
const typet &index_type = to_array_type(array_expr.type()).size().type();
1199-
const std::vector<exprt> &operands = array_expr.operands();
1200-
1201-
exprt result = symbol_generator("out_of_bound_access", char_type);
1202-
1203-
for(std::size_t i = 0; i < operands.size(); ++i)
1204-
{
1205-
// Go in reverse order so that smaller indexes appear first in the result
1206-
const std::size_t pos = operands.size() - 1 - i;
1207-
const equal_exprt equals(index, from_integer(pos, index_type));
1208-
if(operands[pos].type() != char_type)
1209-
{
1210-
INVARIANT(
1211-
operands[pos].id() == ID_unknown,
1212-
string_refinement_invariantt(
1213-
"elements can only have type char or "
1214-
"unknown, and it is not char type"));
1215-
result = if_exprt(equals, exprt(ID_unknown, char_type), result);
1216-
}
1217-
else
1218-
result = if_exprt(equals, operands[pos], result);
1219-
}
1220-
return result;
1200+
const exprt default_val = symbol_generator("out_of_bound_access", char_type);
1201+
const interval_sparse_arrayt sparse_array(array_expr, default_val);
1202+
return sparse_array.to_if_expression(index);
12211203
}
12221204

12231205
static exprt substitute_array_access(

0 commit comments

Comments
 (0)