Skip to content

Commit 646d5f6

Browse files
authored
ParamParse: Find Entries under Prefix (#2043)
Extend the "unused params" checks to find generally parameters under a given inputs prefix. Demonstrator & test in: AMReX-Codes/amrex-tutorials#7
1 parent 8935b4c commit 646d5f6

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

Src/Base/AMReX_ParmParse.H

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include <AMReX_BLassert.H>
77

8+
#include <set>
89
#include <stack>
910
#include <string>
1011
#include <iosfwd>
@@ -935,6 +936,9 @@ public:
935936
//! Returns unused [prefix.]* parameters.
936937
static std::vector<std::string> getUnusedInputs (const std::string& prefix = std::string());
937938

939+
//! Returns [prefix.]* parameters.
940+
static std::set<std::string> getEntries (const std::string& prefix = std::string());
941+
938942
struct PP_entry;
939943
typedef std::list<PP_entry> Table;
940944
static void appendTable(ParmParse::Table& tab);

Src/Base/AMReX_ParmParse.cpp

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include <cctype>
2020
#include <vector>
2121
#include <list>
22+
#include <set>
23+
#include <string>
2224

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

11131115
static
11141116
void
1115-
get_unused_inputs(std::vector<std::string>& unused, const ParmParse::Table& table,
1116-
const std::string& prefix)
1117+
get_entries_under_prefix (std::vector<std::string>& found_entries,
1118+
const ParmParse::Table& table,
1119+
const std::string& prefix,
1120+
const bool only_unused = false,
1121+
const bool add_values = false)
11171122
{
11181123
const std::string prefixdot = prefix.empty() ? std::string() : prefix+".";
11191124
for (auto const& entry : table) {
1120-
if (! entry.m_queried) {
1125+
if ((! only_unused) || (only_unused && ! entry.m_queried)) {
11211126
if (entry.m_name.substr(0,prefixdot.size()) == prefixdot) {
1122-
std::string tmp(entry.m_name + " =");
1123-
for (auto const& v : entry.m_vals) {
1124-
tmp += " " + v;
1127+
std::string tmp(entry.m_name);
1128+
if (add_values) {
1129+
tmp.append(" =");
1130+
for (auto const& v : entry.m_vals) {
1131+
tmp += " " + v;
1132+
}
11251133
}
1126-
unused.emplace_back(std::move(tmp));
1134+
found_entries.emplace_back(std::move(tmp));
11271135
}
11281136
}
11291137

11301138
if (entry.m_table) {
1131-
get_unused_inputs(unused, table, prefix);
1139+
get_entries_under_prefix(found_entries, table, prefix,
1140+
only_unused, add_values);
11321141
}
11331142
}
11341143
}
@@ -1137,10 +1146,18 @@ std::vector<std::string>
11371146
ParmParse::getUnusedInputs (const std::string& prefix)
11381147
{
11391148
std::vector<std::string> r;
1140-
get_unused_inputs(r, g_table, prefix);
1149+
get_entries_under_prefix(r, g_table, prefix, true, true);
11411150
return r;
11421151
}
11431152

1153+
std::set<std::string>
1154+
ParmParse::getEntries (const std::string& prefix)
1155+
{
1156+
std::vector<std::string> r;
1157+
get_entries_under_prefix(r, g_table, prefix, false, false);
1158+
return std::set<std::string>(r.begin(), r.end());
1159+
}
1160+
11441161
void
11451162
ParmParse::Finalize ()
11461163
{

0 commit comments

Comments
 (0)