Changeset 1165


Ignore:
Timestamp:
Feb 26, 2008, 8:06:28 PM (16 years ago)
Author:
Peter
Message:

removed dynamic_casts

Location:
trunk/yat/classifier
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/yat/classifier/Kernel.cc

    r1163 r1165  
    9595
    9696 
    97   const DataLookup2D& Kernel::data(void) const
     97  const MatrixLookup& Kernel::data(void) const
    9898  {
    9999    if (weighted())
    100       return *mlw_;
     100      throw std::runtime_error("Kernel:data when Kernel is weighted");
     101    assert(ml_);
    101102    return *ml_;
     103  }
     104
     105
     106  const MatrixLookupWeighted& Kernel::data_weighted(void) const
     107  {
     108    if (!weighted())
     109      throw std::runtime_error("Kernel:data when Kernel is weighted");
     110    assert(mlw_);
     111    return *mlw_;
    102112  }
    103113
  • trunk/yat/classifier/Kernel.h

    r1163 r1165  
    3636namespace classifier {
    3737
    38   class DataLookup2D;
    3938  class MatrixLookup;
    4039  class MatrixLookupWeighted;
     
    109108    /// @return const reference to the underlying data.
    110109    ///
    111     const DataLookup2D& data(void) const;
     110    /// \throw if data in weighted
     111    ///
     112    const MatrixLookup& data(void) const;
     113
     114    ///
     115    /// @return const reference to the underlying data.
     116    ///
     117    /// \throw if data in unweighted
     118    ///
     119    const MatrixLookupWeighted& data_weighted(void) const;
    112120
    113121    ///
  • trunk/yat/classifier/KernelLookup.cc

    r1134 r1165  
    135135
    136136
    137   const DataLookup2D* KernelLookup::data(void) const
    138   {
    139     return kernel_->data().training_data(column_index_);
     137  utility::SmartPtr<const MatrixLookup> KernelLookup::data(void) const
     138  {
     139    return utility::SmartPtr<const MatrixLookup>
     140      (kernel_->data().training_data(column_index_));
     141  }
     142
     143
     144  utility::SmartPtr<const MatrixLookupWeighted>
     145  KernelLookup::data_weighted(void) const
     146  {
     147    return utility::SmartPtr<const MatrixLookupWeighted>
     148      (kernel_->data_weighted().training_data(column_index_));
    140149  }
    141150
     
    184193    const Kernel* kernel;
    185194    if (kernel_->weighted()){
    186       const MatrixLookupWeighted* ml =
    187         dynamic_cast<const MatrixLookupWeighted*>(data());
    188       assert(ml);
     195      utility::SmartPtr<const MatrixLookupWeighted> ml = data_weighted();
    189196      const MatrixLookupWeighted* ms =
    190197        new MatrixLookupWeighted(*ml,inputs,true);
     
    192199    }
    193200    else {
    194       const MatrixLookup* m =
    195         dynamic_cast<const MatrixLookup*>(data());
    196       assert(m);
     201      utility::SmartPtr<const MatrixLookup> m = data();
    197202      // matrix with selected features
    198203      const MatrixLookup* ms = new MatrixLookup(*m,inputs,true);
     
    206211  {
    207212
    208     assert(data.rows()==kernel_->data().rows());
    209213    if (!weighted()){
     214      assert(data.rows()==kernel_->data().rows());
    210215      utility::Matrix* data_all =
    211216        new utility::Matrix(data.rows(), row_index_.size()+data.columns());
     
    242247    }
    243248
     249    assert(data.rows()==kernel_->data_weighted().rows());
    244250    // kernel_ holds MatrixLookupWeighted, hence new Kernel also
    245251    // should hold a MatrixLookupweighted.
     
    248254    utility::Matrix* weight_all =
    249255      new utility::Matrix(data.rows(), rows()+data.columns(), 1.0);
    250     const MatrixLookupWeighted& kernel_data =
    251       dynamic_cast<const MatrixLookupWeighted&>(kernel_->data());
     256    const MatrixLookupWeighted& kernel_data = kernel_->data_weighted();
    252257
    253258    for (size_t i=0; i<data.rows(); ++i){
     
    292297
    293298    if (weighted()){
    294       const MatrixLookupWeighted& kernel_data =
    295         dynamic_cast<const MatrixLookupWeighted&>(kernel_->data());
     299      const MatrixLookupWeighted& kernel_data = kernel_->data_weighted();
    296300   
    297301      for (size_t i=0; i<data.rows(); ++i){
     
    305309    else {
    306310
    307         dynamic_cast<const MatrixLookupWeighted&>(kernel_->data());
    308    
    309311      for (size_t i=0; i<data.rows(); ++i){
    310312        // first columns are equal to data in kernel_
  • trunk/yat/classifier/KernelLookup.h

    r1134 r1165  
    3838namespace classifier {
    3939
    40   class DataLookup2D;
    4140  class KernelFunction;
    4241  class MatrixLookup;
     
    210209    /// \return data that KernelLookup is built upon.
    211210    ///
    212     /// @note Returns a dynamically allocated MatrixLookup, which has
    213     /// to be deleted by the caller to avoid memory leaks.
    214     ///
    215     const DataLookup2D* data(void) const;
     211    /// \throw if KernelLookup is weighted
     212    ///
     213    utility::SmartPtr<const MatrixLookup> data(void) const;
     214
     215    ///
     216    /// Each column in returned DataLookup corresponds to the column
     217    /// in KernelLookup.
     218    ///
     219    /// \return data that KernelLookup is built upon.
     220    ///
     221    /// \throw if KernelLookup is unweighted
     222    ///
     223    utility::SmartPtr<const MatrixLookupWeighted> data_weighted(void) const;
    216224
    217225    /**
     
    275283       this was built from.
    276284   
    277        @note Returns a dynamically allocated DataLookup2D, which has
     285       @note Returns a dynamically allocated KernelLookup, which has
    278286       to be deleted by the caller to avoid memory leaks.
    279287    */
  • trunk/yat/classifier/SubsetGenerator.h

    r1134 r1165  
    2626*/
    2727
    28 #include "DataLookup2D.h"
    2928#include "FeatureSelector.h"
    3029#include "KernelLookup.h"
     
    280279      training_target_.push_back(Target(target(),training_index(k)));
    281280      validation_target_.push_back(Target(target(),validation_index(k)));
    282       const DataLookup2D* matrix = kernel.data();
    283       // dynamically allocated must be deleted
    284       const DataLookup2D* training_matrix =
    285         matrix->training_data(training_index(k));
    286       if (matrix->weighted()){
    287         const MatrixLookupWeighted& ml =
    288           dynamic_cast<const MatrixLookupWeighted&>(*matrix);
    289         f_selector_->update(MatrixLookupWeighted(ml,training_index(k),false),
     281
     282      if (kernel.weighted()){
     283        utility::SmartPtr<const MatrixLookupWeighted> ml=kernel.data_weighted();
     284        f_selector_->update(MatrixLookupWeighted(*ml,training_index(k),false),
    290285                            training_target(k));
    291286      }
    292287      else {
    293         const MatrixLookup& ml =
    294           dynamic_cast<const MatrixLookup&>(*matrix);
    295         f_selector_->update(MatrixLookup(ml,training_index(k), false),
     288        utility::SmartPtr<const MatrixLookup> ml=kernel.data();
     289        f_selector_->update(MatrixLookup(*ml,training_index(k), false),
    296290                            training_target(k));
    297291      }
     
    300294      //features_.push_back(f_selector_->features());
    301295      const KernelLookup* kl = kernel.selected(features_.back());
    302       utility::yat_assert<std::runtime_error>(training_matrix);
    303       delete training_matrix;
    304296     
    305297      // Dynamically allocated. Must be deleted in destructor.
Note: See TracChangeset for help on using the changeset viewer.