Ignore:
Timestamp:
Apr 30, 2009, 7:39:06 PM (12 years ago)
Author:
Peter
Message:

refs #510. using load function in Vector(istream) constructor

File:
1 edited

Legend:

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

    r1797 r1929  
    7979    // expandable)
    8080    std::vector<std::vector<double> > data_matrix;
    81     unsigned int nof_columns=0;
    82     unsigned int nof_rows=0;
    83     std::string line;
    84     while(getline(is, line, '\n')) {
    85       // Empty lines
    86       if (!line.size())
    87           continue;
    88       nof_rows++;
    89 
    90       data_matrix.push_back(std::vector<double>());
    91       std::vector<double>& v=data_matrix.back();
    92       std::string element;
    93       std::stringstream ss(line);
    94       bool ok=true;
    95       while(ok) {
    96         if(sep=='\0')
    97           ok=(ss>>element);
    98         else
    99           ok=getline(ss, element, sep);
    100         if(!ok)
    101           break;       
    102 
    103         if (!element.size())
    104           v.push_back(std::numeric_limits<double>::quiet_NaN());
    105         else {
    106           try {
    107             v.push_back(convert<double>(element));
    108           }
    109           catch (std::runtime_error& e) {
    110             std::stringstream ss(e.what());
    111             ss << "\nVector.cc: " << element
    112                << " is not accepted as a Vector element\n";
    113             throw IO_error(ss.str());
    114           }
    115         }
    116       }
    117       if(sep!='\0' && line[line.size()-1]==sep) // add NaN for final separator
    118           v.push_back(std::numeric_limits<double>::quiet_NaN());
    119       if (!nof_columns)
    120         nof_columns=v.size();
    121       else if (nof_rows && (nof_columns>1)) {
    122         std::ostringstream s;
    123         s << "Vector::Vector(std::istream&) data file error:\n"
    124           << "    File has inconsistent number of rows (" << nof_rows
    125           << ") and columns (" << nof_columns
    126           << ").\n    Expected a row or a column Vector.";
    127         throw utility::IO_error(s.str());
    128       }
    129       else if (v.size()!=nof_columns) {
    130         std::ostringstream s;
    131         s << "Vector::Vector(std::istream&) data file error:\n"
    132           << "    Line " << nof_rows << " has " << v.size()
    133           << " columns; expected " << nof_columns << " column.";
    134         throw utility::IO_error(s.str());
    135       }
    136     }
    137 
    138     // manipulate the state of the stream to be good
    139     is.clear(std::ios::goodbit);
    140 
    141     if (!nof_columns || !nof_rows)
     81    try {
     82      load(is, data_matrix, sep, '\n', true);
     83    }
     84    catch (utility::IO_error& e) {
     85      std::stringstream ss(e.what());
     86      ss << "\nVector(std::istream&): invalid dimensions\n";
     87      throw IO_error(ss.str());
     88    }
     89    catch (std::runtime_error& e) {
     90      std::stringstream ss(e.what());
     91      ss << "\nVector(std::istream&): invalid vector element\n";
     92      throw IO_error(ss.str());
     93    }
     94
     95    unsigned int nof_rows = data_matrix.size();
     96    // if stream was empty, create nothing
     97    if (!nof_rows)
    14298      return;
     99
     100    unsigned int nof_columns=data_matrix[0].size();
     101
     102    if (!(nof_rows==1 || nof_columns==1)) {
     103      std::stringstream ss;
     104      ss << "\nVector(std::istream&) data file error:\n"
     105         << "    File has inconsistent number of rows (" << nof_rows
     106         << ") and columns (" << nof_columns
     107         << ").\n    Expected a row or column Vector.";
     108      throw IO_error(ss.str());
     109    }
    143110
    144111    // convert the data to a gsl vector
Note: See TracChangeset for help on using the changeset viewer.