Ignore:
Timestamp:
Nov 5, 2008, 10:36:48 PM (13 years ago)
Author:
Peter
Message:

fixes #459

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/yat/random/random.cc

    r1614 r1616  
    180180
    181181  DiscreteGeneral::DiscreteGeneral(const statistics::Histogram& hist)
    182   {
    183     double* p = new double[ hist.nof_bins() ];
     182    : gen_(NULL)
     183  {
     184    p_.reserve(hist.nof_bins());
    184185    for (size_t i=0; i<hist.nof_bins(); i++)
    185       p[ i ] = hist[i];
    186     gen_ = gsl_ran_discrete_preproc( hist.nof_bins(), p );
    187     delete p;
    188     if (!gen_)
    189       throw utility::GSL_error("DiscreteGeneral::DiscreteGeneral failed to setup generator.");
     186      p_.push_back(hist[i]);
     187    preproc();
     188  }
     189
     190
     191  DiscreteGeneral::DiscreteGeneral(const DiscreteGeneral& other)
     192    : gen_(NULL), p_(other.p_)
     193  {
     194    preproc();
    190195  }
    191196
    192197
    193198  DiscreteGeneral::~DiscreteGeneral(void)
     199  {
     200    free();
     201  }
     202
     203
     204  void DiscreteGeneral::free(void)
    194205  {
    195206    if (gen_)
    196207      gsl_ran_discrete_free( gen_ );
    197208    gen_ = NULL;
     209  }
     210
     211
     212  void DiscreteGeneral::preproc(void)
     213  {
     214    assert(!gen_);
     215    assert(p_.size());
     216    gen_ = gsl_ran_discrete_preproc( p_.size(), &p_.front() );
     217    if (!gen_)
     218      throw utility::GSL_error("DiscreteGeneral failed to setup generator.");
     219  }
     220
     221
     222  DiscreteGeneral& DiscreteGeneral::operator=(const DiscreteGeneral& rhs)
     223  {
     224    free();
     225    p_ = rhs.p_;
     226    preproc();
     227    return *this;
    198228  }
    199229
Note: See TracChangeset for help on using the changeset viewer.