Skip to content

Commit f7c89e1

Browse files
Add iterator for symbol_table_baset
1 parent 150f826 commit f7c89e1

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

src/util/journalling_symbol_table.h

+11
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,17 @@ class journalling_symbol_tablet : public symbol_table_baset
124124
base_symbol_table.clear();
125125
}
126126

127+
virtual iteratort begin() override
128+
{
129+
return iteratort(
130+
base_symbol_table.begin(), [this](const irep_idt &id) { on_update(id); });
131+
}
132+
virtual iteratort end() override
133+
{
134+
return iteratort(
135+
base_symbol_table.end(), [this](const irep_idt &id) { on_update(id); });
136+
}
137+
127138
const changesett &get_inserted() const
128139
{
129140
return inserted;

src/util/symbol_table.h

+9
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,15 @@ class symbol_tablet : public symbol_table_baset
109109
internal_symbol_base_map.clear();
110110
internal_symbol_module_map.clear();
111111
}
112+
113+
virtual iteratort begin() override
114+
{
115+
return iteratort(internal_symbols.begin());
116+
}
117+
virtual iteratort end() override
118+
{
119+
return iteratort(internal_symbols.end());
120+
}
112121
};
113122

114123
#endif // CPROVER_UTIL_SYMBOL_TABLE_H

src/util/symbol_table_base.h

+73
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#ifndef CPROVER_UTIL_SYMBOL_TABLE_BASE_H
99
#define CPROVER_UTIL_SYMBOL_TABLE_BASE_H
1010

11+
#include <functional>
1112
#include <iosfwd>
1213
#include <map>
1314
#include <unordered_map>
@@ -117,6 +118,78 @@ class symbol_table_baset
117118
virtual void clear() = 0;
118119

119120
void show(std::ostream &out) const;
121+
122+
class iteratort
123+
{
124+
private:
125+
symbolst::iterator it;
126+
std::function<void(const irep_idt &id)> on_get_writeable;
127+
128+
public:
129+
explicit iteratort(symbolst::iterator it) : it(std::move(it))
130+
{
131+
}
132+
133+
iteratort(
134+
const iteratort &it,
135+
std::function<void(const irep_idt &id)> on_get_writeable)
136+
: it(it.it), on_get_writeable(std::move(on_get_writeable))
137+
{
138+
}
139+
140+
// The following typedefs are NOLINT as they are needed by the STL
141+
typedef symbolst::iterator::difference_type difference_type; // NOLINT
142+
typedef symbolst::const_iterator::value_type value_type; // NOLINT
143+
typedef symbolst::const_iterator::pointer pointer; // NOLINT
144+
typedef symbolst::const_iterator::reference reference; // NOLINT
145+
typedef symbolst::iterator::iterator_category iterator_category; // NOLINT
146+
147+
bool operator==(const iteratort &other) const
148+
{
149+
return it == other.it;
150+
}
151+
152+
/// Preincrement operator
153+
/// Do not call on the end() iterator
154+
iteratort &operator++()
155+
{
156+
++it;
157+
return *this;
158+
}
159+
160+
/// Post-increment operator
161+
/// \remarks Expensive copy. Avoid if possible.
162+
iteratort operator++(int)
163+
{
164+
iteratort copy(*this);
165+
this->operator++();
166+
return copy;
167+
}
168+
169+
/// Dereference operator
170+
/// \remarks Dereferencing end() iterator is undefined behaviour
171+
reference operator*() const
172+
{
173+
return *it;
174+
}
175+
176+
/// Dereference operator (member access)
177+
/// \remarks Dereferencing end() iterator is undefined behaviour
178+
pointer operator->() const
179+
{
180+
return &**this;
181+
}
182+
183+
symbolt &get_writeable_symbol(const irep_idt &identifier)
184+
{
185+
if(on_get_writeable)
186+
on_get_writeable((*this)->first);
187+
return it->second;
188+
}
189+
};
190+
191+
virtual iteratort begin() = 0;
192+
virtual iteratort end() = 0;
120193
};
121194

122195
std::ostream &

0 commit comments

Comments
 (0)