Commit 4252ac5efb5f59c54f31c29d320b468d4a0fd69c
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.
Showing
5 changed files
with
105 additions
and
57 deletions
include/GCsv.hpp
| ... | ... | @@ -36,32 +36,45 @@ |
| 36 | 36 | /***********************************************************************//** |
| 37 | 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 | 54 | class GCsv : public GBase { |
| 42 | 55 | |
| 43 | 56 | public: |
| 44 | 57 | // Constructors and destructors |
| 45 | 58 | GCsv(void); |
| 46 | - GCsv(const std::string& filename, std::string sep = " "); | |
| 59 | + GCsv(const std::string& filename, const std::string& sep = " "); | |
| 47 | 60 | GCsv(const GCsv& csv); |
| 48 | 61 | virtual ~GCsv(void); |
| 49 | 62 | |
| 50 | 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 | 68 | // Methods |
| 56 | 69 | void clear(void); |
| 57 | 70 | GCsv* clone(void) const; |
| 71 | + int size(void) const; | |
| 72 | + const int& ncols(void) const; | |
| 73 | + const int& nrows(void) const; | |
| 58 | 74 | std::string string(const int& row, const int& col) const; |
| 59 | 75 | double real(const int& row, const int& col) const; |
| 60 | 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 | 78 | std::string print(const GChatter& chatter = NORMAL) const; |
| 66 | 79 | |
| 67 | 80 | protected: |
| ... | ... | @@ -76,4 +89,40 @@ protected: |
| 76 | 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 | 128 | #endif /* GCSV_HPP */ | ... | ... |
include/GTools.hpp
| ... | ... | @@ -71,10 +71,13 @@ namespace gammalib { |
| 71 | 71 | std::string toupper(const std::string& s); |
| 72 | 72 | std::string tolower(const std::string& s); |
| 73 | 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 | 81 | std::string parformat(const std::string& s, const int& indent = 0); |
| 79 | 82 | double plaw_photon_flux(const double& emin, |
| 80 | 83 | const double& emax, |
| ... | ... | @@ -87,7 +90,8 @@ namespace gammalib { |
| 87 | 90 | bool file_exists(const std::string& filename); |
| 88 | 91 | bool isinfinite(const double& x); |
| 89 | 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 | 95 | void warning(const std::string& origin, |
| 92 | 96 | const std::string& message); |
| 93 | 97 | } | ... | ... |
pyext/GCsv.i
| ... | ... | @@ -20,7 +20,7 @@ |
| 20 | 20 | ***************************************************************************/ |
| 21 | 21 | /** |
| 22 | 22 | * @file GCsv.hpp |
| 23 | - * @brief Column separated values table class Python bindings | |
| 23 | + * @brief Column separated values table class definition | |
| 24 | 24 | * @author Juergen Knoedlseder |
| 25 | 25 | */ |
| 26 | 26 | %{ |
| ... | ... | @@ -86,20 +86,20 @@ class GCsv : public GBase { |
| 86 | 86 | public: |
| 87 | 87 | // Constructors and destructors |
| 88 | 88 | GCsv(void); |
| 89 | - GCsv(const std::string& filename, std::string sep = " "); | |
| 89 | + GCsv(const std::string& filename, const std::string& sep = " "); | |
| 90 | 90 | GCsv(const GCsv& csv); |
| 91 | 91 | virtual ~GCsv(void); |
| 92 | 92 | |
| 93 | 93 | // Methods |
| 94 | 94 | void clear(void); |
| 95 | 95 | GCsv* clone(void) const; |
| 96 | + int size(void) const; | |
| 97 | + const int& ncols(void) const; | |
| 98 | + const int& nrows(void) const; | |
| 96 | 99 | std::string string(const int& row, const int& col) const; |
| 97 | 100 | double real(const int& row, const int& col) const; |
| 98 | 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 | 113 | void __setitem__(int GCsvInx[], std::string value) { |
| 114 | 114 | (*self)(GCsvInx[0], GCsvInx[1]) = value; |
| 115 | 115 | } |
| 116 | + int __len__() { | |
| 117 | + return (self->size()); | |
| 118 | + } | |
| 116 | 119 | GCsv copy() { |
| 117 | 120 | return (*self); |
| 118 | 121 | } | ... | ... |
src/support/GCsv.cpp
| ... | ... | @@ -34,8 +34,8 @@ |
| 34 | 34 | #include "GException.hpp" |
| 35 | 35 | |
| 36 | 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 | 40 | /* __ Macros _____________________________________________________________ */ |
| 41 | 41 | |
| ... | ... | @@ -69,7 +69,7 @@ GCsv::GCsv(void) |
| 69 | 69 | * @param[in] filename Filename. |
| 70 | 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 | 74 | // Initialise private |
| 75 | 75 | init_members(); |
| ... | ... | @@ -123,8 +123,9 @@ GCsv::~GCsv(void) |
| 123 | 123 | * @brief Assignment operator |
| 124 | 124 | * |
| 125 | 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 | 130 | // Execute only if object is not identical |
| 130 | 131 | if (this != &csv) { |
| ... | ... | @@ -151,7 +152,7 @@ GCsv& GCsv::operator= (const GCsv& csv) |
| 151 | 152 | * @param[in] row Table row. |
| 152 | 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 | 157 | // Perform range check |
| 157 | 158 | #if defined(G_RANGE_CHECK) |
| ... | ... | @@ -171,7 +172,7 @@ std::string& GCsv::operator() (const int& row, const int& col) |
| 171 | 172 | * @param[in] row Table row. |
| 172 | 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 | 177 | // Perform range check |
| 177 | 178 | #if defined(G_RANGE_CHECK) |
| ... | ... | @@ -192,7 +193,7 @@ const std::string& GCsv::operator() (const int& row, const int& col) const |
| 192 | 193 | ==========================================================================*/ |
| 193 | 194 | |
| 194 | 195 | /***********************************************************************//** |
| 195 | - * @brief Clear instance | |
| 196 | + * @brief Clear CSV table | |
| 196 | 197 | * |
| 197 | 198 | * This method properly resets the object to an initial state. |
| 198 | 199 | ***************************************************************************/ |
| ... | ... | @@ -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 | 218 | GCsv* GCsv::clone(void) const |
| 216 | 219 | { |
| ... | ... | @@ -283,16 +286,11 @@ int GCsv::integer(const int& row, const int& col) const |
| 283 | 286 | * Load CSV table from ASCII file. |
| 284 | 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 | 291 | // Clear instance |
| 289 | 292 | clear(); |
| 290 | 293 | |
| 291 | - // If no seperator is given then assume a whitespace | |
| 292 | - if (sep.length() == 0) { | |
| 293 | - sep = " "; | |
| 294 | - } | |
| 295 | - | |
| 296 | 294 | // Allocate line buffer |
| 297 | 295 | const int n = 10000; |
| 298 | 296 | char line[n]; |
| ... | ... | @@ -317,8 +315,9 @@ void GCsv::load(const std::string& filename, std::string sep) |
| 317 | 315 | std::string sline = gammalib::strip_chars(gammalib::strip_whitespace(std::string(line)),"\n"); |
| 318 | 316 | |
| 319 | 317 | // Skip line if empty |
| 320 | - if (sline.length() == 0) | |
| 318 | + if (sline.length() == 0) { | |
| 321 | 319 | continue; |
| 320 | + } | |
| 322 | 321 | |
| 323 | 322 | // Split line in elements |
| 324 | 323 | std::vector<std::string> elements = gammalib::split(sline, sep); |
| ... | ... | @@ -433,10 +432,3 @@ void GCsv::free_members(void) |
| 433 | 432 | // Return |
| 434 | 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& value) |
| 244 | 244 | { |
| 245 | 245 | std::ostringstream s_value; |
| 246 | 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& value) |
| 257 | 257 | { |
| 258 | 258 | std::ostringstream s_value; |
| 259 | 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& value) |
| 270 | 270 | { |
| 271 | 271 | std::ostringstream s_value; |
| 272 | 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& value) |
| 283 | 283 | { |
| 284 | 284 | std::ostringstream s_value; |
| 285 | 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& value) |
| 296 | 296 | { |
| 297 | 297 | std::ostringstream s_value; |
| 298 | 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& value) |
| 309 | 309 | { |
| 310 | 310 | std::ostringstream s_value; |
| 311 | 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& value) |
| 322 | 322 | { |
| 323 | 323 | std::ostringstream s_value; |
| 324 | 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& value) |
| 335 | 335 | { |
| 336 | 336 | std::ostringstream s_value; |
| 337 | 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& value) |
| 348 | 348 | { |
| 349 | 349 | std::ostringstream s_value; |
| 350 | 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& value) |
| 361 | 361 | { |
| 362 | 362 | std::ostringstream s_value; |
| 363 | 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<std::string> gammalib::split(const std::string& s, |
| 598 | 598 | * |
| 599 | 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 | 603 | // Initialise result |
| 604 | 604 | std::string result = ""; |
| ... | ... | @@ -623,7 +623,7 @@ std::string gammalib::fill(const std::string& s, int n) |
| 623 | 623 | * Left justify string by adding whitespace to the right to achieve a length |
| 624 | 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 | 628 | // Compute number of characters to fill right |
| 629 | 629 | int n_right = n - s.length(); |
| ... | ... | @@ -646,7 +646,7 @@ std::string gammalib::left(const std::string& s, int n, char c) |
| 646 | 646 | * Right justify string by adding whitespace to the left to achieve a length |
| 647 | 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 | 651 | // Compute number of characters to fill right |
| 652 | 652 | int n_left = n - s.length(); |
| ... | ... | @@ -669,7 +669,7 @@ std::string gammalib::right(const std::string& s, int n, char c) |
| 669 | 669 | * Center string by adding whitespace to the left and the right to achieve a |
| 670 | 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 | 674 | // Compute number of characters to fill left and right |
| 675 | 675 | int n_right = (n-s.length()) / 2; |
| ... | ... | @@ -833,7 +833,7 @@ bool gammalib::file_exists(const std::string& filename) |
| 833 | 833 | * |
| 834 | 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 | 838 | // Initialise result |
| 839 | 839 | bool result = false; | ... | ... |