GammaLib  1.7.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GOptimizerPars.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GOptimizerPars.hpp - Optimizer parameter container class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2009-2017 by Juergen Knoedlseder *
5  * ----------------------------------------------------------------------- *
6  * *
7  * This program 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 program is distributed in the hope that it will be useful, *
13  * but 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. If not, see <http://www.gnu.org/licenses/>. *
19  * *
20  ***************************************************************************/
21 /**
22  * @file GOptimizerPars.hpp
23  * @brief Optimizer parameters base class definition
24  * @author Juergen Knoedlseder
25  */
26 
27 #ifndef GOPTIMIZERPARS_HPP
28 #define GOPTIMIZERPARS_HPP
29 
30 /* __ Includes ___________________________________________________________ */
31 #include <string>
32 #include <vector>
33 #include "GContainer.hpp"
34 #include "GOptimizerPar.hpp"
35 
36 
37 /***********************************************************************//**
38  * @class GOptimizerPars
39  *
40  * @brief Optimizer parameter container class
41  *
42  * This class is a container class for parameters of a function that are to
43  * be optimized. The optimizer function is defined by the abstract
44  * GOptimizerFunction class.
45  *
46  * The class holds a flat array of pointers to optimizer parameters. The
47  * class can deal with parameters that are allocated by the class itself,
48  * or with pointers that are passed by a client and allocated/deallocated
49  * by the client.
50  ***************************************************************************/
51 class GOptimizerPars : public GContainer {
52 
53 public:
54  // Constructors and destructors
55  GOptimizerPars(void);
56  explicit GOptimizerPars(const int& number);
57  GOptimizerPars(const GOptimizerPars& pars);
58  virtual ~GOptimizerPars(void);
59 
60  // Operators
62  GOptimizerPar* operator[](const int& index);
63  const GOptimizerPar* operator[](const int& index) const;
64  GOptimizerPar* operator[](const std::string& name);
65  const GOptimizerPar* operator[](const std::string& name) const;
66 
67  // Methods
68  void clear(void);
69  GOptimizerPars* clone(void) const;
70  std::string classname(void) const;
71  GOptimizerPar* at(const int& index);
72  const GOptimizerPar* at(const int& index) const;
73  int size(void) const;
74  bool is_empty(void) const;
75  int nfree(void) const;
76  GOptimizerPar* set(const int& index, const GOptimizerPar& par);
77  GOptimizerPar* set(const std::string& name, const GOptimizerPar& par);
78  GOptimizerPar* append(const GOptimizerPar& par);
79  void attach(GOptimizerPar* par);
80  void attach(const int& index, GOptimizerPar* par);
81  void attach(const std::string& name, GOptimizerPar* par);
82  GOptimizerPar* insert(const int& index, const GOptimizerPar& par);
83  GOptimizerPar* insert(const std::string& name, const GOptimizerPar& par);
84  void remove(const int& index);
85  void remove(const std::string& name);
86  void reserve(const int& num);
87  void extend(const GOptimizerPars& pars);
88  bool contains(const std::string& name) const;
89  std::string print(const GChatter& chatter = NORMAL) const;
90 
91 protected:
92  // Protected methods
93  void init_members(void);
94  void copy_members(const GOptimizerPars& pars);
95  void free_members(void);
96  int get_index(const std::string& name) const;
97 
98  // Proteced members
99  std::vector<GOptimizerPar*> m_pars; //!< List of parameters
100  std::vector<bool> m_alloc; //!< Flags allocation
101 };
102 
103 
104 /***********************************************************************//**
105  * @brief Return class name
106  *
107  * @return String containing the class name ("GOptimizerPars").
108  ***************************************************************************/
109 inline
110 std::string GOptimizerPars::classname(void) const
111 {
112  return ("GOptimizerPars");
113 }
114 
115 
116 /***********************************************************************//**
117  * @brief Return pointer to parameter
118  *
119  * @param[in] index Parameter index [0,...,size()-1].
120  * @return Pointer to parameter.
121  *
122  * Returns a pointer to the parameter with the specified @p index.
123  ***************************************************************************/
124 inline
126 {
127  return (m_pars[index]);
128 }
129 
130 
131 /***********************************************************************//**
132  * @brief Return pointer to parameter (const version)
133  *
134  * @param[in] index Parameter index [0,...,size()-1].
135  * @return Pointer to parameter.
136  *
137  * Returns a pointer to the parameter with the specified @p index.
138  ***************************************************************************/
139 inline
140 const GOptimizerPar* GOptimizerPars::operator[](const int& index) const
141 {
142  return (m_pars[index]);
143 }
144 
145 
146 /***********************************************************************//**
147  * @brief Return number of parameters in container
148  *
149  * @return Number of parameters in container.
150  *
151  * Returns the number of parameters in the parameter container.
152  ***************************************************************************/
153 inline
154 int GOptimizerPars::size(void) const
155 {
156  return (int)m_pars.size();
157 }
158 
159 
160 /***********************************************************************//**
161  * @brief Signals if there are no parameters in container
162  *
163  * @return True if container is empty, false otherwise.
164  *
165  * Signals if the parameters container does not contain any parameter.
166  ***************************************************************************/
167 inline
168 bool GOptimizerPars::is_empty(void) const
169 {
170  return (m_pars.empty());
171 }
172 
173 
174 /***********************************************************************//**
175  * @brief Reserves space for parameters in container
176  *
177  * @param[in] num Number of parameters.
178  *
179  * Reserves space for @p num parameters in the container.
180  ***************************************************************************/
181 inline
182 void GOptimizerPars::reserve(const int& num)
183 {
184  m_pars.reserve(num);
185  return;
186 }
187 
188 #endif /* GOPTIMIZERPARS_HPP */
std::string number(const std::string &noun, const int &number)
Convert singular noun into number noun.
Definition: GTools.cpp:1046
GOptimizerPar * set(const int &index, const GOptimizerPar &par)
Set parameter in container.
std::vector< GOptimizerPar * > m_pars
List of parameters.
GOptimizerPar * insert(const int &index, const GOptimizerPar &par)
Insert parameter into container.
GOptimizerPars(void)
Void constructor.
GOptimizerPar * at(const int &index)
Return pointer to parameter.
Optimizer parameter container class.
void reserve(const int &num)
Reserves space for parameters in container.
bool is_empty(void) const
Signals if there are no parameters in container.
void clear(void)
Clear parameter container.
GOptimizerPar * operator[](const int &index)
Return pointer to parameter.
void init_members(void)
Initialise class members.
GOptimizerPars & operator=(const GOptimizerPars &pars)
Assignment operator.
GOptimizerPars * clone(void) const
Clone parameter container.
GChatter
Definition: GTypemaps.hpp:33
std::vector< bool > m_alloc
Flags allocation.
void attach(GOptimizerPar *par)
Attach parameter to container.
virtual ~GOptimizerPars(void)
Destructor.
bool contains(const std::string &name) const
Signals if parameter name exists.
void extend(const GOptimizerPars &pars)
Append parameter container.
GOptimizerPar * append(const GOptimizerPar &par)
Append parameter to container.
int get_index(const std::string &name) const
Return parameter index by name.
std::string classname(void) const
Return class name.
int nfree(void) const
Return number of free parameters.
Optimizer parameter class interface definition.
Definition of interface for container classes.
int size(void) const
Return number of parameters in container.
void free_members(void)
Delete class members.
void copy_members(const GOptimizerPars &pars)
Copy class members.
std::string print(const GChatter &chatter=NORMAL) const
Print parameters.
Interface class for container classes.
Definition: GContainer.hpp:52
Optimizer parameter class.