Skip to content

fix sorting in symbol_table_baset::show #4491

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
May 19, 2019
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: 2 additions & 9 deletions src/goto-programs/show_symbol_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,10 @@ void show_symbol_table_plain(
{
out << '\n' << "Symbols:" << '\n' << '\n';

// we want to sort alphabetically
std::vector<std::string> symbols;
symbols.reserve(symbol_table.symbols.size());

for(const auto &symbol_pair : symbol_table.symbols)
symbols.push_back(id2string(symbol_pair.first));
std::sort(symbols.begin(), symbols.end());

const namespacet ns(symbol_table);

for(const irep_idt &id : symbols)
// we want to sort alphabetically
for(const irep_idt &id : symbol_table.sorted_symbol_names())
{
const symbolt &symbol=ns.lookup(id);

Expand Down
18 changes: 13 additions & 5 deletions src/util/symbol_table_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,30 @@ bool symbol_table_baset::remove(const irep_idt &name)
return false;
}

/// Print the contents of the symbol table.
/// \param out: The ostream to direct output to.
void symbol_table_baset::show(std::ostream &out) const
std::vector<irep_idt> symbol_table_baset::sorted_symbol_names() const
{
std::vector<irep_idt> sorted_names;
sorted_names.reserve(symbols.size());

for(const auto &elem : symbols)
sorted_names.push_back(elem.first);

std::sort(
sorted_names.begin(),
sorted_names.end(),
[](const irep_idt &a, const irep_idt &b) { return a.compare(b); });
[](const irep_idt &a, const irep_idt &b) { return a.compare(b) < 0; });

return sorted_names;
}

/// Print the contents of the symbol table.
/// \param out: The ostream to direct output to.
void symbol_table_baset::show(std::ostream &out) const
{
out << "\n"
<< "Symbols:"
<< "\n";
for(const auto &name : sorted_names)
for(const auto &name : sorted_symbol_names())
out << symbols.at(name);
}

Expand Down
4 changes: 4 additions & 0 deletions src/util/symbol_table_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ class symbol_table_baset

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

/// Build and return a lexicographically sorted vector of symbol names from
/// all symbols stored in this symbol table.
std::vector<irep_idt> sorted_symbol_names() const;

class iteratort
{
private:
Expand Down