Changeset 341


Ignore:
Timestamp:
Jun 7, 2005, 4:41:25 PM (16 years ago)
Author:
Jari Häkkinen
Message:

Added exceptions for tracing file format problems when reading vectors.
Changed implentation for reading ints and doubles into stl vectors, now
unexpected characters are skipped instead of treated as zeros.
Some other minor things.

Location:
trunk
Files:
4 added
7 edited

Legend:

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

    r328 r341  
    55
    66#include <iostream>
     7#include <sstream>
     8#include <string>
    79#include <vector>
    810#include <utility>
     
    5456
    5557
    56   vector::vector(std::istream& is)
     58  vector::vector(std::istream& is) throw (utility::IO_error,std::exception)
    5759    : view_(NULL)
    5860  {
     
    7173      if (!nof_columns)
    7274        nof_columns=v.size();
    73       else if ((nof_rows>1) && (nof_columns>1)) {
    74         std::cerr << "vector::vector(std::istream&) data file error:\n"
    75                   << "file has " << nof_rows << " rows and " << nof_columns
    76                   << " columns; expected a row vector or a column vector."
    77                   << std::endl;
    78         exit(1);
     75      else if (nof_rows && (nof_columns>1)) {
     76        std::ostringstream s;
     77        s << "vector::vector(std::istream&) data file error:\n"
     78          << "    File has inconsistent number of rows (" << nof_rows+1
     79          << ") and columns (" << nof_columns
     80          << ").\n    Expected a row or a column vector.";
     81        std::string m=s.str();
     82        throw utility::IO_error(m);
    7983      }
    8084      else if(v.size()!=nof_columns) {
    81         std::cerr << "vector::vector(std::istream&) data file error: "
    82                   << "line" << nof_rows+1 << " has " << v.size()
    83                   << " columns; expected " << nof_columns << " columns"
    84                   << std::endl;
    85         exit(1);
     85        std::ostringstream s;
     86        s << "vector::vector(std::istream&) data file error:\n"
     87          << "    Line " << (nof_rows+1) << " has " << v.size()
     88          << " columns; expected " << nof_columns << " column.";
     89        std::string m=s.str();
     90        throw utility::IO_error(m);
    8691      }
    8792      data_matrix.push_back(v);
     
    186191
    187192
     193  bool vector::operator==(const vector& a) const
     194  {
     195    if (size()!=a.size())
     196      return false;
     197    for (size_t i=0; i<size(); ++i)
     198      if (gsl_vector_get(v_,i)!=a(i))
     199        return false;
     200    return true;
     201  }
     202
     203
     204
    188205  vector& vector::operator=( const vector& other )
    189206  {
  • trunk/lib/gslapi/vector.h

    r334 r341  
    99
    1010#include <gsl/gsl_vector.h>
     11#include <c++_tools/utility/Exception.h>
    1112
    1213namespace theplu {
     
    8283    /// end of input to the vector is at end of file marker.
    8384    ///
    84     explicit vector(std::istream &);
     85    explicit vector(std::istream &) throw (utility::IO_error,std::exception);
    8586
    8687    ///
     
    309310    double operator*( const vector &other ) const;
    310311
     312    ///
     313    /// Vector with scalar multiplication.
     314    ///
     315    /// @note This operator is not implemented!
     316    ///
     317    vector operator*(const double d) const;
     318
    311319    ///
    312320    /// Vector addition.
     
    315323    ///
    316324    vector operator+( const vector& other ) const;
     325
     326    ///
     327    /// Vector subtraction.
     328    ///
     329    /// @return The resulting vector.
     330    ///
     331    vector operator-( const vector& other ) const;
     332
     333    ///
     334    /// Comparison operator. Takes linear time.
     335    ///
     336    /// @return True if the sequence of the elements in the vectors
     337    /// are element/wise equal.
     338    ///
     339    bool operator==(const vector&) const;
     340
     341    ///
     342    /// The assignment operator. There is no requirements on
     343    /// dimensions.
     344    ///
     345    vector& operator=(const vector&);
    317346
    318347    ///
     
    324353    { gsl_vector_add(v_,other.v_); return *this; }
    325354
    326 
    327     ///
    328     /// Vector subtraction.
    329     ///
    330     /// @return The resulting vector.
    331     ///
    332     vector operator-( const vector& other ) const;
    333 
    334355    ///
    335356    /// Subtract and assign operator.
     
    341362
    342363    ///
    343     /// The assignment operator. There is no requirements on
    344     /// dimensions.
    345     ///
    346     vector& operator=(const vector&);
    347 
    348     ///
    349364    /// Multiply with scalar and assign operator.
    350365    ///
    351366    vector& operator*=(const double d) { gsl_vector_scale(v_,d); return *this; }
    352 
    353     ///
    354     /// Vector with scalar multiplication.
    355     ///
    356     /// \note This operator is not implemented!
    357     ///
    358     vector operator*(const double d) const;
    359367
    360368
  • trunk/lib/utility/Makefile.am

    r327 r341  
    1515
    1616include_utility_HEADERS = \
    17   Alignment.h FileIO.h kNNI.h Merge.h NNI.h PCA.h random_singleton.h \
    18   stl_utility.h SVD.h utility.h WeNNI.h
     17  Alignment.h Exception.h FileIO.h kNNI.h Merge.h NNI.h PCA.h \
     18  random_singleton.h stl_utility.h SVD.h utility.h WeNNI.h
    1919
  • trunk/lib/utility/stl_utility.cc

    r327 r341  
    2020      for (size_t i=0; i<vec_str.size(); i++) {
    2121        if (!is_float(vec_str[i])){
    22           std::cerr << "Warning: " << vec_str[i]
    23                     << " is not a number." << std::endl;
     22          std::cerr << "Warning: '" << vec_str[i]
     23                    << "' is not a number." << std::endl;
    2424        }
    25         vec.push_back(atof(vec_str[i].c_str()));
     25        else
     26          vec.push_back(atof(vec_str[i].c_str()));
    2627      }
    2728      return true;
     
    4041      for (size_t i=0; i<vec_str.size(); i++) {
    4142        if (!is_int(vec_str[i])){
    42           std::cerr << "Warning: " << vec_str[i]
    43                     << " is not an integer." << std::endl;
     43          std::cerr << "Warning: '" << vec_str[i]
     44                    << "' is not an integer." << std::endl;
    4445        }
    45         vec.push_back(atoi(vec_str[i].c_str()));
     46        else
     47          vec.push_back(atoi(vec_str[i].c_str()));
    4648      }
    4749      return true;
  • trunk/lib/utility/stl_utility.h

    r327 r341  
    4242  /// reads the line until next '\\n'. The line is splitted with
    4343  /// respect to whitespaces and push_backed into the vector. The
    44   /// vector is emptied before the reading starts.
     44  /// vector is emptied before the reading starts. Unexpected
     45  /// characters are currently skipped with a warning message.
    4546  ///
    4647  /// @return false if end of stream
     48  ///
     49  /// @note The functionality of this function will change in the
     50  /// future. The overall functionality will be the same but the
     51  /// outcome of unexpected events will change.
     52  ///
     53  /// @todo Change the implementation of this function to follow C/C++
     54  /// style treatment of files and streams.
    4755  ///
    4856  bool read_to_double(std::istream&, std::vector<double>&);
     
    5260  /// reads the line until next '\\n'. The line is splitted with
    5361  /// respect to whitespaces and push_backed into the vector. The
    54   /// vector is emptied before the reading starts.
     62  /// vector is emptied before the reading starts. Unexpected
     63  /// characters are currently skipped with a warning message.
    5564  ///
    5665  /// @return false if end of stream
     66  ///
     67  /// @note The functionality of this function will change in the
     68  /// future. The overall functionality will be the same but the
     69  /// outcome of unexpected events will change.
     70  ///
     71  /// @todo Change the implementation of this function to follow C/C++
     72  /// style treatment of files and streams.
    5773  ///
    5874  bool read_to_int(std::istream&, std::vector<int>&);
  • trunk/lib/utility/utility.h

    r327 r341  
    2424  /// @return true if string is a number
    2525  ///
     26  /// @todo Make sure all C/C++ style floats are accepted, i.e. .5
     27  /// should be accepted.
     28  ///
    2629  bool is_float(const std::string&);
    2730
  • trunk/test/vector_test.cc

    r328 r341  
    11// $Id$
    22
     3#include <c++_tools/utility/Exception.h>
    34#include <c++_tools/utility/FileIO.h>
    45#include <c++_tools/gslapi/vector.h>
     
    67
    78#include <fstream>
     9#include <sstream>
    810
    911using namespace theplu;
     
    1921int main(const int argc,const char* argv[])
    2022
     23  bool print = (argc>1 && argv[1]==std::string("-p"));
    2124  bool ok = true;
    2225
     
    6871    ok=false;
    6972
    70   // checking that reading vectors from file works
    71   std::string data1("data/vector1.data");
    72   std::string data2("data/vector2.data");
    73   std::string data3("data/vector3.data");
    74   std::string data4("data/vector4.data");
    75   check_file_access(data1);
    76   check_file_access(data2);
    77   check_file_access(data3);
    78   check_file_access(data4);
    79   std::ifstream data_stream1(data1.c_str());
    80   std::ifstream data_stream2(data2.c_str());
    81   std::ifstream data_stream3(data3.c_str());
    82   std::ifstream data_stream4(data4.c_str());
    83   vec=gslapi::vector(data_stream1);
    84   if (vec.size()!=9)
     73  // checking that reading vectors from properly formatted files works
     74  try {
     75    std::string data1("data/vector1.data");
     76    std::string data2("data/vector2.data");
     77    std::string data3("data/vector3.data");
     78    std::string data4("data/vector4.data");
     79    check_file_access(data1);
     80    check_file_access(data2);
     81    check_file_access(data3);
     82    check_file_access(data4);
     83    std::ifstream data_stream1(data1.c_str());
     84    std::ifstream data_stream2(data2.c_str());
     85    std::ifstream data_stream3(data3.c_str());
     86    std::ifstream data_stream4(data4.c_str());
     87    vec=gslapi::vector(data_stream1);
     88    if (vec.size()!=9)
     89      ok=false;
     90    vec=gslapi::vector(data_stream2);
     91    if (vec.size()!=9)
     92      ok=false;
     93    vec=gslapi::vector(data_stream3);
     94    if (vec.size()!=12)
     95      ok=false;
     96    vec=gslapi::vector(data_stream4);
     97    if (vec.size()!=12)
     98      ok=false;
     99  } catch (utility::IO_error& err) {
     100    if (print)
     101      std::cerr << err.what() << std::endl;
    85102    ok=false;
    86   vec=gslapi::vector(data_stream2);
    87   if (vec.size()!=9)
    88     ok=false;
    89   vec=gslapi::vector(data_stream3);
    90   if (vec.size()!=12)
    91     ok=false;
    92   vec=gslapi::vector(data_stream4);
    93   if (vec.size()!=12)
     103  }
     104
     105  // Checking that vector stream input operator can read whatever
     106  // vector stream output operator produces.
     107  std::stringstream s;
     108  s << vec;
     109  vec2=gslapi::vector(s);
     110  if (!(vec==vec2))
    94111    ok=false;
    95112
    96   std::cout << "ok: " << ok << std::endl;
     113  // Checking that unexpected characters in a stream are skipped. When
     114  // file read is reimplemented to resemble C/C++ style this should
     115  // check something else or maybe nothing at all.
     116  std::string data5("data/vector5.data");
     117  check_file_access(data5);
     118  std::ifstream data_stream5(data5.c_str());
     119  vec=gslapi::vector(data_stream5);
     120  if (vec.size()!=8)
     121    ok=false;
    97122
    98   if (ok)
    99     return 0;
    100   return -1;
     123  // Checking that badly formatted files are not accepted, or at least
     124  // throws an exception for the programmer to catch.
     125  bool this_ok=false;
     126  try {
     127    std::string data("data/vector6.data");
     128    check_file_access(data);
     129    std::ifstream data_stream(data.c_str());
     130    vec=gslapi::vector(data_stream); // this will gove an exception
     131  } catch (utility::IO_error& err) {
     132    if (print)
     133      std::cerr << err.what() << std::endl;
     134    this_ok=true; // good, exceoption thrown, test passed
     135  }
     136  if (!this_ok)
     137    ok=false;
     138  this_ok=false;
     139  try {
     140    std::string data("data/vector7.data");
     141    check_file_access(data);
     142    std::ifstream data_stream(data.c_str());
     143    vec=gslapi::vector(data_stream); // this will gove an exception
     144  } catch (utility::IO_error& err) {
     145    if (print)
     146      std::cerr << err.what() << std::endl;
     147    this_ok=true; // good, exceoption thrown, test passed
     148  }
     149  if (!this_ok)
     150    ok=false;
     151
     152  return (ok ? 0 : -1);
    101153}
Note: See TracChangeset for help on using the changeset viewer.