Changeset 527

Show
Ignore:
Timestamp:
08/13/09 15:58:32 (15 years ago)
Author:
vbarta
Message:

using shared_ptr in UI (optionally so far; loading & saving Array<T *> still works but should be phased out); testsuite run leaks down from 8822 to 480 bytes

Location:
library
Files:
16 modified

Legend:

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

    r507 r527  
    169169 
    170170void mpdf::from_setting ( const Setting &set ) { 
    171         RV *r = UI::build<RV> ( set, "rv" ); 
     171        shared_ptr<RV> r = UI::build<RV> ( set, "rv", UI::optional ); 
    172172        if ( r ) { 
    173173                set_rv ( *r ); 
    174                 delete r; 
    175         } 
    176  
    177         r = UI::build<RV> ( set, "rvc" ); 
     174        } 
     175 
     176        r = UI::build<RV> ( set, "rvc", UI::optional ); 
    178177        if ( r ) { 
    179178                set_rvc ( *r ); 
    180                 delete r; 
    181179        } 
    182180} 
  • library/bdm/base/bdmbase.h

    r513 r527  
    379379                //!@} 
    380380                void from_setting (const Setting &set) { 
    381                         RV* r = UI::build<RV> (set, "rv"); 
     381                        shared_ptr<RV> r = UI::build<RV> ( set, "rv", UI::optional ); 
    382382                        if (r) { 
    383383                                set_rv (*r); 
    384                                 delete r; 
    385384                        } 
    386385                } 
     
    455454                        return rvc; 
    456455                } 
    457                 int dimension() { 
     456 
     457                int dimension() const { 
    458458                        return ep->dimension(); 
    459459                } 
  • library/bdm/base/datasources.cpp

    r477 r527  
    5757 
    5858void ArxDS::from_setting ( const Setting &set ) { 
    59         RV *yrv = UI::build<RV> ( set, "y" , UI::compulsory ); 
    60         RV *urv = UI::build<RV> ( set, "u" , UI::compulsory ); 
    61         RV *rrv = UI::build<RV> ( set, "rgr" , UI::compulsory ); 
     59        shared_ptr<RV> yrv = UI::build<RV> ( set, "y" , UI::compulsory ); 
     60        shared_ptr<RV> urv = UI::build<RV> ( set, "u" , UI::compulsory ); 
     61        shared_ptr<RV> rrv = UI::build<RV> ( set, "rgr" , UI::compulsory ); 
    6262 
    6363        mat Th; 
     
    112112 
    113113void ITppFileDS::from_setting ( const Setting &set ) { 
    114         RV* rvtmp = UI::build<RV> ( set, "rv" , UI::compulsory ); 
     114        shared_ptr<RV> rvtmp = UI::build<RV> ( set, "rv" , UI::compulsory ); 
    115115 
    116116        it_file it ( set["filename"] ); 
     
    131131        ut.set_length ( 0 ); 
    132132 
     133#if 0 
    133134        RV* rvtmp = UI::build<RV> ( set["IM"], "rvu", UI::compulsory ); 
    134135        //set_drv(rvtmp); 
     136#endif 
    135137} 
  • library/bdm/base/datasources.h

    r477 r527  
    173173        }; 
    174174        //! Set 
    175         void set_drv ( RV &yrv, RV &urv, RV &rrv ) { 
     175        void set_drv ( const RV &yrv, const RV &urv, const RV &rrv ) { 
    176176                Rrv = rrv; 
    177177                Urv = urv; 
     
    265265 
    266266class stateDS : public DS { 
     267private: 
     268        //!conditional pdf of the state evolution \f$ f(x_t|x_{t-1}) \f$ 
     269        shared_ptr<mpdf> IM; 
     270 
     271        //!conditional pdf of the observations \f$ f(d_t|x_t) \f$ 
     272        shared_ptr<mpdf> OM; 
     273 
    267274protected: 
    268         //!conditional pdf of the state evolution \f$ f(x_t|x_{t-1}) \f$ 
    269         mpdf* IM; 
    270         //!conditional pdf of the observations \f$ f(d_t|x_t) \f$ 
    271         mpdf* OM; 
    272275        //! result storage 
    273276        vec dt; 
     
    278281        //! Logger 
    279282        int L_xt; 
     283 
    280284public: 
    281285        void getdata ( vec &dt0 ) { 
     
    286290        } 
    287291 
    288         stateDS ( mpdf* IM0, mpdf* OM0, int usize ) : DS ( ), IM ( IM0 ), OM ( OM0 ), 
    289                         dt ( OM0->dimension() ), xt ( IM0->dimension() ), ut ( usize ) {} 
    290  
    291         stateDS() {} 
    292  
    293         ~stateDS() { 
    294                 delete IM; 
    295                 delete OM; 
    296         } 
     292        stateDS ( const shared_ptr<mpdf> &IM0, const shared_ptr<mpdf> &OM0, int usize ) : IM ( IM0 ), OM ( OM0 ), 
     293                dt ( OM0->dimension() ), xt ( IM0->dimension() ), 
     294                ut ( usize ), L_xt(0) { } 
     295 
     296        stateDS() : L_xt(0) { } 
     297 
    297298        virtual void step() { 
    298299                xt = IM->samplecond ( concat ( xt, ut ) ); 
    299300                dt = OM->samplecond ( concat ( xt, ut ) ); 
    300         }; 
     301        } 
    301302 
    302303        virtual void log_add ( logger &L ) { 
  • library/bdm/base/user_info.h

    r494 r527  
    2424#include "libconfig/libconfig.h++" 
    2525#include "../bdmroot.h" 
     26#include "../shared_ptr.h" 
    2627#include "itpp/itbase.h" 
    2728 
     
    253254        static void assert_type ( const Setting &element, Setting::Type type ); 
    254255 
    255         //! Method assembling a typeless instance, it is implemented in descendant class ParticularUI<T> 
     256        /*! 
     257          \brief Method constructing a configured instance 
     258 
     259          The returned pointer must be allocated using operator new 
     260          (it's deleted at the end of its life cycle). The method is 
     261          implemented in descendant class ParticularUI<T>, which knows 
     262          the correct type T. 
     263        */ 
    256264        virtual root* new_instance() const = 0; 
    257265 
     
    274282        //! This method converts a Setting into a real scalar 
    275283        static void from_setting ( double &real, const Setting &element ); 
     284 
    276285        //! This method converts a Setting into a class T descendant 
    277286        template<class T> static void from_setting ( T* &instance, const Setting &element ) { 
     
    287296                const UI& related_UI = MappedUI::retrieve_ui ( class_name ); 
    288297 
    289                 root* typeless_instance = related_UI.new_instance(); 
     298                root *typeless_instance = related_UI.new_instance(); 
     299                it_assert_debug ( typeless_instance, "UI::new_instance failed" ); 
    290300 
    291301                instance = dynamic_cast<T*> ( typeless_instance ); 
    292                 if ( !instance ) 
     302                if ( !instance ) { 
     303                        delete typeless_instance; 
    293304                        throw UIException ( "class " + class_name + " is not a descendant of the desired output class. Try to call the UI::build<T> function with a different type parameter.", link.result ); 
     305                } 
    294306 
    295307                try { 
     
    301313        } 
    302314 
     315        //! This method converts a Setting into a descendant of class 
     316        //! T, wrapped in an instance of shared_ptr<T> . 
     317        template<class T> 
     318        static void from_setting ( shared_ptr<T> &instance, const Setting &element ) { 
     319                T *tmp_inst = 0; 
     320                from_setting ( tmp_inst, element ); 
     321                it_assert_debug ( tmp_inst, "UI::from_setting failed" ); 
     322                instance = tmp_inst; 
     323        } 
     324 
    303325        //! This methods converts a Setting into a new templated array of type Array<T> 
    304326        template<class T> static void from_setting ( Array<T> &array_to_load, const Setting &element ) { 
     
    320342        //! The exception can help to find the place where the template is misused and also to correct it. 
    321343        template<class T> static void from_setting ( T &variable_to_load, const Setting &element ) { 
    322                 throw UIException ( "from_setting is not implemented for this type", element ); 
     344                std::string msg = "from_setting is not implemented for type "; 
     345                msg += typeid(T).name(); 
     346                throw UIException ( msg, element ); 
    323347        } 
    324348 
     
    341365        //! The new instance of type T* is constructed and initialized with values stored in the Setting element[name] 
    342366        //! 
    343         //! If there is not any sub-element named #name, the null pointer is returned. 
    344         template<class T> static T* build ( const Setting &element, const string &name, SettingPresence settingPresence = optional ) { 
     367        //! If there is not any sub-element named #name and settingPresence is #optional, an empty shared_ptr<T> is returned. When settingPresence is #compulsory, the returned shared_ptr<T> is never empty (an exception is thrown when the object isn't found). 
     368        template<class T> 
     369        static shared_ptr<T> build ( const Setting &element, const string &name, SettingPresence settingPresence = optional ) { 
    345370                if ( !element.exists ( name ) ) { 
    346371                        if ( settingPresence == optional ) 
    347                                 return NULL; 
     372                                return shared_ptr<T>(); 
    348373                        else 
    349374                                throw UIException ( "the compulsory Setting named \"" + name + "\" is missing", element ); 
    350375                } 
    351376 
    352                 T* instance; 
     377                shared_ptr<T> instance; 
    353378                from_setting<T> ( instance, to_child_setting ( element, name ) ); 
    354379                return instance; 
     
    483508        } 
    484509 
     510        template< class T> static void save ( const shared_ptr<T> &instance, Setting &element, const string &name = "" ) { 
     511                save<T> ( instance.get(), element, name ); 
     512        } 
     513 
    485514        //! An Array<T> instance is stored in the new child Setting appended to the passed element 
    486515        template<class T> static void save ( const Array<T> &array_to_save, Setting &element, const string &name = "" ) { 
  • library/bdm/estim/arx.cpp

    r477 r527  
    194194 
    195195void ARX::from_setting ( const Setting &set ) { 
    196         RV *yrv = UI::build<RV> ( set, "y", UI::compulsory ); 
    197         RV *rrv = UI::build<RV> ( set, "rgr", UI::compulsory ); 
     196        shared_ptr<RV> yrv = UI::build<RV> ( set, "y", UI::compulsory ); 
     197        shared_ptr<RV> rrv = UI::build<RV> ( set, "rgr", UI::compulsory ); 
    198198        int ylen = yrv->_dsize(); 
    199199        int rgrlen = rrv->_dsize(); 
     
    220220        //name results (for logging) 
    221221        set_rv ( RV ( "{theta r }", vec_2 ( ylen*rgrlen, ylen*ylen ) ) ); 
    222  
    223         delete yrv; 
    224         delete rrv; 
    225 } 
    226  
    227 } 
     222} 
     223 
     224} 
  • library/bdm/estim/kalman.cpp

    r477 r527  
    6464EKFfull::EKFfull ( ) : BM (), E() {}; 
    6565 
    66 void EKFfull::set_parameters ( diffbifn* pfxu0,  diffbifn* phxu0, const mat Q0, const mat R0 ) { 
     66void EKFfull::set_parameters ( const shared_ptr<diffbifn> &pfxu0, const shared_ptr<diffbifn> &phxu0, const mat Q0, const mat R0 ) { 
    6767        pfxu = pfxu0; 
    6868        phxu = phxu0; 
     
    168168 
    169169 
    170 void EKFCh::set_parameters ( diffbifn* pfxu0,  diffbifn* phxu0, const chmat Q0, const chmat R0 ) { 
     170void EKFCh::set_parameters ( const shared_ptr<diffbifn> &pfxu0, const shared_ptr<diffbifn> &phxu0, const chmat Q0, const chmat R0 ) { 
    171171        pfxu = pfxu0; 
    172172        phxu = phxu0; 
     
    255255 
    256256void EKFCh::from_setting ( const Setting &set ) { 
    257         diffbifn* IM = UI::build<diffbifn> ( set, "IM", UI::compulsory ); 
    258         diffbifn* OM = UI::build<diffbifn> ( set, "OM", UI::compulsory ); 
     257        shared_ptr<diffbifn> IM = UI::build<diffbifn> ( set, "IM", UI::compulsory ); 
     258        shared_ptr<diffbifn> OM = UI::build<diffbifn> ( set, "OM", UI::compulsory ); 
    259259 
    260260        //statistics 
     
    280280 
    281281        //connect 
    282         RV* drv = UI::build<RV> ( set, "drv", UI::compulsory ); 
     282        shared_ptr<RV> drv = UI::build<RV> ( set, "drv", UI::compulsory ); 
    283283        set_drv ( *drv ); 
    284         RV* rv = UI::build<RV> ( set, "rv", UI::compulsory ); 
     284        shared_ptr<RV> rv = UI::build<RV> ( set, "rv", UI::compulsory ); 
    285285        set_rv ( *rv ); 
    286286 
  • library/bdm/estim/kalman.h

    r477 r527  
    190190protected: 
    191191        //! Internal Model f(x,u) 
    192         diffbifn* pfxu; 
     192        shared_ptr<diffbifn> pfxu; 
     193 
    193194        //! Observation Model h(x,u) 
    194         diffbifn* phxu; 
     195        shared_ptr<diffbifn> phxu; 
    195196 
    196197        enorm<fsqmat> E; 
     
    198199        //! Default constructor 
    199200        EKFfull ( ); 
     201 
    200202        //! Set nonlinear functions for mean values and covariance matrices. 
    201         void set_parameters ( diffbifn* pfxu, diffbifn* phxu, const mat Q0, const mat R0 ); 
     203        void set_parameters ( const shared_ptr<diffbifn> &pfxu, const shared_ptr<diffbifn> &phxu, const mat Q0, const mat R0 ); 
     204 
    202205        //! Here dt = [yt;ut] of appropriate dimensions 
    203206        void bayes ( const vec &dt ); 
     
    252255protected: 
    253256        //! Internal Model f(x,u) 
    254         diffbifn* pfxu; 
     257        shared_ptr<diffbifn> pfxu; 
     258 
    255259        //! Observation Model h(x,u) 
    256         diffbifn* phxu; 
     260        shared_ptr<diffbifn> phxu; 
    257261public: 
    258262        //! copy constructor duplicated - calls different set_parameters 
     
    264268        } 
    265269        //! Set nonlinear functions for mean values and covariance matrices. 
    266         void set_parameters ( diffbifn* pfxu, diffbifn* phxu, const chmat Q0, const chmat R0 ); 
     270        void set_parameters ( const shared_ptr<diffbifn> &pfxu, const shared_ptr<diffbifn> &phxu, const chmat Q0, const chmat R0 ); 
     271 
    267272        //! Here dt = [yt;ut] of appropriate dimensions 
    268273        void bayes ( const vec &dt ); 
  • library/bdm/stat/emix.h

    r515 r527  
    355355        //!@} 
    356356        void from_setting ( const Setting &set ) { 
    357                 Array<mpdf*> atmp; //temporary Array 
     357                Array<shared_ptr<mpdf> > atmp; //temporary Array 
    358358                UI::get ( atmp, set, "mpdfs", UI::compulsory ); 
    359                  
    360                 Array<shared_ptr<mpdf> > btmp ( atmp.length() ); 
    361                 for (int i = 0; i < atmp.length(); ++i) { 
    362                         btmp ( i ) = shared_ptr<mpdf> ( atmp ( i ) ); 
    363                 } 
    364  
    365                 set_elements ( btmp ); 
     359                set_elements ( atmp ); 
    366360        } 
    367361 
  • library/bdm/stat/exp_family.h

    r510 r527  
    236236                        UI::get (V, set, "V", UI::compulsory); 
    237237                        set_parameters (dimx, V, nu); 
    238                         RV* rv = UI::build<RV> (set, "rv", UI::compulsory); 
     238                        shared_ptr<RV> rv = UI::build<RV> (set, "rv", UI::compulsory); 
    239239                        set_rv (*rv); 
    240                         delete rv; 
    241240                } 
    242241                //!@} 
     
    574573class mgnorm : public mpdf_internal< enorm< sq_T > > 
    575574{ 
    576         protected: 
     575        private: 
    577576//                      vec &mu; WHY NOT? 
    578                 fnc* g; 
     577                shared_ptr<fnc> g; 
     578 
    579579        public: 
    580580                //!default constructor 
    581581                mgnorm() : mpdf_internal<enorm<sq_T> >() { } 
    582582                //!set mean function 
    583                 inline void set_parameters (fnc* g0, const sq_T &R0); 
     583                inline void set_parameters (const shared_ptr<fnc> &g0, const sq_T &R0); 
    584584                inline void condition (const vec &cond); 
    585585 
     
    613613 
    614614                void from_setting (const Setting &set) { 
    615                         fnc* g = UI::build<fnc> (set, "g", UI::compulsory); 
     615                        shared_ptr<fnc> g = UI::build<fnc> (set, "g", UI::compulsory); 
    616616 
    617617                        mat R; 
     
    13051305/////// 
    13061306template<class sq_T> 
    1307 void mgnorm<sq_T >::set_parameters (fnc* g0, const sq_T &R0) {g = g0; this->iepdf.set_parameters (zeros (g->dimension()), R0);} 
     1307void mgnorm<sq_T >::set_parameters (const shared_ptr<fnc> &g0, const sq_T &R0) { 
     1308        g = g0; 
     1309        this->iepdf.set_parameters (zeros (g->dimension()), R0); 
     1310} 
     1311 
    13081312template<class sq_T> 
    13091313void mgnorm<sq_T >::condition (const vec &cond) {this->iepdf._mu() = g->eval (cond);}; 
  • library/tests/epdf_harness.cpp

    r507 r527  
    1414 
    1515        UIFile in ( config_file_name ); 
    16         Array<epdf_harness *> input; 
     16        Array<shared_ptr<epdf_harness> > input; 
    1717        UI::get ( input, in, "data", UI::compulsory ); 
    1818        int sz = input.size(); 
  • library/tests/mpdf_harness.cpp

    r493 r527  
    1313 
    1414        UIFile in ( config_file_name ); 
    15         Array<mpdf_harness *> input; 
     15        Array<shared_ptr<mpdf_harness> > input; 
    1616        UI::get ( input, in, "data", UI::compulsory ); 
    1717        int sz = input.size(); 
  • library/tests/square_mat_prep.cpp

    r480 r527  
    77#include <iostream> 
    88#include <iomanip> 
    9 #include <memory> 
    109#include <stdlib.h> 
    1110 
     
    6261                cerr << "usage: " << argv[0] << " [ -g generator.cfg ] [ -a agenda_output.cfg ] [ -l agenda_length ]" << endl; 
    6362        } else { 
    64                 Array<square_mat_point *> mag ( agenda_length ); 
     63                Array<shared_ptr<square_mat_point> > mag ( agenda_length ); 
    6564 
    6665                UIFile gspec ( generator_file_name ); 
    67                 auto_ptr<generator> gen ( UI::build<generator> ( gspec, "generator", UI::compulsory ) ); 
     66                shared_ptr<generator> gen ( UI::build<generator> ( gspec, "generator", UI::compulsory ) ); 
    6867                for ( int i = 0; i < agenda_length; ++i ) { 
    6968                        mat m = gen->next(); 
     
    7675                UI::save ( mag, fag, "agenda" ); 
    7776                fag.save ( agenda_file_name ); 
    78  
    79                 for ( int i = 0; i < agenda_length; ++i ) { 
    80                         square_mat_point *p = mag ( i ); 
    81                         mag ( i ) = 0; 
    82                         delete p; 
    83                 } 
    8477        } 
    8578} 
  • library/tests/square_mat_stress.cpp

    r495 r527  
    1717using bdm::chmat; 
    1818using bdm::ldmat; 
     19using bdm::shared_ptr; 
    1920using bdm::UIFile; 
    2021using bdm::UI; 
     
    174175void test_agenda ( FTestMatrix test ) { 
    175176        UIFile fag ( agenda_file_name ); 
    176         Array<square_mat_point *> mag; 
     177        Array<shared_ptr<square_mat_point> > mag; 
    177178        UI::get ( mag, fag, "agenda", UI::compulsory ); 
    178179        int sz = mag.size(); 
    179180        CHECK ( sz > 0 ); 
    180181        for ( int i = 0; i < sz; ++i ) { 
    181                 test ( i, mag ( i ) ); 
    182         } 
    183  
    184         for ( int i = 0; i < sz; ++i ) { 
    185                 square_mat_point *p = mag ( i ); 
    186                 mag ( i ) = 0; 
    187                 delete p; 
     182                test ( i, mag ( i ).get() ); 
    188183        } 
    189184} 
  • library/tests/test_kalman.cpp

    r477 r527  
    8282 
    8383        // EKF 
    84         bilinfn fxu ( A, B ); 
    85         bilinfn hxu ( C, D ); 
     84        shared_ptr<bilinfn> fxu = new bilinfn ( A, B ); 
     85        shared_ptr<bilinfn> hxu = new bilinfn ( C, D ); 
    8686        EKFCh KFE; 
    87         KFE.set_parameters ( &fxu, &hxu, Q, R ); 
     87        KFE.set_parameters ( fxu, hxu, Q, R ); 
    8888        KFE.set_est ( mu0, chmat ( P0 ) ); 
    8989        const epdf& KFEep = KFE.posterior(); 
  • library/tests/user_info_test.cpp

    r499 r527  
    1 #include <memory> 
    21#include <string> 
    32#include <string.h> 
     
    103102public: 
    104103        int kilometers; 
    105         Array<Passenger*> passengers; 
     104        Array<shared_ptr<Passenger> > passengers; 
    106105 
    107106        Car() : Transport() { 
     
    192191TEST ( test_load ) { 
    193192        UIFile in ( "test_user_info_input.cfg" ); 
    194         auto_ptr<Transport> pepikovo ( UI::build<Transport> ( in, "pepikovo", UI::compulsory ) ); 
     193        shared_ptr<Transport> pepikovo ( UI::build<Transport> ( in, "pepikovo", UI::compulsory ) ); 
    195194        CHECK_EQUAL ( string ( "A car made in 1998 by audi, having 25000 kilometers on the clock.The names of passengers are as follows: Karlos Novak -1_CygWin_Matlab_Aimsun Karlosik Novacek " ), pepikovo->to_string() ); 
    196195 
    197         auto_ptr<Transport> jardovo ( UI::build<Transport> ( in, "jardovo", UI::compulsory ) ); 
     196        shared_ptr<Transport> jardovo ( UI::build<Transport> ( in, "jardovo", UI::compulsory ) ); 
    198197        CHECK_EQUAL ( string ( "A car made in 1992 by liaz, having 1555000 kilometers on the clock." ), jardovo->to_string() ); 
    199198 
    200         auto_ptr<Transport> ondrejovo ( UI::build<Transport> ( in, "ondrejovo", UI::compulsory ) ); 
     199        shared_ptr<Transport> ondrejovo ( UI::build<Transport> ( in, "ondrejovo", UI::compulsory ) ); 
    201200        CHECK_EQUAL ( string ( "a bike made in 1996 by author with electric lights included" ), ondrejovo->to_string() ); 
    202201 
    203         auto_ptr<Transport> elisky ( UI::build<Transport> ( in, "elisky", UI::compulsory ) ); 
     202        shared_ptr<Transport> elisky ( UI::build<Transport> ( in, "elisky", UI::compulsory ) ); 
    204203        CHECK_EQUAL ( string ( "A car made in 1992 by liaz, having 1555000 kilometers on the clock." ), elisky->to_string() ); 
    205204 
    206         auto_ptr<Transport> kati ( UI::build<Transport> ( in, "kati", UI::compulsory ) ); 
     205        shared_ptr<Transport> kati ( UI::build<Transport> ( in, "kati", UI::compulsory ) ); 
    207206        CHECK_EQUAL ( string ( "A car made in 1980 by vecernicek, having 250000 kilometers on the clock." ), kati->to_string() ); 
    208207} 
     
    210209TEST ( test_save ) { 
    211210        UIFile in ( "test_user_info_input.cfg" ); 
    212         auto_ptr<Transport> pepikovo ( UI::build<Transport> ( in, "pepikovo", UI::compulsory ) ); 
     211        shared_ptr<Transport> pepikovo ( UI::build<Transport> ( in, "pepikovo", UI::compulsory ) ); 
    213212 
    214213        Car audi ( 1968, "zyl", 200 ); 
     
    220219        UI::save ( &liaz, out, "bohousovo" ); 
    221220        UI::save ( &author, out, "karlovo" ); 
    222         UI::save ( pepikovo.get(), out, "pepikovo" ); 
     221        UI::save ( pepikovo, out, "pepikovo" ); 
    223222        out.save ( "testUI_out.cfg" ); 
    224223