GammaLib  1.7.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-2013 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 Index of time [0,...,size()-1]
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, index, 0, size()-1);
144  }
145  #endif
146 
147  // Return reference
148  return m_times[index];
149 }
150 
151 
152 /***********************************************************************//**
153  * @brief Return reference to time (const version)
154  *
155  * @param[in] index Index of time [0,...,size()-1]
156  *
157  * @exception GException::out_of_range
158  * Time index is out of range.
159  ***************************************************************************/
160 const GTime& GTimes::operator[](const int& index) const
161 {
162  // If index is outside boundary then throw an error
163  #if defined(G_RANGE_CHECK)
164  if (index < 0 || index >= size()) {
165  throw GException::out_of_range(G_OP_ACCESS, index, 0, size()-1);
166  }
167  #endif
168 
169  // Return reference
170  return m_times[index];
171 }
172 
173 
174 /*==========================================================================
175  = =
176  = Public methods =
177  = =
178  ==========================================================================*/
179 
180 /***********************************************************************//**
181  * @brief Clear container
182  ***************************************************************************/
183 void GTimes::clear(void)
184 {
185  // Free members
186  free_members();
187 
188  // Initialise members
189  init_members();
190 
191  // Return
192  return;
193 }
194 
195 
196 /***********************************************************************//**
197  * @brief Clone object
198  *
199  * @return Pointer to deep copy of time container.
200  ***************************************************************************/
201 GTimes* GTimes::clone(void) const
202 {
203  return new GTimes(*this);
204 }
205 
206 
207 /***********************************************************************//**
208  * @brief Append time to container
209  *
210  * @param[in] time Time.
211  *
212  * This method appends a time to the container by copying it.
213  ***************************************************************************/
214 void GTimes::append(const GTime& time)
215 {
216  // Append time to list
217  m_times.push_back(time);
218 
219  // Return
220  return;
221 }
222 
223 
224 /***********************************************************************//**
225  * @brief Insert time into container
226  *
227  * @param[in] index Time index (0,...,size()-1).
228  * @param[in] time Time.
229  *
230  * @exception GException::out_of_range
231  * Time index is out of range.
232  *
233  * Inserts a @p time into the container before the time with the specified
234  * @p index.
235  ***************************************************************************/
236 void GTimes::insert(const int& index, const GTime& time)
237 {
238  // Compile option: raise exception if index is out of range
239  #if defined(G_RANGE_CHECK)
240  if (is_empty()) {
241  if (index > 0) {
242  throw GException::out_of_range(G_INSERT, index, 0, size()-1);
243  }
244  }
245  else {
246  if (index < 0 || index >= size()) {
247  throw GException::out_of_range(G_INSERT, index, 0, size()-1);
248  }
249  }
250  #endif
251 
252  // Inserts time
253  m_times.insert(m_times.begin()+index, time);
254 
255  // Return
256  return;
257 }
258 
259 
260 /***********************************************************************//**
261  * @brief Remove time from container
262  *
263  * @param[in] index Time index (0,...,size()-1).
264  *
265  * @exception GException::out_of_range
266  * Time index is out of range.
267  *
268  * Remove time of specified @p index from container.
269  ***************************************************************************/
270 void GTimes::remove(const int& index)
271 {
272  // Compile option: raise exception if index is out of range
273  #if defined(G_RANGE_CHECK)
274  if (index < 0 || index >= size()) {
275  throw GException::out_of_range(G_REMOVE, index, 0, size()-1);
276  }
277  #endif
278 
279  // Erase time from container
280  m_times.erase(m_times.begin() + index);
281 
282  // Return
283  return;
284 }
285 
286 
287 /***********************************************************************//**
288  * @brief Append time container
289  *
290  * @param[in] times Time container.
291  *
292  * Append time container to the container.
293  ***************************************************************************/
294 void GTimes::extend(const GTimes& times)
295 {
296  // Do nothing if time container is empty
297  if (!times.is_empty()) {
298 
299  // Get size. Note that we extract the size first to avoid an
300  // endless loop that arises when a container is appended to
301  // itself.
302  int num = times.size();
303 
304  // Reserve enough space
305  reserve(size() + num);
306 
307  // Loop over all elements and append them to container
308  for (int i = 0; i < num; ++i) {
309  m_times.push_back(times[i]);
310  }
311 
312  } // endif: time container was not empty
313 
314  // Return
315  return;
316 }
317 
318 
319 /***********************************************************************//**
320  * @brief Reserve memory for times in container
321  *
322  * @param[in] num Number of times.
323  *
324  * This method reserves memory for @p num times in the container.
325  ***************************************************************************/
326 void GTimes::reserve(const int& num)
327 {
328  // Reserve memory
329  m_times.reserve(num);
330 
331  // Return
332  return;
333 }
334 
335 
336 /***********************************************************************//**
337  * @brief Print time container information
338  *
339  * @param[in] chatter Chattiness (defaults to NORMAL).
340  * @return String containing time container information.
341  ***************************************************************************/
342 std::string GTimes::print(const GChatter& chatter) const
343 {
344  // Initialise result string
345  std::string result;
346 
347  // Continue only if chatter is not silent
348  if (chatter != SILENT) {
349 
350  // Append header
351  result.append("=== GTimes ===");
352 
353  // Append time container information
354  result.append("\n"+gammalib::parformat("Number of times"));
355  result.append(gammalib::str(size()));
356 
357  // EXPLICIT: Append times
358  if (chatter >= EXPLICIT) {
359  for (int i = 0; i < size(); ++i) {
360  result.append("\n"+m_times[i].print(chatter));
361  }
362  }
363 
364  } // endif: chatter was not silent
365 
366  // Return result
367  return result;
368 }
369 
370 
371 /*==========================================================================
372  = =
373  = Private methods =
374  = =
375  ==========================================================================*/
376 
377 /***********************************************************************//**
378  * @brief Initialise class members
379  ***************************************************************************/
381 {
382  // Initialise members
383  m_times.clear();
384 
385  // Return
386  return;
387 }
388 
389 
390 /***********************************************************************//**
391  * @brief Copy class members
392  *
393  * @param[in] times Time container.
394  ***************************************************************************/
395 void GTimes::copy_members(const GTimes& times)
396 {
397  // Copy attributes
398  m_times = times.m_times;
399 
400  // Return
401  return;
402 }
403 
404 
405 /***********************************************************************//**
406  * @brief Delete class members
407  ***************************************************************************/
409 {
410  // Return
411  return;
412 }
#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:54
Time container class definition.
Gammalib tools definition.
void extend(const GTimes &times)
Append time container.
Definition: GTimes.cpp:294
Time container class.
Definition: GTimes.hpp:45
#define G_INSERT
Definition: GTimes.cpp:37
void init_members(void)
Initialise class members.
Definition: GTimes.cpp:380
void free_members(void)
Delete class members.
Definition: GTimes.cpp:408
GTime & operator[](const int &index)
Return reference to time.
Definition: GTimes.cpp:138
void clear(void)
Clear container.
Definition: GTimes.cpp:183
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:395
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:326
void remove(const int &index)
Remove time from container.
Definition: GTimes.cpp:270
GTimes * clone(void) const
Clone object.
Definition: GTimes.cpp:201
std::string parformat(const std::string &s, const int &indent=0)
Convert string in parameter format.
Definition: GTools.cpp:1022
GTimes(void)
Void constructor.
Definition: GTimes.cpp:56
void insert(const int &index, const GTime &time)
Insert time into container.
Definition: GTimes.cpp:236
std::string print(const GChatter &chatter=NORMAL) const
Print time container information.
Definition: GTimes.cpp:342
void append(const GTime &time)
Append time to container.
Definition: GTimes.cpp:214
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:413