Skip to content

Commit b05f69f

Browse files
authored
ParmParse::prettyPrintTable (#4101)
Add a new function that prints the ParmParse table without duplicates. Note that these are not duplicates, because they have different values. foo.bar = 1 foo.bar = 2 These are also not considered duplicates, even though the final results are the same. c = 3 foo.bar = c foo.bar = 3 This should help WarpX to reduce duplicates when printing ParmParse parameters. However, there will still be "duplicates" like below because they are different strings. geometry.prob_hi = 20.e-6 20.e-6 geometry.prob_hi = 2.0000000000000002e-05 2.0000000000000002e-05
1 parent dea2432 commit b05f69f

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

Src/Base/AMReX_ParmParse.H

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1352,6 +1352,10 @@ public:
13521352
//! Write the contents of the table in ASCII to the ostream.
13531353
static void dumpTable (std::ostream& os, bool prettyPrint = false);
13541354

1355+
//! Write the table in a pretty way to the ostream. If there are
1356+
//! duplicates, only the last one is printed.
1357+
static void prettyPrintTable (std::ostream& os);
1358+
13551359
//! Add keys and values from a file to the end of the PP table.
13561360
static void addfile (std::string const& filename);
13571361

Src/Base/AMReX_ParmParse.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <iostream>
1313
#include <limits>
1414
#include <numeric>
15+
#include <unordered_map>
1516
#include <regex>
1617
#include <sstream>
1718
#include <stdexcept>
@@ -1209,6 +1210,32 @@ ParmParse::dumpTable (std::ostream& os, bool prettyPrint)
12091210
}
12101211
}
12111212

1213+
void
1214+
ParmParse::prettyPrintTable (std::ostream& os)
1215+
{
1216+
std::vector<std::string> sorted_names;
1217+
sorted_names.reserve(g_table.size());
1218+
for (auto const& [name, entry] : g_table) {
1219+
sorted_names.push_back(name);
1220+
}
1221+
std::sort(sorted_names.begin(), sorted_names.end());
1222+
1223+
for (auto const& name : sorted_names) {
1224+
auto const& entry = g_table[name];
1225+
std::vector<std::string> value_string;
1226+
std::unordered_map<std::string,int> count;
1227+
for (auto const& vals : entry.m_vals) {
1228+
value_string.emplace_back(pp_to_pretty_string(name, vals));
1229+
++count[value_string.back()];
1230+
}
1231+
for (auto const& s : value_string) {
1232+
if (--count[s] == 0) {
1233+
os << s << '\n';
1234+
}
1235+
}
1236+
}
1237+
}
1238+
12121239
int
12131240
ParmParse::countval (const char* name,
12141241
int n) const

0 commit comments

Comments
 (0)