bpp-core3  3.0.0
ParametrizableCollection.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #ifndef BPP_NUMERIC_PARAMETRIZABLECOLLECTION_H
6 #define BPP_NUMERIC_PARAMETRIZABLECOLLECTION_H
7 
8 
10 #include "Parametrizable.h"
11 
12 // From the STL:
13 #include <map>
14 #include <memory>
15 
16 namespace bpp
17 {
28 template<class N>
31 {
32 protected:
37  std::map<size_t, std::shared_ptr<N>> objectsSet_;
38 
45  std::vector<size_t> vChanged_;
46 
47 public:
55  objectsSet_(),
56  vChanged_()
57  {}
58 
61  objectsSet_(),
62  vChanged_(set.vChanged_)
63  {
64  // Duplicate all objects:
65 
66  for (const auto& it:set.objectsSet_)
67  {
68  objectsSet_[it.first] = std::shared_ptr<N>(it.second->clone());
69  }
70  }
71 
73  {
74  clear();
75 
77  vChanged_ = set.vChanged_;
78 
79  // Duplicate all objects:
80 
81  for (const auto& it:set.objectsSet_)
82  {
83  objectsSet_[it.first] = std::shared_ptr<N>(it.second->clone());
84  }
85 
86  return *this;
87  }
88 
93  void clear()
94  {
96 
97  objectsSet_.clear();
98  vChanged_.empty();
99  }
100 
102  {
103  clear();
104  }
105 
107 
108 public:
114  void fireParameterChanged(const ParameterList& parameters)
115  {
116  vChanged_.clear();
117 
118  std::vector<size_t> vCh;
119 
120  std::map<size_t, ParameterList > mNumPl;
121 
122  for (size_t i = 0; i < parameters.size(); i++)
123  {
124  std::string n = parameters[i].getName();
125  size_t t = n.rfind("_");
126  if (t == std::string::npos)
127  continue;
128  size_t num = (size_t)atoi(n.substr(t + 1).c_str());
129  mNumPl[num].addParameter(Parameter(n.substr(0, t), parameters[i].getValue()));
130  }
131 
132  std::map<size_t, ParameterList >::iterator it;
133 
134  // Then we update all objects in the set:
135  for (it = mNumPl.begin(); it != mNumPl.end(); it++)
136  {
137  if (hasObject(it->first) && objectsSet_[it->first]->matchParametersValues(it->second))
138  vChanged_.push_back(it->first);
139  }
140  }
141 
142  std::vector<size_t> hasChanged() const
143  {
144  return vChanged_;
145  }
146 
148  {
149  vChanged_.clear();
150  }
151 
155  size_t getNumberOfObjects() const { return objectsSet_.size(); }
156 
163  bool hasObject(size_t objectIndex) const
164  {
165  return objectsSet_.find(objectIndex) != objectsSet_.end();
166  }
167 
174  bool hasObject(std::shared_ptr<N> object) const
175  {
176  for (const auto key : objectsSet_)
177  {
178  if (key.second == object)
179  return true;
180  }
181  return false;
182  }
183 
192  size_t getFirstKey(std::shared_ptr<N> object) const
193  {
194  for (const auto key:objectsSet_)
195  {
196  if (key.second == object)
197  return key.first;
198  }
199 
200  throw Exception("ParametrizableCollection::getFirstKey: Unknown object");
201  return 0;
202  }
203 
208  const std::vector<size_t> keys() const
209  {
210  std::vector<size_t> vkeys;
211 
212  for (const auto& it:objectsSet_)
213  {
214  vkeys.push_back(it.first);
215  }
216 
217  return vkeys;
218  }
219 
226  std::shared_ptr<const N> operator[](size_t objectIndex) const
227  {
228  const auto it = objectsSet_.find(objectIndex);
229  if (it == objectsSet_.end())
230  throw BadIntegerException("ParametrizableCollection::getObject().", (int)objectIndex);
231 
232  return std::dynamic_pointer_cast<const N>(it->second);
233  }
234 
235  std::shared_ptr<N> operator[](size_t objectIndex)
236  {
237  auto it = objectsSet_.find(objectIndex);
238  if (it == objectsSet_.end())
239  throw BadIntegerException("ParametrizableCollection::getObject().", (int)objectIndex);
240 
241  return it->second;
242  }
243 
251  ParameterList getParametersForObject(size_t objectIndex) const
252  {
253  ParameterList pl;
254  const auto it = objectsSet_.find(objectIndex);
255 
256  if (it != objectsSet_.end())
257  {
258  if (std::dynamic_pointer_cast<const ParameterAliasable>(it->second) != NULL)
259  pl = std::dynamic_pointer_cast<const ParameterAliasable>(it->second)->getIndependentParameters();
260  else
261  pl = it->second->getParameters();
262 
263  for (size_t i = 0; i < pl.size(); i++)
264  {
265  pl[i].setName(pl[i].getName() + "_" + TextTools::toString(objectIndex));
266  }
267  }
268  return pl;
269  }
270 
284  void addObject(std::shared_ptr<N> object, size_t objectIndex)
285  {
286  auto it = objectsSet_.find(objectIndex);
287  if (it != objectsSet_.end())
288  throw BadIntegerException("ParametrizableCollection<N>::addObject. Object objectIndex already used", (int)objectIndex);
289 
290  objectsSet_[objectIndex] = object;
291 
292  // Associate parameters:
293  std::vector<std::string> nplm;
294  nplm = object->getParameters().getParameterNames();
295 
296  for (const auto& pname:nplm)
297  {
298  Parameter* p = new Parameter(object->getParameters().parameter(pname));
299  p->setName(pname + "_" + TextTools::toString(objectIndex));
300  addParameter_(p);
301  }
302 
303  if (std::dynamic_pointer_cast<ParameterAliasable>(object))
304  {
305  auto ppa = std::dynamic_pointer_cast<ParameterAliasable>(object);
306  for (const auto& name:nplm)
307  {
308  std::vector<std::string> va = ppa->getAlias(name);
309  for (const auto& alias:va)
310  {
311  aliasParameters(name + "_" + TextTools::toString(objectIndex), alias + "_" + TextTools::toString(objectIndex));
312  }
313  }
314  }
315  }
316 
324  std::shared_ptr<N> removeObject(size_t objectIndex)
325  {
326  if (objectsSet_.find(objectIndex) == objectsSet_.end())
327  throw BadIntegerException("ParametrizableCollection<N>::removeObject. None Object at this objectIndex", (int)objectIndex);
328 
329  auto pm = objectsSet_[objectIndex];
330  objectsSet_.erase(objectIndex);
331 
332  // Erase all parameter references to this object and translate other indices...
333 
335 
336  for (size_t i = pl.size(); i > 0; i--)
337  {
338  std::string pn = pl[i - 1].getName();
339 
340  size_t pu = pn.rfind("_");
341  int nm = atoi(pn.substr(pu + 1).c_str());
342  if (nm == (int)objectIndex)
343  {
344  std::vector<std::string> alpn = getAlias(pn);
345  for (unsigned j = 0; j < alpn.size(); j++)
346  {
347  try
348  {
349  unaliasParameters(alpn[j], pn);
350  }
351  catch (Exception& e)
352  {
353  continue;
354  }
355  }
356  deleteParameter_(i - 1);
357  }
358  }
359 
360  return pm;
361  }
362 
370  std::shared_ptr<N> replaceObject(std::shared_ptr<N> object, size_t objectIndex)
371  {
372  auto pm = removeObject(objectIndex);
373  addObject(object, objectIndex);
374  return pm;
375  }
376 };
377 } // end of namespace bpp.
378 #endif // BPP_NUMERIC_PARAMETRIZABLECOLLECTION_H
std::shared_ptr< N > operator[](size_t objectIndex)
bool hasObject(std::shared_ptr< N > object) const
Says if there is an object in the map.
Plain collection of parametrizable objects.
virtual std::vector< std::string > getAlias(const std::string &name) const
std::shared_ptr< const N > operator[](size_t objectIndex) const
Get one object from the set knowing its index.
std::vector< size_t > vChanged_
A vector of the numbers of objects that have changed during the last fireParameterChanged.
Extend the Parametrizable interface with support for parameter aliases.
std::shared_ptr< N > replaceObject(std::shared_ptr< N > object, size_t objectIndex)
Replace a object in the set, and returns the replaced one.
void clear()
Resets all the information contained in this object.
Number exception: integers.
Definition: Exceptions.h:77
virtual std::vector< std::string > getAlias(const std::string &name) const =0
void unaliasParameters(const std::string &p1, const std::string &p2)
Detach two parameters previously set as &#39;aliased&#39;.
ParametrizableCollection< N > & operator=(const ParametrizableCollection< N > &set)
size_t size() const
Definition: ParameterList.h:56
This class is designed to facilitate the manipulation of parameters.
Definition: Parameter.h:97
void addParameter_(Parameter *parameter)
ParametrizableCollection< N > * clone() const
Create a copy of this object and send a pointer to it.
The parameter list object.
Definition: ParameterList.h:27
void addObject(std::shared_ptr< N > object, size_t objectIndex)
Add a new object to the set with a given number.
bool hasObject(size_t objectIndex) const
Says if there is a object with a given index.
A partial implementation of the Parametrizable interface.
std::map< size_t, std::shared_ptr< N > > objectsSet_
Contains all objects used.
std::vector< size_t > hasChanged() const
std::shared_ptr< N > removeObject(size_t objectIndex)
Remove a object from the set, and all corresponding parameters.
AbstractParameterAliasable & operator=(const AbstractParameterAliasable &ap)
void fireParameterChanged(const ParameterList &parameters)
virtual void setName(const std::string &name)
Set the name of this parameter.
Definition: Parameter.h:148
Exception base class. Overload exception constructor (to control the exceptions mechanism). Destructor is already virtual (from std::exception)
Definition: Exceptions.h:20
const std::vector< size_t > keys() const
Returns the keys of the set.
ParametrizableCollection()
Create an empty object set.
void aliasParameters(const std::string &p1, const std::string &p2)
alias the parameters.
ParametrizableCollection(const ParametrizableCollection< N > &set)
size_t getFirstKey(std::shared_ptr< N > object) const
Return the first key mapping an object in the map.
std::string toString(T t)
General template method to convert to a string.
Definition: TextTools.h:115
const ParameterList & getParameters() const override
Get all parameters available.
ParameterList getParametersForObject(size_t objectIndex) const
Get the paramters of the Collection corresponding to an object from the set knowing its index...