Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_ParameterList.cpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 //#define TEUCHOS_PARAMETER_LIST_SHOW_TRACE
43 
45 #include "Teuchos_FancyOStream.hpp"
46 #include "Teuchos_StrUtils.hpp"
47 #include "Teuchos_VerboseObject.hpp"
48 
49 
50 namespace {
51 
52 
53 std::string filterValueToString(const Teuchos::ParameterEntry& entry )
54 {
55  return ( entry.isList() ? std::string("...") : toString(entry.getAny()) );
56 }
57 
58 
59 struct ListPlusValidList {
61  Teuchos::ParameterList *validList;
62  ListPlusValidList(
64  ,Teuchos::ParameterList *_validList
65  )
66  :list(_list),validList(_validList)
67  {}
68 };
69 
70 
71 } // namespace
72 
73 
74 namespace Teuchos {
75 
76 
77 // Constructors/Destructor/Info
78 
79 
81  :name_("ANONYMOUS"), disableRecursiveValidation_(false)
82 {}
83 
84 
85 ParameterList::ParameterList(const std::string &name_in)
86  :name_(name_in), disableRecursiveValidation_(false)
87 {}
88 
89 
91 {
92  name_ = source.name_;
93  params_ = source.params_;
94  disableRecursiveValidation_ = source.disableRecursiveValidation_;
95 }
96 
97 
99 {}
100 
101 
103 {
104  return params_.numObjects();
105 }
106 
107 
109 {
110  if (&source == this)
111  return *this;
112  name_ = source.name_;
113  params_ = source.params_;
114  disableRecursiveValidation_ = source.disableRecursiveValidation_;
115  return *this;
116 }
117 
118 
120 {
121  for( ConstIterator i = source.begin(); i != source.end(); ++i ) {
122  const std::string &name_i = this->name(i);
123  const ParameterEntry &entry_i = this->entry(i);
124  if(entry_i.isList()) {
125  this->sublist(name_i,false,entry_i.docString()).setParameters(
126  getValue<ParameterList>(entry_i) );
127  }
128  else {
129  this->setEntry(name_i,entry_i);
130  }
131  }
132  this->updateSubListNames();
133  return *this;
134 }
135 
136 
138  const ParameterList& source
139  )
140 {
141  for( ConstIterator i = source.begin(); i != source.end(); ++i ) {
142  const std::string &name_i = this->name(i);
143  const ParameterEntry &entry_i = this->entry(i);
144  if(entry_i.isList()) {
145  this->sublist(name_i,false,entry_i.docString()).setParametersNotAlreadySet(
146  getValue<ParameterList>(entry_i) );
147  }
148  else {
149  const ParameterEntry
150  *thisEntryPtr = this->getEntryPtr(name_i);
151  // If the entry does not already exist, then set it. Otherwise, leave the
152  // existing intery allow
153  if(!thisEntryPtr)
154  this->setEntry(name_i,entry_i);
155  }
156  }
157  this->updateSubListNames();
158  return *this;
159 }
160 
161 
163 {
164  disableRecursiveValidation_ = true;
165  return *this;
166 }
167 
168 
169 void ParameterList::unused(std::ostream& os) const
170 {
171  for (ConstIterator i = this->begin(); i != this->end(); ++i) {
172  if (!(entry(i).isUsed())) {
173  os << "WARNING: Parameter \"" << name(i) << "\" " << entry(i)
174  << " is unused" << std::endl;
175  }
176  }
177 }
178 
179 
181 {
182  std::ostringstream oss;
183  oss << " {\n";
185  int i;
186  for( itr = this->begin(), i = 0; itr != this->end(); ++itr, ++i ) {
187  const std::string &entryName = this->name(itr);
188  const ParameterEntry &theEntry = this->entry(itr);
189  oss
190  << " \""<<entryName<<"\" : "<<theEntry.getAny().typeName()
191  <<" = "<<filterValueToString(theEntry) << "\n";
192  }
193  oss << " }\n";
194  return oss.str();
195 }
196 
197 
198 bool ParameterList::isSublist(const std::string& name_in) const
199 {
201  const Ordinal param_idx = params_.getObjOrdinalIndex(name_in);
202  if (param_idx != SIOVOCB::getInvalidOrdinal()) {
203  return params_.getObjPtr(param_idx)->isList();
204  }
205  return false;
206 }
207 
208 
209 bool ParameterList::isParameter(const std::string& name_in) const
210 {
212  const Ordinal param_idx = params_.getObjOrdinalIndex(name_in);
213  if (param_idx != SIOVOCB::getInvalidOrdinal()) {
214  return true;
215  }
216  return false;
217 }
218 
219 
221  std::string const& name_in, bool throwIfNotExists
222  )
223 {
225  const Ordinal param_idx = params_.getObjOrdinalIndex(name_in);
226  if (param_idx != SIOVOCB::getInvalidOrdinal()) {
227  // Parameter exists
228  params_.removeObj(param_idx);
229  return true;
230  }
231  // Parameter does not exist
232  if (throwIfNotExists) {
233  validateEntryExists("get", name_in, 0); // Will throw
234  }
235  return false; // Param does not exist but that is okay
236 }
237 
238 
240  const std::string& name_in, bool mustAlreadyExist,
241  const std::string& docString
242  )
243 {
245 
246  const Ordinal param_idx = params_.getObjOrdinalIndex(name_in);
247 
248  Ptr<ParameterEntry> sublist_entry_ptr;
249 
250  if (param_idx != SIOVOCB::getInvalidOrdinal()) {
251  // Sublist parameter exists
252  sublist_entry_ptr = params_.getNonconstObjPtr(param_idx);
253  validateEntryIsList(name_in, *sublist_entry_ptr);
254  }
255  else {
256  // Sublist does not exist so we need to create a new one
257  validateMissingSublistMustExist(this->name(), name_in, mustAlreadyExist);
258  const Ordinal new_param_idx =
259  params_.setObj(
260  name_in,
262  ParameterList(this->name()+std::string("->")+name_in),
263  false,
264  true,
265  docString
266  )
267  );
268  sublist_entry_ptr = params_.getNonconstObjPtr(new_param_idx);
269  }
270 
271  return any_cast<ParameterList>(sublist_entry_ptr->getAny(false));
272 }
273 
274 
275 const ParameterList& ParameterList::sublist(const std::string& name_in) const
276 {
278 
279  const Ordinal param_idx = params_.getObjOrdinalIndex(name_in);
280  if (param_idx == SIOVOCB::getInvalidOrdinal()) {
281  validateMissingSublistMustExist(this->name(), name_in, true);
282  }
283 
284  Ptr<const ParameterEntry> sublist_entry_ptr = params_.getObjPtr(param_idx);
285  validateEntryIsList(name_in, *sublist_entry_ptr);
286 
287  return any_cast<ParameterList>(sublist_entry_ptr->getAny(false));
288 }
289 
290 
292 {
294 }
295 
296 
297 std::ostream& ParameterList::print(std::ostream& os, const PrintOptions &printOptions ) const
298 {
299  const int indent = printOptions.indent();
300  const bool showTypes = printOptions.showTypes();
301  const bool showFlags = printOptions.showFlags();
302  const bool showDoc = printOptions.showDoc();
303  const std::string linePrefix(indent,' ');
305  out = getFancyOStream(rcp(&os,false));
306  OSTab tab(out,indent);
307  if (this->begin() == this->end()) {
308  *out <<"[empty list]" << std::endl;
309  }
310  else {
311  // Print parameters first
312  for (ConstIterator i = this->begin(); i != this->end(); ++i)
313  {
314  const std::string &name_i = this->name(i);
315  const ParameterEntry &entry_i = entry(i);
317  validator = entry_i.validator();
318  if(entry_i.isList())
319  continue;
320  *out << name_i;
321  const std::string &docString = entry_i.docString();
322  if(showTypes)
323  *out << " : " << entry_i.getAny(false).typeName();
324  *out << " = "; entry_i.leftshift(os,showFlags); *out << std::endl;
325  if (showDoc) {
326  if (nonnull(validator)) {
327  validator->printDoc(docString,OSTab(os).o());
328  }
329  else if (docString.length()) {
330  StrUtils::printLines(OSTab(out).o(),"# ",docString);
331  }
332  }
333  }
334  // Print sublists second
335  for (ConstIterator i = this->begin(); i != this->end(); ++i)
336  {
337  const ParameterEntry &entry_i = entry(i);
338  if(!entry_i.isList())
339  continue;
340  const std::string &docString = entry_i.docString();
341  const std::string &name_i = this->name(i);
342  *out << name_i << " -> " << std::endl;
343  if( docString.length() && showDoc ) {
344  StrUtils::printLines(OSTab(out).o(),"# ",docString);
345  }
346  getValue<ParameterList>(entry_i).print(OSTab(out).o(), printOptions.copy().indent(0));
347  }
348  }
349  return os;
350 }
351 
352 
353 std::ostream& ParameterList::print(std::ostream& os, int indent, bool showTypes, bool showFlags) const
354 {
355  return this->print(os,PrintOptions().indent(indent).showTypes(showTypes).showFlags(showFlags));
356 }
357 
358 
360  ParameterList const& validParamList,
361  int const depth,
362  EValidateUsed const validateUsed,
363  EValidateDefaults const validateDefaults
364  ) const
365 {
366  typedef std::deque<ListPlusValidList> sublist_list_t;
367 #ifdef TEUCHOS_PARAMETER_LIST_SHOW_TRACE
369  OSTab tab(out);
370  *out << "\n*** Entering ParameterList::validateParameters(...) for "
371  "this->name()=\""<<this->name()<<"\"...\n";
372 #endif
373  //
374  // First loop through and validate the parameters at this level.
375  //
376  // Here we generate a list of sublists that we will search next
377  //
378  sublist_list_t sublist_list;
379  ConstIterator itr;
380  for (itr = this->begin(); itr != this->end(); ++itr) {
381  const std::string &entryName = this->name(itr);
382  const ParameterEntry &theEntry = this->entry(itr);
383 #ifdef TEUCHOS_PARAMETER_LIST_SHOW_TRACE
384  OSTab tab(out);
385  *out << "\nentryName=\""<<entryName<<"\"\n";
386 #endif
387  if(
388  ( theEntry.isUsed() && validateUsed!=VALIDATE_USED_ENABLED )
389  ||
390  ( theEntry.isDefault() && validateDefaults!=VALIDATE_DEFAULTS_ENABLED )
391  )
392  {
393  continue;
394  }
395  const ParameterEntry *validEntry = validParamList.getEntryPtr(entryName);
398  ,"Error, the parameter {name=\""<<entryName<<"\","
399  "type=\""<<theEntry.getAny(false).typeName()<<"\""
400  ",value=\""<<filterValueToString(theEntry)<<"\"}"
401  "\nin the parameter (sub)list \""<<this->name()<<"\""
402  "\nwas not found in the list of valid parameters!"
403  "\n\nThe valid parameters and types are:\n"
404  <<validParamList.currentParametersString()
405  );
407  if (nonnull(validator=validEntry->validator())) {
408  validator->validate(theEntry, entryName, this->name());
409  }
410  else {
411  const bool validType =
412  ( validEntry!=NULL
413  ? theEntry.getAny(false).type() == validEntry->getAny(false).type()
414  : false
415  );
418  ,"Error, the parameter {name=\""<<entryName<<"\","
419  "type=\""<<theEntry.getAny(false).typeName()<<"\""
420  ",value=\""<<filterValueToString(theEntry)<<"\"}"
421  "\nin the parameter (sub)list \""<<this->name()<<"\""
422  "\nexists in the list of valid parameters but has the wrong type."
423  "\n\nThe correct type is \""
424  << validEntry->getAny(false).typeName() << "\"."
425  );
426  }
427  if( theEntry.isList() && depth > 0 ) {
428  sublist_list.push_back(
429  ListPlusValidList(
430  &getValue<ParameterList>(theEntry),&getValue<ParameterList>(*validEntry)
431  )
432  );
433  }
434  }
435  //
436  // Now loop through the sublists and validate their parameters
437  //
438  for(
439  sublist_list_t::const_iterator sl_itr = sublist_list.begin();
440  sl_itr != sublist_list.end();
441  ++sl_itr
442  )
443  {
444  if (!sl_itr->validList->disableRecursiveValidation_) {
445  sl_itr->list->validateParameters(
446  *sl_itr->validList
447  ,depth-1
448  ,validateUsed
449  ,validateDefaults
450  );
451  }
452  }
453 #ifdef TEUCHOS_PARAMETER_LIST_SHOW_TRACE
454  *out << "\n*** Existing ParameterList::validateParameters(...) for "
455  "this->name()=\""<<this->name()<<"\"...\n";
456 #endif
457 }
458 
459 
461  ParameterList const& validParamList,
462  int const depth
463  )
464 {
465  typedef std::deque<ListPlusValidList> sublist_list_t;
466 #ifdef TEUCHOS_PARAMETER_LIST_SHOW_TRACE
468  OSTab tab(out);
469  *out << "\n*** Entering ParameterList::validateParametersAndSetDefaults(...) "
470  "for this->name()=\""<<this->name()<<"\"...\n";
471 #endif
472  //
473  // A) loop through and validate the parameters at this level.
474  //
475  // Here we generate a list of sublists that we will search next
476  //
477  sublist_list_t sublist_list;
478  {
479  Iterator itr;
480  for (itr = this->nonconstBegin(); itr != this->nonconstEnd(); ++itr) {
481  const std::string &entryName = this->name(itr);
482  ParameterEntry &theEntry = this->nonconstEntry(itr);
483 #ifdef TEUCHOS_PARAMETER_LIST_SHOW_TRACE
484  OSTab tab(out);
485  *out << "\nentryName=\""<<entryName<<"\"\n";
486 #endif
487  const ParameterEntry *validEntry = validParamList.getEntryPtr(entryName);
490  ,"Error, the parameter {name=\""<<entryName<<"\","
491  "type=\""<<theEntry.getAny(false).typeName()<<"\""
492  ",value=\""<<filterValueToString(theEntry)<<"\"}"
493  "\nin the parameter (sub)list \""<<this->name()<<"\""
494  "\nwas not found in the list of valid parameters!"
495  "\n\nThe valid parameters and types are:\n"
496  <<validParamList.currentParametersString()
497  );
499  if (nonnull(validator=validEntry->validator())) {
500  validator->validateAndModify(entryName, this->name(), &theEntry);
501  theEntry.setValidator(validator);
502  }
503  else {
504  const bool validType =
505  ( validEntry!=NULL
506  ? theEntry.getAny(false).type() == validEntry->getAny(false).type()
507  : false
508  );
511  ,"Error, the parameter {name=\""<<entryName<<"\","
512  "type=\""<<theEntry.getAny(false).typeName()<<"\""
513  ",value=\""<<filterValueToString(theEntry)<<"\"}"
514  "\nin the parameter (sub)list \""<<this->name()<<"\""
515  "\nexists in the list of valid parameters but has the wrong type."
516  "\n\nThe correct type is \""
517  << validEntry->getAny(false).typeName() << "\"."
518  );
519  // Note: If there is no validator for this item, then we can not
520  // validate the value of the parameter, only its type!
521  }
522  if( theEntry.isList() && depth > 0 ) {
523  sublist_list.push_back(
524  ListPlusValidList(
525  &getValue<ParameterList>(theEntry),
526  &getValue<ParameterList>(*validEntry)
527  )
528  );
529  }
530  }
531  }
532  //
533  // B) Loop through the valid parameters at this level that are not set in
534  // *this, and set their defaults.
535  //
536  {
537  ConstIterator itr;
538  for (itr = validParamList.begin(); itr != validParamList.end(); ++itr) {
539  const std::string &validEntryName = validParamList.name(itr);
540  const ParameterEntry &validEntry = validParamList.entry(itr);
541  const ParameterEntry *theEntry = this->getEntryPtr(validEntryName);
542  if (!theEntry) {
543  // This entry does not exist, so add it. Here we will only set the
544  // value of the entry and its validator and and leave off the
545  // documentation. The reason that the validator is set is so that it
546  // can be used to extract and validate entries in the transformed list
547  // *this without having to refer back to the valid parameter list.
548  ParameterEntry newEntry;
549  newEntry.setAnyValue(
550  validEntry.getAny(),
551  true // isDefault
552  );
553  newEntry.setValidator(validEntry.validator());
554  this->setEntry(validEntryName,newEntry);
555  }
556  }
557  }
558  //
559  // C) Loop through the sublists and validate their parameters and set their
560  // defaults!
561  //
562  for (
563  sublist_list_t::iterator sl_itr = sublist_list.begin();
564  sl_itr != sublist_list.end();
565  ++sl_itr
566  )
567  {
568  if (!sl_itr->validList->disableRecursiveValidation_) {
569  sl_itr->list->validateParametersAndSetDefaults(*sl_itr->validList,depth-1);
570  }
571  }
572 #ifdef TEUCHOS_PARAMETER_LIST_SHOW_TRACE
573  *out << "\n*** Existing ParameterList::validateParametersAndSetDefaults(...) "
574  "for this->name()=\""<<this->name()<<"\"...\n";
575 #endif
576 }
577 
578 
579 // private
580 
581 
582 void ParameterList::updateSubListNames(int depth)
583 {
584  const std::string this_name = this->name();
585  Iterator itr;
586  for( itr = this->nonconstBegin(); itr != this->nonconstEnd(); ++itr ) {
587  const std::string &entryName = this->name(itr);
588  const ParameterEntry &theEntry = this->entry(itr);
589  if(theEntry.isList()) {
590  ParameterList &sublistEntry = getValue<ParameterList>(theEntry);
591  sublistEntry.setName(this_name+std::string("->")+entryName);
592  if(depth > 0)
593  sublistEntry.updateSubListNames(depth-1);
594  }
595  }
596 }
597 
598 
599 void ParameterList::validateEntryExists(
600  const std::string & /*funcName*/, const std::string &name_in,
601  const ParameterEntry *entry_in
602  ) const
603 {
605  entry_in==NULL, Exceptions::InvalidParameterName
606  ,"Error! The parameter \""<<name_in<<"\" does not exist"\
607  "\nin the parameter (sub)list \""<<this->name()<<"\"."
608  "\n\nThe current parameters set in (sub)list \""<<this->name()<<"\" are:\n\n"
609  << this->currentParametersString()
610  );
611 }
612 
613 
614 void ParameterList::validateEntryIsList(
615  const std::string &name_in, const ParameterEntry &entry_in
616  ) const
617 {
619  !entry_in.isList(), Exceptions::InvalidParameterType
620  ,"Error, the parameter \"" << name_in << "\" is not a list, it is of type \""
621  <<entry_in.getAny(false).typeName()<<"\"!" );
622 }
623 
624 
625 void ParameterList::validateMissingSublistMustExist(const std::string &baselist_name,
626  const std::string &sublist_name, const bool mustAlreadyExist) const
627 {
629  mustAlreadyExist, Exceptions::InvalidParameterName
630  ,"The sublist "<<baselist_name<<"->\""<<sublist_name<<"\" does not exist!"
631  );
632 }
633 
634 
635 
636 } // namespace Teuchos
637 
638 
639 bool Teuchos::operator==( const ParameterList& list1, const ParameterList& list2 )
640 {
641  // Check that the top-level names of the two parameter lists are the same
642  //const std::string &paramListName1 = list1.name();
643  //const std::string &paramListName2 = list2.name();
644  //if ( paramListName1 != paramListName2 ) {
645  // return false;
646  //}
647  ParameterList::ConstIterator itr1, itr2;
648  for(
649  itr1 = list1.begin(), itr2 = list2.begin();
650  itr1 != list1.end() && itr2 != list2.end();
651  ++itr1, ++itr2
652  )
653  {
654  const std::string &entryName1 = list1.name(itr1);
655  const std::string &entryName2 = list2.name(itr2);
656  const ParameterEntry &entry1 = list1.entry(itr1);
657  const ParameterEntry &entry2 = list2.entry(itr2);
658  if( entryName1 != entryName2 ) {
659  return false;
660  }
661  else if( entry1 != entry2 ) {
662  return false;
663  }
664  // Note that the above statement automatically recursively compare the
665  // sublists since ParameterList objects are stored in the 'any' variable
666  // held by the ParameterEntry object and this same comparison operator will
667  // be used.
668  }
669  // Check that the two parameter lists are the same length:
670  if ((itr1 != list1.end()) || (itr2 != list2.end())) {
671  return false;
672  }
673  return true;
674 }
675 
676 
677 bool Teuchos::haveSameValues( const ParameterList& list1, const ParameterList& list2, bool verbose )
678 {
679  // Check that the top-level names of the two parameter lists are the same
680  //const std::string &paramListName1 = list1.name();
681  //const std::string &paramListName2 = list2.name();
682  //if ( paramListName1 != paramListName2 ) {
683  // return false;
684  //}
685  ParameterList::ConstIterator itr1, itr2;
686  for(
687  itr1 = list1.begin(), itr2 = list2.begin();
688  itr1 != list1.end() && itr2 != list2.end();
689  ++itr1, ++itr2
690  )
691  {
692  const std::string &entryName1 = list1.name(itr1);
693  const std::string &entryName2 = list2.name(itr2);
694  const ParameterEntry &entry1 = list1.entry(itr1);
695  const ParameterEntry &entry2 = list2.entry(itr2);
696  if( entryName1 != entryName2 ) {
697  if (verbose) std::cerr << "entryName1 \"" << entryName1 << "\" != entryName2 \"" << entryName2 << "\"\n";
698  return false;
699  }
700  if( entry1.isList() && entry2.isList() ) {
701  if (
703  getValue<ParameterList>(entry1),
704  getValue<ParameterList>(entry2),
705  verbose)
706  )
707  {
708  // Note: Above we cast to a non-const ParameterList even through we
709  // only need a const ParameterList. We have to do this since a
710  // non-const ParameterList is always added initially which determines
711  // the value.
712  if (verbose) std::cerr << "sublists \"" << entryName1 << "\" differ\n";
713  return false;
714  }
715  }
716  else {
717  if( entry1.getAny() != entry2.getAny() ) {
718  if (verbose) std::cerr << "for key \"" << entryName1 << "\", value \"" << entry1.getAny() << "\" != \"" << entry2.getAny() << "\"\n";
719  return false;
720  }
721  }
722  }
723  // Check that the two parameter lists are the same length:
724  if ((itr1 != list1.end()) || (itr2 != list2.end())) {
725  if (verbose) std::cerr << "lists are not the same size\n";
726  return false;
727  }
728  return true;
729 }
730 
731 
732 bool Teuchos::haveSameValuesSorted( const ParameterList& list1, const ParameterList& list2, bool verbose )
733 {
734  // Check that the top-level names of the two parameter lists are the same
735  //const std::string &paramListName1 = list1.name();
736  //const std::string &paramListName2 = list2.name();
737  //if ( paramListName1 != paramListName2 ) {
738  // return false;
739  //}
740  ParameterList::ConstIterator itr1, itr2;
741  Array<std::string> arr1, arr2;
742  for(itr1 = list1.begin(); itr1 != list1.end(); ++itr1){
743  arr1.push_back(list1.name(itr1));
744  }
745  for(itr2 = list2.begin(); itr2 != list2.end(); ++itr2){
746  arr2.push_back(list2.name(itr2));
747  }
748  // Check that the two parameter lists are the same length:
749  if (arr1.size() != arr2.size()) {
750  if (verbose) std::cerr << "lists are not the same size\n";
751  return false;
752  }
753  std::sort(arr1.begin(), arr1.end());
754  std::sort(arr2.begin(), arr2.end());
755  Array<std::string>::iterator iarr1, iarr2;
756  for(
757  iarr1 = arr1.begin(), iarr2 = arr2.begin();
758  iarr1 != arr1.end() && iarr2 != arr2.end();
759  ++iarr1, ++iarr2
760  )
761  {
762  const std::string &entryName1 = *iarr1;
763  const std::string &entryName2 = *iarr2;
764  const ParameterEntry &entry1 = list1.getEntry(entryName1);
765  const ParameterEntry &entry2 = list2.getEntry(entryName2);
766  if( entryName1 != entryName2 ) {
767  if (verbose) std::cerr << "entryName1 \"" << entryName1 << "\" != entryName2 \"" << entryName2 << "\"\n";
768  return false;
769  }
770  if( entry1.isList() && entry2.isList() ) {
771  if (
773  getValue<ParameterList>(entry1),
774  getValue<ParameterList>(entry2),
775  verbose)
776  )
777  {
778  // Note: Above we cast to a non-const ParameterList even through we
779  // only need a const ParameterList. We have to do this since a
780  // non-const ParameterList is always added initially which determines
781  // the value.
782  if (verbose) std::cerr << "sublists \"" << entryName1 << "\" differ\n";
783  return false;
784  }
785  }
786  else {
787  if( entry1.getAny() != entry2.getAny() ) {
788  if (verbose) std::cerr << "for key \"" << entryName1 << "\", value \"" << entry1.getAny() << "\" != \"" << entry2.getAny() << "\"\n";
789  return false;
790  }
791  }
792  }
793  return true;
794 }
void print() const
Print function to use in debugging in a debugger.
const std::string & name() const
The name of this ParameterList.
C++ Standard Library compatable filtered iterator.
Ordinal getObjOrdinalIndex(const std::string &key) const
Get the ordinal index given the string key.
ConstIterator end() const
An iterator pointing beyond the last entry.
basic_OSTab< char > OSTab
ParameterList & disableRecursiveValidation()
void setValidator(RCP< const ParameterEntryValidator > const &validator)
Set the validator.
RCP< const ParameterEntryValidator > validator() const
Return the (optional) validator object.
std::ostream & leftshift(std::ostream &os, bool printFlags=true) const
Output a non-list parameter to the given output stream.
std::string currentParametersString() const
Create a single formated std::string of all of the zero-level parameters in this list.
TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT bool haveSameValues(const ParameterList &list1, const ParameterList &list2, bool verbose=false)
Returns true if two parameter lists have the same values.
bool nonnull(const std::shared_ptr< T > &p)
Returns true if p.get()!=NULL.
This object is held as the &quot;value&quot; in the Teuchos::ParameterList std::map.
void setAnyValue(const any &value, bool isDefault=false)
Set the value as an any object.
Ordinal numParams() const
Get the number of stored parameters.
static std::ostream & printLines(std::ostream &os, const std::string &linePrefix, const std::string &lines)
Print lines with prefix first.
bool isDefault() const
Indicate whether this entry takes on the default value.
Tabbing class for helping to create formated, indented output for a basic_FancyOStream object...
ParameterEntry * getEntryPtr(const std::string &name)
Retrieves the pointer for an entry with the name name if it exists.
std::string toString(const any &rhs)
Converts the value in any to a std::string.
A std::string utilities class for Teuchos.
ParameterList & setEntry(const std::string &name, const ParameterEntry &entry)
Set a parameter directly as a ParameterEntry.
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).
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
Templated Parameter List class.
bool isSublist(const std::string &name) const
Whether the given sublist exists in this list.
ParameterList & operator=(const ParameterList &source)
Replace the current parameter list with source.
static RCP< FancyOStream > getDefaultOStream()
Get the default output stream object.
void validateParametersAndSetDefaults(ParameterList const &validParamList, int const depth=1000)
Validate the parameters in this list given valid selections in the input list and set defaults for th...
Utility class for setting and passing in print options.
ConstIterator begin() const
An iterator pointing to the first entry.
bool isList() const
Return whether or not the value itself is a list.
Ptr< const ObjType > getObjPtr(const Ordinal &idx) const
Get a const semi-persisting association with the stored object indexed by ordinal.
A list of parameters of arbitrary type.
void validateParameters(ParameterList const &validParamList, int const depth=1000, EValidateUsed const validateUsed=VALIDATE_USED_ENABLED, EValidateDefaults const validateDefaults=VALIDATE_DEFAULTS_ENABLED) const
Validate the parameters in this list given valid selections in the input list.
ParameterList & setParameters(const ParameterList &source)
const ParameterEntry & entry(ConstIterator i) const
Access to ParameterEntry (i.e., returns i-&gt;second)
any & getAny(bool activeQry=true)
Direct access to the Teuchos::any data value underlying this object. The bool argument activeQry (def...
Ordinal setObj(const std::string &key, const ObjType &obj)
Set (or reset) object by value and return its ordinal index.
ParameterList & setParametersNotAlreadySet(const ParameterList &source)
EValidateDefaults
Validation defaults enum.
void unused(std::ostream &os) const
Print out unused parameters in the ParameterList.
std::string typeName() const
Return the name of the type.
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...
ParameterList & setName(const std::string &name)
Set the name of *this list.
Smart reference counting pointer class for automatic garbage collection.
void removeObj(const Ordinal &idx)
Remove an object given its ordinal index.
const std::type_info & type() const
Return the type of value being stored.
virtual ~ParameterList()
Destructor.
TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT bool haveSameValuesSorted(const ParameterList &list1, const ParameterList &list2, bool verbose=false)
Returns true if two parameter lists have the same values independent of ordering. ...
std::string docString() const
Return the (optional) documentation std::string.
bool isUsed() const
Return whether or not the value has been used; i.e., whether or not the value has been retrieved via ...
std::vector< std::string >::iterator iterator
The type of a forward iterator.
Ptr< ObjType > getNonconstObjPtr(const Ordinal &idx)
Get a nonconst semi-persisting association with the stored object indexed by ordinal.
EValidateUsed
Validation used enum.
Simple wrapper class for raw pointers to single objects where no persisting relationship exists...
#define TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.