bpp-core  2.1.0
 All Classes Namespaces Files Functions Variables Typedefs Friends
ParameterList.cpp
Go to the documentation of this file.
1 //
2 // File: ParameterList.cpp
3 // Created by: Julien Dutheil
4 // Created on: Wed Oct 15 18:17:29 2003
5 //
6 
7 /*
8  Copyright or © or Copr. Julien Dutheil, (November 19, 2004)
9 
10  This software is a computer program whose purpose is to provide classes
11  for numerical calculus.
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 #include "ParameterList.h"
41 
42 using namespace bpp;
43 
44 #include <iostream>
45 #include <algorithm>
46 using namespace std;
47 
51  parameters_(pl.size())
52 {
53  // Now copy all parameters:
54  for (unsigned int i = 0; i < size(); i++)
55  {
56  parameters_[i] = dynamic_cast<Parameter*>(pl.parameters_[i]->clone());
57  }
58 }
59 
63 {
64  // First delete all parameters:
65  reset();
66 
67  // Then resize the vector:
68  parameters_.resize(pl.size());
69 
70  // Now copy all parameters:
71  for (unsigned int i = 0; i < pl.size(); i++)
72  {
73  parameters_[i] = dynamic_cast<Parameter*>(pl.parameters_[i]->clone());
74  }
75 
76  return *this;
77 }
78 
82 {
83  // Delete all parameter objects.
84  reset();
85 }
86 
87 /******************************************************************************/
88 const Parameter& ParameterList::getParameter(const std::string& name) const throw (ParameterNotFoundException)
89 {
90  for (unsigned int i = 0; i < size(); i++)
91  {
92  const Parameter* p = parameters_[i];
93  if (p->getName() == name) return *p;
94  }
95  throw ParameterNotFoundException("ParameterList::getParameter('name').", name);
96 }
97 
98 /******************************************************************************/
99 double ParameterList::getParameterValue(const std::string& name) const throw (ParameterNotFoundException)
100 {
101  for (unsigned int i = 0; i < size(); i++)
102  {
103  const Parameter* p = parameters_[i];
104  if (p->getName() == name) return p->getValue();
105  }
106  throw ParameterNotFoundException("ParameterList::getParameterValue('name').", name);
107 }
108 
109 /******************************************************************************/
111 {
112  for (unsigned int i = 0; i < size(); i++)
113  {
114  Parameter* p = parameters_[i];
115  if (p->getName() == name) return *p;
116  }
117  throw ParameterNotFoundException("ParameterList::getParameter('name').", name);
118 }
119 
120 /******************************************************************************/
121 ParameterList ParameterList::subList(const std::vector<std::string>& names) const throw (ParameterNotFoundException)
122 {
123  ParameterList pl;
124  for (unsigned int i = 0; i < names.size(); i++)
125  {
126  Parameter param = getParameter(names[i]);
127  pl.addParameter(param);
128  }
129  return pl;
130 }
131 
132 /******************************************************************************/
134 {
135  ParameterList pl;
136  Parameter param = getParameter(name);
137  pl.addParameter(param);
138  return pl;
139 }
140 
141 /******************************************************************************/
142 ParameterList ParameterList::subList(const std::vector<size_t>& parameters) const
143 {
144  ParameterList pl;
145  for (unsigned int i = 0; i < parameters.size(); i++)
146  {
147  if (parameters[i] < size()) pl.parameters_.push_back(dynamic_cast<Parameter*>(parameters_[parameters[i]]->clone()));
148  }
149  return pl;
150 }
151 
152 /******************************************************************************/
153 ParameterList ParameterList::subList(size_t parameter) const
154 {
155  ParameterList pl;
156  if (parameter < size()) pl.parameters_.push_back(dynamic_cast<Parameter*>(parameters_[parameter]->clone()));
157  return pl;
158 }
159 
160 /******************************************************************************/
162 {
163  ParameterList pl;
164  for (unsigned int i = 0; i < params.size(); i++)
165  {
166  const Parameter& p = params[i];
167  if (hasParameter(p.getName()))
168  pl.parameters_.push_back(dynamic_cast<Parameter*>(p.clone()));
169  // We use push_back instead of addParameter because we are sure the name is not duplicated.
170  }
171 
172  return pl;
173 }
174 
175 /******************************************************************************/
176 
177 std::vector<std::string> ParameterList::getParameterNames() const
178 {
179  vector<string> pNames(size());
180  for (unsigned int i = 0; i < size(); i++)
181  {
182  pNames[i] = parameters_[i]->getName();
183  }
184  return pNames;
185 }
186 
187 /******************************************************************************/
188 
190 {
191  if (hasParameter(param.getName()))
192  throw ParameterException("ParameterList::addParameter. Parameter with name '" + param.getName() + "' already exists.", &param);
193  parameters_.push_back(dynamic_cast<Parameter*>(param.clone()));
194 }
195 
196 /******************************************************************************/
197 
199 {
200  if (hasParameter(param->getName()))
201  throw ParameterException("ParameterList::addParameter. Parameter with name '" + param->getName() + "' already exists.", param);
202  parameters_.push_back(param);
203 }
204 
205 /******************************************************************************/
206 
207 void ParameterList::setParameter(size_t index, const Parameter& param) throw (IndexOutOfBoundsException)
208 {
209  if (index >= size()) throw IndexOutOfBoundsException("ParameterList::setParameter.", index, 0, size());
210  delete parameters_[index];
211  parameters_[index] = dynamic_cast<Parameter*>(param.clone());
212 }
213 
214 /******************************************************************************/
215 
217 {
218  for (unsigned int i = 0; i < params.size(); i++)
219  {
220  if (hasParameter(params[i].getName()))
221  setParameterValue(params[i].getName(), params[i].getValue());
222  else
223  parameters_.push_back(dynamic_cast<Parameter*>(params[i].clone()));
224  }
225 }
226 
227 /******************************************************************************/
228 
230 throw (ParameterException)
231 {
232  for (unsigned int i = 0; i < params.size(); i++)
233  {
234  addParameter(params[i]);
235  }
236 }
237 
238 /******************************************************************************/
239 
240 void ParameterList::setParameterValue(const string& name, double value)
242 {
243  Parameter* p = &getParameter(name);
244  p->setValue(value);
245 }
246 
247 /******************************************************************************/
248 
251 {
252  // First we check if all values are correct:
253  for (vector<Parameter*>::iterator it = parameters_.begin(); it < parameters_.end(); it++)
254  {
255  const Parameter* p = &params.getParameter((*it)->getName());
256  if ((*it)->hasConstraint() && !(*it)->getConstraint()->isCorrect(p->getValue()))
257  throw ConstraintException("ParameterList::setParametersValues()", *it, p->getValue());
258  }
259 
260  // If all values are ok, we set them:
261  for (vector<Parameter*>::iterator it = parameters_.begin(); it < parameters_.end(); it++)
262  {
263  const Parameter* p = &params.getParameter((*it)->getName());
264  (*it)->setValue(p->getValue());
265  }
266 }
267 
268 /******************************************************************************/
269 
271 {
272  // First we check if all values are correct:
273  for (vector<Parameter*>::const_iterator it = params.parameters_.begin(); it < params.parameters_.end(); it++)
274  {
275  if (hasParameter((*it)->getName()))
276  {
277  Parameter* p = &getParameter((*it)->getName());
278  if (p->hasConstraint() && !p->getConstraint()->isCorrect((*it)->getValue()))
279  throw ConstraintException("ParameterList::setParametersValues()", p, (*it)->getValue());
280  }
281  }
282 
283  // If all values are ok, we set them:
284  {
285  for (vector<Parameter*>::const_iterator it = params.parameters_.begin(); it < params.parameters_.end(); it++)
286  {
287  if (hasParameter((*it)->getName()))
288  {
289  Parameter* p = &getParameter((*it)->getName());
290  p->setValue((*it)->getValue());
291  }
292  }
293  }
294 }
295 
296 /******************************************************************************/
297 
299 {
300  // First we check if all values are correct:
301  for (vector<Parameter*>::const_iterator it = params.parameters_.begin(); it < params.parameters_.end(); it++)
302  {
303  if (hasParameter((*it)->getName()))
304  {
305  const Parameter* p = &getParameter((*it)->getName());
306  if (p->hasConstraint() && !p->getConstraint()->isCorrect((*it)->getValue()))
307  throw ConstraintException("ParameterList::matchParametersValues()", p, (*it)->getValue());
308  }
309  }
310 
311  // If all values are ok, we test them:
312  bool ch = 0;
313 
314  for (vector<Parameter*>::const_iterator it = params.parameters_.begin(); it < params.parameters_.end(); it++)
315  {
316  if (hasParameter((*it)->getName()))
317  {
318  const Parameter* p = &getParameter((*it)->getName());
319  if (p->getValue() != (*it)->getValue())
320  ch |= 1;
321  }
322  }
323  return ch;
324 }
325 
326 /******************************************************************************/
327 
329 throw (ConstraintException)
330 {
331  // First we check if all values are correct:
332  for (vector<Parameter*>::const_iterator it = params.parameters_.begin(); it < params.parameters_.end(); it++)
333  {
334  if (hasParameter((*it)->getName()))
335  {
336  Parameter* p = &getParameter((*it)->getName());
337  if (p->hasConstraint() && !p->getConstraint()->isCorrect((*it)->getValue()))
338  throw ConstraintException("ParameterList::matchParametersValues()", p, (*it)->getValue());
339  }
340  }
341 
342  // If all values are ok, we set them:
343  bool ch = 0;
344 
345  for (vector<Parameter*>::const_iterator it = params.parameters_.begin(); it < params.parameters_.end(); it++)
346  {
347  if (hasParameter((*it)->getName()))
348  {
349  Parameter* p = &getParameter((*it)->getName());
350  if (p->getValue() != (*it)->getValue())
351  ch |= 1;
352  p->setValue((*it)->getValue());
353  }
354  }
355  return ch;
356 }
357 
358 /******************************************************************************/
361 {
362  for (vector<Parameter*>::iterator it = parameters_.begin(); it < parameters_.end(); it++)
363  {
364  const Parameter* p = &params.getParameter((*it)->getName());
365  **it = *p;
366  }
367 }
368 
369 /******************************************************************************/
372 {
373  for (vector<Parameter*>::const_iterator it = params.parameters_.begin(); it < params.parameters_.end(); it++)
374  {
375  Parameter* p = &getParameter((*it)->getName());
376  *p = **it;
377  }
378 }
379 
380 /******************************************************************************/
381 bool ParameterList::hasParameter(const std::string& name) const
382 {
383  for (unsigned int i = 0; i < size(); i++)
384  {
385  const Parameter* p = parameters_[i];
386  if (p->getName() == name)
387  return true;
388  }
389  return false;
390 }
391 
392 /******************************************************************************/
394 {
395  for (vector<Parameter*>::const_iterator it = params.parameters_.begin(); it < params.parameters_.end(); it++)
396  {
397  if (hasParameter((*it)->getName()))
398  {
399  Parameter* p = &getParameter((*it)->getName());
400  *p = **it;
401  }
402  }
403 }
404 
405 /******************************************************************************/
406 void ParameterList::deleteParameter(const std::string& name) throw (ParameterNotFoundException)
407 {
408  for (unsigned int i = 0; i < size(); i++)
409  {
410  Parameter* p = parameters_[i];
411  if (p->getName() == name)
412  {
413  delete p;
414  parameters_.erase(parameters_.begin() + i);
415  return;
416  }
417  }
418  throw ParameterNotFoundException("ParameterList::deleteParameter", name);
419 }
420 
421 /******************************************************************************/
422 void ParameterList::deleteParameters(const std::vector<std::string>& names) throw (ParameterNotFoundException)
423 {
424  for (unsigned int i = 0; i < names.size(); i++)
425  {
426  deleteParameter(names[i]);
427  }
428 }
429 
430 /******************************************************************************/
432 {
433  if (index >= size()) throw IndexOutOfBoundsException("ParameterList::deleteParameter.", index, 0, size());
434  Parameter* p = parameters_[index];
435  delete p;
436  parameters_.erase(parameters_.begin() + index);
437 }
438 
439 /******************************************************************************/
440 void ParameterList::deleteParameters(const std::vector<size_t>& indices) throw (IndexOutOfBoundsException)
441 {
442  vector<size_t> tmp(indices);
443  sort(tmp.begin(), tmp.end());
444  for (vector<size_t>::reverse_iterator i = tmp.rbegin(); i != tmp.rend(); i++)
445  {
446  size_t index = *i;
447  if (index >= size()) throw IndexOutOfBoundsException("ParameterList::deleteParameter.", index, 0, size());
448  Parameter* p = parameters_[index];
449  delete p;
450  parameters_.erase(parameters_.begin() + index);
451  }
452 }
453 
454 /******************************************************************************/
455 size_t ParameterList::whichParameterHasName(const std::string& name) const throw (ParameterNotFoundException)
456 {
457  for (size_t i = 0; i < size(); i++)
458  {
459  if (parameters_[i]->getName() == name) return i;
460  }
461  throw ParameterNotFoundException("ParameterList::whichParameterHasName.", name);
462 }
463 
464 /******************************************************************************/
466 {
467  (out << "Name:\tValue:\tConstraint:").endLine();
468  (out << "_________________________________________________").endLine();
469  for (unsigned int i = 0; i < size(); i++)
470  {
471  out << parameters_[i]->getName();
472  out << "\t" << parameters_[i]->getValue();
473  out << (parameters_[i]->hasConstraint() ? "\t" + parameters_[i]->getConstraint()->getDescription() : string(""));
474  out.endLine();
475  }
476 }
477 
478 /******************************************************************************/
480 {
481  for (unsigned int i = 0; i < size(); i++)
482  {
483  delete parameters_[i];
484  }
485  parameters_.resize(0);
486 }
487 
488 /******************************************************************************/
489