Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:22:04

0001 // @(#)root/mathcore:$Id$
0002 // Author: L. Moneta Nov 2010
0003 
0004 /**********************************************************************
0005  *                                                                    *
0006  * Copyright (c) 2010  LCG ROOT Math Team, CERN/PH-SFT                *
0007  *                                                                    *
0008  *                                                                    *
0009  **********************************************************************/
0010 
0011 #ifndef ROOT_Math_GenAlgoOptions
0012 #define ROOT_Math_GenAlgoOptions
0013 
0014 
0015 #include "Math/IOptions.h"
0016 
0017 #include <map>
0018 #include <iomanip>
0019 #include <string>
0020 #include <vector>
0021 
0022 namespace ROOT {
0023       namespace Math {
0024 
0025 //_______________________________________________________________________________
0026 /**
0027     class implementing generic options for a numerical algorithm
0028     Just store the options in a map of string-value pairs
0029 
0030     @ingroup NumAlgo
0031 */
0032 class GenAlgoOptions : public IOptions {
0033 
0034 public:
0035 
0036    GenAlgoOptions() /* : fExtraOptions(0) */  {}
0037 
0038    ~GenAlgoOptions() override {}// { if (fExtraOptions) delete fExtraOptions; }
0039 
0040    // use default copy constructor and assignment operator
0041 
0042    /** generic  methods for  retrieving options */
0043 
0044 
0045    // methods implementing the  IOptions interface
0046 
0047    IOptions * Clone() const override {
0048       return new GenAlgoOptions(*this);
0049    }
0050 
0051    // t.b.d need probably to implement in a .cxx file for CINT
0052 
0053 
0054    bool GetRealValue(const char * name, double & val) const override {
0055       const double * pval = FindValue(name, fRealOpts);
0056       if (!pval) return false;
0057       val = *pval;
0058       return true;
0059    }
0060 
0061    bool GetIntValue(const char * name, int & val) const override {
0062       const int * pval = FindValue(name, fIntOpts);
0063       if (!pval) return false;
0064       val = *pval;
0065       return true;
0066    }
0067 
0068    bool GetNamedValue(const char * name, std::string & val) const override {
0069       const std::string * pval = FindValue(name, fNamOpts);
0070       if (!pval) return false;
0071       val = *pval;
0072       return true;
0073    }
0074 
0075    /// Method that needs to be re-implemented by the derived classes.
0076    void SetRealValue(const char * name, double val) override  {
0077       InsertValue(name, fRealOpts, val);
0078    }
0079 
0080    void SetIntValue(const char * name , int val) override {
0081       InsertValue(name, fIntOpts, val);
0082    }
0083 
0084    void SetNamedValue(const char * name, const char * val) override {
0085       InsertValue(name, fNamOpts, std::string(val));
0086    }
0087 
0088    std::vector<std::string>  GetAllNamedKeys() {
0089       std::vector<std::string> names;
0090       names.reserve(fNamOpts.size());
0091       // start by named options
0092       for (auto const & e : fNamOpts)
0093          names.push_back(e.first);
0094       return names;
0095    }
0096    std::vector<std::string>  GetAllRealKeys() {
0097       std::vector<std::string> names;
0098       names.reserve(fRealOpts.size());
0099       // start by named options
0100       for (auto const & e : fRealOpts)
0101          names.push_back(e.first);
0102       return names;
0103    }
0104    std::vector<std::string>  GetAllIntKeys() {
0105       std::vector<std::string> names;
0106       names.reserve(fIntOpts.size());
0107       // start by named options
0108       for (auto const & e : fIntOpts)
0109          names.push_back(e.first);
0110       return names;
0111    }
0112 
0113    /// print options
0114    void Print(std::ostream & os = std::cout ) const override {
0115       Print(fNamOpts,os);
0116       Print(fIntOpts,os);
0117       Print(fRealOpts,os);
0118    }
0119 
0120 
0121    // static methods to retrieve the default options
0122 
0123    // find the option given a name
0124    // return 0 if the option is not found
0125    static IOptions * FindDefault(const char * algoname);
0126 
0127    // retrieve options given the name
0128    // if option is not found create a new GenAlgoOption for the given name
0129    static IOptions & Default(const char * algoname);
0130 
0131    /// print all the default options
0132    static void PrintAllDefault(std::ostream & os = std::cout);
0133 
0134 
0135 protected:
0136 
0137 
0138 
0139 private:
0140 
0141    template<class M>
0142    static const typename M::mapped_type * FindValue(const std::string &  name, const M & opts) {
0143       typename M::const_iterator pos;
0144       pos = opts.find(name);
0145       if (pos == opts.end()) {
0146          return nullptr;
0147       }
0148       return  &((*pos).second);
0149    }
0150 
0151    template<class M>
0152    static void InsertValue(const std::string &name, M & opts, const typename M::mapped_type & value) {
0153       typename M::iterator pos;
0154       pos = opts.find(name);
0155       if (pos != opts.end()) {
0156          pos->second = value;
0157       }
0158       else {
0159          opts.insert(typename M::value_type(name, value) );
0160       }
0161    }
0162 
0163    template<class M>
0164    static void Print( const M & opts, std::ostream & os) {
0165       //const std::ios_base::fmtflags prevFmt = os.flags();
0166       for (typename M::const_iterator pos = opts.begin(); pos != opts.end(); ++pos)
0167          os << std::setw(25) << pos->first << " : " << std::setw(15) << pos->second << std::endl;
0168    }
0169 
0170    std::map<std::string, int>         fIntOpts;    ///< map of the integer options
0171    std::map<std::string, double>      fRealOpts;   ///< map of the real options
0172    std::map<std::string, std::string> fNamOpts;    ///< map of the named options
0173 
0174 };
0175 
0176 
0177 
0178    } // end namespace Math
0179 
0180 } // end namespace ROOT
0181 
0182 #endif