Skip to content

Commit 0d03591

Browse files
Add an at function for access in sparse arrays
1 parent 848dd95 commit 0d03591

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/solvers/refinement/string_refinement_util.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ exprt sparse_arrayt::to_if_expression(const exprt &index) const
123123
});
124124
}
125125

126+
exprt sparse_arrayt::at(const std::size_t index) const
127+
{
128+
const auto it = entries.find(index);
129+
return it != entries.end() ? it->second : default_value;
130+
}
131+
126132
exprt interval_sparse_arrayt::to_if_expression(const exprt &index) const
127133
{
128134
return std::accumulate(
@@ -188,6 +194,13 @@ interval_sparse_arrayt::of_expr(const exprt &expr, const exprt &extra_value)
188194
return {};
189195
}
190196

197+
exprt interval_sparse_arrayt::at(const std::size_t index) const
198+
{
199+
// First element at or after index
200+
const auto it = entries.lower_bound(index);
201+
return it != entries.end() ? it->second : default_value;
202+
}
203+
191204
void equation_symbol_mappingt::add(const std::size_t i, const exprt &expr)
192205
{
193206
equations_containing[expr].push_back(i);

src/solvers/refinement/string_refinement_util.h

+8
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ class sparse_arrayt
9696
/// at the given index
9797
virtual exprt to_if_expression(const exprt &index) const;
9898

99+
/// Get the value at the specified index.
100+
/// Complexity is linear in the number of entries.
101+
virtual exprt at(std::size_t index) const;
102+
99103
protected:
100104
exprt default_value;
101105
std::map<std::size_t, exprt> entries;
@@ -137,6 +141,10 @@ class interval_sparse_arrayt final : public sparse_arrayt
137141
static optionalt<interval_sparse_arrayt>
138142
of_expr(const exprt &expr, const exprt &extra_value);
139143

144+
/// Get the value at the specified index.
145+
/// Complexity is linear in the number of entries.
146+
exprt at(std::size_t index) const override;
147+
140148
private:
141149
explicit interval_sparse_arrayt(exprt default_value)
142150
: sparse_arrayt(default_value)

0 commit comments

Comments
 (0)