Changeset 2333


Ignore:
Timestamp:
Oct 15, 2010, 2:44:23 AM (11 years ago)
Author:
Peter
Message:

fixes #556

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/histogram_test.cc

    r2202 r2333  
    2929#include <iostream>
    3030
     31using namespace theplu::yat::statistics;
     32using namespace theplu::yat;
     33
     34void test_stream_constructor(test::Suite&);
     35
    3136int main(int argc, char* argv[])
    3237{
    33   using namespace theplu::yat::statistics;
    34   using namespace theplu::yat;
    3538  theplu::yat::test::Suite suite(argc, argv);
    3639  suite.err() << "testing histogram" << std::endl;
     
    5861        boost::forward_iterator_archetype<DataWeight>());
    5962  }
     63  test_stream_constructor(suite);
    6064
    6165  return suite.return_value();
    6266}
     67
     68void test_stream_constructor(test::Suite& suite)
     69{
     70  Histogram hist(0, 10, 10);
     71  hist.add(3,2);
     72  hist.add(1,3.14);
     73  hist.add(20);
     74  std::ostringstream oss;
     75  oss << hist;
     76  std::istringstream iss(oss.str());
     77  Histogram hist2(iss);
     78  std::ostringstream oss2;
     79  oss2 << hist2;
     80  if (oss.str() != oss2.str()) {
     81    suite.add(false);
     82    suite.err() << "output:\n" << oss.str()
     83                << "not equal to\n" << oss2.str();
     84     
     85  }
     86}
  • trunk/yat/statistics/Histogram.cc

    r2119 r2333  
    2626#include "Histogram.h"
    2727
     28#include <yat/utility/utility.h>
     29
    2830#include <algorithm>
    2931#include <cassert>
    3032#include <cmath>
    3133#include <functional>
     34#include <istream>
    3235#include <ostream>
    3336
     
    4043    : xmax_(0), xmin_(0), sum_all_(), sum_histogram_()
    4144  {
     45  }
     46
     47
     48  Histogram::Histogram(std::istream& is)
     49  {
     50    std::string line;
     51    getline(is, line, ':');
     52    getline(is, line, '\n');
     53    xmin_ = utility::convert<double>(line);
     54    getline(is, line, ':');
     55    getline(is, line, '\n');
     56    xmax_ = utility::convert<double>(line);
     57    getline(is, line, ':');
     58    getline(is, line, '\n');
     59    size_t n = utility::convert<size_t>(line);
     60    histogram_.resize(n);
     61    getline(is, line, '\n');
     62    getline(is, line, ':');
     63    getline(is, line, '\n');
     64    double total = utility::convert<double>(line);
     65    getline(is, line, '\n');
     66    getline(is, line, '\n');
     67    std::vector<std::vector<double> > data;
     68    utility::load(is, data);
     69    for (size_t i=0; i<histogram_.size(); ++i) {
     70      assert(data[i].size()==2);
     71      add(data[i][0], data[i][1]);
     72    }
     73    add(xmax_, total - averager_all().sum_w());
    4274  }
    4375
  • trunk/yat/statistics/Histogram.h

    r2263 r2333  
    3333#include <boost/concept_check.hpp>
    3434
     35#include <iosfwd>
    3536#include <string>
    3637#include <vector>
     
    5758    ///
    5859    Histogram(void);
     60
     61    /**
     62       \brief The istream constructor.
     63
     64       This constructor creates a Histogram from output (possibly)
     65       created with operator<<. Note that the output from operator<<
     66       does not contain all information required to restore the
     67       Histogram. The values of all bins are restored, but the
     68       averagers, averager_all(void) and averager_histogram(void), and
     69       more specifically the second moment (sum_xx) in the averagers
     70       cannot be restored due to lack of information in the output.
     71
     72       \since New in yat 0.7
     73    */
     74    Histogram(std::istream&);
    5975
    6076    ///
Note: See TracChangeset for help on using the changeset viewer.