Changeset 1922


Ignore:
Timestamp:
Apr 24, 2009, 11:48:08 PM (12 years ago)
Author:
Peter
Message:

working on ticket 510 (refs #510)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/yat/utility/utility.h

    r1834 r1922  
    3636#include <cmath>
    3737#include <limits>
     38#include <istream>
    3839#include <string>
    3940#include <stdexcept>
     
    113114  bool is_nan(const std::string& s);
    114115
     116  /**
     117     The std::istream will be interpreted as outlined here:
     118
     119     Lines are separated by character \a line_sep and rows are
     120     separated by character \a sep.
     121     
     122     The first line is read into a stringstream, which is used to
     123     load the first vector (vec[0]) with elements using
     124     load(stringstream, vec[0], sep).
     125     
     126     Therefore, column elements separation has two modes depending
     127     on the value of \a sep.
     128     
     129     - If \a sep is the default '\\0' value then column elements are
     130     separated with white space characters except the new line
     131     character. Multiple sequential white space characters are treated
     132     as one separator.
     133     
     134     - Setting \a sep to something else than the default value will
     135     change the behaviour to use the \a sep character as the separator
     136     between column elements. Multiple sequential \a sep characters
     137     will be treated as separating elements with missing values.
     138
     139     There is no restriction that rows must contain same number of
     140     elements.
     141
     142     \see load(std::istream&, std::vector<T>&, char sep='\\0')
     143
     144     \note Requirement on T: utility::convert<T> must be supported
     145   */
     146  template<typename T>
     147  void load(std::istream& is, std::vector<std::vector<T> >& vec, char sep='\0',
     148            char line_sep='\n');
     149
     150  /**
     151     \brief Fill a vector<T> with elements from istream
     152
     153     Element separation has two modes depending on the value of \a
     154     sep.
     155     
     156     - If \a sep is the default '\\0' value then elements are
     157     separated with white space characters. Multiple sequential white
     158     space characters are treated as one separator.
     159     
     160     - Setting \a sep to something else than the default value will
     161     change the behaviour to use the \a sep character as the
     162     separator between column elements. Multiple sequential \a sep
     163     characters will be treated as separating elements with missing
     164     values. Missing values are set to std::numeric_limits<T>::quiet_NaN
     165     
     166     \note Requirement on T: utility::convert<T> must be supported
     167   */
     168  template<typename T>
     169  void load(std::istream& is, std::vector<T>& vec, char sep='\0');
     170 
     171  // template implementations
    115172
    116173  template<typename InputIterator, typename OutputIterator>
     
    183240  }
    184241
     242  template<typename T>
     243  void load(std::istream& is, std::vector<std::vector<T> >& matrix,
     244            char sep='\0', char line_sep='\n')
     245  {
     246    size_t nof_columns=0;
     247    std::string line;
     248    while(getline(is, line, line_sep)){
     249      matrix.resize(matrix.size()+1);
     250      std::vector<double>& v=matrix.back();
     251      v.reserve(nof_columns);
     252      std::stringstream ss(line);
     253      load(ss, v, sep);
     254      if(sep!='\0' && line[line.size()-1]==sep) // add NaN for final separator
     255          v.push_back(std::numeric_limits<T>::quiet_NaN());
     256      nof_columns = std::max(nof_columns, v.size());
     257    }
     258
     259    // manipulate the state of the stream to be good
     260    is.clear(std::ios::goodbit);
     261  }
     262
     263  template<typename T>
     264  void load(std::istream& is, std::vector<T>& vec, char sep='\0')
     265  {
     266    std::string element;
     267    bool ok=true;
     268    while(ok) {
     269      if(sep=='\0')
     270        ok=(is>>element);
     271      else
     272        ok=getline(is, element, sep);
     273      if(!ok)
     274        break;
     275     
     276      if (!element.size())
     277        vec.push_back(std::numeric_limits<T>::quiet_NaN());
     278      else {
     279        vec.push_back(convert<T>(element));
     280      }
     281    }
     282  }           
     283
    185284}}} // of namespace utility, yat, and theplu
    186285
Note: See TracChangeset for help on using the changeset viewer.