Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_ParameterListModifier.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
12 #include "Teuchos_StrUtils.hpp"
13 
14 namespace Teuchos {
15 
16 
17 // Constructors and/or destructors
19  :name_(name_in)
20 {}
21 
23 {}
24 
25 
26 void ParameterListModifier::printDoc(std::string const& docString, std::ostream &out) const
27 {
28  StrUtils::printLines(out,"# ",docString);
29  out << "# Modifier Used: " << name_ << std::endl;
30 }
31 
32 
34  const std::string &base_name, const bool &find_parameters, const bool &find_sublists) const
35 {
36  Array<std::string> matches(0);
38  for (itr = pl.begin(); itr != pl.end(); ++itr) {
39  const std::string &name = pl.name(itr);
40  std::size_t found = name.find(base_name);
41  if (found == 0){
42  if (pl.isSublist(name)){
43  if (find_sublists){
44  matches.push_back(name);
45  }
46  } else{
47  if (find_parameters){
48  matches.push_back(name);
49  }
50  }
51  }
52  }
53  return matches;
54 }
55 
56 
58  const std::string &param_template_name, ParameterList &pl, ParameterList &valid_pl,
59  const Array<std::string> &exclude_parameters) const
60 {
61  int replacements = 0;
62  auto ignore_names = exclude_parameters;
63  std::sort(ignore_names.begin(), ignore_names.end());
65  if (valid_pl.isParameter(param_template_name)){
66  ParameterEntry &valid_pl_entry = valid_pl.getEntry(param_template_name);
67  for (itr = pl.begin(); itr != pl.end(); ++itr) {
68  const std::string &param_name = pl.name(itr);
69  if (!pl.isSublist(param_name)){
70  if (!std::binary_search(ignore_names.begin(), ignore_names.end(), param_name)){
71  valid_pl.setEntry(param_name, valid_pl_entry);
72  replacements += 1;
73  }
74  }
75  }
76  valid_pl.remove(param_template_name);
77  }
78  return replacements;
79 }
80 
81 
83  const std::string &sublist_template_name, ParameterList &pl, ParameterList &valid_pl,
84  const Array<std::string> &exclude_sublists) const
85 {
86  int replacements = 0;
87  auto ignore_names = exclude_sublists;
88  std::sort(ignore_names.begin(), ignore_names.end());
90  if (valid_pl.isSublist(sublist_template_name)){
91  ParameterList &valid_pl_sublist = valid_pl.get<ParameterList>(sublist_template_name);
92  for (itr = pl.begin(); itr != pl.end(); ++itr) {
93  const std::string &subname = pl.name(itr);
94  if (pl.isSublist(subname)){
95  if (!std::binary_search(ignore_names.begin(), ignore_names.end(), subname)){
96  valid_pl.set(subname, valid_pl_sublist);
97  replacements += 1;
98  }
99  }
100  }
101  valid_pl.remove(sublist_template_name);
102  }
103  return replacements;
104 }
105 
106 
108  const std::string &base_name, ParameterList &pl, ParameterList &valid_pl,
109  const bool &allow_base_name) const
110 {
111  int replacements = 0;
112  bool delete_base_name = true;
113  if (valid_pl.isSublist(base_name)){
114  if (pl.isSublist(base_name)){
115  TEUCHOS_TEST_FOR_EXCEPTION(!allow_base_name, std::logic_error,
116  "Sublist can't have the same name as the parameter template name "
117  "without `allow_base_name=true`.");
118  delete_base_name = false;
119  }
120  Array<std::string> matches = findMatchingBaseNames(pl, base_name, false, true);
121  replacements = matches.length();
122  for (const std::string &match_name : matches){
123  valid_pl.set(match_name, valid_pl.get<ParameterList>(base_name));
124  }
125  if (delete_base_name){
126  valid_pl.remove(base_name);
127  }
128  }
129  return replacements;
130 }
131 
132 
133 int ParameterListModifier::setDefaultsInSublists(const std::string &param_name,
134  ParameterList &pl, const Array<std::string> &sublist_names,
135  const bool remove_param) const
136 {
137  int num_defaults = 0;
138  if (pl.isParameter(param_name)){
139  for (const std::string &sublist_name : sublist_names){
140  if (pl.isSublist(sublist_name)){
141  ParameterList &sublist = pl.sublist(sublist_name);
142  if (!sublist.isParameter(param_name)){
143  ParameterEntry &pl_entry = pl.getEntry(param_name);
144  sublist.setEntry(param_name, pl_entry);
145  }
146  }
147  }
148  if (remove_param){
149  pl.remove(param_name);
150  }
151  }
152  return num_defaults;
153 }
154 
155 
156 } // namespace Teuchos
const std::string & name() const
The name of this ParameterList.
C++ Standard Library compatable filtered iterator.
ParameterList & setEntry(const std::string &name, U &&entry)
Set a parameter directly as a ParameterEntry.
ConstIterator end() const
An iterator pointing beyond the last entry.
T & get(const std::string &name, T def_value)
Return the parameter&#39;s value, or the default value if it is not there.
This object is held as the &quot;value&quot; in the Teuchos::ParameterList std::map.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
static std::ostream & printLines(std::ostream &os, const std::string &linePrefix, const std::string &lines)
Print lines with prefix first.
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
Templated set method.
A std::string utilities class for Teuchos.
bool isParameter(const std::string &name) const
Whether the given parameter exists in this list.
bool remove(std::string const &name, bool throwIfNotExists=true)
Remove a parameter (does not depend on the type of the parameter).
int expandParameters(const std::string &paramTemplateName, ParameterList &paramList, ParameterList &validParamList, const Array< std::string > &excludeParameters=Array< std::string >()) const
Create parameters in the valid parameter list using a template parameter from the valid parameter lis...
int setDefaultsInSublists(const std::string &paramName, ParameterList &paramList, const Array< std::string > &sublistNames, const bool removeParam=true) const
Copy a parameter into desired sublists.
Templated Parameter List class.
bool isSublist(const std::string &name) const
Whether the given sublist exists in this list.
Array< std::string > findMatchingBaseNames(const ParameterList &paramList, const std::string &baseName, const bool &findParameters=true, const bool &findSublists=true) const
Find the parameters and/or sublists with matching base names.
int expandSublistsUsingBaseName(const std::string &baseName, ParameterList &paramList, ParameterList &validParamList, const bool &allowBaseName=true) const
Create sublists in the valid parameter list using a base name and the corresponding sublists in the p...
ConstIterator begin() const
An iterator pointing to the first entry.
A list of parameters of arbitrary type.
Parameter List Modifier class.
void push_back(const value_type &x)
int length() const
Return number of elements in the array.
ParameterList & sublist(const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
Creates an empty sublist and returns a reference to the sublist name. If the list already exists...
void printDoc(std::string const &docString, std::ostream &out) const
Print documentation for this parameter list modifier.
ParameterEntry & getEntry(const std::string &name)
Retrieves an entry with the name name.
ParameterListModifier()=default
Constructor.
int expandSublists(const std::string &sublistTemplateName, ParameterList &paramList, ParameterList &validParamList, const Array< std::string > &excludeSublists=Array< std::string >()) const
Create sublists in the valid parameter list using a template parameter from the valid parameter list ...