Changeset 434


Ignore:
Timestamp:
Dec 15, 2005, 12:23:46 AM (17 years ago)
Author:
Markus Ringnér
Message:

Modified istream constructors to support both the old format and CSV files. Also support has been added for NaN:s

Location:
trunk/lib/gslapi
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/gslapi/matrix.cc

    r421 r434  
    55#include <c++_tools/gslapi/vector.h>
    66#include <c++_tools/utility/stl_utility.h>
     7#include <c++_tools/utility/utility.h>
    78
    89#include <cmath>
     
    3132
    3233
    33 
    3434  // Constructor that gets data from istream
    35   matrix::matrix(std::istream& is) throw (utility::IO_error,std::exception)
     35  matrix::matrix(std::istream& is, char sep)
     36    throw (utility::IO_error,std::exception)
    3637    : view_(NULL)
    3738  {
     39    // Markus to Jari, somewhere we should check that quiet_NaNs are supported
     40    // std::numeric_limits<double>::has_quiet_NaN has to be true.
     41    // Also in vector
     42
    3843    // read the data file and store in stl vectors (dynamically
    3944    // expandable)
     
    4146    u_int nof_columns=0;
    4247    u_int nof_rows = 0;
    43     std::vector<double> v;
    44     for (nof_rows = 0; utility::read_to_double(is, v); nof_rows++) {
     48    std::string line;
     49    while(getline(is, line, '\n')){
    4550      // Ignoring empty lines
    46       if (!v.size()) {
    47         nof_rows--;
     51      if (!line.size()) {
    4852        continue;
    4953      }
     54      nof_rows++;
     55      std::vector<double> v;
     56      std::string element;
     57      std::stringstream ss(line);
     58      bool ok=true;
     59      while(ok) {
     60        if(sep=='\0')
     61          ok=(ss>>element);
     62        else
     63          ok=getline(ss, element, sep);
     64        if(!ok)
     65          break;
     66
     67        if(utility::is_double(element)) {
     68          v.push_back(atof(element.c_str()));
     69        }
     70        else if (!element.size() || utility::is_nan(element)) {
     71          v.push_back(std::numeric_limits<double>::quiet_NaN());
     72        }
     73        else {
     74          // Jari, this should be communicated with as an exception.
     75          //          std::cerr << "Warning: '" << vec_str[i]
     76          //                    << "' is not an integer." << std::endl;
     77        }
     78      }           
    5079      if (!nof_columns)
    5180        nof_columns=v.size();
    5281      else if (v.size()!=nof_columns) {
    5382        std::ostringstream s;
    54         s << "matrix::matrix(std::istream&) data file error: "
     83        s << "matrix::matrix(std::istream&, char) data file error: "
    5584          << "line" << nof_rows+1 << " has " << v.size()
    5685          << " columns; expected " << nof_columns << " columns.";
     
    6897        gsl_matrix_set( m_, i, j, data_matrix[i][j] );
    6998  }
    70 
    7199
    72100
  • trunk/lib/gslapi/matrix.h

    r420 r434  
    9797    ///
    9898    /// Matrix rows are sepearated with the new line character, and
    99     /// column elements in a row must be separated with white space
    100     /// characters except the new line (\\n) character. Rows, and
     99    /// column elements in a row must be separated either with white space
     100    /// characters except the new line (\\n) character (default), or
     101    /// by the delimiter \a sep.
     102    ///. Rows, and
    101103    /// columns, are read consecutively and only rectangular matrices
    102104    /// are supported. Empty lines are ignored. End of input is at end
    103105    /// of file marker.
    104106    ///
    105     explicit matrix(std::istream &) throw (utility::IO_error,std::exception);
     107    //    explicit matrix(std::istream &) throw (utility::IO_error,std::exception);
     108    explicit matrix(std::istream &, char sep='\0')
     109      throw (utility::IO_error,std::exception);
     110
    106111
    107112    ///
  • trunk/lib/gslapi/vector.cc

    r420 r434  
    44#include <c++_tools/gslapi/matrix.h>
    55#include <c++_tools/utility/stl_utility.h>
     6#include <c++_tools/utility/utility.h>
    67
    78#include <sstream>
     
    6364
    6465
    65   vector::vector(std::istream& is) throw (utility::IO_error,std::exception)
     66  vector::vector(std::istream& is, char sep)
     67    throw (utility::IO_error,std::exception)
    6668    : view_(NULL), const_view_(NULL)
    6769  {
     
    7173    u_int nof_columns=0;
    7274    u_int nof_rows=0;
    73     std::vector<double> v;
    74     for (nof_rows = 0; utility::read_to_double(is, v); nof_rows++) {
    75       // Ignoring empty lines
    76       if (!v.size()) {
    77         nof_rows--;
    78         continue;
    79       }
     75    std::string line;
     76    while(getline(is, line, '\n')) {
     77      // Empty lines
     78      if (!line.size())
     79          continue;
     80      nof_rows++;
     81     
     82      std::vector<double> v;
     83      std::string element;
     84      std::stringstream ss(line);
     85      bool ok=true;
     86      while(ok) {
     87        if(sep=='\0')
     88          ok=(ss>>element);
     89        else
     90          ok=getline(ss, element, sep);
     91        if(!ok)
     92          break;       
     93
     94        if(utility::is_double(element)) {
     95          v.push_back(atof(element.c_str()));
     96        }
     97        else if (!element.size() || utility::is_nan(element)) {
     98          v.push_back(std::numeric_limits<double>::quiet_NaN());
     99        }
     100        else {
     101          // Jari, this should be communicated with as an exception.
     102          // std::cerr << "Warning: '" << element
     103          //           << "' is not an integer." << std::endl;
     104        }
     105      }
    80106      if (!nof_columns)
    81107        nof_columns=v.size();
  • trunk/lib/gslapi/vector.h

    r431 r434  
    147147    /// The istream constructor.
    148148    ///
    149     /// Elements should be separated with white space characters, the
     149    /// Either elements should be separated
     150    /// with white space characters (default), or elements should be separated
     151    /// by the delimiter \a sep. When delimiter \a sep is used empty elements
     152    /// are stored as NaN's (except that empty lines are ignored). The
    150153    /// end of input to the vector is at end of file marker.
    151154    ///
    152     explicit vector(std::istream &) throw (utility::IO_error,std::exception);
     155    explicit vector(std::istream &, char sep='\0') throw (utility::IO_error,std::exception);
    153156
    154157    ///
Note: See TracChangeset for help on using the changeset viewer.