Commit 4252ac5efb5f59c54f31c29d320b468d4a0fd69c

Authored by Jürgen Knödlseder
1 parent d866ddac

Review GTools and GCsv class.

Some arguments that were not already passed as const references are now declared as const references. Inline methods were added to GCvs.
include/GCsv.hpp
@@ -36,32 +36,45 @@ @@ -36,32 +36,45 @@
36 /***********************************************************************//** 36 /***********************************************************************//**
37 * @class GCsv 37 * @class GCsv
38 * 38 *
39 - * @brief Column separated values table class definition 39 + * @brief Column separated values table class
  40 + *
  41 + * This class implements a table of std::string elements that is loaded
  42 + * from a column separated value ASCII file. The column separation string
  43 + * can be specified upon loading of the file (by default the class assumes
  44 + * that elements are separated by a white space).
  45 + *
  46 + * The class provides operators for string element access, and methods for
  47 + * conversion of the string values:
  48 + *
  49 + * double real = csv.real(row,col);
  50 + * int integer = csv.integer(row,col);
  51 + * std::string string = csv.string(row,col);
  52 + *
40 ***************************************************************************/ 53 ***************************************************************************/
41 class GCsv : public GBase { 54 class GCsv : public GBase {
42 55
43 public: 56 public:
44 // Constructors and destructors 57 // Constructors and destructors
45 GCsv(void); 58 GCsv(void);
46 - GCsv(const std::string& filename, std::string sep = " "); 59 + GCsv(const std::string& filename, const std::string& sep = " ");
47 GCsv(const GCsv& csv); 60 GCsv(const GCsv& csv);
48 virtual ~GCsv(void); 61 virtual ~GCsv(void);
49 62
50 // Operators 63 // Operators
51 - GCsv& operator= (const GCsv& csv);  
52 - std::string& operator() (const int& row, const int& col);  
53 - const std::string& operator() (const int& row, const int& col) const; 64 + GCsv& operator=(const GCsv& csv);
  65 + std::string& operator()(const int& row, const int& col);
  66 + const std::string& operator()(const int& row, const int& col) const;
54 67
55 // Methods 68 // Methods
56 void clear(void); 69 void clear(void);
57 GCsv* clone(void) const; 70 GCsv* clone(void) const;
  71 + int size(void) const;
  72 + const int& ncols(void) const;
  73 + const int& nrows(void) const;
58 std::string string(const int& row, const int& col) const; 74 std::string string(const int& row, const int& col) const;
59 double real(const int& row, const int& col) const; 75 double real(const int& row, const int& col) const;
60 int integer(const int& row, const int& col) const; 76 int integer(const int& row, const int& col) const;
61 - void load(const std::string& filename, std::string sep = " ");  
62 - int ncols(void) const { return m_cols; }  
63 - int nrows(void) const { return m_rows; }  
64 - int size(void) const { return m_rows*m_cols; } 77 + void load(const std::string& filename, const std::string& sep = " ");
65 std::string print(const GChatter& chatter = NORMAL) const; 78 std::string print(const GChatter& chatter = NORMAL) const;
66 79
67 protected: 80 protected:
@@ -76,4 +89,40 @@ protected: @@ -76,4 +89,40 @@ protected:
76 std::vector<std::vector<std::string> > m_data; //!< CSV table data 89 std::vector<std::vector<std::string> > m_data; //!< CSV table data
77 }; 90 };
78 91
  92 +
  93 +/***********************************************************************//**
  94 + * @brief Return table size (columns times rows)
  95 + *
  96 + * @return Table size.
  97 + ***************************************************************************/
  98 +inline
  99 +int GCsv::size(void) const
  100 +{
  101 + return m_rows*m_cols;
  102 +}
  103 +
  104 +
  105 +/***********************************************************************//**
  106 + * @brief Return number of columns
  107 + *
  108 + * @return Number of columns.
  109 + ***************************************************************************/
  110 +inline
  111 +const int& GCsv::ncols(void) const
  112 +{
  113 + return m_cols;
  114 +}
  115 +
  116 +
  117 +/***********************************************************************//**
  118 + * @brief Return number of rows
  119 + *
  120 + * @return Number of rows.
  121 + ***************************************************************************/
  122 +inline
  123 +const int& GCsv::nrows(void) const
  124 +{
  125 + return m_rows;
  126 +}
  127 +
79 #endif /* GCSV_HPP */ 128 #endif /* GCSV_HPP */
include/GTools.hpp
@@ -71,10 +71,13 @@ namespace gammalib { @@ -71,10 +71,13 @@ namespace gammalib {
71 std::string toupper(const std::string& s); 71 std::string toupper(const std::string& s);
72 std::string tolower(const std::string& s); 72 std::string tolower(const std::string& s);
73 std::vector<std::string> split(const std::string& s, const std::string& sep); 73 std::vector<std::string> split(const std::string& s, const std::string& sep);
74 - std::string fill(const std::string& s, int n);  
75 - std::string left(const std::string& s, int n, char c = ' ');  
76 - std::string right(const std::string& s, int n, char c = ' ');  
77 - std::string center(const std::string& s, int n, char c = ' '); 74 + std::string fill(const std::string& s, const int& n);
  75 + std::string left(const std::string& s, const int& n,
  76 + const char& c = ' ');
  77 + std::string right(const std::string& s, const int& n,
  78 + const char& c = ' ');
  79 + std::string center(const std::string& s, const int& n,
  80 + const char& c = ' ');
78 std::string parformat(const std::string& s, const int& indent = 0); 81 std::string parformat(const std::string& s, const int& indent = 0);
79 double plaw_photon_flux(const double& emin, 82 double plaw_photon_flux(const double& emin,
80 const double& emax, 83 const double& emax,
@@ -87,7 +90,8 @@ namespace gammalib { @@ -87,7 +90,8 @@ namespace gammalib {
87 bool file_exists(const std::string& filename); 90 bool file_exists(const std::string& filename);
88 bool isinfinite(const double& x); 91 bool isinfinite(const double& x);
89 bool isnotanumber(const double& x); 92 bool isnotanumber(const double& x);
90 - bool contains(std::string str1, std::string str2); 93 + bool contains(const std::string& str,
  94 + const std::string& substring);
91 void warning(const std::string& origin, 95 void warning(const std::string& origin,
92 const std::string& message); 96 const std::string& message);
93 } 97 }
pyext/GCsv.i
@@ -20,7 +20,7 @@ @@ -20,7 +20,7 @@
20 ***************************************************************************/ 20 ***************************************************************************/
21 /** 21 /**
22 * @file GCsv.hpp 22 * @file GCsv.hpp
23 - * @brief Column separated values table class Python bindings 23 + * @brief Column separated values table class definition
24 * @author Juergen Knoedlseder 24 * @author Juergen Knoedlseder
25 */ 25 */
26 %{ 26 %{
@@ -86,20 +86,20 @@ class GCsv : public GBase { @@ -86,20 +86,20 @@ class GCsv : public GBase {
86 public: 86 public:
87 // Constructors and destructors 87 // Constructors and destructors
88 GCsv(void); 88 GCsv(void);
89 - GCsv(const std::string& filename, std::string sep = " "); 89 + GCsv(const std::string& filename, const std::string& sep = " ");
90 GCsv(const GCsv& csv); 90 GCsv(const GCsv& csv);
91 virtual ~GCsv(void); 91 virtual ~GCsv(void);
92 92
93 // Methods 93 // Methods
94 void clear(void); 94 void clear(void);
95 GCsv* clone(void) const; 95 GCsv* clone(void) const;
  96 + int size(void) const;
  97 + const int& ncols(void) const;
  98 + const int& nrows(void) const;
96 std::string string(const int& row, const int& col) const; 99 std::string string(const int& row, const int& col) const;
97 double real(const int& row, const int& col) const; 100 double real(const int& row, const int& col) const;
98 int integer(const int& row, const int& col) const; 101 int integer(const int& row, const int& col) const;
99 - void load(const std::string& filename, std::string sep = " ");  
100 - int ncols(void) const { return m_cols; }  
101 - int nrows(void) const { return m_rows; }  
102 - int size(void) const { return m_rows*m_cols; } 102 + void load(const std::string& filename, const std::string& sep = " ");
103 }; 103 };
104 104
105 105
@@ -113,6 +113,9 @@ public: @@ -113,6 +113,9 @@ public:
113 void __setitem__(int GCsvInx[], std::string value) { 113 void __setitem__(int GCsvInx[], std::string value) {
114 (*self)(GCsvInx[0], GCsvInx[1]) = value; 114 (*self)(GCsvInx[0], GCsvInx[1]) = value;
115 } 115 }
  116 + int __len__() {
  117 + return (self->size());
  118 + }
116 GCsv copy() { 119 GCsv copy() {
117 return (*self); 120 return (*self);
118 } 121 }
src/support/GCsv.cpp
@@ -34,8 +34,8 @@ @@ -34,8 +34,8 @@
34 #include "GException.hpp" 34 #include "GException.hpp"
35 35
36 /* __ Method name definitions ____________________________________________ */ 36 /* __ Method name definitions ____________________________________________ */
37 -#define G_ACCESS "GCsv::operator() (int&, int&)"  
38 -#define G_LOAD "GCsv::load(std::string&, std::string)" 37 +#define G_ACCESS "GCsv::operator()(int&, int&)"
  38 +#define G_LOAD "GCsv::load(std::string&, std::string&)"
39 39
40 /* __ Macros _____________________________________________________________ */ 40 /* __ Macros _____________________________________________________________ */
41 41
@@ -69,7 +69,7 @@ GCsv::GCsv(void) @@ -69,7 +69,7 @@ GCsv::GCsv(void)
69 * @param[in] filename Filename. 69 * @param[in] filename Filename.
70 * @param[in] sep Column separator (default is whitespace). 70 * @param[in] sep Column separator (default is whitespace).
71 ***************************************************************************/ 71 ***************************************************************************/
72 -GCsv::GCsv(const std::string& filename, std::string sep) 72 +GCsv::GCsv(const std::string& filename, const std::string& sep)
73 { 73 {
74 // Initialise private 74 // Initialise private
75 init_members(); 75 init_members();
@@ -123,8 +123,9 @@ GCsv::~GCsv(void) @@ -123,8 +123,9 @@ GCsv::~GCsv(void)
123 * @brief Assignment operator 123 * @brief Assignment operator
124 * 124 *
125 * @param[in] csv Column separated values table. 125 * @param[in] csv Column separated values table.
  126 + * @return Column separated values table.
126 ***************************************************************************/ 127 ***************************************************************************/
127 -GCsv& GCsv::operator= (const GCsv& csv) 128 +GCsv& GCsv::operator=(const GCsv& csv)
128 { 129 {
129 // Execute only if object is not identical 130 // Execute only if object is not identical
130 if (this != &csv) { 131 if (this != &csv) {
@@ -151,7 +152,7 @@ GCsv&amp; GCsv::operator= (const GCsv&amp; csv) @@ -151,7 +152,7 @@ GCsv&amp; GCsv::operator= (const GCsv&amp; csv)
151 * @param[in] row Table row. 152 * @param[in] row Table row.
152 * @param[in] col Table column. 153 * @param[in] col Table column.
153 ***************************************************************************/ 154 ***************************************************************************/
154 -std::string& GCsv::operator() (const int& row, const int& col) 155 +std::string& GCsv::operator()(const int& row, const int& col)
155 { 156 {
156 // Perform range check 157 // Perform range check
157 #if defined(G_RANGE_CHECK) 158 #if defined(G_RANGE_CHECK)
@@ -171,7 +172,7 @@ std::string&amp; GCsv::operator() (const int&amp; row, const int&amp; col) @@ -171,7 +172,7 @@ std::string&amp; GCsv::operator() (const int&amp; row, const int&amp; col)
171 * @param[in] row Table row. 172 * @param[in] row Table row.
172 * @param[in] col Table column. 173 * @param[in] col Table column.
173 ***************************************************************************/ 174 ***************************************************************************/
174 -const std::string& GCsv::operator() (const int& row, const int& col) const 175 +const std::string& GCsv::operator()(const int& row, const int& col) const
175 { 176 {
176 // Perform range check 177 // Perform range check
177 #if defined(G_RANGE_CHECK) 178 #if defined(G_RANGE_CHECK)
@@ -192,7 +193,7 @@ const std::string&amp; GCsv::operator() (const int&amp; row, const int&amp; col) const @@ -192,7 +193,7 @@ const std::string&amp; GCsv::operator() (const int&amp; row, const int&amp; col) const
192 ==========================================================================*/ 193 ==========================================================================*/
193 194
194 /***********************************************************************//** 195 /***********************************************************************//**
195 - * @brief Clear instance 196 + * @brief Clear CSV table
196 * 197 *
197 * This method properly resets the object to an initial state. 198 * This method properly resets the object to an initial state.
198 ***************************************************************************/ 199 ***************************************************************************/
@@ -210,7 +211,9 @@ void GCsv::clear(void) @@ -210,7 +211,9 @@ void GCsv::clear(void)
210 211
211 212
212 /***********************************************************************//** 213 /***********************************************************************//**
213 - * @brief Clone instance 214 + * @brief Clone CSV table
  215 + *
  216 + * @return Pointer to deep copy of CSV table.
214 **************************************************************************/ 217 **************************************************************************/
215 GCsv* GCsv::clone(void) const 218 GCsv* GCsv::clone(void) const
216 { 219 {
@@ -283,16 +286,11 @@ int GCsv::integer(const int&amp; row, const int&amp; col) const @@ -283,16 +286,11 @@ int GCsv::integer(const int&amp; row, const int&amp; col) const
283 * Load CSV table from ASCII file. 286 * Load CSV table from ASCII file.
284 * Any environment variable present in the filename will be expanded. 287 * Any environment variable present in the filename will be expanded.
285 **************************************************************************/ 288 **************************************************************************/
286 -void GCsv::load(const std::string& filename, std::string sep) 289 +void GCsv::load(const std::string& filename, const std::string& sep)
287 { 290 {
288 // Clear instance 291 // Clear instance
289 clear(); 292 clear();
290 293
291 - // If no seperator is given then assume a whitespace  
292 - if (sep.length() == 0) {  
293 - sep = " ";  
294 - }  
295 -  
296 // Allocate line buffer 294 // Allocate line buffer
297 const int n = 10000; 295 const int n = 10000;
298 char line[n]; 296 char line[n];
@@ -317,8 +315,9 @@ void GCsv::load(const std::string&amp; filename, std::string sep) @@ -317,8 +315,9 @@ void GCsv::load(const std::string&amp; filename, std::string sep)
317 std::string sline = gammalib::strip_chars(gammalib::strip_whitespace(std::string(line)),"\n"); 315 std::string sline = gammalib::strip_chars(gammalib::strip_whitespace(std::string(line)),"\n");
318 316
319 // Skip line if empty 317 // Skip line if empty
320 - if (sline.length() == 0) 318 + if (sline.length() == 0) {
321 continue; 319 continue;
  320 + }
322 321
323 // Split line in elements 322 // Split line in elements
324 std::vector<std::string> elements = gammalib::split(sline, sep); 323 std::vector<std::string> elements = gammalib::split(sline, sep);
@@ -433,10 +432,3 @@ void GCsv::free_members(void) @@ -433,10 +432,3 @@ void GCsv::free_members(void)
433 // Return 432 // Return
434 return; 433 return;
435 } 434 }
436 -  
437 -  
438 -/*==========================================================================  
439 - = =  
440 - = Friends =  
441 - = =  
442 - ==========================================================================*/  
src/support/GTools.cpp
@@ -244,7 +244,7 @@ std::string gammalib::str(const unsigned short int&amp; value) @@ -244,7 +244,7 @@ std::string gammalib::str(const unsigned short int&amp; value)
244 { 244 {
245 std::ostringstream s_value; 245 std::ostringstream s_value;
246 s_value << value; 246 s_value << value;
247 - return s_value.str(); 247 + return s_value.str();
248 } 248 }
249 249
250 250
@@ -257,7 +257,7 @@ std::string gammalib::str(const unsigned int&amp; value) @@ -257,7 +257,7 @@ std::string gammalib::str(const unsigned int&amp; value)
257 { 257 {
258 std::ostringstream s_value; 258 std::ostringstream s_value;
259 s_value << value; 259 s_value << value;
260 - return s_value.str(); 260 + return s_value.str();
261 } 261 }
262 262
263 263
@@ -270,7 +270,7 @@ std::string gammalib::str(const unsigned long int&amp; value) @@ -270,7 +270,7 @@ std::string gammalib::str(const unsigned long int&amp; value)
270 { 270 {
271 std::ostringstream s_value; 271 std::ostringstream s_value;
272 s_value << value; 272 s_value << value;
273 - return s_value.str(); 273 + return s_value.str();
274 } 274 }
275 275
276 276
@@ -283,7 +283,7 @@ std::string gammalib::str(const unsigned long long int&amp; value) @@ -283,7 +283,7 @@ std::string gammalib::str(const unsigned long long int&amp; value)
283 { 283 {
284 std::ostringstream s_value; 284 std::ostringstream s_value;
285 s_value << value; 285 s_value << value;
286 - return s_value.str(); 286 + return s_value.str();
287 } 287 }
288 288
289 289
@@ -296,7 +296,7 @@ std::string gammalib::str(const short int&amp; value) @@ -296,7 +296,7 @@ std::string gammalib::str(const short int&amp; value)
296 { 296 {
297 std::ostringstream s_value; 297 std::ostringstream s_value;
298 s_value << value; 298 s_value << value;
299 - return s_value.str(); 299 + return s_value.str();
300 } 300 }
301 301
302 302
@@ -309,7 +309,7 @@ std::string gammalib::str(const int&amp; value) @@ -309,7 +309,7 @@ std::string gammalib::str(const int&amp; value)
309 { 309 {
310 std::ostringstream s_value; 310 std::ostringstream s_value;
311 s_value << value; 311 s_value << value;
312 - return s_value.str(); 312 + return s_value.str();
313 } 313 }
314 314
315 315
@@ -322,7 +322,7 @@ std::string gammalib::str(const long int&amp; value) @@ -322,7 +322,7 @@ std::string gammalib::str(const long int&amp; value)
322 { 322 {
323 std::ostringstream s_value; 323 std::ostringstream s_value;
324 s_value << value; 324 s_value << value;
325 - return s_value.str(); 325 + return s_value.str();
326 } 326 }
327 327
328 328
@@ -335,7 +335,7 @@ std::string gammalib::str(const long long int&amp; value) @@ -335,7 +335,7 @@ std::string gammalib::str(const long long int&amp; value)
335 { 335 {
336 std::ostringstream s_value; 336 std::ostringstream s_value;
337 s_value << value; 337 s_value << value;
338 - return s_value.str(); 338 + return s_value.str();
339 } 339 }
340 340
341 341
@@ -348,7 +348,7 @@ std::string gammalib::str(const float&amp; value) @@ -348,7 +348,7 @@ std::string gammalib::str(const float&amp; value)
348 { 348 {
349 std::ostringstream s_value; 349 std::ostringstream s_value;
350 s_value << value; 350 s_value << value;
351 - return s_value.str(); 351 + return s_value.str();
352 } 352 }
353 353
354 354
@@ -361,7 +361,7 @@ std::string gammalib::str(const double&amp; value) @@ -361,7 +361,7 @@ std::string gammalib::str(const double&amp; value)
361 { 361 {
362 std::ostringstream s_value; 362 std::ostringstream s_value;
363 s_value << value; 363 s_value << value;
364 - return s_value.str(); 364 + return s_value.str();
365 } 365 }
366 366
367 367
@@ -598,7 +598,7 @@ std::vector&lt;std::string&gt; gammalib::split(const std::string&amp; s, @@ -598,7 +598,7 @@ std::vector&lt;std::string&gt; gammalib::split(const std::string&amp; s,
598 * 598 *
599 * Replicates a given string n time. 599 * Replicates a given string n time.
600 ***************************************************************************/ 600 ***************************************************************************/
601 -std::string gammalib::fill(const std::string& s, int n) 601 +std::string gammalib::fill(const std::string& s, const int& n)
602 { 602 {
603 // Initialise result 603 // Initialise result
604 std::string result = ""; 604 std::string result = "";
@@ -623,7 +623,7 @@ std::string gammalib::fill(const std::string&amp; s, int n) @@ -623,7 +623,7 @@ std::string gammalib::fill(const std::string&amp; s, int n)
623 * Left justify string by adding whitespace to the right to achieve a length 623 * Left justify string by adding whitespace to the right to achieve a length
624 * of n characters. 624 * of n characters.
625 ***************************************************************************/ 625 ***************************************************************************/
626 -std::string gammalib::left(const std::string& s, int n, char c) 626 +std::string gammalib::left(const std::string& s, const int& n, const char& c)
627 { 627 {
628 // Compute number of characters to fill right 628 // Compute number of characters to fill right
629 int n_right = n - s.length(); 629 int n_right = n - s.length();
@@ -646,7 +646,7 @@ std::string gammalib::left(const std::string&amp; s, int n, char c) @@ -646,7 +646,7 @@ std::string gammalib::left(const std::string&amp; s, int n, char c)
646 * Right justify string by adding whitespace to the left to achieve a length 646 * Right justify string by adding whitespace to the left to achieve a length
647 * of n characters. 647 * of n characters.
648 ***************************************************************************/ 648 ***************************************************************************/
649 -std::string gammalib::right(const std::string& s, int n, char c) 649 +std::string gammalib::right(const std::string& s, const int& n, const char& c)
650 { 650 {
651 // Compute number of characters to fill right 651 // Compute number of characters to fill right
652 int n_left = n - s.length(); 652 int n_left = n - s.length();
@@ -669,7 +669,7 @@ std::string gammalib::right(const std::string&amp; s, int n, char c) @@ -669,7 +669,7 @@ std::string gammalib::right(const std::string&amp; s, int n, char c)
669 * Center string by adding whitespace to the left and the right to achieve a 669 * Center string by adding whitespace to the left and the right to achieve a
670 * length of n characters. 670 * length of n characters.
671 ***************************************************************************/ 671 ***************************************************************************/
672 -std::string gammalib::center(const std::string& s, int n, char c) 672 +std::string gammalib::center(const std::string& s, const int& n, const char& c)
673 { 673 {
674 // Compute number of characters to fill left and right 674 // Compute number of characters to fill left and right
675 int n_right = (n-s.length()) / 2; 675 int n_right = (n-s.length()) / 2;
@@ -833,7 +833,7 @@ bool gammalib::file_exists(const std::string&amp; filename) @@ -833,7 +833,7 @@ bool gammalib::file_exists(const std::string&amp; filename)
833 * 833 *
834 * Checks if substring is contained in str 834 * Checks if substring is contained in str
835 ***************************************************************************/ 835 ***************************************************************************/
836 -bool gammalib::contains(std::string str, std::string substring) 836 +bool gammalib::contains(const std::string& str, const std::string& substring)
837 { 837 {
838 // Initialise result 838 // Initialise result
839 bool result = false; 839 bool result = false;