bpp-core  2.2.0
ParametrizableCollection.h
Go to the documentation of this file.
1 //
2 // File: ParametrizableCollection.h
3 // Created by: Laurent Guéguen
4 // Created on: mercredi 12 juin 2013, à 14h 24
5 //
6 
7 /*
8  Copyright or (c) or Copr. Bio++ Development Team, (November 16, 2004)
9 
10  This software is a computer program whose purpose is to provide classes
11  for phylogenetic data analysis.
12 
13  This software is governed by the CeCILL license under French law and
14  abiding by the rules of distribution of free software. You can use,
15  modify and/ or redistribute the software under the terms of the CeCILL
16  license as circulated by CEA, CNRS and INRIA at the following URL
17  "http://www.cecill.info".
18 
19  As a counterpart to the access to the source code and rights to copy,
20  modify and redistribute granted by the license, users are provided only
21  with a limited warranty and the software's author, the holder of the
22  economic rights, and the successive licensors have only limited
23  liability.
24 
25  In this respect, the user's attention is drawn to the risks associated
26  with loading, using, modifying and/or developing or reproducing the
27  software by the user in light of its specific status of free software,
28  that may mean that it is complicated to manipulate, and that also
29  therefore means that it is reserved for developers and experienced
30  professionals having in-depth computer knowledge. Users are therefore
31  encouraged to load and test the software's suitability as regards their
32  requirements in conditions enabling the security of their systems and/or
33  data to be ensured and, more generally, to use and operate it in the
34  same conditions as regards security.
35 
36  The fact that you are presently reading this means that you have had
37  knowledge of the CeCILL license and that you accept its terms.
38 */
39 
40 #ifndef _PARAMETRIZABLECOLLECTION_H_
41 #define _PARAMETRIZABLECOLLECTION_H_
42 
43 #include "Parametrizable.h"
45 
46 // From the STL:
47 #include <map>
48 #include <memory>
49 
50 namespace bpp
51 {
62  template <class N>
65  {
66  protected:
71  std::map<size_t, N* > objectsSet_;
72 
79  std::vector<size_t> vChanged_;
80 
81  public:
89  objectsSet_(),
90  vChanged_()
91  {
92  }
93 
96  objectsSet_(),
97  vChanged_(set.vChanged_)
98  {
99  // Duplicate all objects:
100  typename std::map<size_t, N* >::const_iterator it;
101  for (it=set.objectsSet_.begin(); it!=set.objectsSet_.end(); it++)
102  objectsSet_[it->first]=it->second->clone();
103  }
104 
106  {
107  clear();
108 
110  vChanged_ = set.vChanged_;
111 
112  // Duplicate all objects:
113  typename std::map<size_t, N* >::const_iterator it;
114  for (it=set.objectsSet_.begin(); it!=set.objectsSet_.end(); it++)
115  objectsSet_[it->first]=it->second->clone();
116 
117  return *this;
118  }
119 
125  void clear()
126  {
128 
129  typename std::map<size_t, N*>::const_iterator it;
130  for (it=objectsSet_.begin(); it!=objectsSet_.end(); it++)
131  delete it->second;
132  objectsSet_.clear();
133  vChanged_.empty();
134  }
135 
137  {
138  clear();
139  }
140 
142 
143  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  if (hasObject(it->first) && objectsSet_[it->first]->matchParametersValues(it->second))
173  vChanged_.push_back(it->first);
174  }
175  }
176 
177  std::vector<size_t> hasChanged() const
178  {
179  return vChanged_;
180  }
181 
183  {
184  vChanged_.clear();
185  }
186 
191  size_t getNumberOfObjects() const { return objectsSet_.size(); }
192 
200  const bool hasObject(size_t objectIndex) const
201  {
202  return (objectsSet_.find(objectIndex)!=objectsSet_.end());
203  }
204 
210  const std::vector<size_t> keys() const
211  {
212  std::vector<size_t> vkeys;
213  typename std::map<size_t, N*>::const_iterator it;
214 
215  for (it=objectsSet_.begin(); it!=objectsSet_.end(); it++)
216  vkeys.push_back(it->first);
217 
218  return vkeys;
219  }
220 
221 
229  const N* operator[](size_t objectIndex) const
230  {
231  typename std::map<size_t, N*>::const_iterator it=objectsSet_.find(objectIndex);
232  if (it==objectsSet_.end())
233  throw BadIntegerException("ParametrizableCollection::getObject().", (int)objectIndex);
234 
235  return dynamic_cast<const N*>(it->second);
236  }
237 
238  N* operator[](size_t objectIndex)
239  {
240  typename std::map<size_t, N*>::iterator it=objectsSet_.find(objectIndex);
241  if (it==objectsSet_.end())
242  throw BadIntegerException("ParametrizableCollection::getObject().", (int)objectIndex);
243 
244  return it->second;
245  }
246 
261  void addObject(N* object, size_t objectIndex)
262  {
263  typename std::map<size_t, N*>::iterator it=objectsSet_.find(objectIndex);
264  if (it!=objectsSet_.end())
265  throw BadIntegerException("ParametrizableCollection<N>::addObject. Object objectIndex already used", (int)objectIndex);
266 
267  objectsSet_[objectIndex]=object;
268 
269  // Associate parameters:
270  std::string pname;
271  std::vector<std::string> nplm;
272  nplm=object->getParameters().getParameterNames();
273 
274  for (size_t i = 0; i < nplm.size(); i++)
275  {
276  pname = nplm[i];
277  Parameter* p = new Parameter(object->getParameters().getParameter(pname));
278  p->setName(pname + "_" + TextTools::toString(objectIndex));
279  addParameter_(p);
280  }
281 
282  if (dynamic_cast<ParameterAliasable*>(object)){
283  ParameterAliasable* ppa=dynamic_cast<ParameterAliasable*>(object);
284  for (size_t i = 0; i < nplm.size(); i++)
285  {
286  std::vector<std::string> va=ppa->getAlias(nplm[i]);
287  for (size_t j=0;j<va.size();j++)
288  aliasParameters(nplm[i] + "_" + TextTools::toString(objectIndex), va[j] + "_" + TextTools::toString(objectIndex));
289  }
290  }
291  }
292 
301  N* removeObject(size_t objectIndex)
302  {
303  if (objectsSet_.find(objectIndex)==objectsSet_.end())
304  throw BadIntegerException("ParametrizableCollection<N>::removeObject. None Object at this objectIndex", (int)objectIndex);
305 
306  N* pm=objectsSet_[objectIndex];
307  objectsSet_.erase(objectIndex);
308 
309  // Erase all parameter references to this object and translate other indices...
310 
312 
313  for (size_t i = pl.size(); i>0; i--)
314  {
315  std::string pn=pl[i-1].getName();
316 
317  size_t pu=pn.rfind("_");
318  int nm=atoi(pn.substr(pu+1).c_str());
319  if (nm==(int)objectIndex){
320  std::vector<std::string> alpn=getAlias(pn);
321  for (unsigned j=0; j<alpn.size(); j++)
322  try {
323  unaliasParameters(alpn[j],pn);
324  }
325  catch (Exception& e)
326  {
327  continue;
328  }
329  deleteParameter_(i-1);
330  }
331  }
332 
333  return pm;
334  }
335 
344  N* replaceObject(N* object, size_t objectIndex)
345  {
346  N* pm=removeObject(objectIndex);
347  addObject(object, objectIndex);
348  return pm;
349  }
350 
351 
352  };
353 } // end of namespace bpp.
354 
355 #endif // _PARAMETRIZABLECOLLECTION_H_
356 
void addObject(N *object, size_t objectIndex)
Add a new object to the set with a given number.
Plain collection of parametrizable objects.
std::vector< std::string > getAlias(const std::string &name) const
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.
void clear()
Resets all the information contained in this object.
This class allows to perform a correspondence analysis.
Number exception: integers.
Definition: Exceptions.h:175
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;.
std::map< size_t, N *> objectsSet_
Contains all objects used.
ParametrizableCollection< N > & operator=(const ParametrizableCollection< N > &set)
size_t size() const
Definition: ParameterList.h:90
This class is designed to facilitate the manipulation of parameters.
Definition: Parameter.h:135
void addParameter_(Parameter *parameter)
ParametrizableCollection< N > * clone() const
Create a copy of this object and send a pointer to it.
const N * operator[](size_t objectIndex) const
Get one object from the set knowing its index.
static std::string toString(T t)
General template method to convert to a string.
Definition: TextTools.h:189
The parameter list object.
Definition: ParameterList.h:61
A partial implementation of the Parametrizable interface.
std::vector< size_t > hasChanged() const
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:208
Exception base class.
Definition: Exceptions.h:57
const std::vector< size_t > keys() const
Returns the keys of the set.
const ParameterList & getParameters() const
Get all parameters available.
ParametrizableCollection()
Create an empty object set.
void aliasParameters(const std::string &p1, const std::string &p2)
Set two parameters as &#39;aliased&#39;.
ParametrizableCollection(const ParametrizableCollection< N > &set)
const bool hasObject(size_t objectIndex) const
Says if there is a object with a given index.
N * replaceObject(N *object, size_t objectIndex)
Replace a object in the set, and returns the replaced one.
N * removeObject(size_t objectIndex)
Remove a object from the set, and all corresponding parameters.