Skip to content

Commit

Permalink
ParamParse: Find Entries under Prefix (#2043)
Browse files Browse the repository at this point in the history
Extend the "unused params" checks to find generally parameters under a given inputs prefix.

Demonstrator & test in: AMReX-Codes/amrex-tutorials#7
  • Loading branch information
ax3l authored May 24, 2021
1 parent 8935b4c commit 646d5f6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
4 changes: 4 additions & 0 deletions Src/Base/AMReX_ParmParse.H
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <AMReX_BLassert.H>

#include <set>
#include <stack>
#include <string>
#include <iosfwd>
Expand Down Expand Up @@ -935,6 +936,9 @@ public:
//! Returns unused [prefix.]* parameters.
static std::vector<std::string> getUnusedInputs (const std::string& prefix = std::string());

//! Returns [prefix.]* parameters.
static std::set<std::string> getEntries (const std::string& prefix = std::string());

struct PP_entry;
typedef std::list<PP_entry> Table;
static void appendTable(ParmParse::Table& tab);
Expand Down
35 changes: 26 additions & 9 deletions Src/Base/AMReX_ParmParse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include <cctype>
#include <vector>
#include <list>
#include <set>
#include <string>

extern "C" void amrex_init_namelist (const char*);
extern "C" void amrex_finalize_namelist ();
Expand Down Expand Up @@ -1112,23 +1114,30 @@ ParmParse::hasUnusedInputs (const std::string& prefix)

static
void
get_unused_inputs(std::vector<std::string>& unused, const ParmParse::Table& table,
const std::string& prefix)
get_entries_under_prefix (std::vector<std::string>& found_entries,
const ParmParse::Table& table,
const std::string& prefix,
const bool only_unused = false,
const bool add_values = false)
{
const std::string prefixdot = prefix.empty() ? std::string() : prefix+".";
for (auto const& entry : table) {
if (! entry.m_queried) {
if ((! only_unused) || (only_unused && ! entry.m_queried)) {
if (entry.m_name.substr(0,prefixdot.size()) == prefixdot) {
std::string tmp(entry.m_name + " =");
for (auto const& v : entry.m_vals) {
tmp += " " + v;
std::string tmp(entry.m_name);
if (add_values) {
tmp.append(" =");
for (auto const& v : entry.m_vals) {
tmp += " " + v;
}
}
unused.emplace_back(std::move(tmp));
found_entries.emplace_back(std::move(tmp));
}
}

if (entry.m_table) {
get_unused_inputs(unused, table, prefix);
get_entries_under_prefix(found_entries, table, prefix,
only_unused, add_values);
}
}
}
Expand All @@ -1137,10 +1146,18 @@ std::vector<std::string>
ParmParse::getUnusedInputs (const std::string& prefix)
{
std::vector<std::string> r;
get_unused_inputs(r, g_table, prefix);
get_entries_under_prefix(r, g_table, prefix, true, true);
return r;
}

std::set<std::string>
ParmParse::getEntries (const std::string& prefix)
{
std::vector<std::string> r;
get_entries_under_prefix(r, g_table, prefix, false, false);
return std::set<std::string>(r.begin(), r.end());
}

void
ParmParse::Finalize ()
{
Expand Down

0 comments on commit 646d5f6

Please sign in to comment.