Changeset 1616


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

fixes #459

Location:
trunk/yat/random
Files:
2 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
  • trunk/yat/random/random.h

    r1614 r1616  
    3131#include <algorithm>
    3232#include <string>
     33#include <vector>
    3334
    3435namespace theplu {
     
    275276    DiscreteGeneral(const statistics::Histogram& hist);
    276277   
     278    /**
     279       \brief Copy constructor
     280
     281       \since Explicitely implemented in yat 0.5
     282     */
     283    DiscreteGeneral(const DiscreteGeneral&);
     284
    277285    ///
    278286    /// @brief Destructor
     
    290298    unsigned long operator()(void) const;
    291299
    292   private:
    293      gsl_ran_discrete_t* gen_;
     300    /**
     301       \brief Assignment operator
     302
     303       \since Explicitely implemented in yat 0.5
     304     */
     305    DiscreteGeneral& operator=(const DiscreteGeneral&);
     306
     307  private:
     308    void free(void);
     309    void preproc(void);
     310
     311    gsl_ran_discrete_t* gen_;
     312    std::vector<double> p_;
    294313  };
    295314
Note: See TracChangeset for help on using the changeset viewer.