xc
FactorsMap.h
1 // -*-c++-*-
2 //----------------------------------------------------------------------------
3 // xc utils library; general purpose classes and functions.
4 //
5 // Copyright (C) Luis C. PĂ©rez Tato
6 //
7 // XC utils is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // This software is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program.
19 // If not, see <http://www.gnu.org/licenses/>.
20 //----------------------------------------------------------------------------
21 //FactorsMap.h
22 //Contenedor de coeficientes de simultaneidad de acciones.
23 
24 #ifndef FACTORSMAP_H
25 #define FACTORSMAP_H
26 
27 #include "utility/kernel/CommandEntity.h"
28 #include <map>
29 #include "utility/utils/misc_utils/colormod.h"
30 
31 
32 namespace cmb_acc {
33 
34 class LoadCombinationVector;
35 
37 //
39 template <class Factors>
40 class FactorsMap: public CommandEntity, public std::map<std::string,Factors>
41  {
42  public:
43  typedef typename std::map<std::string,Factors>::const_iterator const_iterator;
44  typedef typename std::map<std::string,Factors>::iterator iterator;
45  private:
46  static Factors default_factors;
47  bool exists(const std::string &) const;
48  Factors *getPtrCoefs(const std::string &);
49  Factors *crea_coefs(const std::string &);
50  void print_err_not_found(const std::string &, const std::string &) const;
51  public:
52  FactorsMap(void);
53  const Factors *getPtrCoefs(const std::string &) const;
54  const Factors &BuscaCoefs(const std::string &) const;
55  void insert(const std::string &,const Factors &);
56  std::deque<std::string> getNames(void) const;
57  boost::python::list getKeys(void) const;
58  };
59 
60 template <class Factors>
62 
64 template <class Factors>
65 bool FactorsMap<Factors>::exists(const std::string &name) const
66  { return (this->find(name)!=this->end()); }
67 
69 template <class Factors>
70 void FactorsMap<Factors>::print_err_not_found(const std::string &functionName, const std::string &name) const
71  {
72  std::cerr << Color::red << getClassName() << "::" << functionName
73  << "; factors with name: '"
74  << name << "' not found."
75  << Color::def << std::endl;
76 
77  const std::deque<std::string> candidates= this->getNames();
78  if(!candidates.empty())
79  {
80  std::cerr << Color::red << " Candidates are: ";
81  std::deque<std::string>::const_iterator i= candidates.begin();
82  std::cerr << *i;
83  i++;
84  for(;i!=candidates.end();i++)
85  std::cerr << ", " << *i;
86  std::cerr << Color::def << std::endl;
87  }
88  else
89  {
90  std::cerr << Color::red << " " << getClassName() << "::" << functionName
91  << "; the factors container is empty."
92  << Color::def << std::endl;
93  }
94  }
95 
97 template <class Factors>
98 std::deque<std::string> FactorsMap<Factors>::getNames(void) const
99  {
100  std::deque<std::string> retval;
101  for(const_iterator i= this->begin();i!= this->end();i++)
102  retval.push_back(i->first);
103  return retval;
104  }
105 
107 template <class Factors>
108 boost::python::list FactorsMap<Factors>::getKeys(void) const
109  {
110  boost::python::list retval;
111  for(const_iterator i=this->begin();i!=this->end();i++)
112  retval.append((*i).first);
113  return retval;
114  }
115 
118 template <class Factors>
119 Factors *FactorsMap<Factors>::getPtrCoefs(const std::string &name)
120  {
121  Factors *retval= nullptr;
122  if(this->exists(name))
123  retval= &((*this)[name]);
124  else
125  this->print_err_not_found(__FUNCTION__,name);
126  return retval;
127  }
128 
130 template <class Factors>
131 const Factors &FactorsMap<Factors>::BuscaCoefs(const std::string &name) const
132  {
133  if(this->exists(name))
134  return this->find(name)->second;
135  else
136  {
137  this->print_err_not_found(__FUNCTION__,name);
138  return default_factors;
139  }
140  }
141 
143 template <class Factors>
144 const Factors *FactorsMap<Factors>::getPtrCoefs(const std::string &name) const
145  {
146  const Factors *retval= nullptr;
147  if(this->exists(name))
148  retval= &(this->find(name)->second);
149  else
150  this->print_err_not_found(__FUNCTION__,name);
151  return retval;
152  }
153 
155 template <class Factors>
157  : CommandEntity() {}
158 
160 template <class Factors>
161 Factors *FactorsMap<Factors>::crea_coefs(const std::string &name)
162  {
163  Factors *retval= nullptr;
164  if(this->exists(name))
165  retval= &(this->find(name)->second);
166  else //los coeficientes son nuevos.
167  {
168  Factors tmp;
169  (*this)[name]= tmp;
170  retval= getPtrCoefs(name);
171  }
172  return retval;
173  }
174 
178 template <class Factors>
179 void FactorsMap<Factors>::insert(const std::string &name,const Factors &c)
180  { (*this)[name]= c; }
181 
182 } // fin namespace cmb_acc
183 
184 #endif
Routines to generate combinations of actions.
Almacena todas las familias de acciones.
Definition: Factors.h:34
Combination factor container.
Definition: FactorsMap.h:40
FactorsMap(void)
Default constructor.
Definition: FactorsMap.h:156
boost::python::list getKeys(void) const
Return the names of the factor families.
Definition: FactorsMap.h:108
virtual std::string getClassName(void) const
Returns demangled class name.
Definition: EntityWithOwner.cc:90
Objet that can execute python scripts.
Definition: CommandEntity.h:40
const Factors & BuscaCoefs(const std::string &) const
Return the factors identified by name.
Definition: FactorsMap.h:131
std::deque< std::string > getNames(void) const
Return the names of the factor families.
Definition: FactorsMap.h:98
void insert(const std::string &, const Factors &)
Inserts the coefficients.
Definition: FactorsMap.h:179