Zoltan2  Version of the Day
Zoltan2_IntegerRangeList.hpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // Zoltan2: A package of combinatorial algorithms for scientific computing
6 // Copyright 2012 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Karen Devine (kddevin@sandia.gov)
39 // Erik Boman (egboman@sandia.gov)
40 // Siva Rajamanickam (srajama@sandia.gov)
41 //
42 // ***********************************************************************
43 //
44 // @HEADER
45 
50 #ifndef _ZOLTAN2_INTEGERRANGELIST_HPP_
51 #define _ZOLTAN2_INTEGERRANGELIST_HPP_
52 
53 #include <Zoltan2_config.h>
54 
55 #include <Teuchos_ParameterEntryValidator.hpp>
56 #include <Teuchos_ValidatorXMLConverter.hpp>
57 #include <Teuchos_XMLObject.hpp>
58 #include <Teuchos_ValidatorMaps.hpp>
59 #include <Teuchos_DummyObjectGetter.hpp>
60 #include <Teuchos_StrUtils.hpp>
61 
62 // Had to redefine this type from Teuchos_ParameterEntryValidator.hpp.
63 // Compiler stumbled on it.
64 typedef Teuchos::RCP<const Teuchos::Array<std::string> > ValidStringsList;
65 
66 namespace Zoltan2{
67 
71 enum RangeType {
76 };
77 
78 template <typename T> class IntegerRangeListValidator;
79 
81 // Helper functions for interpreting integer range list arrays.
83 
90 template <typename Integral>
91  bool validIntegralRangeList(const Teuchos::Array<Integral> &vals)
92 {
93  typedef typename Teuchos::Array<Integral>::size_type arraySize_t;
94  arraySize_t len = vals.size();
95  if (len==0)
96  return false;
97 
98  Integral flag = vals[len-1];
99  if ((flag != RANGE_INCLUDES_ALL) && (flag != RANGE_IS_EMPTY) &&
100  (flag != RANGE_IS_LISTED))
101  return false;
102 
103  return true;
104 }
105 
120 template <typename Integral>
121  bool allValuesAreInRangeList(const Teuchos::Array<Integral> &vals)
122 {
123  if (!validIntegralRangeList(vals))
124  throw std::runtime_error("list is not a valid range list");
125  return vals[vals.size()-1] == RANGE_INCLUDES_ALL;
126 }
127 
142 template <typename Integral>
143  bool allValuesAreInRangeList(const Teuchos::ParameterEntry &e)
144 {
145  if (!e.isType<Teuchos::Array<Integral> >())
146  throw std::runtime_error("Should not call until modified");
147 
148  Teuchos::Array<Integral> *valPtr=NULL;
149  Teuchos::Array<Integral> &vals = e.getValue(valPtr);
150  return allValuesAreInRangeList(vals);
151 }
152 
167 template <typename Integral>
168  bool noValuesAreInRangeList(const Teuchos::Array<Integral> &vals)
169 {
170  if (!validIntegralRangeList(vals))
171  throw std::runtime_error("list is not a valid range list");
172  return vals[vals.size()-1] == RANGE_IS_EMPTY;
173 }
174 
189 template <typename Integral>
190  bool noValuesAreInRangeList(const Teuchos::ParameterEntry &e)
191 {
192  if (!e.isType<Teuchos::Array<Integral> >())
193  throw std::runtime_error("Should not call until modified");
194 
195  Teuchos::Array<Integral> *valPtr=NULL;
196  Teuchos::Array<Integral> &vals = e.getValue(valPtr);
197  return noValuesAreInRangeList(vals);
198 }
199 
200 // TODO :
201 // inList(std::vector<Integral> &val, std::vector<bool> &result)
202 
214 template <typename Integral>
215  bool IsInRangeList(const Integral val, const Teuchos::Array<Integral> &valList, bool sorted=true)
216 {
217  if (allValuesAreInRangeList(valList))
218  return true;
219  else if (noValuesAreInRangeList(valList))
220  return false;
221 
222  if (sorted){
223  typename Teuchos::Array<Integral>::const_iterator flag = valList.end();
224  --flag;
225  if (std::binary_search(valList.begin(), flag, val))
226  return true;
227  else
228  return false;
229  }
230  else{
231  for (typename Teuchos::Array<Integral>::size_type i=0; i < valList.size()-1; i++){
232  if (valList[i] == val)
233  return true;
234  }
235  return false;
236  }
237 }
238 
249 template <typename Integral>
250  bool IsInRangeList(const Integral val, const Teuchos::ParameterEntry &e)
251 {
252  if (!e.isType<Teuchos::Array<Integral> >())
253  throw std::runtime_error("Should not call until modified");
254 
255  Teuchos::Array<Integral> *valPtr=NULL;
256  Teuchos::Array<Integral> &valList = e.getValue(valPtr);
257 
259  RCP<const irl_t> irl;
260  bool fail = false;
261 
262  RCP<const Teuchos::ParameterEntryValidator> valtor = e.validator();
263  if (!valtor.is_null()){
264  try{
265  irl = rcp_dynamic_cast<const irl_t>(valtor);
266  }
267  catch (...){
268  fail = true;
269  }
270  }
271  else{
272  fail = true;
273  }
274 
275  if (fail)
276  throw std::runtime_error("wrong type of parameter entry");
277 
278  bool sorted = irl->inputListWillBeSorted();
279 
280  return IsInRangeList(val, valList, sorted);
281 }
282 
291 template <typename Integral>
292  Teuchos::ArrayView<Integral> getList(Teuchos::Array<Integral> &irl)
293 {
294  Teuchos::ArrayView<Integral> av; // av.size() == 0
295 
298  av = irl.view(0, irl.size()-1); // skip last value, it's a flag
299 
300  return av;
301 }
302 
311 template <typename Integral>
312  void printIntegralRangeList(std::ostream &os, Teuchos::Array<Integral> &irl)
313 {
315  os << "all";
316  else if (Zoltan2::noValuesAreInRangeList(irl))
317  os << "empty";
318  else{
319  Teuchos::ArrayView<const Integral> view =
320  irl.view(0, irl.size()-1); // skip last value, it's a flag
321  os << view;
322  }
323 }
324 
326 // Validator class
328 
388 template <typename Integral>
389  class IntegerRangeListValidator : public Teuchos::ParameterEntryValidator
390 {
391 private:
392 
393 #ifndef DOXYGEN_SHOULD_SKIP_THIS
394 
395  Integral min_;
396  Integral max_;
397  bool unsorted_;
398 
399  static const std::string listDelim_;
400  static const std::string rangeDelim_;
401  static const std::string allText_;
402 
403  static void checkValid(char c);
404  static bool listSaysAll(std::string &l);
405  static int breakRange(std::string &range, std::string &from, std::string &to);
406 
407 #endif
408 
409 public:
415  IntegerRangeListValidator(bool unsorted=false);
416 
426  IntegerRangeListValidator(Integral validMin, Integral validMax,
427  bool unsorted=false);
428 
429  // Implementation of ParameterEntryValidator interface
430 
431  const std::string getXMLTypeName() const;
432 
433  void printDoc(std::string const& docString, std::ostream &out) const;
434 
436 
437  void validate( Teuchos::ParameterEntry const& entry,
438  std::string const& paramName, std::string const& sublistName
439  ) const;
440 
441  void validateAndModify( std::string const& paramName,
442  std::string const& sublistName, Teuchos::ParameterEntry * entry
443  ) const;
444 
445  // IntegerRangeListValidator methods
446 
452  Integral getAllowedMinimum() const { return min_;}
453 
459  Integral getAllowedMaximum() const { return max_;}
460 
467  bool inputListWillBeSorted() const { return !unsorted_;}
468 
469 }; // end class
470 
472 // Class definitions
474 
475 template <typename Integral>
477 
478 template <typename Integral>
480 
481 template <typename Integral>
482 const std::string IntegerRangeListValidator<Integral>::allText_("all");
483 
484 template <typename Integral>
486 {
487  if (std::isspace(c) || std::isdigit(c) || (c == ',') || (c == '-'))
488  return;
489  else
490  throw std::runtime_error("invalid integer range list");
491 }
492 
493 template <typename Integral>
495 {
496  std::transform(l.begin(), l.end(), l.begin(), (int(*)(int)) tolower);
497  if (l.find(allText_) != std::string::npos)
498  return true; // "all" is in the string
499  else
500  return false;
501 }
502 
503 template <typename Integral>
505  std::string &range, std::string &from, std::string &to)
506 {
507  from.clear();
508  to.clear();
509  std::string::size_type loc = range.find(rangeDelim_);
510  if (loc == std::string::npos){
511  from = range;
512  }
513  else{
514  from = range.substr(0, loc);
515  to = range.substr(loc+1, range.size());
516  }
517  long a, b;
518  std::istringstream iss1(from);
519  iss1 >> a;
520  b = a;
521  if (to.size() > 0){
522  std::istringstream iss2(to);
523  iss2 >> b;
524  if (b < a)
525  std::swap(from,to);
526  }
527  return (b != a) ? 2 : 1;
528 }
529 
530 
531 template <typename Integral>
533  bool unsorted): min_(1), max_(0), unsorted_(unsorted)
534 {
535 }
536 
537 template <typename Integral>
539  Integral validMin, Integral validMax, bool unsorted) :
540  min_(validMin), max_(validMax), unsorted_(unsorted)
541 {
542  if (min_ < max_) std::swap(min_,max_);
543 }
544 
545  // Implementation of ParameterEntryValidator interface
546 
547 template <typename Integral>
548  const std::string
550 {
551  std::string className("IntegerRangeListValidator");
552  std::string classType("("+Teuchos::TypeNameTraits<Integral>::name()+")");
553  return std::string(className + classType);
554 }
555 
556 template <typename Integral>
558  std::string const& docString, std::ostream &out) const
559 {
560  Teuchos::StrUtils::printLines(out,"# ",docString);
561  out << "#\tAn integer range list is a string which can contain:\n";
562  out << "#\t\tthe text \"all\", which indicates all values\n";
563  out << "#\t\ta list of integer ranges separated by commas.\n";
564  out << "#\tA range is one value, or two values separated by a dash.\n";
565  out << "#\tExample: \"all\" or \"1-10\" or \"3, 10-12\" or \"25\"\n";
566  if (max_ >= min_){
567  out << "#\tThe range of valid integers is [";
568  out << min_ << "," << max_ << "]\n";
569  }
570 }
571 
572 template <typename Integral>
574 {
575  return Teuchos::null;
576 }
577 
578 template <typename Integral>
580  Teuchos::ParameterEntry const& entry,
581  std::string const& paramName, std::string const& sublistName) const
582 {
583  if (!entry.isType<std::string>()){
584  return; // already converted to an an array
585  }
586  std::string *sptr=NULL;
587  std::string &rangeList = entry.getValue(sptr);
588  std::string inValue(rangeList);
589 
590  if (listSaysAll(inValue))
591  return; // "all" is in the string
592 
593  // throw error if invalid integer range list
594  std::for_each(inValue.begin(), inValue.end(), checkValid);
595 
596  if (max_ >= min_){
597  std::string::const_iterator rangeBegin = inValue.begin();
598  std::string::const_iterator valueEnd = inValue.end();
599 
600  while (rangeBegin != valueEnd){
601  std::string::const_iterator rangeEnd = std::search(
602  rangeBegin, valueEnd, listDelim_.begin(), listDelim_.end());
603  std::string range(rangeBegin, rangeEnd);
604  std::string aHalf, bHalf;
605  int count = breakRange(range, aHalf, bHalf);
606 
607  Integral a, b;
608  std::istringstream iss1(aHalf);
609  iss1 >> a;
610  if (count > 1){
611  std::istringstream iss2(bHalf);
612  iss2 >> b;
613  }
614  else
615  b = a;
616 
617  if ((a < min_) || (b > max_)){
618  std::ostringstream oss;
619  oss << "input range [" << a << "," << b << "] ";
620  oss << "exceeds valid range [" << min_ << "," << max_ << "] ";
621  throw std::runtime_error(oss.str());
622  }
623  if (rangeEnd == valueEnd)
624  rangeBegin = rangeEnd;
625  else
626  rangeBegin = ++rangeEnd;
627  }
628  }
629 }
630 
631 template <typename Integral>
633  std::string const& paramName, std::string const& sublistName,
634  Teuchos::ParameterEntry * entry) const
635 {
636  typedef typename Teuchos::Array<Integral>::size_type arraySize_t;
637  if (!entry->isType<std::string>()){
638  return;
639  }
640 
641  std::string *sptr=NULL;
642  std::string &rangeList = entry->getValue(sptr);
643  Teuchos::Array<Integral> valueList;
644 
645  std::string inValue(rangeList);
646 
647  if (listSaysAll(inValue)){
648  valueList.push_back(RANGE_INCLUDES_ALL);
649  }
650  else{
651  // throw error if invalid integer range list
652  std::for_each(inValue.begin(), inValue.end(), checkValid);
653 
654  std::string::const_iterator rangeBegin = inValue.begin();
655  std::string::const_iterator valueEnd = inValue.end();
656 
657  while (rangeBegin != valueEnd){
658  std::string::const_iterator rangeEnd = std::search(rangeBegin,
659  valueEnd, listDelim_.begin(), listDelim_.end());
660  std::string range(rangeBegin, rangeEnd);
661  std::string aHalf, bHalf;
662  int count = breakRange(range, aHalf, bHalf);
663 
664  Integral a, b;
665  std::istringstream iss1(aHalf);
666  iss1 >> a;
667  if (count > 1){
668  std::istringstream iss2(bHalf);
669  iss2 >> b;
670  }
671  else
672  b = a;
673 
674  if ((max_ >= min_) && ((a < min_) || (b > max_))){
675  std::ostringstream oss;
676  oss << "input range [" << a << "," << b << "] ";
677  oss << "exceeds valid range [" << min_ << "," << max_ << "] ";
678  throw std::runtime_error(oss.str());
679  }
680  for (Integral i=a; i <=b; i++)
681  valueList.push_back(i);
682  if (rangeEnd == valueEnd)
683  rangeBegin = rangeEnd;
684  else
685  rangeBegin = ++rangeEnd;
686  }
687  if (!unsorted_ && valueList.size() > 1){ // sort & remove duplicates
688  std::sort(valueList.begin(), valueList.end());
689  arraySize_t listEnd = 0;
690  arraySize_t length = valueList.size();
691  for (arraySize_t i=1; i < length; i++){
692  if (valueList[i] > valueList[listEnd]){
693  listEnd++;
694  if (listEnd != i)
695  valueList[listEnd] = valueList[i];
696  }
697  }
698  if (++listEnd < length)
699  valueList.resize(listEnd);
700  }
701 
702  Integral flag = RANGE_IS_LISTED;
703  if (valueList.size() == 0){
704  flag = RANGE_IS_EMPTY;
705  }
706  else if (max_ >= min_){
707  Integral allSize = max_ - min_ + 1;
708  if (valueList.size() == allSize){
709  flag = RANGE_INCLUDES_ALL;
710  valueList.clear();
711  }
712  }
713  valueList.push_back(flag);
714  }
715  entry->setValue(valueList);
716 }
717 
719 // Parameter entry validator <-> XML conversion
721 
734 template <typename Integral>
735 class IntegerRangeListValidatorXMLConverter : public Teuchos::ValidatorXMLConverter
736 {
737 
738 public:
739 
740  RCP<Teuchos::ParameterEntryValidator> convertXML(
741  const Teuchos::XMLObject& xmlObj,
742  const Teuchos::IDtoValidatorMap& validatorIDsMap) const;
743 
744  void convertValidator(
745  const RCP<const Teuchos::ParameterEntryValidator> validator,
746  Teuchos::XMLObject& xmlObj,
747  const Teuchos::ValidatortoIDMap& validatorIDsMap) const;
748 
749 #ifdef HAVE_TEUCHOS_DEBUG
750 
751  RCP<const Teuchos::ParameterEntryValidator> getDummyValidator() const{
752  return Teuchos::DummyObjectGetter<IntegerRangeListValidator<Integral> >::getDummyObject();
753  }
754 #endif
755 };
756 
757 template <typename Integral>
758  RCP<Teuchos::ParameterEntryValidator>
760  const Teuchos::XMLObject& xmlObj,
761  const Teuchos::IDtoValidatorMap& /*validatorIDsMap*/) const
762 {
763  Integral minValue=0, maxValue=0;
764  bool unsorted=false, hasMin=false, hasMax=false;
765 
766  if (xmlObj.hasAttribute(string("min"))) {
767  minValue = xmlObj.getRequired<Integral>(string("min"));
768  hasMin = true;
769  }
770 
771  if (xmlObj.hasAttribute(string("max"))) {
772  maxValue = xmlObj.getRequired<Integral>(string("max"));
773  hasMax = true;
774  }
775 
776  if (xmlObj.hasAttribute(string("unsorted")))
777  unsorted = xmlObj.getRequired<bool>(string("unsorted"));
778 
779  RCP<Teuchos::ParameterEntryValidator> toReturn;
780 
781  if (hasMin && hasMax)
782  toReturn = rcp(new IntegerRangeListValidator<Integral>(minValue, maxValue, unsorted));
783  else if (!hasMin && !hasMax)
784  toReturn = rcp(new IntegerRangeListValidator<Integral>(unsorted));
785  else
786  throw std::runtime_error("invalid XML representation");
787 
788  return toReturn;
789 }
790 
791 template<typename Integral>
793  const RCP<const Teuchos::ParameterEntryValidator > validator,
794  Teuchos::XMLObject& xmlObj,
795  const Teuchos::ValidatortoIDMap& /*validatorIDsMap*/) const
796 {
797  RCP<const IntegerRangeListValidator<Integral> > castedValidator =
798  rcp_dynamic_cast<const IntegerRangeListValidator<Integral> >(
799  validator, true);
800 
801  Integral minValue = castedValidator->getAllowedMinimum();
802  Integral maxValue = castedValidator->getAllowedMaximum();
803  bool unsorted = castedValidator->inputListWillBeSorted();
804 
805  if (minValue < maxValue){
806  xmlObj.addAttribute<Integral>(string("min"), minValue);
807  xmlObj.addAttribute<Integral>(string("max"), maxValue);
808  }
809 
810  xmlObj.addAttribute<bool>(string("unsorted"), unsorted);
811 }
812 
813 }
814  // end of namespace Zoltan2
815 
816 #endif
void validateAndModify(std::string const &paramName, std::string const &sublistName, Teuchos::ParameterEntry *entry) const
the listed values are in the Array<int>
paramName
Definition: xml2dox.py:207
XML conversion code for IntegerRangeListValidator.
RangeType
Codes that indicate how to interpret the Array<int> representing the user&#39;s integer range list...
void convertValidator(const RCP< const Teuchos::ParameterEntryValidator > validator, Teuchos::XMLObject &xmlObj, const Teuchos::ValidatortoIDMap &validatorIDsMap) const
A ParameterList validator for integer range lists.
Integral getAllowedMinimum() const
Return the minimum value permitted in the list.
dictionary vals
Definition: xml2dox.py:186
bool inputListWillBeSorted() const
Return whether the list is sorted or not.
Integral getAllowedMaximum() const
Return the maximum value permitted in the list.
bool validIntegralRangeList(const Teuchos::Array< Integral > &vals)
A helper function that indicates whether an array is a valid integer range list.
bool IsInRangeList(const Integral val, const Teuchos::Array< Integral > &valList, bool sorted=true)
A helper function that determines if a value is in the list.
Teuchos::RCP< const Teuchos::Array< std::string > > ValidStringsList
void validate(Teuchos::ParameterEntry const &entry, std::string const &paramName, std::string const &sublistName) const
bool noValuesAreInRangeList(const Teuchos::Array< Integral > &vals)
A helper function that determines if no values are in the list.
IntegerRangeListValidator(bool unsorted=false)
Constructor: any Integral is valid.
void printIntegralRangeList(std::ostream &os, Teuchos::Array< Integral > &irl)
A helper function that prints the meaning of an encoded integer range list.
Teuchos::ArrayView< Integral > getList(Teuchos::Array< Integral > &irl)
A helper function that returns a view of the list.
void printDoc(std::string const &docString, std::ostream &out) const
RCP< Teuchos::ParameterEntryValidator > convertXML(const Teuchos::XMLObject &xmlObj, const Teuchos::IDtoValidatorMap &validatorIDsMap) const
bool allValuesAreInRangeList(const Teuchos::Array< Integral > &vals)
A helper function that determines if all values are in the list.