Skip to content

Add iterator for symbol_table_baset #1802

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/util/journalling_symbol_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,17 @@ class journalling_symbol_tablet : public symbol_table_baset
base_symbol_table.clear();
}

virtual iteratort begin() override
{
return iteratort(
base_symbol_table.begin(), [this](const irep_idt &id) { on_update(id); });
}
virtual iteratort end() override
{
return iteratort(
base_symbol_table.end(), [this](const irep_idt &id) { on_update(id); });
}

const changesett &get_inserted() const
{
return inserted;
Expand Down
9 changes: 9 additions & 0 deletions src/util/symbol_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ class symbol_tablet : public symbol_table_baset
internal_symbol_base_map.clear();
internal_symbol_module_map.clear();
}

virtual iteratort begin() override
{
return iteratort(internal_symbols.begin());
}
virtual iteratort end() override
{
return iteratort(internal_symbols.end());
}
};

#endif // CPROVER_UTIL_SYMBOL_TABLE_H
73 changes: 73 additions & 0 deletions src/util/symbol_table_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#ifndef CPROVER_UTIL_SYMBOL_TABLE_BASE_H
#define CPROVER_UTIL_SYMBOL_TABLE_BASE_H

#include <functional>
#include <iosfwd>
#include <map>
#include <unordered_map>
Expand Down Expand Up @@ -117,6 +118,78 @@ class symbol_table_baset
virtual void clear() = 0;

void show(std::ostream &out) const;

class iteratort
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't iterators inherit some tag class to indicate they're a forward, bidir, random-access iterator etc? Or does inheriting category below achieve this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, that's what the iterator_category is.

{
private:
symbolst::iterator it;
std::function<void(const irep_idt &id)> on_get_writeable;

public:
explicit iteratort(symbolst::iterator it) : it(std::move(it))
{
}

iteratort(
const iteratort &it,
std::function<void(const irep_idt &id)> on_get_writeable)
: it(it.it), on_get_writeable(std::move(on_get_writeable))
{
}

// The following typedefs are NOLINT as they are needed by the STL
typedef symbolst::iterator::difference_type difference_type; // NOLINT
typedef symbolst::const_iterator::value_type value_type; // NOLINT
typedef symbolst::const_iterator::pointer pointer; // NOLINT
typedef symbolst::const_iterator::reference reference; // NOLINT
typedef symbolst::iterator::iterator_category iterator_category; // NOLINT

bool operator==(const iteratort &other) const
{
return it == other.it;
}

/// Preincrement operator
/// Do not call on the end() iterator
iteratort &operator++()
{
++it;
return *this;
}

/// Post-increment operator
/// \remarks Expensive copy. Avoid if possible.
iteratort operator++(int)
{
iteratort copy(*this);
this->operator++();
return copy;
}

/// Dereference operator
/// \remarks Dereferencing end() iterator is undefined behaviour
reference operator*() const
{
return *it;
}

/// Dereference operator (member access)
/// \remarks Dereferencing end() iterator is undefined behaviour
pointer operator->() const
{
return &**this;
}

symbolt &get_writeable_symbol(const irep_idt &identifier)
{
if(on_get_writeable)
on_get_writeable((*this)->first);
return it->second;
}
};

virtual iteratort begin() = 0;
virtual iteratort end() = 0;
};

std::ostream &
Expand Down