Changeset 1928


Ignore:
Timestamp:
Apr 30, 2009, 6:48:07 PM (12 years ago)
Author:
Peter
Message:

refs #510 - allow function 'load' to ignore empty lines and check that columns contains equally many columns. remove corresponding code in Matrix.

Location:
trunk/yat/utility
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/yat/utility/Matrix.cc

    r1927 r1928  
    7878    std::vector<std::vector<double> > data_matrix;
    7979    try {
    80       load(is, data_matrix, sep);
     80      load(is, data_matrix, sep, '\n', true);
     81    }
     82    catch (utility::IO_error& e) {
     83      std::stringstream ss(e.what());
     84      ss << "\nMatrix(std::istream&): invalid dimensions\n";
     85      throw IO_error(ss.str());
    8186    }
    8287    catch (std::runtime_error& e) {
    8388      std::stringstream ss(e.what());
    84       ss << "\nMatrix.cc: invalid matrix element\n";
     89      ss << "\nMatrix(std::istream&): invalid matrix element\n";
    8590      throw IO_error(ss.str());
    8691    }
    8792
    88     unsigned int nof_columns=0;
    89     unsigned int nof_rows = 0;
    90     for (size_t row=0; row<data_matrix.size(); ++row) {
    91       // Ignoring empty lines
    92       if (!data_matrix[row].size()) {
    93         continue;
    94       }
    95       ++nof_rows;
    96       if (!nof_columns)
    97         nof_columns=data_matrix[row].size();
    98       else if (data_matrix[row].size()!=nof_columns) {
    99         std::ostringstream s;
    100         s << "Matrix::Matrix(std::istream&, char) data file error: "
    101           << "line " << nof_rows << " has " << data_matrix[row].size()
    102           << " columns; expected " << nof_columns << " columns.";
    103         throw utility::IO_error(s.str());
    104       }
    105     }
    106 
     93    unsigned int nof_rows = data_matrix.size();
    10794    // if stream was empty, create nothing
    108     if (!nof_columns || !nof_rows)
     95    if (!nof_rows)
    10996      return;
     97
     98    unsigned int nof_columns=data_matrix[0].size();
    11099
    111100    // convert the data to a gsl matrix
     
    116105    // if gsl error handler disabled, out of bounds index will not
    117106    // abort the program.
    118     size_t row=0;
    119107    for (size_t i=0; i<data_matrix.size(); ++i) {
    120       if (data_matrix[i].empty()) // ignore empty lines
    121         continue;
    122108      assert(data_matrix[i].size()==columns());
    123       assert(row<rows());
    124       std::copy(data_matrix[i].begin(), data_matrix[i].end(), begin_row(row));
    125       ++row;
     109      assert(i<rows());
     110      std::copy(data_matrix[i].begin(), data_matrix[i].end(), begin_row(i));
    126111    }
    127112  }
  • trunk/yat/utility/utility.h

    r1923 r1928  
    3333
    3434#include "deprecate.h"
     35#include "Exception.h"
    3536
    3637#include <cmath>
     
    137138     will be treated as separating elements with missing values.
    138139
    139      There is no restriction that rows must contain same number of
    140      elements.
     140     If \a rectangle is true, rows must contain same number of
     141     elements or function will throw.
     142
     143     If \a ignore_empty is true empty lines are ignored.
    141144
    142145     \see load(std::istream&, std::vector<T>&, char sep='\\0')
     
    146149  template<typename T>
    147150  void load(std::istream& is, std::vector<std::vector<T> >& vec, char sep='\0',
    148             char line_sep='\n');
     151            char line_sep='\n', bool ignore_empty=false, bool rectangle=true);
    149152
    150153  /**
     
    242245  template<typename T>
    243246  void load(std::istream& is, std::vector<std::vector<T> >& matrix,
    244             char sep='\0', char line_sep='\n')
     247            char sep, char line_sep, bool ignore_empty,
     248            bool rectangle)
    245249  {
    246250    size_t nof_columns=0;
    247251    std::string line;
    248252    while(getline(is, line, line_sep)){
     253      if (line.empty() && ignore_empty)
     254        continue;
    249255      matrix.resize(matrix.size()+1);
    250256      std::vector<double>& v=matrix.back();
     
    255261      if(sep!='\0' && !line.empty() && line[line.size()-1]==sep)
    256262        v.push_back(std::numeric_limits<T>::quiet_NaN());
     263     
     264      if (rectangle && nof_columns && v.size()!=nof_columns) {
     265        std::ostringstream s;
     266        s << "load data file error: "
     267          << "line " << matrix.size() << " has " << v.size()
     268          << " columns; expected " << nof_columns << " columns.";
     269        throw utility::IO_error(s.str());
     270      }       
    257271      nof_columns = std::max(nof_columns, v.size());
    258272    }
Note: See TracChangeset for help on using the changeset viewer.