Skip to content

Commit 5f07bf0

Browse files
Initialization of sparse array from array-list
1 parent 5b4d618 commit 5f07bf0

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/solvers/refinement/string_refinement_util.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,24 @@ interval_sparse_arrayt::interval_sparse_arrayt(
158158
}
159159
}
160160

161+
interval_sparse_arrayt interval_sparse_arrayt::of_array_list(
162+
const exprt &expr,
163+
const exprt &extra_value)
164+
{
165+
PRECONDITION(expr.operands().size() % 2 == 0);
166+
interval_sparse_arrayt sparse_array(extra_value);
167+
for(std::size_t i = 0; i < expr.operands().size(); i += 2)
168+
{
169+
const auto index = numeric_cast<std::size_t>(expr.operands()[i]);
170+
INVARIANT(
171+
expr.operands()[i + 1].type() == extra_value.type(),
172+
"all values in array should have the same type");
173+
if(index.has_value() && expr.operands()[i + 1].id() != ID_unknown)
174+
sparse_array.entries[*index] = expr.operands()[i + 1];
175+
}
176+
return sparse_array;
177+
}
178+
161179
void equation_symbol_mappingt::add(const std::size_t i, const exprt &expr)
162180
{
163181
equations_containing[expr].push_back(i);

src/solvers/refinement/string_refinement_util.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,19 @@ class interval_sparse_arrayt final : public sparse_arrayt
124124
/// `extra_value`.
125125
interval_sparse_arrayt(const array_exprt &expr, const exprt &extra_value);
126126

127+
/// Initialize a sparse array from an array represented by a list of
128+
/// index-value pairs, and setting the default to `extra_value`.
129+
/// Indexes must be constant expressions, and negative indexes are ignored.
130+
static interval_sparse_arrayt
131+
of_array_list(const exprt &expr, const exprt &extra_value);
132+
127133
exprt to_if_expression(const exprt &index) const override;
134+
135+
private:
136+
explicit interval_sparse_arrayt(exprt default_value)
137+
: sparse_arrayt(default_value)
138+
{
139+
}
128140
};
129141

130142
/// Maps equation to expressions contained in them and conversely expressions to

0 commit comments

Comments
 (0)