Changeset 529

Show
Ignore:
Timestamp:
08/14/09 09:03:02 (15 years ago)
Author:
vbarta
Message:

defined *_ptr wrappers of shared pointers

Location:
library
Files:
20 modified

Legend:

Unmodified
Added
Removed
  • library/bdm/base/bdmbase.h

    r527 r529  
    226226}; 
    227227UIREGISTER (RV); 
     228SHAREDPTR (RV); 
    228229 
    229230//! Concat two random variables 
     
    386387 
    387388}; 
    388  
     389SHAREDPTR(epdf); 
    389390 
    390391//! Conditional probability density, e.g. modeling some dependencies. 
     
    487488                //!@} 
    488489}; 
     490SHAREDPTR(mpdf); 
    489491 
    490492template <class EPDF> 
     
    740742}; 
    741743UIREGISTER (mepdf); 
     744SHAREDPTR (mepdf); 
    742745 
    743746//! \brief Combines RVs from a list of mpdfs to a single one. 
     
    990993}; 
    991994 
     995typedef Array<shared_ptr<epdf> > epdf_array; 
     996 
     997typedef Array<shared_ptr<mpdf> > mpdf_array; 
     998 
    992999template<class EPDF> 
    9931000vec mpdf_internal<EPDF>::samplecond (const vec &cond) 
  • library/bdm/base/datasources.h

    r527 r529  
    107107 
    108108UIREGISTER ( ITppFileDS ); 
     109SHAREDPTR ( ITppFileDS ); 
    109110 
    110111/*! 
     
    263264 
    264265UIREGISTER ( ArxDS ); 
     266SHAREDPTR ( ArxDS ); 
    265267 
    266268class stateDS : public DS { 
     
    346348 
    347349UIREGISTER ( stateDS ); 
     350SHAREDPTR ( stateDS ); 
    348351 
    349352}; //namespace 
  • library/bdm/base/loggers.h

    r477 r529  
    9292 
    9393UIREGISTER ( memlog ); 
     94SHAREDPTR ( memlog ); 
    9495 
    9596/*! 
     
    139140 
    140141UIREGISTER ( dirfilelog ); 
     142SHAREDPTR ( dirfilelog ); 
    141143 
    142144}; 
  • library/bdm/base/user_info.h

    r527 r529  
    550550        ParticularUI<T> ( const string &class_name ) : UI ( class_name, &typeid ( T ) ) {}; 
    551551 
    552         //! A method returning a brand new instance of class T, this method is the reason why there have to be a parameterless construcotor in class T 
     552        //! A method returning a brand new instance of class T, this method is the reason why there have to be a parameterless constructor in class T 
    553553        root* new_instance() const { 
    554554                return new T(); 
     
    565565  \brief Macro for registration of class into map of user-infos, registered class is scriptable using UI static methods 
    566566 
    567   Argument \a class_name has to be a descendant of root class and also, it has to have parameterless constructor prepared. 
     567  Argument \a class_name has to be a descendant of root class and also to have a default constructor. 
    568568  This macro should be used in header file, immediately after a class declaration. 
    569569 
  • library/bdm/estim/arx.h

    r477 r529  
    164164 
    165165UIREGISTER ( ARX ); 
     166SHAREDPTR ( ARX ); 
    166167 
    167168} 
  • library/bdm/estim/kalman.h

    r527 r529  
    280280 
    281281UIREGISTER ( EKFCh ); 
     282SHAREDPTR ( EKFCh ); 
    282283 
    283284 
     
    466467 
    467468UIREGISTER ( MultiModel ); 
     469SHAREDPTR ( MultiModel ); 
    468470 
    469471 
  • library/bdm/mex/mex_datasource.h

    r477 r529  
    4040                it_assert_debug ( time < Data.cols(), "MemDS delays are too high." ); 
    4141 
    42                 RV* r = UI::build<RV> ( set, "rv", UI::compulsory ); 
     42                shared_ptr<RV> r = UI::build<RV> ( set, "rv", UI::compulsory ); 
    4343                RV ru = RV(); 
    4444                set_rvs ( *r, ru ); 
     
    5050 
    5151UIREGISTER ( MexDS ); 
     52SHAREDPTR ( MexDS ); 
    5253 
    5354} 
  • library/bdm/mex/mex_logger.h

    r477 r529  
    7171}; 
    7272UIREGISTER ( mexlog ); 
     73SHAREDPTR ( mexlog ); 
    7374} 
  • library/bdm/shared_ptr.h

    r477 r529  
    11/*! 
    22  \file 
    3   \brief BDM's own smart pointer. 
     3  \brief BDM's own smart pointers. 
    44  \author Vaclav Barta. 
    55 
     
    2222namespace bdm { 
    2323 
    24 //! A naive implementation of roughly a subset of the std::tr1:shared_ptr spec (really just roughly - it ignores memory exceptions, for example; also note I didn't read the spec). 
    25 // The standard template would naturally be preferable, _if_ it was 
    26 // included in the standard libraries of all supported compilers - but 
    27 // that's exactly what remains to be seen... 
     24/*! \brief A naive implementation of roughly a subset of the std::tr1:shared_ptr spec 
     25 
     26  Really just roughly - it ignores memory 
     27  exceptions, for example; also note I didn't read the spec. 
     28 
     29  The standard template would naturally be preferable, _if_ it was 
     30  included in the standard libraries of all supported compilers - but 
     31  as of 2009, that's still a problem... 
     32*/ 
    2833template <typename T> 
    2934class shared_ptr { 
     
    4146        } 
    4247 
    43         //! Constructs a shared_ptr that owns the pointer p (unless p is 
    44         //! null, in which case this constructor creates an empty 
    45         //! shared_ptr). 
     48        /*!  
     49          Constructs a shared_ptr that owns the pointer p (unless p 
     50          is NULL, in which case this constructor creates an empty 
     51          shared_ptr). When p isn't null, it must have been alllocated 
     52          by new! 
     53        */ 
    4654        shared_ptr ( T *p ) : 
    4755                        payload ( p ), 
     
    7078        } 
    7179 
    72         shared_ptr &operator= ( const shared_ptr &other ) { 
     80        shared_ptr<T> &operator= ( const shared_ptr<T> &other ) { 
    7381                other.add_ref(); 
    7482                del_ref(); 
     
    178186} 
    179187 
     188/*! \brief A wrapper of shared_ptr which is never empty. 
     189 
     190  T must have a default constructor. 
     191 
     192  Note that shared_ptr's destructor isn't virtual - don't call delete 
     193  on pointers to instances of this class. 
     194 */ 
     195template <typename T> 
     196class object_ptr : public shared_ptr<T> 
     197{ 
     198public: 
     199        /*! 
     200          \brief Default constructor 
     201 
     202          Calls T's default constructor. 
     203        */ 
     204        object_ptr() : shared_ptr<T> ( new T() ) { } 
     205 
     206        /*! 
     207          \brief Upcast from shared_ptr<T> to object_ptr<T> 
     208 
     209          \param b The shared pointer, which must not be empty. 
     210        */ 
     211        object_ptr ( const shared_ptr<T> &b ) : shared_ptr<T> ( b ) { 
     212                it_assert_debug ( this->get(), "object_ptr cannot be empty" ); 
     213        } 
     214 
     215        /*!  
     216          Constructs an object_ptr that owns the pointer p. p must 
     217          have been alllocated by new! 
     218        */ 
     219        object_ptr ( T *p ) : shared_ptr<T> ( p ) { 
     220                it_assert_debug ( p, "object_ptr cannot be empty" ); 
     221        } 
     222 
     223        object_ptr<T> &operator= ( const object_ptr<T> &other ) { 
     224                shared_ptr<T>::operator= ( other ); 
     225                return *this; 
     226        } 
     227}; 
     228 
     229#define SHAREDPTR(class_name) typedef bdm::object_ptr< class_name > class_name##_ptr 
     230 
     231#define SHAREDPTR2(template_class_name, template_parameter_name) typedef bdm::object_ptr< template_class_name < template_parameter_name > > template_class_name##_##template_parameter_name##_ptr 
     232 
    180233} 
    181234 
  • library/bdm/stat/emix.h

    r527 r529  
    186186        } 
    187187}; 
    188  
     188SHAREDPTR( emix ); 
    189189 
    190190/*! 
     
    362362}; 
    363363UIREGISTER ( mprod ); 
     364SHAREDPTR ( mprod ); 
    364365 
    365366//! Product of independent epdfs. For dependent pdfs, use mprod. 
  • library/bdm/stat/exp_family.h

    r527 r529  
    165165}; 
    166166UIREGISTER (enorm<chmat>); 
     167SHAREDPTR2 ( enorm, chmat ); 
    167168UIREGISTER (enorm<ldmat>); 
     169SHAREDPTR2 ( enorm, ldmat ); 
    168170UIREGISTER (enorm<fsqmat>); 
     171SHAREDPTR2 ( enorm, fsqmat ); 
    169172 
    170173 
     
    241244                //!@} 
    242245}; 
    243 UIREGISTER (egiw); 
     246UIREGISTER ( egiw ); 
     247SHAREDPTR ( egiw ); 
    244248 
    245249/*! \brief Dirichlet posterior density 
     
    399403}; 
    400404UIREGISTER (egamma); 
     405SHAREDPTR ( egamma ); 
     406 
    401407/*! 
    402408 \brief Inverse-Gamma posterior density 
     
    566572}; 
    567573UIREGISTER (mlnorm<ldmat>); 
     574SHAREDPTR2 ( mlnorm, ldmat ); 
    568575UIREGISTER (mlnorm<fsqmat>); 
     576SHAREDPTR2 ( mlnorm, fsqmat ); 
    569577UIREGISTER (mlnorm<chmat>); 
     578SHAREDPTR2 ( mlnorm, chmat ); 
    570579 
    571580//! Mpdf with general function for mean value 
     
    627636 
    628637UIREGISTER (mgnorm<chmat>); 
     638SHAREDPTR2 ( mgnorm, chmat ); 
    629639 
    630640 
     
    716726}; 
    717727UIREGISTER (mgamma); 
     728SHAREDPTR (mgamma); 
    718729 
    719730/*! 
     
    860871 
    861872UIREGISTER (migamma_ref); 
     873SHAREDPTR (migamma_ref); 
    862874 
    863875/*! Log-Normal probability density 
     
    941953 
    942954UIREGISTER (mlognorm); 
     955SHAREDPTR (mlognorm); 
    943956 
    944957/*! inverse Wishart density defined on Choleski decomposition 
  • library/bdm/stat/merger.h

    r507 r529  
    316316}; 
    317317UIREGISTER ( merger_base ); 
     318SHAREDPTR ( merger_base ); 
    318319 
    319320class merger_mix : public merger_base { 
     
    398399}; 
    399400UIREGISTER ( merger_mix ); 
     401SHAREDPTR ( merger_mix ); 
    400402 
    401403} 
  • library/tests/emix_test.cpp

    r522 r529  
    1 #include "shared_ptr.h" 
    21#include "stat/exp_family.h" 
    32#include "stat/emix.h" 
     
    2019        vec mu0 ( "1.00054 1.0455" ); 
    2120 
    22         shared_ptr<enorm<ldmat> > E1 = new enorm<ldmat>(); 
     21        enorm_ldmat_ptr E1; 
    2322        E1->set_rv ( xy ); 
    2423        E1->set_parameters ( mu0 , mat ( "0.740142 -0.259015; -0.259015 1.0302" ) ); 
    2524 
    26         shared_ptr<enorm<ldmat> > E2 = new enorm<ldmat>(); 
     25        enorm_ldmat_ptr E2; 
    2726        E2->set_rv ( xy ); 
    2827        E2->set_parameters ( "-1.2 -0.1" , mat ( "1 0.4; 0.4 0.5" ) ); 
    2928 
    30         Array<shared_ptr<epdf> > A1 ( 1 ); 
     29        epdf_array A1 ( 1 ); 
    3130        A1 ( 0 ) = E1; 
    3231 
     
    3635 
    3736        // test if ARX and emix with one ARX are the same 
    38         shared_ptr<epdf> Mm = M1.marginal ( y ); 
    39         shared_ptr<epdf> Am = E1->marginal ( y ); 
    40         shared_ptr<mpdf> Mc = M1.condition ( y ); 
    41         shared_ptr<mpdf> Ac = E1->condition ( y ); 
     37        epdf_ptr Mm = M1.marginal ( y ); 
     38        epdf_ptr Am = E1->marginal ( y ); 
     39        mpdf_ptr Mc = M1.condition ( y ); 
     40        mpdf_ptr Ac = E1->condition ( y ); 
    4241 
    4342        mlnorm<ldmat> *wacnd = dynamic_cast<mlnorm<ldmat> *>( Ac.get() ); 
     
    5655 
    5756        // mixture with two components 
    58         Array<shared_ptr<epdf> > A2 ( 2 ); 
     57        epdf_array A2 ( 2 ); 
    5958        A2 ( 0 ) = E1; 
    6059        A2 ( 1 ) = E2; 
     
    8079        check_covariance ( M2, N, observedR, 2.0); 
    8180 
    82         shared_ptr<epdf> Mg = M2.marginal ( y ); 
     81        epdf_ptr Mg = M2.marginal ( y ); 
    8382        CHECK ( Mg.get() ); 
    84         shared_ptr<mpdf> Cn = M2.condition ( x ); 
     83        mpdf_ptr Cn = M2.condition ( x ); 
    8584        CHECK ( Cn.get() ); 
    8685 
     
    9089 
    9190TEST ( test_emix_2 ) { 
    92         int N = 10000; //number of samples 
     91        int N = 10000; // number of samples 
    9392        vec mu0 ( "1.5 1.7" ); 
    9493        mat V0 ( "1.2 0.3; 0.3 5" ); 
    9594        ldmat R = ldmat ( V0 ); 
    9695 
    97         shared_ptr<enorm<ldmat> > eN = new enorm<ldmat>(); 
     96        enorm_ldmat_ptr eN; 
    9897        eN->set_parameters ( mu0, R ); 
    9998 
    10099        vec a = "100000,10000"; 
    101100        vec b = a / 10.0; 
    102         shared_ptr<egamma> eG = new egamma(); 
     101        egamma_ptr eG; 
    103102        eG->set_parameters ( a, b ); 
    104103 
    105104        emix eMix; 
    106         Array<shared_ptr<epdf> > Coms ( 2 ); 
     105        epdf_array Coms ( 2 ); 
    107106        Coms ( 0 ) = eG; 
    108107        Coms ( 1 ) = eN; 
  • library/tests/epdf_harness.cpp

    r527 r529  
    6464        } 
    6565 
    66         if ( mrv.get() ) { 
     66        if ( mrv ) { 
    6767                RV crv = hepdf->_rv().subt ( *mrv ); 
    68                 shared_ptr<epdf> m = hepdf->marginal ( *mrv ); 
    69                 shared_ptr<mpdf> c = hepdf->condition ( crv ); 
    70  
    71                 Array<shared_ptr<mpdf> > aa ( 2 ); 
     68                epdf_ptr m = hepdf->marginal ( *mrv ); 
     69                mpdf_ptr c = hepdf->condition ( crv ); 
     70 
     71                mpdf_array aa ( 2 ); 
    7272                aa ( 0 ) = c; 
    7373                aa ( 1 ) = new mepdf ( m ); 
  • library/tests/merger_2d_test.cpp

    r507 r529  
    2121        xy.add ( y ); 
    2222 
    23         shared_ptr<enorm<fsqmat> > f1 = new enorm<fsqmat>(); 
     23        enorm_fsqmat_ptr f1; 
    2424        f1->set_rv ( xy ); 
    25         shared_ptr<enorm<fsqmat> > f2 = new enorm<fsqmat>(); 
     25        enorm_fsqmat_ptr f2; 
    2626        f2->set_rv ( xy ); 
    2727 
     
    3131        f2->set_parameters ( "1 1", mat ( "0.5 0; 0 0.1" ) ); 
    3232 
    33         Array<shared_ptr<mpdf> > A ( 2 ); 
    34         shared_ptr<mepdf> A1 = new mepdf ( f1 ); 
    35         shared_ptr<mepdf> A2 = new mepdf ( f2 ); 
     33        mpdf_array A ( 2 ); 
     34        mepdf_ptr A1 = new mepdf ( f1 ); 
     35        mepdf_ptr A2 = new mepdf ( f2 ); 
    3636        A ( 0 ) = A1; 
    3737        A ( 1 ) = A2; 
  • library/tests/merger_iter_test.cpp

    r507 r529  
    2121        xy.add ( y ); 
    2222 
    23         shared_ptr<enorm<fsqmat> > f1 = new enorm<fsqmat>(); 
     23        enorm_fsqmat_ptr f1; 
    2424        f1->set_rv ( xy ); 
    25         shared_ptr<enorm<fsqmat> > f2 = new enorm<fsqmat>(); 
     25        enorm_fsqmat_ptr f2; 
    2626        f2->set_rv ( xy ); 
    27         shared_ptr<enorm<fsqmat> > f3 = new enorm<fsqmat>(); 
     27        enorm_fsqmat_ptr f3; 
    2828        f3->set_rv ( y ); 
    2929 
     
    3232        f3->set_parameters ( "2", mat ( "0.4" ) ); 
    3333 
    34         Array<shared_ptr<mpdf> > A ( 3 ); 
     34        mpdf_array A ( 3 ); 
    3535        A ( 0 ) = new mepdf ( f1 ); 
    3636        A ( 1 ) = new mepdf ( f2 ); 
  • library/tests/merger_test.cpp

    r518 r529  
    1313        RV z ( x ); 
    1414 
    15         shared_ptr<enorm<fsqmat> > f1 = new enorm<fsqmat>(); 
     15        enorm_fsqmat_ptr f1; 
    1616        f1->set_rv ( x ); 
    17         shared_ptr<enorm<fsqmat> > f2 = new enorm<fsqmat>(); 
     17        enorm_fsqmat_ptr f2; 
    1818        f2->set_rv ( x ); 
    1919 
     
    2121        f2->set_parameters ( "5", mat ( "10" ) ); 
    2222 
    23         Array<shared_ptr<mpdf> > A ( 2 ); 
     23        mpdf_array A ( 2 ); 
    2424        A ( 0 ) = new mepdf ( f1 ); 
    2525        A ( 1 ) = new mepdf ( f2 ); 
  • library/tests/mixtures_test.cpp

    r504 r529  
    5353        fsqmat V2 ( mat ( "2 -0.1; -0.1 2" ) ); 
    5454 
    55         shared_ptr<enorm<fsqmat> > C1 = new enorm<fsqmat>(); 
     55        enorm_fsqmat_ptr C1; 
    5656        C1->set_rv ( x ); 
    5757        C1->set_parameters ( m1, V1 ); 
    58         shared_ptr<enorm<fsqmat> > C2 = new enorm<fsqmat>(); 
     58        enorm_fsqmat_ptr C2; 
    5959        C2->set_rv ( x ); 
    6060        C2->set_parameters ( m2, V2 ); 
    6161 
    62         Array<shared_ptr<epdf> > Sim ( 2 ); 
     62        epdf_array Sim ( 2 ); 
    6363        Sim ( 0 ) = C1; 
    6464        Sim ( 1 ) = C2; 
  • library/tests/mpdf_test.cpp

    r524 r529  
    3838        ldmat R = ldmat ( V0 ); 
    3939 
    40         shared_ptr<enorm<ldmat> > eN = new enorm<ldmat>(); 
     40        enorm_ldmat_ptr eN; 
    4141        eN->set_parameters ( mu0, R ); 
    4242 
    43         shared_ptr<mgamma> mG = new mgamma(); 
     43        mgamma_ptr mG; 
    4444        double k = 10.0; 
    4545        mG->set_parameters ( k, mu0 ); 
    4646 
    4747        mmix mMix; 
    48         Array<shared_ptr<mpdf> > mComs ( 2 ); 
     48        mpdf_array mComs ( 2 ); 
    4949 
    5050        // mmix::set_parameters requires the first mpdf to be named 
     
    5454 
    5555        eN->set_mu ( vec_2 ( 0.0, 0.0 ) ); 
    56         shared_ptr<mepdf> mEnorm = new mepdf ( eN ); 
     56        mepdf_ptr mEnorm = new mepdf ( eN ); 
    5757        mComs ( 1 ) = mEnorm; 
    5858 
    5959        mMix.set_parameters ( vec_2 ( 0.5, 0.5 ), mComs ); 
    6060 
    61         double tolerance = 0.1; 
    62  
    6361        vec tmu = 0.5 * eN->mean() + 0.5 * mu0; 
    64         check_mean ( mMix, mu0, N, tmu, tolerance ); 
     62        check_mean ( mMix, mu0, N, tmu, 0.1 ); 
    6563 
    6664        mat observedR ( "1.27572 0.778247; 0.778247 3.33129" ); 
    67         check_covariance( mMix, mu0, N, observedR, tolerance); 
     65        check_covariance( mMix, mu0, N, observedR, 0.2 ); 
    6866} 
    6967 
     
    7573        ldmat R = ldmat ( V0 ); 
    7674 
    77         shared_ptr<enorm<ldmat> > eN = new enorm<ldmat>(); 
     75        enorm_ldmat_ptr eN; 
    7876        eN->set_parameters ( mu0, R ); 
    7977 
    8078        vec a = "100000,10000"; 
    8179        vec b = a / 10.0; 
    82         shared_ptr<egamma> eG = new egamma(); 
     80        egamma_ptr eG; 
    8381        eG->set_parameters ( a, b ); 
    8482 
    85         shared_ptr<emix> eMix = new emix(); 
    86         Array<shared_ptr<epdf> > Coms ( 2 ); 
     83        emix_ptr eMix; 
     84        epdf_array Coms ( 2 ); 
    8785        Coms ( 0 ) = eG; 
    8886        Coms ( 1 ) = eN; 
  • library/tests/mprod.cfg

    r519 r529  
    3838  R = ( "matrix", 2, 2, [ 2.0, 0.5, 0.5, 0.5 ] ); 
    3939  variance = [ 1.2, 5.0 ]; 
    40   tolerance = 0.2; 
     40  tolerance = 0.3; 
    4141}, 
    4242{