bpp-core3  3.0.0
ParametrizableCollection.h
Go to the documentation of this file.
1 //
2 // File: ParametrizableCollection.h
3 // Authors:
4 // Laurent Guéguen
5 // Created: mercredi 12 juin 2013, à 14h 24
6 //
7 
8 /*
9  Copyright or (c) or Copr. Bio++ Development Team, (November 16, 2004)
10 
11  This software is a computer program whose purpose is to provide classes
12  for phylogenetic data analysis.
13 
14  This software is governed by the CeCILL license under French law and
15  abiding by the rules of distribution of free software. You can use,
16  modify and/ or redistribute the software under the terms of the CeCILL
17  license as circulated by CEA, CNRS and INRIA at the following URL
18  "http://www.cecill.info".
19 
20  As a counterpart to the access to the source code and rights to copy,
21  modify and redistribute granted by the license, users are provided only
22  with a limited warranty and the software's author, the holder of the
23  economic rights, and the successive licensors have only limited
24  liability.
25 
26  In this respect, the user's attention is drawn to the risks associated
27  with loading, using, modifying and/or developing or reproducing the
28  software by the user in light of its specific status of free software,
29  that may mean that it is complicated to manipulate, and that also
30  therefore means that it is reserved for developers and experienced
31  professionals having in-depth computer knowledge. Users are therefore
32  encouraged to load and test the software's suitability as regards their
33  requirements in conditions enabling the security of their systems and/or
34  data to be ensured and, more generally, to use and operate it in the
35  same conditions as regards security.
36 
37  The fact that you are presently reading this means that you have had
38  knowledge of the CeCILL license and that you accept its terms.
39 */
40 
41 #ifndef BPP_NUMERIC_PARAMETRIZABLECOLLECTION_H
42 #define BPP_NUMERIC_PARAMETRIZABLECOLLECTION_H
43 
44 
46 #include "Parametrizable.h"
47 
48 // From the STL:
49 #include <map>
50 #include <memory>
51 
52 namespace bpp
53 {
64 template<class N>
67 {
68 protected:
73  std::map<size_t, std::shared_ptr<N> > objectsSet_;
74 
81  std::vector<size_t> vChanged_;
82 
83 public:
91  objectsSet_(),
92  vChanged_()
93  {}
94 
97  objectsSet_(),
98  vChanged_(set.vChanged_)
99  {
100  // Duplicate all objects:
101 
102  for (const auto& it:set.objectsSet_)
103  {
104  objectsSet_[it.first] = std::shared_ptr<N>(it.second->clone());
105  }
106  }
107 
109  {
110  clear();
111 
113  vChanged_ = set.vChanged_;
114 
115  // Duplicate all objects:
116 
117  for (const auto& it:set.objectsSet_)
118  {
119  objectsSet_[it.first] = std::shared_ptr<N>(it.second->clone());
120  }
121 
122  return *this;
123  }
124 
129  void clear()
130  {
132 
133  objectsSet_.clear();
134  vChanged_.empty();
135  }
136 
138  {
139  clear();
140  }
141 
143 
144 public:
150  void fireParameterChanged(const ParameterList& parameters)
151  {
152  vChanged_.clear();
153 
154  std::vector<size_t> vCh;
155 
156  std::map<size_t, ParameterList > mNumPl;
157 
158  for (size_t i = 0; i < parameters.size(); i++)
159  {
160  std::string n = parameters[i].getName();
161  size_t t = n.rfind("_");
162  if (t == std::string::npos)
163  continue;
164  size_t num = (size_t)atoi(n.substr(t + 1).c_str());
165  mNumPl[num].addParameter(Parameter(n.substr(0, t), parameters[i].getValue()));
166  }
167 
168  std::map<size_t, ParameterList >::iterator it;
169 
170  // Then we update all objects in the set:
171  for (it = mNumPl.begin(); it != mNumPl.end(); it++)
172  {
173  if (hasObject(it->first) && objectsSet_[it->first]->matchParametersValues(it->second))
174  vChanged_.push_back(it->first);
175  }
176  }
177 
178  std::vector<size_t> hasChanged() const
179  {
180  return vChanged_;
181  }
182 
184  {
185  vChanged_.clear();
186  }
187 
191  size_t getNumberOfObjects() const { return objectsSet_.size(); }
192 
199  bool hasObject(size_t objectIndex) const
200  {
201  return objectsSet_.find(objectIndex) != objectsSet_.end();
202  }
203 
210  bool hasObject(std::shared_ptr<N> object) const
211  {
212  for (const auto key : objectsSet_)
213  {
214  if (key.second == object)
215  return true;
216  }
217  return false;
218  }
219 
228  size_t getFirstKey(std::shared_ptr<N> object) const
229  {
230  for (const auto key:objectsSet_)
231  {
232  if (key.second == object)
233  return key.first;
234  }
235 
236  throw Exception("ParametrizableCollection::getFirstKey: Unknown object");
237  return 0;
238  }
239 
244  const std::vector<size_t> keys() const
245  {
246  std::vector<size_t> vkeys;
247 
248  for (const auto& it:objectsSet_)
249  {
250  vkeys.push_back(it.first);
251  }
252 
253  return vkeys;
254  }
255 
262  std::shared_ptr<const N> operator[](size_t objectIndex) const
263  {
264  const auto it = objectsSet_.find(objectIndex);
265  if (it == objectsSet_.end())
266  throw BadIntegerException("ParametrizableCollection::getObject().", (int)objectIndex);
267 
268  return std::dynamic_pointer_cast<const N>(it->second);
269  }
270 
271  std::shared_ptr<N> operator[](size_t objectIndex)
272  {
273  auto it = objectsSet_.find(objectIndex);
274  if (it == objectsSet_.end())
275  throw BadIntegerException("ParametrizableCollection::getObject().", (int)objectIndex);
276 
277  return it->second;
278  }
279 
287  ParameterList getParametersForObject(size_t objectIndex) const
288  {
289  ParameterList pl;
290  const auto it = objectsSet_.find(objectIndex);
291 
292  if (it != objectsSet_.end())
293  {
294  if (std::dynamic_pointer_cast<const ParameterAliasable>(it->second) != NULL)
295  pl = std::dynamic_pointer_cast<const ParameterAliasable>(it->second)->getIndependentParameters();
296  else
297  pl = it->second->getParameters();
298 
299  for (size_t i = 0; i < pl.size(); i++)
300  {
301  pl[i].setName(pl[i].getName() + "_" + TextTools::toString(objectIndex));
302  }
303  }
304  return pl;
305  }
306 
320  void addObject(std::shared_ptr<N> object, size_t objectIndex)
321  {
322  auto it = objectsSet_.find(objectIndex);
323  if (it != objectsSet_.end())
324  throw BadIntegerException("ParametrizableCollection<N>::addObject. Object objectIndex already used", (int)objectIndex);
325 
326  objectsSet_[objectIndex] = object;
327 
328  // Associate parameters:
329  std::vector<std::string> nplm;
330  nplm = object->getParameters().getParameterNames();
331 
332  for (const auto& pname:nplm)
333  {
334  Parameter* p = new Parameter(object->getParameters().getParameter(pname));
335  p->setName(pname + "_" + TextTools::toString(objectIndex));
336  addParameter_(p);
337  }
338 
339  if (std::dynamic_pointer_cast<ParameterAliasable>(object))
340  {
341  auto ppa = std::dynamic_pointer_cast<ParameterAliasable>(object);
342  for (const auto& name:nplm)
343  {
344  std::vector<std::string> va = ppa->getAlias(name);
345  for (const auto& alias:va)
346  {
347  aliasParameters(name + "_" + TextTools::toString(objectIndex), alias + "_" + TextTools::toString(objectIndex));
348  }
349  }
350  }
351  }
352 
360  std::shared_ptr<N> removeObject(size_t objectIndex)
361  {
362  if (objectsSet_.find(objectIndex) == objectsSet_.end())
363  throw BadIntegerException("ParametrizableCollection<N>::removeObject. None Object at this objectIndex", (int)objectIndex);
364 
365  auto pm = objectsSet_[objectIndex];
366  objectsSet_.erase(objectIndex);
367 
368  // Erase all parameter references to this object and translate other indices...
369 
371 
372  for (size_t i = pl.size(); i > 0; i--)
373  {
374  std::string pn = pl[i - 1].getName();
375 
376  size_t pu = pn.rfind("_");
377  int nm = atoi(pn.substr(pu + 1).c_str());
378  if (nm == (int)objectIndex)
379  {
380  std::vector<std::string> alpn = getAlias(pn);
381  for (unsigned j = 0; j < alpn.size(); j++)
382  {
383  try
384  {
385  unaliasParameters(alpn[j], pn);
386  }
387  catch (Exception& e)
388  {
389  continue;
390  }
391  }
392  deleteParameter_(i - 1);
393  }
394  }
395 
396  return pm;
397  }
398 
406  std::shared_ptr<N> replaceObject(std::shared_ptr<N> object, size_t objectIndex)
407  {
408  auto pm = removeObject(objectIndex);
409  addObject(object, objectIndex);
410  return pm;
411  }
412 };
413 } // end of namespace bpp.
414 #endif // BPP_NUMERIC_PARAMETRIZABLECOLLECTION_H
A partial implementation of the Parametrizable interface.
virtual std::vector< std::string > getAlias(const std::string &name) const
void unaliasParameters(const std::string &p1, const std::string &p2)
Detach two parameters previously set as 'aliased'.
void addParameter_(Parameter *parameter)
AbstractParameterAliasable & operator=(const AbstractParameterAliasable &ap)
void aliasParameters(const std::string &p1, const std::string &p2)
alias the parameters.
const ParameterList & getParameters() const
Get all parameters available.
Number exception: integers.
Definition: Exceptions.h:116
Exception base class. Overload exception constructor (to control the exceptions mechanism)....
Definition: Exceptions.h:59
The parameter list object.
Definition: ParameterList.h:65
size_t size() const
Definition: ParameterList.h:92
virtual void addParameter(const Parameter &param)
Add a new parameter at the end of the list.
This class is designed to facilitate the manipulation of parameters.
Definition: Parameter.h:135
virtual void setName(const std::string &name)
Set the name of this parameter.
Definition: Parameter.h:185
Plain collection of parametrizable objects.
ParametrizableCollection()
Create an empty object set.
ParametrizableCollection< N > & operator=(const ParametrizableCollection< N > &set)
size_t getFirstKey(std::shared_ptr< N > object) const
Return the first key mapping an object in the map.
std::map< size_t, std::shared_ptr< N > > objectsSet_
Contains all objects used.
ParameterList getParametersForObject(size_t objectIndex) const
Get the paramters of the Collection corresponding to an object from the set knowing its index.
const std::vector< size_t > keys() const
Returns the keys of the set.
ParametrizableCollection(const ParametrizableCollection< N > &set)
ParametrizableCollection< N > * clone() const
Create a copy of this object and send a pointer to it.
bool hasObject(std::shared_ptr< N > object) const
Says if there is an object in the map.
void fireParameterChanged(const ParameterList &parameters)
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 addObject(std::shared_ptr< N > object, size_t objectIndex)
Add a new object to the set with a given number.
std::shared_ptr< N > removeObject(size_t objectIndex)
Remove a object from the set, and all corresponding parameters.
std::shared_ptr< N > operator[](size_t objectIndex)
std::shared_ptr< const N > operator[](size_t objectIndex) const
Get one object from the set knowing its index.
bool hasObject(size_t objectIndex) const
Says if there is a object with a given index.
void clear()
Resets all the information contained in this object.
std::vector< size_t > hasChanged() const
std::vector< size_t > vChanged_
A vector of the numbers of objects that have changed during the last fireParameterChanged.
std::string toString(T t)
General template method to convert to a string.
Definition: TextTools.h:153