GammaLib  2.1.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GTimes.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GTimes.cpp - Time container class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2012-2021 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 GTimes.cpp
23  * @brief Time container class implementation
24  * @author Juergen Knoedlseder
25  */
26 
27 /* __ Includes ___________________________________________________________ */
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 #include "GTools.hpp"
32 #include "GException.hpp"
33 #include "GTimes.hpp"
34 
35 /* __ Method name definitions ____________________________________________ */
36 #define G_OP_ACCESS "GTimes::operator[](int&)"
37 #define G_INSERT "GTimes::insert(int&, GTime&)"
38 #define G_REMOVE "GTimes::remove(int&)"
39 
40 /* __ Macros _____________________________________________________________ */
41 
42 /* __ Coding definitions _________________________________________________ */
43 
44 /* __ Debug definitions __________________________________________________ */
45 
46 
47 /*==========================================================================
48  = =
49  = Constructors/destructors =
50  = =
51  ==========================================================================*/
52 
53 /***********************************************************************//**
54  * @brief Void constructor
55  ***************************************************************************/
57 {
58  // Initialise members
59  init_members();
60 
61  // Return
62  return;
63 }
64 
65 
66 /***********************************************************************//**
67  * @brief Copy constructor
68  *
69  * @param times Photon container.
70  ***************************************************************************/
71 GTimes::GTimes(const GTimes& times)
72 {
73  // Initialise members
74  init_members();
75 
76  // Copy members
77  copy_members(times);
78 
79  // Return
80  return;
81 }
82 
83 
84 /***********************************************************************//**
85  * @brief Destructor
86  ***************************************************************************/
88 {
89  // Free members
90  free_members();
91 
92  // Return
93  return;
94 }
95 
96 
97 /*==========================================================================
98  = =
99  = Operators =
100  = =
101  ==========================================================================*/
102 
103 /***********************************************************************//**
104  * @brief Assignment operator
105  *
106  * @param[in] times Time container.
107  * @return Time container.
108  ***************************************************************************/
110 {
111  // Execute only if object is not identical
112  if (this != &times) {
113 
114  // Free members
115  free_members();
116 
117  // Initialise members
118  init_members();
119 
120  // Copy members
121  copy_members(times);
122 
123  } // endif: object was not identical
124 
125  // Return this object
126  return *this;
127 }
128 
129 
130 /***********************************************************************//**
131  * @brief Return reference to time
132  *
133  * @param[in] index Time index [0,...,size()[.
134  *
135  * @exception GException::out_of_range
136  * Time index is out of range.
137  ***************************************************************************/
138 GTime& GTimes::operator[](const int& index)
139 {
140  // If index is outside boundary then throw an error
141  #if defined(G_RANGE_CHECK)
142  if (index < 0 || index >= size()) {
143  throw GException::out_of_range(G_OP_ACCESS, "Time index",
144  index, size());
145  }
146  #endif
147 
148  // Return reference
149  return m_times[index];
150 }
151 
152 
153 /***********************************************************************//**
154  * @brief Return reference to time (const version)
155  *
156  * @param[in] index Time index [0,...,size()[.
157  *
158  * @exception GException::out_of_range
159  * Time index is out of range.
160  ***************************************************************************/
161 const GTime& GTimes::operator[](const int& index) const
162 {
163  // If index is outside boundary then throw an error
164  #if defined(G_RANGE_CHECK)
165  if (index < 0 || index >= size()) {
166  throw GException::out_of_range(G_OP_ACCESS, "Time index",
167  index, size());
168  }
169  #endif
170 
171  // Return reference
172  return m_times[index];
173 }
174 
175 
176 /*==========================================================================
177  = =
178  = Public methods =
179  = =
180  ==========================================================================*/
181 
182 /***********************************************************************//**
183  * @brief Clear container
184  ***************************************************************************/
185 void GTimes::clear(void)
186 {
187  // Free members
188  free_members();
189 
190  // Initialise members
191  init_members();
192 
193  // Return
194  return;
195 }
196 
197 
198 /***********************************************************************//**
199  * @brief Clone object
200  *
201  * @return Pointer to deep copy of time container.
202  ***************************************************************************/
203 GTimes* GTimes::clone(void) const
204 {
205  return new GTimes(*this);
206 }
207 
208 
209 /***********************************************************************//**
210  * @brief Append time to container
211  *
212  * @param[in] time Time.
213  *
214  * This method appends a time to the container by copying it.
215  ***************************************************************************/
216 void GTimes::append(const GTime& time)
217 {
218  // Append time to list
219  m_times.push_back(time);
220 
221  // Return
222  return;
223 }
224 
225 
226 /***********************************************************************//**
227  * @brief Insert time into container
228  *
229  * @param[in] index Time index [0,...,size()[.
230  * @param[in] time Time.
231  *
232  * @exception GException::out_of_range
233  * Time index is out of range.
234  *
235  * Inserts a @p time into the container before the time with the specified
236  * @p index.
237  ***************************************************************************/
238 void GTimes::insert(const int& index, const GTime& time)
239 {
240  // Compile option: raise exception if index is out of range
241  #if defined(G_RANGE_CHECK)
242  if (is_empty()) {
243  if (index > 0) {
244  throw GException::out_of_range(G_INSERT, "Time index",
245  index, size());
246  }
247  }
248  else {
249  if (index < 0 || index >= size()) {
250  throw GException::out_of_range(G_INSERT, "Time index",
251  index, size());
252  }
253  }
254  #endif
255 
256  // Inserts time
257  m_times.insert(m_times.begin()+index, time);
258 
259  // Return
260  return;
261 }
262 
263 
264 /***********************************************************************//**
265  * @brief Remove time from container
266  *
267  * @param[in] index Time index [0,...,size()[.
268  *
269  * @exception GException::out_of_range
270  * Time index is out of range.
271  *
272  * Remove time of specified @p index from container.
273  ***************************************************************************/
274 void GTimes::remove(const int& index)
275 {
276  // Compile option: raise exception if index is out of range
277  #if defined(G_RANGE_CHECK)
278  if (index < 0 || index >= size()) {
279  throw GException::out_of_range(G_REMOVE, "Time index",
280  index, size());
281  }
282  #endif
283 
284  // Erase time from container
285  m_times.erase(m_times.begin() + index);
286 
287  // Return
288  return;
289 }
290 
291 
292 /***********************************************************************//**
293  * @brief Append time container
294  *
295  * @param[in] times Time container.
296  *
297  * Append time container to the container.
298  ***************************************************************************/
299 void GTimes::extend(const GTimes& times)
300 {
301  // Do nothing if time container is empty
302  if (!times.is_empty()) {
303 
304  // Get size. Note that we extract the size first to avoid an
305  // endless loop that arises when a container is appended to
306  // itself.
307  int num = times.size();
308 
309  // Reserve enough space
310  reserve(size() + num);
311 
312  // Loop over all elements and append them to container
313  for (int i = 0; i < num; ++i) {
314  m_times.push_back(times[i]);
315  }
316 
317  } // endif: time container was not empty
318 
319  // Return
320  return;
321 }
322 
323 
324 /***********************************************************************//**
325  * @brief Reserve memory for times in container
326  *
327  * @param[in] num Number of times.
328  *
329  * This method reserves memory for @p num times in the container.
330  ***************************************************************************/
331 void GTimes::reserve(const int& num)
332 {
333  // Reserve memory
334  m_times.reserve(num);
335 
336  // Return
337  return;
338 }
339 
340 
341 /***********************************************************************//**
342  * @brief Print time container information
343  *
344  * @param[in] chatter Chattiness.
345  * @return String containing time container information.
346  ***************************************************************************/
347 std::string GTimes::print(const GChatter& chatter) const
348 {
349  // Initialise result string
350  std::string result;
351 
352  // Continue only if chatter is not silent
353  if (chatter != SILENT) {
354 
355  // Append header
356  result.append("=== GTimes ===");
357 
358  // Append time container information
359  result.append("\n"+gammalib::parformat("Number of times"));
360  result.append(gammalib::str(size()));
361 
362  // EXPLICIT: Append times
363  if (chatter >= EXPLICIT) {
364  for (int i = 0; i < size(); ++i) {
365  result.append("\n"+m_times[i].print(chatter));
366  }
367  }
368 
369  } // endif: chatter was not silent
370 
371  // Return result
372  return result;
373 }
374 
375 
376 /*==========================================================================
377  = =
378  = Private methods =
379  = =
380  ==========================================================================*/
381 
382 /***********************************************************************//**
383  * @brief Initialise class members
384  ***************************************************************************/
386 {
387  // Initialise members
388  m_times.clear();
389 
390  // Return
391  return;
392 }
393 
394 
395 /***********************************************************************//**
396  * @brief Copy class members
397  *
398  * @param[in] times Time container.
399  ***************************************************************************/
400 void GTimes::copy_members(const GTimes& times)
401 {
402  // Copy attributes
403  m_times = times.m_times;
404 
405  // Return
406  return;
407 }
408 
409 
410 /***********************************************************************//**
411  * @brief Delete class members
412  ***************************************************************************/
414 {
415  // Return
416  return;
417 }
#define G_OP_ACCESS
Definition: GTimes.cpp:36
int size(void) const
Return number of times.
Definition: GTimes.hpp:100
#define G_REMOVE
Definition: GTimes.cpp:38
Time class.
Definition: GTime.hpp:55
Time container class definition.
Gammalib tools definition.
void extend(const GTimes &times)
Append time container.
Definition: GTimes.cpp:299
Time container class.
Definition: GTimes.hpp:45
#define G_INSERT
Definition: GTimes.cpp:37
void init_members(void)
Initialise class members.
Definition: GTimes.cpp:385
void free_members(void)
Delete class members.
Definition: GTimes.cpp:413
GTime & operator[](const int &index)
Return reference to time.
Definition: GTimes.cpp:138
void clear(void)
Clear container.
Definition: GTimes.cpp:185
GChatter
Definition: GTypemaps.hpp:33
GTimes & operator=(const GTimes &times)
Assignment operator.
Definition: GTimes.cpp:109
void copy_members(const GTimes &times)
Copy class members.
Definition: GTimes.cpp:400
virtual ~GTimes(void)
Destructor.
Definition: GTimes.cpp:87
Exception handler interface definition.
void reserve(const int &num)
Reserve memory for times in container.
Definition: GTimes.cpp:331
void remove(const int &index)
Remove time from container.
Definition: GTimes.cpp:274
GTimes * clone(void) const
Clone object.
Definition: GTimes.cpp:203
std::string parformat(const std::string &s, const int &indent=0)
Convert string in parameter format.
Definition: GTools.cpp:1143
GTimes(void)
Void constructor.
Definition: GTimes.cpp:56
void insert(const int &index, const GTime &time)
Insert time into container.
Definition: GTimes.cpp:238
std::string print(const GChatter &chatter=NORMAL) const
Print time container information.
Definition: GTimes.cpp:347
void append(const GTime &time)
Append time to container.
Definition: GTimes.cpp:216
std::vector< GTime > m_times
List of times.
Definition: GTimes.hpp:78
bool is_empty(void) const
Signal if there are no times.
Definition: GTimes.hpp:112
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
Definition: GTools.cpp:489